Skip to content

Commit

Permalink
fix<mysql>: get selected database if none specified
Browse files Browse the repository at this point in the history
Gets the selected database from the server if not detected by configuration before getting columns on mysql.
Closes #10
  • Loading branch information
kmpm authored and timabell committed Jan 28, 2024
1 parent 251dc14 commit f54f0c1
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,11 @@ func buildQuery(table *schema.Table, params *params.TableParams, peekFinder *dri
func (model mysqlModel) getColumns(dbc *sql.DB, table *schema.Table) (cols []*schema.Column, err error) {
// todo: parameterise
// todo: read all tables' columns in one query hit
sql := fmt.Sprintf("select column_name, data_type, is_nullable, character_maximum_length from information_schema.columns where table_schema = '%s' and table_name='%s' order by ordinal_position;", opts.Database, table.Name)
dbname := opts.Database
if dbname == "" {
dbname, _ = model.getSelectedDatabase(dbc)
}
sql := fmt.Sprintf("select column_name, data_type, is_nullable, character_maximum_length from information_schema.columns where table_schema = '%s' and table_name='%s' order by ordinal_position;", dbname, table.Name)

rows, err := dbc.Query(sql)
if err != nil {
Expand Down Expand Up @@ -584,3 +588,17 @@ func (model mysqlModel) SetTableDescription(database string, table string, descr
func (model mysqlModel) SetColumnDescription(database string, table string, column string, description string) (err error) {
return
}

func (model mysqlModel) getSelectedDatabase(dbc *sql.DB) (name string, err error) {
sql := "SELECT DATABASE();"
rows, err := dbc.Query(sql)
if err != nil {
log.Print(sql)
return
}
defer rows.Close()
for rows.Next() {
rows.Scan(&name)
}
return
}

0 comments on commit f54f0c1

Please sign in to comment.