@@ -382,6 +382,25 @@ type NameAndSchema interface {
382382 Schema () sql.Schema
383383}
384384
385+ func convertColumnDefaultToString (ctx * sql.Context , def * sql.ColumnDefaultValue ) (string , error ) {
386+ // TODO : string literals should have character set introducer
387+ colDefaultStr := def .String ()
388+ defType := def .Type ()
389+
390+ // These types do not need to be quoted
391+ if ! def .IsLiteral () || colDefaultStr == "NULL" || types .IsTime (defType ) || types .IsText (defType ) {
392+ return colDefaultStr , nil
393+ }
394+ v , err := def .Eval (ctx , nil )
395+ if err != nil {
396+ return "" , err
397+ }
398+ if types .IsBit (def .OutType ) {
399+ return fmt .Sprintf ("b'%b'" , v ), nil
400+ }
401+ return fmt .Sprintf ("'%v'" , v ), nil
402+ }
403+
385404func (i * showCreateTablesIter ) produceCreateTableStatement (ctx * sql.Context , table sql.Table , schema sql.Schema , pkSchema sql.PrimaryKeySchema ) (string , error ) {
386405 colStmts := make ([]string , len (schema ))
387406 var primaryKeyCols []string
@@ -395,26 +414,20 @@ func (i *showCreateTablesIter) produceCreateTableStatement(ctx *sql.Context, tab
395414 tableCollation := table .Collation ()
396415 for i , col := range schema {
397416 var colDefaultStr string
417+ var err error
398418 if col .Default != nil && col .Generated == nil {
399419 // TODO : string literals should have character set introducer
400- colDefaultStr = col .Default .String ()
401- if colDefaultStr != "NULL" && col .Default .IsLiteral () && ! types .IsTime (col .Default .Type ()) && ! types .IsText (col .Default .Type ()) {
402- v , err := col .Default .Eval (ctx , nil )
403- if err != nil {
404- return "" , err
405- }
406- colDefaultStr = fmt .Sprintf ("'%v'" , v )
420+ colDefaultStr , err = convertColumnDefaultToString (ctx , col .Default )
421+ if err != nil {
422+ return "" , err
407423 }
408424 }
425+
409426 var onUpdateStr string
410427 if col .OnUpdate != nil {
411- onUpdateStr = col .OnUpdate .String ()
412- if onUpdateStr != "NULL" && col .OnUpdate .IsLiteral () && ! types .IsTime (col .OnUpdate .Type ()) && ! types .IsText (col .OnUpdate .Type ()) {
413- v , err := col .OnUpdate .Eval (ctx , nil )
414- if err != nil {
415- return "" , err
416- }
417- onUpdateStr = fmt .Sprintf ("'%v'" , v )
428+ onUpdateStr , err = convertColumnDefaultToString (ctx , col .OnUpdate )
429+ if err != nil {
430+ return "" , err
418431 }
419432 }
420433
0 commit comments