Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the pagination logic for queries done from the SQL editor #151

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
33 changes: 11 additions & 22 deletions components/results_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,36 +609,25 @@ 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)
App.Draw()
table.SetError(err.Error(), nil)
App.Draw()
} else {
table.UpdateRows(rows)
table.SetIsFiltering(false)

if len(rows) > 1 {
App.SetFocus(table)
table.HighlightTable()
table.Editor.SetBlur()
table.SetInputCapture(table.tableInputCapture)
App.Draw()
} else if len(rows) == 1 {
table.SetInputCapture(nil)
App.SetFocus(table.Editor)
table.Editor.Highlight()
table.RemoveHighlightTable()
table.SetIsFiltering(true)
App.Draw()
}
table.SetLoading(false)
table.SetIsFiltering(false)
table.HighlightTable()
table.Editor.SetBlur()
table.SetInputCapture(table.tableInputCapture)
table.EditorPages.SwitchToPage(pageNameTableEditorTable)
App.SetFocus(table)
App.Draw()
}
table.EditorPages.SwitchToPage(pageNameTableEditorTable)
App.Draw()
} else {
table.SetRecords([][]string{})
table.SetLoading(true)
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)
svanharmelen marked this conversation as resolved.
Show resolved Hide resolved
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
Loading