Skip to content

Commit

Permalink
refactor(gui): refactor the databse helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
danvergara committed May 3, 2021
1 parent 809292e commit 7b19d6d
Showing 1 changed file with 50 additions and 98 deletions.
148 changes: 50 additions & 98 deletions pkg/gui/database_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,40 +50,61 @@ func (gui *Gui) showTables() error {
return nil
}

// runQuery run the introduced query in the query panel.
func (gui *Gui) runQuery() func(g *gocui.Gui, v *gocui.View) error {
return func(g *gocui.Gui, v *gocui.View) error {
resultSet := [][]string{}
// renderTable renders the result set as a table in the terminal output.
func renderTable(v *gocui.View, columns []string, resultSet [][]string) {
table := tablewriter.NewWriter(v)
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: true})
table.SetHeader(columns)
// Add Bulk Data.
table.AppendBulk(resultSet)
table.Render()
}

// Cleans the view.
v.Rewind()
// query returns performs the query and returns the result set and the colum names.
func (gui *Gui) query(q string) ([][]string, []string, error) {
resultSet := [][]string{}

// Runs the query extracting the content of the view calling the Buffer method.
rows, err := gui.c.DB().Queryx(v.Buffer())
// Runs the query extracting the content of the view calling the Buffer method.
rows, err := gui.c.DB().Queryx(q)
if err != nil {
return nil, nil, err
}

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

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

// Gets the names of the columns of the result set.
columnNames, err := rows.Columns()
if err != nil {
return err
// Convert []interface{} into []string.
s := make([]string, len(cols))
for i, v := range cols {
s[i] = fmt.Sprint(v)
}

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

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

resultSet = append(resultSet, s)
// runQuery run the introduced query in the query panel.
func (gui *Gui) inputQuery() func(g *gocui.Gui, v *gocui.View) error {
return func(g *gocui.Gui, v *gocui.View) error {

// Cleans the view.
v.Rewind()

resultSet, columnNames, err := gui.query(v.Buffer())
if err != nil {
return err
}

ov, err := gui.g.View("rows")
Expand All @@ -95,18 +116,13 @@ func (gui *Gui) runQuery() func(g *gocui.Gui, v *gocui.View) error {
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()
renderTable(ov, columnNames, resultSet)

return nil
}
}

// selectTable perfoms a select statement based on the selected table.
func (gui *Gui) selectTable(g *gocui.Gui, v *gocui.View) error {
_, cy := v.Cursor()

Expand All @@ -116,36 +132,11 @@ func (gui *Gui) selectTable(g *gocui.Gui, v *gocui.View) error {
}

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

rows, err := gui.c.DB().Queryx(query)
resultSet, columnNames, err := gui.query(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
Expand All @@ -155,46 +146,7 @@ func (gui *Gui) selectTable(g *gocui.Gui, v *gocui.View) error {
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()
renderTable(ov, columnNames, resultSet)

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 7b19d6d

Please sign in to comment.