From 251dc14e79274024c0b3c5781d237ef82c354f4d Mon Sep 17 00:00:00 2001 From: Tim Abell Date: Sun, 28 Jan 2024 03:30:15 +0000 Subject: [PATCH 1/2] Test mysql with connectionstring (locally) https://github.com/timabell/schema-explorer/issues/10 https://github.com/timabell/schema-explorer/pull/14 --- mysql/test-mysql-connectionstring.sh | 15 +++++++++++++++ test.sh | 1 + 2 files changed, 16 insertions(+) create mode 100755 mysql/test-mysql-connectionstring.sh 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 From f54f0c10ce2558684af22e33c829b08295508e9a Mon Sep 17 00:00:00 2001 From: kmpm Date: Wed, 13 Nov 2019 11:17:36 +0100 Subject: [PATCH 2/2] fix: get selected database if none specified Gets the selected database from the server if not detected by configuration before getting columns on mysql. Closes #10 --- mysql/mysql.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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 +}