@@ -166,7 +166,8 @@ func BuildStatementWithComments(stripped, raw string) (Statement, error) {
166166 return & ShowDatabasesStatement {}, nil
167167 case showCreateTableRe .MatchString (stripped ):
168168 matched := showCreateTableRe .FindStringSubmatch (stripped )
169- return & ShowCreateTableStatement {Table : unquoteIdentifier (matched [1 ])}, nil
169+ schema , table := extractSchemaAndTable (unquoteIdentifier (matched [1 ]))
170+ return & ShowCreateTableStatement {Schema : schema , Table : table }, nil
170171 case showTablesRe .MatchString (stripped ):
171172 matched := showTablesRe .FindStringSubmatch (stripped )
172173 return & ShowTablesStatement {Schema : unquoteIdentifier (matched [1 ])}, nil
@@ -186,10 +187,12 @@ func BuildStatementWithComments(stripped, raw string) (Statement, error) {
186187 }
187188 case showColumnsRe .MatchString (stripped ):
188189 matched := showColumnsRe .FindStringSubmatch (stripped )
189- return & ShowColumnsStatement {Table : unquoteIdentifier (matched [1 ])}, nil
190+ schema , table := extractSchemaAndTable (unquoteIdentifier (matched [1 ]))
191+ return & ShowColumnsStatement {Schema : schema , Table : table }, nil
190192 case showIndexRe .MatchString (stripped ):
191193 matched := showIndexRe .FindStringSubmatch (stripped )
192- return & ShowIndexStatement {Table : unquoteIdentifier (matched [1 ])}, nil
194+ schema , table := extractSchemaAndTable (unquoteIdentifier (matched [1 ]))
195+ return & ShowIndexStatement {Schema : schema , Table : table }, nil
193196 case dmlRe .MatchString (stripped ):
194197 return & DmlStatement {Dml : raw }, nil
195198 case pdmlRe .MatchString (stripped ):
@@ -211,7 +214,7 @@ func BuildStatementWithComments(stripped, raw string) (Statement, error) {
211214}
212215
213216func unquoteIdentifier (input string ) string {
214- return strings .Trim (input , "`" )
217+ return strings .Trim (strings . TrimSpace ( input ) , "`" )
215218}
216219
217220type SelectStatement struct {
@@ -431,7 +434,8 @@ func (s *ShowDatabasesStatement) Execute(ctx context.Context, session *Session)
431434}
432435
433436type ShowCreateTableStatement struct {
434- Table string
437+ Schema string
438+ Table string
435439}
436440
437441func (s * ShowCreateTableStatement ) Execute (ctx context.Context , session * Session ) (* Result , error ) {
@@ -444,26 +448,38 @@ func (s *ShowCreateTableStatement) Execute(ctx context.Context, session *Session
444448 return nil , err
445449 }
446450 for _ , stmt := range ddlResponse .Statements {
447- if isCreateTableDDL (stmt , s .Table ) {
451+ if isCreateTableDDL (stmt , s .Schema , s .Table ) {
452+ var fqn string
453+ if s .Schema == "" {
454+ fqn = s .Table
455+ } else {
456+ fqn = fmt .Sprintf ("%s.%s" , s .Schema , s .Table )
457+ }
458+
448459 resultRow := Row {
449- Columns : []string {s . Table , stmt },
460+ Columns : []string {fqn , stmt },
450461 }
451462 result .Rows = append (result .Rows , resultRow )
452463 break
453464 }
454465 }
455466 if len (result .Rows ) == 0 {
456- return nil , fmt .Errorf ("table %q doesn't exist" , s .Table )
467+ return nil , fmt .Errorf ("table %q doesn't exist in schema %q " , s .Table , s . Schema )
457468 }
458469
459470 result .AffectedRows = len (result .Rows )
460471
461472 return result , nil
462473}
463474
464- func isCreateTableDDL (ddl string , table string ) bool {
475+ func isCreateTableDDL (ddl string , schema string , table string ) bool {
465476 table = regexp .QuoteMeta (table )
466- re := fmt .Sprintf ("(?i)^CREATE TABLE (%s|`%s`)\\ s*\\ (" , table , table )
477+ var re string
478+ if schema == "" {
479+ re = fmt .Sprintf ("(?i)^CREATE TABLE (%s|`%s`)\\ s*\\ (" , table , table )
480+ } else {
481+ re = fmt .Sprintf ("(?i)^CREATE TABLE (%s|`%s`)\\ .(%s|`%s`)\\ s*\\ (" , schema , schema , table , table )
482+ }
467483 return regexp .MustCompile (re ).MatchString (ddl )
468484}
469485
@@ -620,7 +636,8 @@ func processPlanImpl(plan *pb.QueryPlan, withStats bool) (rows []Row, predicates
620636}
621637
622638type ShowColumnsStatement struct {
623- Table string
639+ Schema string
640+ Table string
624641}
625642
626643func (s * ShowColumnsStatement ) Execute (ctx context.Context , session * Session ) (* Result , error ) {
@@ -646,10 +663,10 @@ LEFT JOIN
646663LEFT JOIN
647664 INFORMATION_SCHEMA.COLUMN_OPTIONS CO USING(TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME)
648665WHERE
649- C.TABLE_SCHEMA = '' AND LOWER(C.TABLE_NAME) = LOWER(@table_name)
666+ LOWER( C.TABLE_SCHEMA) = LOWER(@table_schema) AND LOWER(C.TABLE_NAME) = LOWER(@table_name)
650667ORDER BY
651668 C.ORDINAL_POSITION ASC` ,
652- Params : map [string ]interface {}{"table_name" : s .Table }}
669+ Params : map [string ]interface {}{"table_name" : s .Table , "table_schema" : s . Schema }}
653670
654671 iter , _ := session .RunQuery (ctx , stmt )
655672 defer iter .Stop ()
@@ -659,7 +676,7 @@ ORDER BY
659676 return nil , err
660677 }
661678 if len (rows ) == 0 {
662- return nil , fmt .Errorf ("table %q doesn't exist" , s .Table )
679+ return nil , fmt .Errorf ("table %q doesn't exist in schema %q " , s .Table , s . Schema )
663680 }
664681
665682 return & Result {
@@ -669,8 +686,17 @@ ORDER BY
669686 }, nil
670687}
671688
689+ func extractSchemaAndTable (s string ) (string , string ) {
690+ schema , table , found := strings .Cut (s , "." )
691+ if ! found {
692+ return "" , unquoteIdentifier (s )
693+ }
694+ return unquoteIdentifier (schema ), unquoteIdentifier (table )
695+ }
696+
672697type ShowIndexStatement struct {
673- Table string
698+ Schema string
699+ Table string
674700}
675701
676702func (s * ShowIndexStatement ) Execute (ctx context.Context , session * Session ) (* Result , error ) {
@@ -692,8 +718,8 @@ func (s *ShowIndexStatement) Execute(ctx context.Context, session *Session) (*Re
692718FROM
693719 INFORMATION_SCHEMA.INDEXES I
694720WHERE
695- I.TABLE_SCHEMA = '' AND LOWER(TABLE_NAME) = LOWER(@table_name)` ,
696- Params : map [string ]interface {}{"table_name" : s .Table }}
721+ LOWER( I.TABLE_SCHEMA) = @table_schema AND LOWER(TABLE_NAME) = LOWER(@table_name)` ,
722+ Params : map [string ]interface {}{"table_name" : s .Table , "table_schema" : s . Schema }}
697723
698724 iter , _ := session .RunQuery (ctx , stmt )
699725 defer iter .Stop ()
@@ -703,7 +729,7 @@ WHERE
703729 return nil , err
704730 }
705731 if len (rows ) == 0 {
706- return nil , fmt .Errorf ("table %q doesn't exist" , s .Table )
732+ return nil , fmt .Errorf ("table %q doesn't exist in schema %q " , s .Table , s . Schema )
707733 }
708734
709735 return & Result {
0 commit comments