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

Support DROP VIEW statement #128

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ A list of ZetaSQL ( Google Standard SQL ) specifications and features supported
- [x] DROP TABLE
- [ ] DROP SNAPSHOT TABLE
- [ ] DROP EXTERNAL TABLE
- [ ] DROP VIEW
- [x] DROP VIEW
- [ ] DROP MATERIALIZED VIEW
- [x] DROP FUNCTION
- [ ] DROP TABLE FUNCTION
Expand Down
12 changes: 12 additions & 0 deletions exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ INSERT recreate_table (b) VALUES ('hello');
CREATE OR REPLACE TABLE TableA(product string, quantity int64);
INSERT TableA (product, quantity) SELECT 'top load washer', 10;
INSERT INTO TableA (product, quantity) SELECT * FROM UNNEST([('microwave', 20), ('dishwasher', 30)]);
`,
},
{
name: "create view",
query: `
CREATE VIEW _view_a AS SELECT * FROM TableA
`,
},
{
name: "drop view",
query: `
DROP VIEW IF EXISTS _view_a
`,
},
{
Expand Down
5 changes: 3 additions & 2 deletions internal/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1344,10 +1344,11 @@ func (n *DropStmtNode) FormatSQL(ctx context.Context) (string, error) {
}
namePath := namePathFromContext(ctx)
tableName := namePath.format(n.node.NamePath())
objectType := n.node.ObjectType()
if n.node.IsIfExists() {
return fmt.Sprintf("DROP TABLE IF EXISTS `%s`", tableName), nil
return fmt.Sprintf("DROP %s IF EXISTS `%s`", objectType, tableName), nil
}
return fmt.Sprintf("DROP TABLE `%s`", tableName), nil
return fmt.Sprintf("DROP %s `%s`", objectType, tableName), nil
}

func (n *DropMaterializedViewStmtNode) FormatSQL(ctx context.Context) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/stmt_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ type DropStmtAction struct {

func (a *DropStmtAction) exec(ctx context.Context, conn *Conn) error {
switch a.objectType {
case "TABLE":
case "TABLE", "VIEW":
if _, err := conn.ExecContext(ctx, a.formattedQuery, a.args...); err != nil {
return fmt.Errorf("failed to exec %s: %w", a.query, err)
}
Expand Down