Skip to content

Commit dcf349f

Browse files
committed
Fix drop view if exists for doltgres
1 parent 8a3476a commit dcf349f

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

sql/planbuilder/ddl.go

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,7 @@ func (b *Builder) buildDDL(inScope *scope, subQuery string, fullQuery string, c
167167
return
168168
}
169169
if len(c.FromViews) != 0 {
170-
plans := make([]sql.Node, len(c.FromViews))
171-
for i, v := range c.FromViews {
172-
db := b.resolveDbForTable(v)
173-
plans[i] = plan.NewSingleDropView(db, v.Name.String())
174-
}
175-
outScope.node = plan.NewDropView(plans, c.IfExists)
176-
return
170+
return b.buildDropView(inScope, c)
177171
}
178172
return b.buildDropTable(inScope, c)
179173
case ast.AlterStr:
@@ -193,6 +187,41 @@ func (b *Builder) buildDDL(inScope *scope, subQuery string, fullQuery string, c
193187
return
194188
}
195189

190+
func (b *Builder) buildDropView(inScope *scope, c *ast.DDL) (outScope *scope) {
191+
outScope = inScope.push()
192+
var dropViews []sql.Node
193+
dbName := c.FromViews[0].DbQualifier.String()
194+
if dbName == "" {
195+
dbName = b.currentDb().Name()
196+
}
197+
for _, v := range c.FromViews {
198+
if v.DbQualifier.String() != "" && v.DbQualifier.String() != dbName {
199+
err := sql.ErrUnsupportedFeature.New("dropping views on multiple databases in the same statement")
200+
b.handleErr(err)
201+
}
202+
viewName := strings.ToLower(v.Name.String())
203+
if c.IfExists {
204+
_, _, err := b.cat.Table(b.ctx, dbName, viewName)
205+
if sql.ErrTableNotFound.Is(err) && b.ctx != nil && b.ctx.Session != nil {
206+
b.ctx.Session.Warn(&sql.Warning{
207+
Level: "Note",
208+
Code: mysql.ERBadTable,
209+
Message: fmt.Sprintf("Unknown view '%s'", viewName),
210+
})
211+
continue
212+
} else if err != nil {
213+
b.handleErr(err)
214+
}
215+
}
216+
217+
db := b.resolveDbForTable(v)
218+
dropViews = append(dropViews, plan.NewSingleDropView(db, v.Name.String()))
219+
}
220+
221+
outScope.node = plan.NewDropView(dropViews, c.IfExists)
222+
return
223+
}
224+
196225
func (b *Builder) buildDropTable(inScope *scope, c *ast.DDL) (outScope *scope) {
197226
outScope = inScope.push()
198227
var dropTables []sql.Node

0 commit comments

Comments
 (0)