Skip to content

Commit

Permalink
Add show tables with schema (#173)
Browse files Browse the repository at this point in the history
* Implement SHOW TABLES <schema> command

* Fix command description in README.md
  • Loading branch information
apstndb authored Jul 9, 2024
1 parent f1ab4c9 commit 9a93cfa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ and `{}` for a mutually exclusive keyword.
| 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). |
| Create database | `CREATE DATABSE <database>;` | |
| Drop database | `DROP DATABASE <database>;` | |
| List tables | `SHOW TABLES;` | |
| List tables | `SHOW TABLES [<schema>];` | If schema is not provided, default schema is used |
| Show table schema | `SHOW CREATE TABLE <table>;` | |
| Show columns | `SHOW COLUMNS FROM <table>;` | |
| Show indexes | `SHOW INDEX FROM <table>;` | |
Expand Down
12 changes: 8 additions & 4 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -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+)?(.+)$`)
Expand Down Expand Up @@ -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] != ""
Expand Down Expand Up @@ -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() {
Expand All @@ -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()
Expand Down
10 changes: 10 additions & 0 deletions statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 9a93cfa

Please sign in to comment.