Skip to content

Commit

Permalink
feat(gui): add a function used to perform a select query based on the…
Browse files Browse the repository at this point in the history
… selected table
  • Loading branch information
danvergara committed May 3, 2021
1 parent 75fd4a1 commit 2b64c3a
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions pkg/gui/database_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,95 @@ func (gui *Gui) runQuery() func(g *gocui.Gui, v *gocui.View) error {
return nil
}
}

func (gui *Gui) selectTable(g *gocui.Gui, v *gocui.View) error {
_, cy := v.Cursor()

t, err := v.Line(cy)
if err != nil {
return err
}

query := fmt.Sprintf("SELECT * FROM %s;", t)

rows, err := gui.c.DB().Queryx(query)
if err != nil {
return err
}

// Gets the names of the columns of the result set.
columnNames, err := rows.Columns()
if err != nil {
return err
}

resultSet := [][]string{}

for rows.Next() {
// cols is an []interface{} of all of the column results.
cols, err := rows.SliceScan()
if err != nil {
return err
}

// Convert []interface{} into []string.
s := make([]string, len(cols))
for i, v := range cols {
s[i] = fmt.Sprint(v)
}

resultSet = append(resultSet, s)
}

ov, err := gui.g.View("rows")
if err != nil {
return err
}

// Cleans the view.
ov.Rewind()
ov.Clear()

// Setup the table.
table := tablewriter.NewWriter(ov)
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: true})
table.SetHeader(columnNames)
// Add Bulk Data.
table.AppendBulk(resultSet)
table.Render()
return nil
}

func cursorUp(g *gocui.Gui, v *gocui.View) error {
if v != nil {
ox, oy := v.Origin()
cx, cy := v.Cursor()

if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 {
if err := v.SetOrigin(ox, oy-1); err != nil {
return err
}
}
}
return nil
}

func cursorDown(g *gocui.Gui, v *gocui.View) error {
if v != nil {
cx, cy := v.Cursor()

l, err := v.Line(cy + 1)
if err != nil {
return err
}
if l != "" {
if err := v.SetCursor(cx, cy+1); err != nil {
ox, oy := v.Origin()
if err := v.SetOrigin(ox, oy+1); err != nil {
return err
}
}
}
}
return nil
}

0 comments on commit 2b64c3a

Please sign in to comment.