Skip to content

Commit

Permalink
support DROP VIEW statement (goccy#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy authored and ohaibbq committed Jan 23, 2024
1 parent c9242d3 commit 3b4e4a6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
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

0 comments on commit 3b4e4a6

Please sign in to comment.