Skip to content

Commit 0ec3300

Browse files
committed
Merge PR cloudspannerecosystem#368: feat: support DROP VIEW IF EXISTS
2 parents 04460dd + c3a1c31 commit 0ec3300

6 files changed

Lines changed: 49 additions & 5 deletions

File tree

ast/ast.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,12 +2832,13 @@ type CreateView struct {
28322832

28332833
// DropView is DROP VIEW statement node.
28342834
//
2835-
// DROP VIEW {{.Name | sql}}
2835+
// DROP VIEW {{if .IfExists}}IF EXISTS{{end}} {{.Name | sql}}
28362836
type DropView struct {
28372837
// pos = Drop
28382838
// end = Name.end
28392839

2840-
Drop token.Pos
2840+
Drop token.Pos
2841+
IfExists bool
28412842

28422843
Name *Path
28432844
}

ast/sql.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,9 @@ func (c *CreateView) SQL() string {
860860
" SQL SECURITY " + string(c.SecurityType) + " AS " + c.Query.SQL()
861861
}
862862

863-
func (d *DropView) SQL() string { return "DROP VIEW " + d.Name.SQL() }
863+
func (d *DropView) SQL() string {
864+
return "DROP VIEW " + strOpt(d.IfExists, "IF EXISTS ") + d.Name.SQL()
865+
}
864866

865867
func (c *ColumnDef) SQL() string {
866868
return c.Name.SQL() + " " + c.Type.SQL() +

parser.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3485,11 +3485,13 @@ func (p *Parser) parseCreateView(pos token.Pos, orReplace bool) *ast.CreateView
34853485

34863486
func (p *Parser) parseDropView(pos token.Pos) *ast.DropView {
34873487
p.expectKeywordLike("VIEW")
3488+
ifExists := p.parseIfExists()
34883489
name := p.parsePath()
34893490

34903491
return &ast.DropView{
3491-
Drop: pos,
3492-
Name: name,
3492+
Drop: pos,
3493+
IfExists: ifExists,
3494+
Name: name,
34933495
}
34943496
}
34953497

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP VIEW IF EXISTS SingersView
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--- drop_view_if_exists.sql
2+
DROP VIEW IF EXISTS SingersView
3+
4+
--- AST
5+
&ast.DropView{
6+
IfExists: true,
7+
Name: &ast.Path{
8+
Idents: []*ast.Ident{
9+
&ast.Ident{
10+
NamePos: 20,
11+
NameEnd: 31,
12+
Name: "SingersView",
13+
},
14+
},
15+
},
16+
}
17+
18+
--- SQL
19+
DROP VIEW IF EXISTS SingersView
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--- drop_view_if_exists.sql
2+
DROP VIEW IF EXISTS SingersView
3+
4+
--- AST
5+
&ast.DropView{
6+
IfExists: true,
7+
Name: &ast.Path{
8+
Idents: []*ast.Ident{
9+
&ast.Ident{
10+
NamePos: 20,
11+
NameEnd: 31,
12+
Name: "SingersView",
13+
},
14+
},
15+
},
16+
}
17+
18+
--- SQL
19+
DROP VIEW IF EXISTS SingersView

0 commit comments

Comments
 (0)