Skip to content

Commit

Permalink
Implement DROP VIEW statement
Browse files Browse the repository at this point in the history
  • Loading branch information
apstndb committed Oct 20, 2024
1 parent 90f4dcb commit 46a94ff
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (CreateDatabase) isStatement() {}
func (CreateTable) isStatement() {}
func (CreateSequence) isStatement() {}
func (CreateView) isStatement() {}
func (DropView) isStatement() {}
func (CreateIndex) isStatement() {}
func (CreateVectorIndex) isStatement() {}
func (CreateRole) isStatement() {}
Expand Down Expand Up @@ -256,6 +257,7 @@ type DDL interface {
func (CreateDatabase) isDDL() {}
func (CreateTable) isDDL() {}
func (CreateView) isDDL() {}
func (DropView) isDDL() {}
func (CreateSequence) isDDL() {}
func (AlterTable) isDDL() {}
func (DropTable) isDDL() {}
Expand Down Expand Up @@ -1797,6 +1799,18 @@ type CreateView struct {
Query QueryExpr
}

// DropView is DROP VIEW statement node.
//
// DROP VIEW {{.Name | sql}}
type DropView struct {
// pos = Drop
// end = Name.end

Drop token.Pos

Name *Ident
}

// AlterTable is ALTER TABLE statement node.
//
// ALTER TABLE {{.Name | sql}} {{.TableAlteration | sql}}
Expand Down
3 changes: 3 additions & 0 deletions ast/pos.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,9 @@ func (c *CreateView) End() token.Pos {
return c.Query.End()
}

func (d *DropView) Pos() token.Pos { return d.Drop }
func (d *DropView) End() token.Pos { return d.Name.End() }

func (c *ColumnDef) Pos() token.Pos {
return c.Name.Pos()
}
Expand Down
2 changes: 2 additions & 0 deletions ast/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,8 @@ func (c *CreateView) SQL() string {
return sql
}

func (d *DropView) SQL() string { return "DROP VIEW " + d.Name.SQL() }

func (c *ColumnDef) SQL() string {
sql := c.Name.SQL() + " " + c.Type.SQL()
if c.NotNull {
Expand Down
12 changes: 12 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2160,6 +2160,8 @@ func (p *Parser) parseDDL() ast.DDL {
return p.parseDropVectorIndex(pos)
case p.Token.IsKeywordLike("SEQUENCE"):
return p.parseDropSequence(pos)
case p.Token.IsKeywordLike("VIEW"):
return p.parseDropView(pos)
case p.Token.IsKeywordLike("ROLE"):
return p.parseDropRole(pos)
case p.Token.IsKeywordLike("CHANGE"):
Expand Down Expand Up @@ -2314,6 +2316,16 @@ func (p *Parser) parseCreateView(pos token.Pos) *ast.CreateView {
}
}

func (p *Parser) parseDropView(pos token.Pos) *ast.DropView {
p.expectKeywordLike("VIEW")
name := p.parseIdent()

return &ast.DropView{
Drop: pos,
Name: name,
}
}

func (p *Parser) parseColumnDef() *ast.ColumnDef {
name := p.parseIdent()
t, notNull, null := p.parseTypeNotNull()
Expand Down
1 change: 1 addition & 0 deletions testdata/input/ddl/drop_view.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP VIEW SingersView
14 changes: 14 additions & 0 deletions testdata/result/ddl/drop_view.sql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--- drop_view.sql
DROP VIEW SingersView
--- AST
&ast.DropView{
Drop: 0,
Name: &ast.Ident{
NamePos: 10,
NameEnd: 21,
Name: "SingersView",
},
}

--- SQL
DROP VIEW SingersView
14 changes: 14 additions & 0 deletions testdata/result/statement/drop_view.sql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--- drop_view.sql
DROP VIEW SingersView
--- AST
&ast.DropView{
Drop: 0,
Name: &ast.Ident{
NamePos: 10,
NameEnd: 21,
Name: "SingersView",
},
}

--- SQL
DROP VIEW SingersView

0 comments on commit 46a94ff

Please sign in to comment.