diff --git a/mysql/mysql.go b/mysql/mysql.go index fcedf54..26718f1 100644 --- a/mysql/mysql.go +++ b/mysql/mysql.go @@ -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 { @@ -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 +} diff --git a/mysql/test-mysql-connectionstring.sh b/mysql/test-mysql-connectionstring.sh new file mode 100755 index 0000000..96b8379 --- /dev/null +++ b/mysql/test-mysql-connectionstring.sh @@ -0,0 +1,15 @@ +#!/bin/sh +set -e + +echo "======================" +echo "mysql connectionstring" +echo "======================" + +./setup.sh + +cd .. +export schemaexplorer_driver=mysql +export schemaexplorer_live=false +export schemaexplorer_mysql_connection_string="ssetestusr:ssetestusrpass@tcp(localhost:3306)/ssetest" +go clean -testcache +go test sse_test.go #-test.v diff --git a/test.sh b/test.sh index eb6a65e..fbf93d6 100755 --- a/test.sh +++ b/test.sh @@ -18,6 +18,7 @@ popd > /dev/null pushd . > /dev/null cd mysql ./test-mysql.sh +./test-mysql-connectionstring.sh popd > /dev/null pushd . > /dev/null