From 2f01c7e017f524fddaa82e84c674373fb30b0097 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Omar=20Vergara=20P=C3=A9rez?= Date: Sat, 16 Nov 2024 23:29:31 -0600 Subject: [PATCH] fix(pkg): address ccoVeille comments --- pkg/client/client.go | 33 ++++++++++++++++++++++++++------- pkg/client/client_test.go | 8 ++++---- pkg/tui/tui.go | 4 ---- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 144901e..c37803f 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -149,15 +149,22 @@ func (c *Client) Query(q string, args ...interface{}) ([][]string, []string, err var ( resultSet = [][]string{} db *sqlx.DB + ok bool ) + db = c.db + if c.activeDatabase != "" { switch c.driver { case drivers.Postgres, drivers.PostgreSQL, drivers.MySQL: - db = c.dbs[c.activeDatabase] + db, ok = c.dbs[c.activeDatabase] + if !ok { + return nil, nil, fmt.Errorf( + "connection with %s database not found", + c.activeDatabase, + ) + } } - } else { - db = c.db } // Runs the query extracting the content of the view calling the Buffer method. @@ -194,6 +201,9 @@ func (c *Client) Query(q string, args ...interface{}) ([][]string, []string, err resultSet = append(resultSet, s) } + if err := rows.Err(); err != nil { + return nil, nil, err + } return resultSet, columnNames, nil } @@ -301,6 +311,9 @@ func (c *Client) ShowTablesPerDB(database string) ([]string, error) { tables = append(tables, table) } + if err := rows.Err(); err != nil { + return nil, err + } return tables, nil } @@ -333,6 +346,9 @@ func (c *Client) ShowTables() ([]string, error) { tables = append(tables, table) } + if err := rows.Err(); err != nil { + return nil, err + } return tables, nil } @@ -365,6 +381,9 @@ func (c *Client) ShowDatabases() ([]string, error) { databases = append(databases, database) } + if err := rows.Err(); err != nil { + return nil, err + } return databases, nil } @@ -446,9 +465,10 @@ func (c *Client) indexes(tableName string) ([][]string, []string, error) { func getDB(driver, connString, database string) (*sqlx.DB, error) { var newConnString string - if driver == drivers.MySQL { + switch driver { + case drivers.MySQL: newConnString = strings.Replace(connString, "/", fmt.Sprintf("/%s", database), 1) - } else { + default: u, err := url.Parse(connString) if err != nil { return nil, err @@ -460,8 +480,7 @@ func getDB(driver, connString, database string) (*sqlx.DB, error) { db, err := sqlx.Open(driver, newConnString) if err != nil { - - panic(err) + return nil, err } return db, nil diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 2bddfa7..a1c3749 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -285,8 +285,8 @@ func TestConstraints(t *testing.T) { t.Logf("constraints content %v", r) assert.NoError(t, err) - assert.Greater(t, len(r), 0) - assert.Greater(t, len(co), 0) + assert.NotEmpty(t, r) + assert.NotEmpty(t, co) } func TestIndexes(t *testing.T) { @@ -311,8 +311,8 @@ func TestIndexes(t *testing.T) { r, co, err := c.indexes("products") assert.NoError(t, err) - assert.Greater(t, len(r), 0) - assert.Greater(t, len(co), 0) + assert.NotEmpty(t, r) + assert.NotEmpty(t, co) } func TestMetadata(t *testing.T) { diff --git a/pkg/tui/tui.go b/pkg/tui/tui.go index 6f07fe7..890fa6b 100644 --- a/pkg/tui/tui.go +++ b/pkg/tui/tui.go @@ -419,10 +419,6 @@ func (t *Tui) updateTableMetadataOnChange(tableName string) { for i, sr := range m.TableContent.Rows { for j, sc := range sr { - // if i == 0 { - // t.aw.content.SetCell(i+1, j, &tview.TableCell{Text: sc, Color: tcell.ColorRed}) - // } else { - // } t.aw.content.SetCellSimple(i+1, j, sc) } }