@@ -47,7 +47,10 @@ func (b *Builder) resolveDb(name string) sql.Database {
4747 return database
4848}
4949
50- func (b * Builder ) resolveDbForTable (table ast.TableName ) sql.Database {
50+ // resolveDbForTable attempts to resolve the database and schema name qualifiers
51+ // for a table. If the database is not specified, the current database is used.
52+ // If the specified schema is not found, `ok` will be false.
53+ func (b * Builder ) resolveDbForTable (table ast.TableName ) (sql.Database , bool ) {
5154 dbName := table .DbQualifier .String ()
5255 if dbName == "" {
5356 dbName = b .ctx .GetCurrentDatabase ()
@@ -72,12 +75,11 @@ func (b *Builder) resolveDbForTable(table ast.TableName) sql.Database {
7275 if err != nil {
7376 b .handleErr (err )
7477 }
75- if ! ok {
76- b .handleErr (sql .ErrDatabaseSchemaNotFound .New (schema ))
77- }
78+
79+ return database , ok
7880 }
7981
80- return database
82+ return database , true
8183}
8284
8385// buildAlterTable converts AlterTable AST nodes. If there is a single clause in the statement, it is returned as
@@ -167,13 +169,7 @@ func (b *Builder) buildDDL(inScope *scope, subQuery string, fullQuery string, c
167169 return
168170 }
169171 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
172+ return b .buildDropView (inScope , c )
177173 }
178174 return b .buildDropTable (inScope , c )
179175 case ast .AlterStr :
@@ -193,6 +189,41 @@ func (b *Builder) buildDDL(inScope *scope, subQuery string, fullQuery string, c
193189 return
194190}
195191
192+ func (b * Builder ) buildDropView (inScope * scope , c * ast.DDL ) (outScope * scope ) {
193+ outScope = inScope .push ()
194+ var dropViews []sql.Node
195+ dbName := c .FromViews [0 ].DbQualifier .String ()
196+ if dbName == "" {
197+ dbName = b .currentDb ().Name ()
198+ }
199+ for _ , v := range c .FromViews {
200+ if v .DbQualifier .String () != "" && v .DbQualifier .String () != dbName {
201+ err := sql .ErrUnsupportedFeature .New ("dropping views on multiple databases in the same statement" )
202+ b .handleErr (err )
203+ }
204+
205+ viewName := strings .ToLower (v .Name .String ())
206+ db , ok := b .resolveDbForTable (v )
207+ if ! ok {
208+ if c .IfExists {
209+ b .ctx .Session .Warn (& sql.Warning {
210+ Level : "Note" ,
211+ Code : mysql .ERBadTable ,
212+ Message : fmt .Sprintf ("Unknown view '%s'" , viewName ),
213+ })
214+ continue
215+ } else {
216+ b .handleErr (sql .ErrDatabaseSchemaNotFound .New (v .SchemaQualifier .String ()))
217+ }
218+ }
219+
220+ dropViews = append (dropViews , plan .NewSingleDropView (db , v .Name .String ()))
221+ }
222+
223+ outScope .node = plan .NewDropView (dropViews , c .IfExists )
224+ return
225+ }
226+
196227func (b * Builder ) buildDropTable (inScope * scope , c * ast.DDL ) (outScope * scope ) {
197228 outScope = inScope .push ()
198229 var dropTables []sql.Node
@@ -252,7 +283,10 @@ func (b *Builder) buildCreateTable(inScope *scope, c *ast.DDL) (outScope *scope)
252283 return b .buildCreateTableLike (inScope , c )
253284 }
254285
255- database := b .resolveDbForTable (c .Table )
286+ database , ok := b .resolveDbForTable (c .Table )
287+ if ! ok {
288+ b .handleErr (sql .ErrDatabaseSchemaNotFound .New (c .Table .SchemaQualifier .String ()))
289+ }
256290
257291 // In the case that no table spec is given but a SELECT Statement return the CREATE TABLE node.
258292 // if the table spec != nil it will get parsed below.
@@ -372,7 +406,11 @@ func (b *Builder) getIndexDefs(table sql.Table) sql.IndexDefs {
372406}
373407
374408func (b * Builder ) buildCreateTableLike (inScope * scope , ct * ast.DDL ) * scope {
375- database := b .resolveDbForTable (ct .Table )
409+ database , ok := b .resolveDbForTable (ct .Table )
410+ if ! ok {
411+ b .handleErr (sql .ErrDatabaseSchemaNotFound .New (ct .Table .SchemaQualifier .String ()))
412+ }
413+
376414 newTableName := strings .ToLower (ct .Table .Name .String ())
377415
378416 var pkSch sql.PrimaryKeySchema
@@ -383,7 +421,6 @@ func (b *Builder) buildCreateTableLike(inScope *scope, ct *ast.DDL) *scope {
383421 pkSch , coll , _ = b .tableSpecToSchema (inScope , outScope , database , strings .ToLower (ct .Table .Name .String ()), ct .TableSpec , false )
384422 }
385423
386- var ok bool
387424 var pkOrdinals []int
388425 var newSch sql.Schema
389426 newSchMap := make (map [string ]struct {})
0 commit comments