Skip to content

Commit

Permalink
Update the pagination logic for queries done from the SQL editor
Browse files Browse the repository at this point in the history
  • Loading branch information
svanharmelen committed Jan 5, 2025
1 parent dbcb957 commit 44b15f3
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 69 deletions.
14 changes: 10 additions & 4 deletions components/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ func (pagination *Pagination) SetTotalRecords(total int) {
pagination.state.TotalRecords = total

offset := pagination.GetOffset()
limit := pagination.GetLimit() + offset

if offset < total {
offset++
}

limit := pagination.GetLimit() + pagination.GetOffset()
if limit > total {
limit = total
}
Expand All @@ -84,11 +84,14 @@ func (pagination *Pagination) SetLimit(limit int) {
offset := pagination.GetOffset()
total := pagination.GetTotalRecords()

if offset < total {
offset++
}
if limit > total {
limit = total
}

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

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

if offset < total {
offset++
}
if limit > total {
limit = total
}

pagination.textView.SetText(fmt.Sprintf("%d-%d of %d rows", offset+1, limit, total))
pagination.textView.SetText(fmt.Sprintf("%d-%d of %d rows", offset, limit, total))
}
6 changes: 3 additions & 3 deletions components/results_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,9 +609,9 @@ func (table *ResultsTable) subscribeToEditorChanges() {
table.SetLoading(true)
App.Draw()

rows, err := table.DBDriver.ExecuteQuery(query)
table.Pagination.SetTotalRecords(len(rows))
table.Pagination.SetLimit(len(rows))
rows, records, err := table.DBDriver.ExecuteQuery(query)
table.Pagination.SetTotalRecords(records)
table.Pagination.SetLimit(records)

if err != nil {
table.SetLoading(false)
Expand Down
7 changes: 5 additions & 2 deletions drivers/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ type Driver interface {
UpdateRecord(database, table, column, value, primaryKeyColumnName, primaryKeyValue string) error
DeleteRecord(database, table string, primaryKeyColumnName, primaryKeyValue string) error
ExecuteDMLStatement(query string) (string, error)
ExecuteQuery(query string) ([][]string, error)
ExecuteQuery(query string) ([][]string, int, error)
ExecutePendingChanges(changes []models.DBDMLChange) error
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
GetProvider() string
GetPrimaryKeyColumnNames(database, table string) ([]string, error)

// NOTE: This is used to get the primary key from the database table until I
// find a better way to do it. See *ResultsTable.GetPrimaryKeyValue()
SetProvider(provider string)
}
47 changes: 24 additions & 23 deletions drivers/mssqql.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (db *MSSQL) GetForeignKeys(database, table string) ([][]string, error) {

func (db *MSSQL) GetIndexes(database, table string) ([][]string, error) {
query := `
SELECT
SELECT
t.name AS table_name,
i.name AS index_name,
i.is_unique AS is_unique,
Expand All @@ -201,33 +201,33 @@ func (db *MSSQL) GetIndexes(database, table string) ([][]string, error) {
ic.is_included_column AS is_included,
i.has_filter AS has_filter,
i.filter_definition AS filter_definition
FROM
sys.tables t
FROM
sys.tables t
INNER JOIN
sys.schemas s
sys.schemas s
ON
t.schema_id = s.schema_id
INNER JOIN
sys.indexes i
sys.indexes i
ON
t.object_id = i.object_id
INNER JOIN
sys.index_columns ic
sys.index_columns ic
ON
i.object_id = ic.object_id
AND
i.index_id = ic.index_id
INNER JOIN
sys.columns c
sys.columns c
ON
t.object_id = c.object_id
AND
ic.column_id = c.column_id
WHERE
WHERE
DB_NAME() = @p1
AND
t.name = @p2
ORDER BY
ORDER BY
i.type_desc
`
return db.getTableInformations(query, database, table)
Expand Down Expand Up @@ -395,26 +395,24 @@ func (db *MSSQL) ExecuteDMLStatement(query string) (string, error) {
return fmt.Sprintf("%d rows affected", rowsAffected), nil
}

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

results := make([][]string, 0)
rows, err := db.Connection.Query(query)
if err != nil {
return nil, err
return nil, 0, err
}

defer rows.Close()

columns, err := rows.Columns()
if err != nil {
return nil, err
return nil, 0, err
}

results = append(results, columns)

records := make([][]string, 0)
for rows.Next() {
rowValues := make([]any, len(columns))

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

if err := rows.Scan(rowValues...); err != nil {
return nil, err
return nil, 0, err
}

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

results = append(results, row)
records = append(records, row)
}

if err := rows.Err(); err != nil {
return nil, err
return nil, 0, err
}

return results, nil
// Prepend the columns to the records.
results := append([][]string{columns}, records...)

return results, len(records), nil
}

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

pkColumnName := make([]string, 0)
query := `
SELECT
SELECT
c.name AS column_name
FROM
FROM
sys.tables t
INNER JOIN
sys.schemas s
Expand All @@ -604,7 +605,7 @@ func (db *MSSQL) GetPrimaryKeyColumnNames(database, table string) ([]string, err
ic.column_id = c.column_id
AND
t.object_id = c.object_id
WHERE
WHERE
DB_NAME() = @p2
AND
t.name = @p3
Expand Down
32 changes: 17 additions & 15 deletions drivers/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (db *MySQL) GetTableColumns(database, table string) (results [][]string, er
return nil, err
}

return
return results, nil
}

func (db *MySQL) GetConstraints(database, table string) (results [][]string, err error) {
Expand Down Expand Up @@ -188,7 +188,7 @@ func (db *MySQL) GetConstraints(database, table string) (results [][]string, err
return nil, err
}

return
return results, nil
}

func (db *MySQL) GetForeignKeys(database, table string) (results [][]string, err error) {
Expand Down Expand Up @@ -237,7 +237,7 @@ func (db *MySQL) GetForeignKeys(database, table string) (results [][]string, err
return nil, err
}

return
return results, nil
}

func (db *MySQL) GetIndexes(database, table string) (results [][]string, err error) {
Expand Down Expand Up @@ -287,7 +287,7 @@ func (db *MySQL) GetIndexes(database, table string) (results [][]string, err err
return nil, err
}

return
return results, nil
}

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

return
return paginatedResults, totalRecords, nil
}

func (db *MySQL) ExecuteQuery(query string) (results [][]string, err error) {
func (db *MySQL) ExecuteQuery(query string) ([][]string, int, error) {
rows, err := db.Connection.Query(query)
if err != nil {
return nil, err
return nil, 0, err
}
defer rows.Close()

columns, err := rows.Columns()
if err != nil {
return nil, err
return nil, 0, err
}

results = append(results, columns)

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

err = rows.Scan(rowValues...)
if err != nil {
return nil, err
return nil, 0, err
}

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

results = append(results, row)
records = append(records, row)
}
if err := rows.Err(); err != nil {
return nil, err
return nil, 0, err
}

return
// Prepend the columns to the records.
results := append([][]string{columns}, records...)

return results, len(records), nil
}

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

return
return primaryKeyColumnName, nil
}

func (db *MySQL) SetProvider(provider string) {
Expand Down
19 changes: 11 additions & 8 deletions drivers/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,19 +672,19 @@ func (db *Postgres) ExecuteDMLStatement(query string) (result string, err error)
return fmt.Sprintf("%d rows affected", rowsAffected), nil
}

func (db *Postgres) ExecuteQuery(query string) ([][]string, error) {
func (db *Postgres) ExecuteQuery(query string) ([][]string, int, error) {
rows, err := db.Connection.Query(query)
if err != nil {
return nil, err
return nil, 0, err
}
defer rows.Close()

columns, err := rows.Columns()
if err != nil {
return nil, err
return nil, 0, err
}

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

err = rows.Scan(rowValues...)
if err != nil {
return nil, err
return nil, 0, err
}

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

results = append(results, row)
records = append(records, row)
}
if err := rows.Err(); err != nil {
return nil, err
return nil, 0, err
}

return results, nil
// Prepend the columns to the records.
results := append([][]string{columns}, records...)

return results, len(records), nil
}

func (db *Postgres) ExecutePendingChanges(changes []models.DBDMLChange) error {
Expand Down
Loading

0 comments on commit 44b15f3

Please sign in to comment.