Skip to content

Commit

Permalink
Add query history navigation shortcuts and functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
murat-cileli committed Jun 19, 2024
1 parent e53c6d0 commit f141dda
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ DBee is a terminal-based TUI application written in Go, designed for connecting
- List database tables and views
- View table structures and browse data
- Execute SQL queries
- SQL query history **(New!)**
- Fixed columns in results table

[](https://github.com/murat-cileli/dbee/assets/6532000/d9d2cd86-e505-471d-91e4-d56cf8d34725)
Expand Down Expand Up @@ -51,6 +52,8 @@ DBee is a terminal-based TUI application written in Go, designed for connecting
**Main Page -> Query Pane**
<kbd>Alt</kbd> + <kbd>Q</kbd> : Focus query pane
<kbd>Alt</kbd> + <kbd>Enter</kbd> : Execute SQL query
<kbd>Alt</kbd> + <kbd>Up</kbd> : Go back in query history
<kbd>Alt</kbd> + <kbd>Down</kbd> : Go forward in query history
<kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>V</kbd> : Paste
<kbd>Ctrl</kbd> + <kbd>Z</kbd> : Undo

Expand Down
Binary file modified bin/darwin-amd64/dbee
Binary file not shown.
Binary file modified bin/darwin-arm64/dbee
Binary file not shown.
Binary file modified bin/freebsd-amd64/dbee
Binary file not shown.
Binary file modified bin/freebsd-i386/dbee
Binary file not shown.
Binary file modified bin/linux-amd64/dbee
Binary file not shown.
Binary file modified bin/linux-i386/dbee
Binary file not shown.
Binary file modified bin/windows-amd64/dbee.exe
Binary file not shown.
Binary file modified bin/windows-i386/dbee.exe
Binary file not shown.
7 changes: 5 additions & 2 deletions src/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ func (database *databaseType) Connect() error {
return nil
}

func (database *databaseType) Query(query string) (*sql.Rows, error) {
func (database *databaseType) Query(query string, addToHistory bool) (*sql.Rows, error) {
rows, err := database.DB.Query(query)
if err != nil {
pageAlert.show(err.Error(), "error")
} else if addToHistory {
queryHistory.add(query)
queryHistory.resetIndex()
}

return rows, err
Expand All @@ -68,5 +71,5 @@ func (database *databaseType) getTables() (*sql.Rows, error) {
} else if database.Driver == "postgres" {
query = "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema'"
}
return database.Query(query)
return database.Query(query, false)
}
16 changes: 13 additions & 3 deletions src/page_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,20 @@ func (pageMain *pageMainType) build() {
textAreaQuery.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyEnter && event.Modifiers() == tcell.ModAlt {
event = nil
results, err := database.Query(textAreaQuery.GetText())
results, err := database.Query(textAreaQuery.GetText(), true)
if err == nil {
pageMain.loadQueryResults(results)
}
} else if event.Key() == tcell.KeyUp && event.Modifiers() == tcell.ModAlt {
queryHistory := queryHistory.back()
if queryHistory != "" {
textAreaQuery.SetText(queryHistory, true)
}
} else if event.Key() == tcell.KeyDown && event.Modifiers() == tcell.ModAlt {
queryHistory := queryHistory.forward()
if queryHistory != "" {
textAreaQuery.SetText(queryHistory, true)
}
}
return event
})
Expand Down Expand Up @@ -102,15 +112,15 @@ func (pageMain *pageMainType) describeDatabaseObject() {
} else if database.DriverName == "PostgreSQL" {
query = "SELECT * FROM information_schema.columns WHERE table_name = '" + selectedObject + "'"
}
results, err := database.Query(query)
results, err := database.Query(query, false)
if err == nil {
pageMain.loadQueryResults(results)
}
}

func (pageMain *pageMainType) browseDatabaseObject() {
selectedObject, _ := listDatabaseObjects.GetItemText(listDatabaseObjects.GetCurrentItem())
results, err := database.Query("SELECT * FROM " + selectedObject + " LIMIT 10")
results, err := database.Query("SELECT * FROM "+selectedObject+" LIMIT 10", false)
if err == nil {
pageMain.loadQueryResults(results)
}
Expand Down
38 changes: 38 additions & 0 deletions src/query_history.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

type queryHistoryType struct {
history []string
index int
}

var queryHistory queryHistoryType

func (queryHistory *queryHistoryType) back() string {
if queryHistory.getUpperIndex() > 0 && queryHistory.index > 0 {
queryHistory.index--
return queryHistory.history[queryHistory.index]
}
queryHistory.index = 0
return ""
}

func (queryHistory *queryHistoryType) forward() string {
if queryHistory.index < queryHistory.getUpperIndex() {
queryHistory.index++
return queryHistory.history[queryHistory.index]
}
queryHistory.resetIndex()
return ""
}

func (queryHistory *queryHistoryType) add(historyEntry string) {
queryHistory.history = append(queryHistory.history, historyEntry)
}

func (queryHistory *queryHistoryType) getUpperIndex() int {
return len(queryHistory.history) - 1
}

func (queryHistory *queryHistoryType) resetIndex() {
queryHistory.index = queryHistory.getUpperIndex()
}

0 comments on commit f141dda

Please sign in to comment.