From 9a93cfafedd32a1c3aa7b57e9cf4c58c497dd157 Mon Sep 17 00:00:00 2001 From: apstndb <803393+apstndb@users.noreply.github.com> Date: Tue, 9 Jul 2024 21:42:24 +0900 Subject: [PATCH] Add show tables with schema (#173) * Implement SHOW TABLES command * Fix command description in README.md --- README.md | 2 +- statement.go | 12 ++++++++---- statement_test.go | 10 ++++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 86f0154..77292ed 100644 --- a/README.md +++ b/README.md @@ -192,7 +192,7 @@ and `{}` for a mutually exclusive keyword. | Switch database | `USE [ROLE ];` | The role you set is used for accessing with [fine-grained access control](https://cloud.google.com/spanner/docs/fgac-about). | | Create database | `CREATE DATABSE ;` | | | Drop database | `DROP DATABASE ;` | | -| List tables | `SHOW TABLES;` | | +| List tables | `SHOW TABLES [];` | If schema is not provided, default schema is used | | Show table schema | `SHOW CREATE TABLE ;` | | | Show columns | `SHOW COLUMNS FROM
;` | | | Show indexes | `SHOW INDEX FROM
;` | | diff --git a/statement.go b/statement.go index ecd89b5..92e70f9 100644 --- a/statement.go +++ b/statement.go @@ -116,7 +116,7 @@ var ( useRe = regexp.MustCompile(`(?is)^USE\s+([^\s]+)(?:\s+ROLE\s+(.+))?$`) showDatabasesRe = regexp.MustCompile(`(?is)^SHOW\s+DATABASES$`) showCreateTableRe = regexp.MustCompile(`(?is)^SHOW\s+CREATE\s+TABLE\s+(.+)$`) - showTablesRe = regexp.MustCompile(`(?is)^SHOW\s+TABLES$`) + showTablesRe = regexp.MustCompile(`(?is)^SHOW\s+TABLES(?:\s+(.+))?$`) showColumnsRe = regexp.MustCompile(`(?is)^(?:SHOW\s+COLUMNS\s+FROM)\s+(.+)$`) showIndexRe = regexp.MustCompile(`(?is)^SHOW\s+(?:INDEX|INDEXES|KEYS)\s+FROM\s+(.+)$`) explainRe = regexp.MustCompile(`(?is)^(?:EXPLAIN|DESC(?:RIBE)?)\s+(ANALYZE\s+)?(.+)$`) @@ -168,7 +168,8 @@ func BuildStatementWithComments(stripped, raw string) (Statement, error) { matched := showCreateTableRe.FindStringSubmatch(stripped) return &ShowCreateTableStatement{Table: unquoteIdentifier(matched[1])}, nil case showTablesRe.MatchString(stripped): - return &ShowTablesStatement{}, nil + matched := showTablesRe.FindStringSubmatch(stripped) + return &ShowTablesStatement{Schema: unquoteIdentifier(matched[1])}, nil case explainRe.MatchString(stripped): matched := explainRe.FindStringSubmatch(stripped) isAnalyze := matched[1] != "" @@ -466,7 +467,9 @@ func isCreateTableDDL(ddl string, table string) bool { return regexp.MustCompile(re).MatchString(ddl) } -type ShowTablesStatement struct{} +type ShowTablesStatement struct { + Schema string +} func (s *ShowTablesStatement) Execute(ctx context.Context, session *Session) (*Result, error) { if session.InReadWriteTransaction() { @@ -476,7 +479,8 @@ func (s *ShowTablesStatement) Execute(ctx context.Context, session *Session) (*R } alias := fmt.Sprintf("Tables_in_%s", session.databaseId) - stmt := spanner.NewStatement(fmt.Sprintf("SELECT t.TABLE_NAME AS `%s` FROM INFORMATION_SCHEMA.TABLES AS t WHERE t.TABLE_CATALOG = '' and t.TABLE_SCHEMA = ''", alias)) + stmt := spanner.NewStatement(fmt.Sprintf("SELECT t.TABLE_NAME AS `%s` FROM INFORMATION_SCHEMA.TABLES AS t WHERE t.TABLE_CATALOG = '' and t.TABLE_SCHEMA = @schema", alias)) + stmt.Params["schema"] = s.Schema iter, _ := session.RunQuery(ctx, stmt) defer iter.Stop() diff --git a/statement_test.go b/statement_test.go index 7657fc3..5236fcb 100644 --- a/statement_test.go +++ b/statement_test.go @@ -452,6 +452,16 @@ func TestBuildStatement(t *testing.T) { input: "SHOW TABLES", want: &ShowTablesStatement{}, }, + { + desc: "SHOW TABLES statement with schema", + input: "SHOW TABLES sch1", + want: &ShowTablesStatement{Schema: "sch1"}, + }, + { + desc: "SHOW TABLES statement with quoted schema", + input: "SHOW TABLES `sch1`", + want: &ShowTablesStatement{Schema: "sch1"}, + }, { desc: "SHOW INDEX statement", input: "SHOW INDEX FROM t1",