diff --git a/README.md b/README.md index 0fd8e70..0483dde 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/exec_test.go b/exec_test.go index afedd69..a1b9948 100644 --- a/exec_test.go +++ b/exec_test.go @@ -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 `, }, { diff --git a/internal/formatter.go b/internal/formatter.go index 7acd748..b5e776d 100644 --- a/internal/formatter.go +++ b/internal/formatter.go @@ -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) { diff --git a/internal/stmt_action.go b/internal/stmt_action.go index c19a26f..828ced9 100644 --- a/internal/stmt_action.go +++ b/internal/stmt_action.go @@ -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) }