Skip to content

Commit

Permalink
Add database results visualization menu
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgerojas26 committed Jul 25, 2023
1 parent 51770d9 commit 33f17ad
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 84 deletions.
82 changes: 82 additions & 0 deletions components/ResultTable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package components

import (
"lazysql/drivers"
"lazysql/utils"

"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)

func RenderColumns(tableName string, table *tview.Table) {
columns := drivers.Database.DescribeTable(tableName)
table.Clear()
utils.AddTableRows(table, columns)
}

func RenderConstraints(tableName string, table *tview.Table) {
constraints := drivers.Database.GetTableConstraints(tableName)
table.Clear()
utils.AddTableRows(table, constraints)
}

func RenderForeignKeys(tableName string, table *tview.Table) {
foreignKeys := drivers.Database.GetTableForeignKeys(tableName)
table.Clear()
utils.AddTableRows(table, foreignKeys)
}

func RenderIndexes(tableName string, table *tview.Table) {
indexes := drivers.Database.GetTableIndexes(tableName)
table.Clear()
utils.AddTableRows(table, indexes)
}

func AddVimKeyBindings(table *tview.Table, tableTree *tview.Flex, App *tview.Application, dbTablePages *tview.Pages) {

table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
selectedRow, selectedColumn := table.GetSelection()
rowCount := table.GetRowCount()

if event.Rune() == 99 { // c Key
cell := table.GetCell(selectedRow, selectedColumn)
inputField := tview.NewInputField()
inputField.SetText(cell.Text)
inputField.SetDoneFunc(func(key tcell.Key) {
cell.SetText(inputField.GetText())
dbTablePages.RemovePage("edit")
App.SetFocus(table)
})
x, y, width := cell.GetLastPosition()
inputField.SetRect(x, y, width+1, 1)
dbTablePages.AddPage("edit", inputField, false, true)
App.SetFocus(inputField)

} else if event.Rune() == 36 { // $ Key
colCount := table.GetColumnCount()
table.Select(selectedRow, colCount-1)
} else if event.Rune() == 48 { // 0 Key
table.Select(selectedRow, 0)
} else if event.Rune() == 103 { // g Key
go table.Select(1, selectedColumn)
} else if event.Rune() == 71 { // G Key
go table.Select(rowCount-1, selectedColumn)
} else if event.Rune() == 100 { // d Key
if selectedRow+7 > rowCount-1 {
go table.Select(rowCount-1, selectedColumn)
} else {
go table.Select(selectedRow+7, selectedColumn)
}
} else if event.Rune() == 117 { // u Key
if selectedRow-7 < 1 {
go table.Select(1, selectedColumn)
} else {
go table.Select(selectedRow-7, selectedColumn)
}
} else if event.Rune() == 72 { // H Key
App.SetFocus(tableTree)
}

return event
})
}
53 changes: 53 additions & 0 deletions drivers/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func (db *MySql) Connect() error {
var err error

db.connection, err = dburl.Open(db.connectionString)
db.connection.SetConnMaxIdleTime(10000)
db.connection.SetConnMaxLifetime(10000)
if err != nil {
return err
}
Expand Down Expand Up @@ -120,6 +122,57 @@ func (db *MySql) DescribeTable(table string) (results [][]string) {
return
}

func (db *MySql) GetTableConstraints(table string) (results [][]string) {
rows, _ := db.connection.Query("SELECT COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_COLUMN_NAME, REFERENCED_TABLE_NAME FROM information_schema.KEY_COLUMN_USAGE where TABLE_NAME = " + "'" + table + "'")
defer rows.Close()

results = append(results, []string{"COLUMN_NAME", "CONSTRAINT_NAME", "REFERENCED_COLUMN_NAME", "REFERENCED_TABLE_NAME"})

for rows.Next() {
var columnName, constraintName, referencedColumnName, referencedTableName string
rows.Scan(&columnName, &constraintName, &referencedColumnName, &referencedTableName)

results = append(results, []string{columnName, constraintName, referencedColumnName, referencedTableName})

}

return
}

func (db *MySql) GetTableForeignKeys(table string) (results [][]string) {
rows, _ := db.connection.Query("SELECT TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM information_schema.KEY_COLUMN_USAGE where REFERENCED_TABLE_NAME = " + "'" + table + "'")
defer rows.Close()

results = append(results, []string{"TABLE_NAME", "COLUMN_NAME", "CONSTRAINT_NAME", "REFERENCED_TABLE_NAME", "REFERENCED_COLUMN_NAME"})

for rows.Next() {
var tableName, columnName, constraintName, referencedTableName, referencedColumnName string
rows.Scan(&tableName, &columnName, &constraintName, &referencedTableName, &referencedColumnName)

results = append(results, []string{tableName, columnName, constraintName, referencedTableName, referencedColumnName})

}

return
}

func (db *MySql) GetTableIndexes(table string) (results [][]string) {
rows, _ := db.connection.Query("SHOW INDEX FROM " + table)
defer rows.Close()

results = append(results, []string{"Table", "Non_unique", "Key_name", "Seq_in_index", "Column_name", "Collation", "Cardinality", "Sub_part", "Packed", "Null", "Index_type", "Comment"})

for rows.Next() {
var tableName, nonUnique, keyName, seqInIndex, columnName, collation, cardinality, subPart, packed, null, indexType, comment string
rows.Scan(&tableName, &nonUnique, &keyName, &seqInIndex, &columnName, &collation, &cardinality, &subPart, &packed, &null, &indexType, &comment)

results = append(results, []string{tableName, nonUnique, keyName, seqInIndex, columnName, collation, cardinality, subPart, packed, null, indexType, comment})

}

return
}

func (db *MySql) GetTableData(table string, offset int, limit int, appendColumns bool) (results [][]string) {
defaultLimit := 100

Expand Down
Loading

0 comments on commit 33f17ad

Please sign in to comment.