Skip to content

Commit 9a93cfa

Browse files
authored
Add show tables with schema (#173)
* Implement SHOW TABLES <schema> command * Fix command description in README.md
1 parent f1ab4c9 commit 9a93cfa

3 files changed

Lines changed: 19 additions & 5 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ and `{}` for a mutually exclusive keyword.
192192
| Switch database | `USE <database> [ROLE <role>];` | The role you set is used for accessing with [fine-grained access control](https://cloud.google.com/spanner/docs/fgac-about). |
193193
| Create database | `CREATE DATABSE <database>;` | |
194194
| Drop database | `DROP DATABASE <database>;` | |
195-
| List tables | `SHOW TABLES;` | |
195+
| List tables | `SHOW TABLES [<schema>];` | If schema is not provided, default schema is used |
196196
| Show table schema | `SHOW CREATE TABLE <table>;` | |
197197
| Show columns | `SHOW COLUMNS FROM <table>;` | |
198198
| Show indexes | `SHOW INDEX FROM <table>;` | |

statement.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ var (
116116
useRe = regexp.MustCompile(`(?is)^USE\s+([^\s]+)(?:\s+ROLE\s+(.+))?$`)
117117
showDatabasesRe = regexp.MustCompile(`(?is)^SHOW\s+DATABASES$`)
118118
showCreateTableRe = regexp.MustCompile(`(?is)^SHOW\s+CREATE\s+TABLE\s+(.+)$`)
119-
showTablesRe = regexp.MustCompile(`(?is)^SHOW\s+TABLES$`)
119+
showTablesRe = regexp.MustCompile(`(?is)^SHOW\s+TABLES(?:\s+(.+))?$`)
120120
showColumnsRe = regexp.MustCompile(`(?is)^(?:SHOW\s+COLUMNS\s+FROM)\s+(.+)$`)
121121
showIndexRe = regexp.MustCompile(`(?is)^SHOW\s+(?:INDEX|INDEXES|KEYS)\s+FROM\s+(.+)$`)
122122
explainRe = regexp.MustCompile(`(?is)^(?:EXPLAIN|DESC(?:RIBE)?)\s+(ANALYZE\s+)?(.+)$`)
@@ -168,7 +168,8 @@ func BuildStatementWithComments(stripped, raw string) (Statement, error) {
168168
matched := showCreateTableRe.FindStringSubmatch(stripped)
169169
return &ShowCreateTableStatement{Table: unquoteIdentifier(matched[1])}, nil
170170
case showTablesRe.MatchString(stripped):
171-
return &ShowTablesStatement{}, nil
171+
matched := showTablesRe.FindStringSubmatch(stripped)
172+
return &ShowTablesStatement{Schema: unquoteIdentifier(matched[1])}, nil
172173
case explainRe.MatchString(stripped):
173174
matched := explainRe.FindStringSubmatch(stripped)
174175
isAnalyze := matched[1] != ""
@@ -466,7 +467,9 @@ func isCreateTableDDL(ddl string, table string) bool {
466467
return regexp.MustCompile(re).MatchString(ddl)
467468
}
468469

469-
type ShowTablesStatement struct{}
470+
type ShowTablesStatement struct {
471+
Schema string
472+
}
470473

471474
func (s *ShowTablesStatement) Execute(ctx context.Context, session *Session) (*Result, error) {
472475
if session.InReadWriteTransaction() {
@@ -476,7 +479,8 @@ func (s *ShowTablesStatement) Execute(ctx context.Context, session *Session) (*R
476479
}
477480

478481
alias := fmt.Sprintf("Tables_in_%s", session.databaseId)
479-
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))
482+
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))
483+
stmt.Params["schema"] = s.Schema
480484

481485
iter, _ := session.RunQuery(ctx, stmt)
482486
defer iter.Stop()

statement_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,16 @@ func TestBuildStatement(t *testing.T) {
452452
input: "SHOW TABLES",
453453
want: &ShowTablesStatement{},
454454
},
455+
{
456+
desc: "SHOW TABLES statement with schema",
457+
input: "SHOW TABLES sch1",
458+
want: &ShowTablesStatement{Schema: "sch1"},
459+
},
460+
{
461+
desc: "SHOW TABLES statement with quoted schema",
462+
input: "SHOW TABLES `sch1`",
463+
want: &ShowTablesStatement{Schema: "sch1"},
464+
},
455465
{
456466
desc: "SHOW INDEX statement",
457467
input: "SHOW INDEX FROM t1",

0 commit comments

Comments
 (0)