Skip to content

Commit 6feb35c

Browse files
authored
Merge pull request #151 from svanharmelen/fix/pagination
Update the pagination logic for queries done from the SQL editor
2 parents dbcb957 + bbbdfab commit 6feb35c

File tree

7 files changed

+94
-88
lines changed

7 files changed

+94
-88
lines changed

components/pagination.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ func (pagination *Pagination) SetTotalRecords(total int) {
6666
pagination.state.TotalRecords = total
6767

6868
offset := pagination.GetOffset()
69+
limit := pagination.GetLimit() + offset
70+
6971
if offset < total {
7072
offset++
7173
}
72-
73-
limit := pagination.GetLimit() + pagination.GetOffset()
7474
if limit > total {
7575
limit = total
7676
}
@@ -84,11 +84,14 @@ func (pagination *Pagination) SetLimit(limit int) {
8484
offset := pagination.GetOffset()
8585
total := pagination.GetTotalRecords()
8686

87+
if offset < total {
88+
offset++
89+
}
8790
if limit > total {
8891
limit = total
8992
}
9093

91-
pagination.textView.SetText(fmt.Sprintf("%d-%d of %d rows", offset+1, limit, total))
94+
pagination.textView.SetText(fmt.Sprintf("%d-%d of %d rows", offset, limit, total))
9295
}
9396

9497
func (pagination *Pagination) SetOffset(offset int) {
@@ -97,9 +100,12 @@ func (pagination *Pagination) SetOffset(offset int) {
97100
limit := pagination.GetLimit() + offset
98101
total := pagination.GetTotalRecords()
99102

103+
if offset < total {
104+
offset++
105+
}
100106
if limit > total {
101107
limit = total
102108
}
103109

104-
pagination.textView.SetText(fmt.Sprintf("%d-%d of %d rows", offset+1, limit, total))
110+
pagination.textView.SetText(fmt.Sprintf("%d-%d of %d rows", offset, limit, total))
105111
}

components/results_table.go

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -609,36 +609,25 @@ func (table *ResultsTable) subscribeToEditorChanges() {
609609
table.SetLoading(true)
610610
App.Draw()
611611

612-
rows, err := table.DBDriver.ExecuteQuery(query)
613-
table.Pagination.SetTotalRecords(len(rows))
614-
table.Pagination.SetLimit(len(rows))
612+
rows, records, err := table.DBDriver.ExecuteQuery(query)
613+
table.Pagination.SetTotalRecords(records)
614+
table.Pagination.SetLimit(records)
615615

616616
if err != nil {
617617
table.SetLoading(false)
618-
App.Draw()
619618
table.SetError(err.Error(), nil)
619+
App.Draw()
620620
} else {
621621
table.UpdateRows(rows)
622-
table.SetIsFiltering(false)
623-
624-
if len(rows) > 1 {
625-
App.SetFocus(table)
626-
table.HighlightTable()
627-
table.Editor.SetBlur()
628-
table.SetInputCapture(table.tableInputCapture)
629-
App.Draw()
630-
} else if len(rows) == 1 {
631-
table.SetInputCapture(nil)
632-
App.SetFocus(table.Editor)
633-
table.Editor.Highlight()
634-
table.RemoveHighlightTable()
635-
table.SetIsFiltering(true)
636-
App.Draw()
637-
}
638622
table.SetLoading(false)
623+
table.SetIsFiltering(false)
624+
table.HighlightTable()
625+
table.Editor.SetBlur()
626+
table.SetInputCapture(table.tableInputCapture)
627+
table.EditorPages.SwitchToPage(pageNameTableEditorTable)
628+
App.SetFocus(table)
629+
App.Draw()
639630
}
640-
table.EditorPages.SwitchToPage(pageNameTableEditorTable)
641-
App.Draw()
642631
} else {
643632
table.SetRecords([][]string{})
644633
table.SetLoading(true)

drivers/driver.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ type Driver interface {
1717
UpdateRecord(database, table, column, value, primaryKeyColumnName, primaryKeyValue string) error
1818
DeleteRecord(database, table string, primaryKeyColumnName, primaryKeyValue string) error
1919
ExecuteDMLStatement(query string) (string, error)
20-
ExecuteQuery(query string) ([][]string, error)
20+
ExecuteQuery(query string) ([][]string, int, error)
2121
ExecutePendingChanges(changes []models.DBDMLChange) error
22-
SetProvider(provider string) // NOTE: This is used to get the primary key from the database table until i find a better way to do it. See ResultsTable.go GetPrimaryKeyValue function
2322
GetProvider() string
2423
GetPrimaryKeyColumnNames(database, table string) ([]string, error)
24+
25+
// NOTE: This is used to get the primary key from the database table until I
26+
// find a better way to do it. See *ResultsTable.GetPrimaryKeyValue()
27+
SetProvider(provider string)
2528
}

drivers/mssqql.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func (db *MSSQL) GetForeignKeys(database, table string) ([][]string, error) {
190190

191191
func (db *MSSQL) GetIndexes(database, table string) ([][]string, error) {
192192
query := `
193-
SELECT
193+
SELECT
194194
t.name AS table_name,
195195
i.name AS index_name,
196196
i.is_unique AS is_unique,
@@ -201,33 +201,33 @@ func (db *MSSQL) GetIndexes(database, table string) ([][]string, error) {
201201
ic.is_included_column AS is_included,
202202
i.has_filter AS has_filter,
203203
i.filter_definition AS filter_definition
204-
FROM
205-
sys.tables t
204+
FROM
205+
sys.tables t
206206
INNER JOIN
207-
sys.schemas s
207+
sys.schemas s
208208
ON
209209
t.schema_id = s.schema_id
210210
INNER JOIN
211-
sys.indexes i
211+
sys.indexes i
212212
ON
213213
t.object_id = i.object_id
214214
INNER JOIN
215-
sys.index_columns ic
215+
sys.index_columns ic
216216
ON
217217
i.object_id = ic.object_id
218218
AND
219219
i.index_id = ic.index_id
220220
INNER JOIN
221-
sys.columns c
221+
sys.columns c
222222
ON
223223
t.object_id = c.object_id
224224
AND
225225
ic.column_id = c.column_id
226-
WHERE
226+
WHERE
227227
DB_NAME() = @p1
228228
AND
229229
t.name = @p2
230-
ORDER BY
230+
ORDER BY
231231
i.type_desc
232232
`
233233
return db.getTableInformations(query, database, table)
@@ -395,26 +395,24 @@ func (db *MSSQL) ExecuteDMLStatement(query string) (string, error) {
395395
return fmt.Sprintf("%d rows affected", rowsAffected), nil
396396
}
397397

398-
func (db *MSSQL) ExecuteQuery(query string) ([][]string, error) {
398+
func (db *MSSQL) ExecuteQuery(query string) ([][]string, int, error) {
399399
if query == "" {
400-
return nil, errors.New("query can not be empty")
400+
return nil, 0, errors.New("query can not be empty")
401401
}
402402

403-
results := make([][]string, 0)
404403
rows, err := db.Connection.Query(query)
405404
if err != nil {
406-
return nil, err
405+
return nil, 0, err
407406
}
408407

409408
defer rows.Close()
410409

411410
columns, err := rows.Columns()
412411
if err != nil {
413-
return nil, err
412+
return nil, 0, err
414413
}
415414

416-
results = append(results, columns)
417-
415+
records := make([][]string, 0)
418416
for rows.Next() {
419417
rowValues := make([]any, len(columns))
420418

@@ -423,22 +421,25 @@ func (db *MSSQL) ExecuteQuery(query string) ([][]string, error) {
423421
}
424422

425423
if err := rows.Scan(rowValues...); err != nil {
426-
return nil, err
424+
return nil, 0, err
427425
}
428426

429427
var row []string
430428
for _, col := range rowValues {
431429
row = append(row, string(*col.(*sql.RawBytes)))
432430
}
433431

434-
results = append(results, row)
432+
records = append(records, row)
435433
}
436434

437435
if err := rows.Err(); err != nil {
438-
return nil, err
436+
return nil, 0, err
439437
}
440438

441-
return results, nil
439+
// Prepend the columns to the records.
440+
results := append([][]string{columns}, records...)
441+
442+
return results, len(records), nil
442443
}
443444

444445
func (db *MSSQL) ExecutePendingChanges(changes []models.DBDMLChange) error {
@@ -578,9 +579,9 @@ func (db *MSSQL) GetPrimaryKeyColumnNames(database, table string) ([]string, err
578579

579580
pkColumnName := make([]string, 0)
580581
query := `
581-
SELECT
582+
SELECT
582583
c.name AS column_name
583-
FROM
584+
FROM
584585
sys.tables t
585586
INNER JOIN
586587
sys.schemas s
@@ -604,7 +605,7 @@ func (db *MSSQL) GetPrimaryKeyColumnNames(database, table string) ([]string, err
604605
ic.column_id = c.column_id
605606
AND
606607
t.object_id = c.object_id
607-
WHERE
608+
WHERE
608609
DB_NAME() = @p2
609610
AND
610611
t.name = @p3

drivers/mysql.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func (db *MySQL) GetTableColumns(database, table string) (results [][]string, er
139139
return nil, err
140140
}
141141

142-
return
142+
return results, nil
143143
}
144144

145145
func (db *MySQL) GetConstraints(database, table string) (results [][]string, err error) {
@@ -188,7 +188,7 @@ func (db *MySQL) GetConstraints(database, table string) (results [][]string, err
188188
return nil, err
189189
}
190190

191-
return
191+
return results, nil
192192
}
193193

194194
func (db *MySQL) GetForeignKeys(database, table string) (results [][]string, err error) {
@@ -237,7 +237,7 @@ func (db *MySQL) GetForeignKeys(database, table string) (results [][]string, err
237237
return nil, err
238238
}
239239

240-
return
240+
return results, nil
241241
}
242242

243243
func (db *MySQL) GetIndexes(database, table string) (results [][]string, err error) {
@@ -287,7 +287,7 @@ func (db *MySQL) GetIndexes(database, table string) (results [][]string, err err
287287
return nil, err
288288
}
289289

290-
return
290+
return results, nil
291291
}
292292

293293
func (db *MySQL) GetRecords(database, table, where, sort string, offset, limit int) (paginatedResults [][]string, totalRecords int, err error) {
@@ -373,23 +373,22 @@ func (db *MySQL) GetRecords(database, table, where, sort string, offset, limit i
373373
return nil, 0, err
374374
}
375375

376-
return
376+
return paginatedResults, totalRecords, nil
377377
}
378378

379-
func (db *MySQL) ExecuteQuery(query string) (results [][]string, err error) {
379+
func (db *MySQL) ExecuteQuery(query string) ([][]string, int, error) {
380380
rows, err := db.Connection.Query(query)
381381
if err != nil {
382-
return nil, err
382+
return nil, 0, err
383383
}
384384
defer rows.Close()
385385

386386
columns, err := rows.Columns()
387387
if err != nil {
388-
return nil, err
388+
return nil, 0, err
389389
}
390390

391-
results = append(results, columns)
392-
391+
records := make([][]string, 0)
393392
for rows.Next() {
394393
rowValues := make([]interface{}, len(columns))
395394
for i := range columns {
@@ -398,21 +397,24 @@ func (db *MySQL) ExecuteQuery(query string) (results [][]string, err error) {
398397

399398
err = rows.Scan(rowValues...)
400399
if err != nil {
401-
return nil, err
400+
return nil, 0, err
402401
}
403402

404403
var row []string
405404
for _, col := range rowValues {
406405
row = append(row, string(*col.(*sql.RawBytes)))
407406
}
408407

409-
results = append(results, row)
408+
records = append(records, row)
410409
}
411410
if err := rows.Err(); err != nil {
412-
return nil, err
411+
return nil, 0, err
413412
}
414413

415-
return
414+
// Prepend the columns to the records.
415+
results := append([][]string{columns}, records...)
416+
417+
return results, len(records), nil
416418
}
417419

418420
func (db *MySQL) UpdateRecord(database, table, column, value, primaryKeyColumnName, primaryKeyValue string) error {
@@ -587,7 +589,7 @@ func (db *MySQL) GetPrimaryKeyColumnNames(database, table string) (primaryKeyCol
587589
return nil, rows.Err()
588590
}
589591

590-
return
592+
return primaryKeyColumnName, nil
591593
}
592594

593595
func (db *MySQL) SetProvider(provider string) {

drivers/postgres.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -672,19 +672,19 @@ func (db *Postgres) ExecuteDMLStatement(query string) (result string, err error)
672672
return fmt.Sprintf("%d rows affected", rowsAffected), nil
673673
}
674674

675-
func (db *Postgres) ExecuteQuery(query string) ([][]string, error) {
675+
func (db *Postgres) ExecuteQuery(query string) ([][]string, int, error) {
676676
rows, err := db.Connection.Query(query)
677677
if err != nil {
678-
return nil, err
678+
return nil, 0, err
679679
}
680680
defer rows.Close()
681681

682682
columns, err := rows.Columns()
683683
if err != nil {
684-
return nil, err
684+
return nil, 0, err
685685
}
686686

687-
results := [][]string{columns}
687+
records := make([][]string, 0)
688688
for rows.Next() {
689689
rowValues := make([]interface{}, len(columns))
690690
for i := range columns {
@@ -693,21 +693,24 @@ func (db *Postgres) ExecuteQuery(query string) ([][]string, error) {
693693

694694
err = rows.Scan(rowValues...)
695695
if err != nil {
696-
return nil, err
696+
return nil, 0, err
697697
}
698698

699699
var row []string
700700
for _, col := range rowValues {
701701
row = append(row, string(*col.(*sql.RawBytes)))
702702
}
703703

704-
results = append(results, row)
704+
records = append(records, row)
705705
}
706706
if err := rows.Err(); err != nil {
707-
return nil, err
707+
return nil, 0, err
708708
}
709709

710-
return results, nil
710+
// Prepend the columns to the records.
711+
results := append([][]string{columns}, records...)
712+
713+
return results, len(records), nil
711714
}
712715

713716
func (db *Postgres) ExecutePendingChanges(changes []models.DBDMLChange) error {

0 commit comments

Comments
 (0)