From 37db765987a30f5e288a0d277eac56e2baf39a0e Mon Sep 17 00:00:00 2001 From: apstndb <803393+apstndb@users.noreply.github.com> Date: Tue, 9 Jul 2024 15:39:17 +0900 Subject: [PATCH 1/2] Implement SHOW TABLES command --- README.md | 7 ++++--- statement.go | 12 ++++++++---- statement_test.go | 10 ++++++++++ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 86f0154..dfcf145 100644 --- a/README.md +++ b/README.md @@ -192,7 +192,8 @@ 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 in a default schema| `SHOW TABLES;` | | +| List tables in a named schema | `SHOW TABLES ;` | | | Show table schema | `SHOW CREATE TABLE ;` | | | Show columns | `SHOW COLUMNS FROM
;` | | | Show indexes | `SHOW INDEX FROM
;` | | @@ -218,8 +219,8 @@ and `{}` for a mutually exclusive keyword. | Commit Read-Write Transaction | `COMMIT;` | | | Rollback Read-Write Transaction | `ROLLBACK;` | | | Start Read-Only Transaction | `BEGIN RO [{\|}] [PRIORITY {HIGH\|MEDIUM\|LOW}] [TAG ];` | `` and `` is used for stale read. See [Request Priority](#request-priority) for details on the priority. The tag you set is used as request tag. See also [Transaction Tags and Request Tags](#transaction-tags-and-request-tags).| -| End Read-Only Transaction | `CLOSE;` | | -| Exit CLI | `EXIT;` | | +| End Read-Only Transaction | `CLOSE;` | | +| Exit CLI | `EXIT;` | | ## Customize prompt 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", From 0d7393de10bf1a597b0276803a03ec9da1761dca Mon Sep 17 00:00:00 2001 From: apstndb <803393+apstndb@users.noreply.github.com> Date: Tue, 9 Jul 2024 21:32:34 +0900 Subject: [PATCH 2/2] Fix command description in README.md --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index dfcf145..77292ed 100644 --- a/README.md +++ b/README.md @@ -192,8 +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 in a default schema| `SHOW TABLES;` | | -| List tables in a named schema | `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
;` | | @@ -219,8 +218,8 @@ and `{}` for a mutually exclusive keyword. | Commit Read-Write Transaction | `COMMIT;` | | | Rollback Read-Write Transaction | `ROLLBACK;` | | | Start Read-Only Transaction | `BEGIN RO [{\|}] [PRIORITY {HIGH\|MEDIUM\|LOW}] [TAG ];` | `` and `` is used for stale read. See [Request Priority](#request-priority) for details on the priority. The tag you set is used as request tag. See also [Transaction Tags and Request Tags](#transaction-tags-and-request-tags).| -| End Read-Only Transaction | `CLOSE;` | | -| Exit CLI | `EXIT;` | | +| End Read-Only Transaction | `CLOSE;` | | +| Exit CLI | `EXIT;` | | ## Customize prompt