Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add show tables with schema #173

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ 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 in a default schema| `SHOW TABLES;` | |
| List tables in a named schema | `SHOW TABLES <schema>;` | |
apstndb marked this conversation as resolved.
Show resolved Hide resolved
| Show table schema | `SHOW CREATE TABLE <table>;` | |
| Show columns | `SHOW COLUMNS FROM <table>;` | |
| Show indexes | `SHOW INDEX FROM <table>;` | |
Expand All @@ -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 [{<seconds>\|<RFC3339-formatted time>}] [PRIORITY {HIGH\|MEDIUM\|LOW}] [TAG <tag>];` | `<seconds>` and `<RFC3339-formatted time>` 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;` | |
apstndb marked this conversation as resolved.
Show resolved Hide resolved
| Exit CLI | `EXIT;` | |
apstndb marked this conversation as resolved.
Show resolved Hide resolved

## Customize prompt

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
Loading