Skip to content

Commit

Permalink
Implement SHOW DDLS statement (#74)
Browse files Browse the repository at this point in the history
* Implement SHOW DDLS statement

* Update README.md

* Make SHOW DDLS headers empty

* Update golangci-lint to avoid timeout
  • Loading branch information
apstndb authored Dec 6, 2024
1 parent 0362a09 commit d6d0852
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.60
version: latest
args: --timeout=5m
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ test:

fasttest:
go test --tags skip_slow_test -v ./...

lint:
golangci-lint run
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ and `{}` for a mutually exclusive keyword.
| Set type query parameter | `SET PARAM <name> <type>;` | |
| Set value query parameter | `SET PARAM <name> = <value>;` | |
| Show variables | `SHOW PARAMS;` | |
| Show DDLs | `SHOW DDLS;` | |


## Customize prompt
Expand Down
23 changes: 23 additions & 0 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ var (
mutateRe = regexp.MustCompile(`(?is)MUTATE\s+(\S+)\s+(INSERT|UPDATE|INSERT_OR_UPDATE|REPLACE|DELETE)\s+(.+)$`)
showQueryProfilesRe = regexp.MustCompile(`(?is)^SHOW\s+QUERY\s+PROFILES$`)
showQueryProfileRe = regexp.MustCompile(`(?is)^SHOW\s+QUERY\s+PROFILE\s+(.*)$`)
showDdlsRe = regexp.MustCompile(`(?is)^SHOW\s+DDLS$`)
)

var (
Expand Down Expand Up @@ -300,6 +301,8 @@ func BuildCLIStatement(trimmed string) (Statement, error) {
return nil, err
}
return &ShowQueryProfileStatement{Fprint: fprint}, nil
case showDdlsRe.MatchString(trimmed):
return &ShowDdlsStatement{}, nil
default:
return nil, errStatementNotMatched
}
Expand Down Expand Up @@ -1415,6 +1418,26 @@ ORDER BY INTERVAL_END DESC`,
return result, nil
}

type ShowDdlsStatement struct{}

func (s *ShowDdlsStatement) Execute(ctx context.Context, session *Session) (*Result, error) {
resp, err := session.adminClient.GetDatabaseDdl(ctx, &adminpb.GetDatabaseDdlRequest{
Database: session.DatabasePath(),
})
if err != nil {
return nil, err
}

return &Result{
KeepVariables: true,
// intentionally empty column name to make TAB format valid DDL
ColumnNames: sliceOf(""),
Rows: sliceOf(toRow(hiter.StringsCollect(0, xiter.Map(
func(s string) string { return s + ";\n" },
slices.Values(resp.GetStatements()))))),
}, nil
}

type NopStatement struct{}

func (s *NopStatement) Execute(ctx context.Context, session *Session) (*Result, error) {
Expand Down
5 changes: 5 additions & 0 deletions statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,11 @@ func TestBuildStatement(t *testing.T) {
input: `SHOW PARAMS`,
want: &ShowParamsStatement{},
},
{
desc: "SHOW DDLS statement",
input: `SHOW DDLS`,
want: &ShowDdlsStatement{},
},
} {
t.Run(test.desc, func(t *testing.T) {
got, err := BuildStatement(test.input)
Expand Down

0 comments on commit d6d0852

Please sign in to comment.