Skip to content

Commit 6406dba

Browse files
committed
add formatter for CreateTable statement
1 parent 4c8fcb3 commit 6406dba

File tree

2 files changed

+228
-6
lines changed

2 files changed

+228
-6
lines changed

sqlparser/ast.go

Lines changed: 211 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/hex"
2222
"encoding/json"
2323
"errors"
24+
"fmt"
2425
"strings"
2526

2627
"github.com/knocknote/vitess-sqlparser/sqltypes"
@@ -511,10 +512,118 @@ type DDL struct {
511512
IfExists bool
512513
}
513514

515+
type TableOption struct {
516+
Type TableOptionType
517+
StrValue string
518+
UintValue uint64
519+
}
520+
521+
type TableOptionType int
522+
523+
// TableOption types.
524+
const (
525+
TableOptionNone TableOptionType = iota
526+
TableOptionEngine
527+
TableOptionCharset
528+
TableOptionCollate
529+
TableOptionAutoIncrement
530+
TableOptionComment
531+
TableOptionAvgRowLength
532+
TableOptionCheckSum
533+
TableOptionCompression
534+
TableOptionConnection
535+
TableOptionPassword
536+
TableOptionKeyBlockSize
537+
TableOptionMaxRows
538+
TableOptionMinRows
539+
TableOptionDelayKeyWrite
540+
TableOptionRowFormat
541+
TableOptionStatsPersistent
542+
TableOptionShardRowID
543+
TableOptionPackKeys
544+
)
545+
546+
func (t TableOptionType) String() string {
547+
switch t {
548+
case TableOptionEngine:
549+
return "ENGINE"
550+
case TableOptionCharset:
551+
return "DEFAULT CHARSET"
552+
case TableOptionCollate:
553+
return "DEFAULT COLLATE"
554+
case TableOptionAutoIncrement:
555+
return "AUTO_INCREMENT"
556+
case TableOptionComment:
557+
return "COMMENT"
558+
case TableOptionAvgRowLength:
559+
return "AVG_ROW_LENGTH"
560+
case TableOptionCheckSum:
561+
return "CHECKSUM"
562+
case TableOptionConnection:
563+
return "CONNECTION"
564+
case TableOptionPassword:
565+
return "PASSWORD"
566+
case TableOptionKeyBlockSize:
567+
return "KEY_BLOCK_SIZE"
568+
case TableOptionMaxRows:
569+
return "MAX_ROWS"
570+
case TableOptionMinRows:
571+
return "MIN_ROWS"
572+
case TableOptionDelayKeyWrite:
573+
return "DELAY_KEY_WRITE"
574+
case TableOptionRowFormat:
575+
return "ROW_FORMAT"
576+
case TableOptionStatsPersistent:
577+
return "STATS_PERSISTENT"
578+
case TableOptionPackKeys:
579+
return "PACK_KEYS"
580+
case TableOptionCompression:
581+
case TableOptionShardRowID:
582+
default:
583+
}
584+
return ""
585+
}
586+
587+
func (o *TableOption) String() string {
588+
if o.StrValue != "" {
589+
return fmt.Sprintf("%s=%s", o.Type, o.StrValue)
590+
}
591+
return fmt.Sprintf("%s=%d", o.Type, o.UintValue)
592+
}
593+
514594
type CreateTable struct {
515595
*DDL
516596
Columns []*ColumnDef
517597
Constraints []*Constraint
598+
Options []*TableOption
599+
}
600+
601+
func (t *CreateTable) Format(buf *TrackedBuffer) {
602+
columns := []string{}
603+
for _, column := range t.Columns {
604+
columns = append(columns, column.String())
605+
}
606+
column := strings.Join(columns, ",\n\t")
607+
if len(t.Constraints) > 0 {
608+
constraints := []string{}
609+
for _, constraint := range t.Constraints {
610+
constraints = append(constraints, constraint.String())
611+
}
612+
column += ",\n\t" + strings.Join(constraints, ",\n\t")
613+
}
614+
options := []string{}
615+
for _, option := range t.Options {
616+
options = append(options, option.String())
617+
}
618+
option := strings.Join(options, " ")
619+
620+
if column != "" {
621+
text := fmt.Sprintf("CREATE TABLE `%v` (\n\t%s\n) %s", t.NewName.Name, column, option)
622+
buf.Myprintf("%s", text)
623+
} else {
624+
text := fmt.Sprintf("CREATE TABLE `%v` %s", t.NewName.Name, option)
625+
buf.Myprintf("%s", text)
626+
}
518627
}
519628

520629
// DDL strings.
@@ -528,8 +637,6 @@ const (
528637
// Format formats the node.
529638
func (node *DDL) Format(buf *TrackedBuffer) {
530639
switch node.Action {
531-
case CreateStr:
532-
buf.Myprintf("%s table %v", node.Action, node.NewName)
533640
case DropStr:
534641
exists := ""
535642
if node.IfExists {
@@ -772,6 +879,28 @@ const (
772879
ConstraintFulltext
773880
)
774881

882+
func (t ConstraintType) String() string {
883+
switch t {
884+
case ConstraintPrimaryKey:
885+
return "PRIMARY KEY"
886+
case ConstraintKey:
887+
return "KEY"
888+
case ConstraintIndex:
889+
return "INDEX"
890+
case ConstraintUniq:
891+
return "UNIQUE"
892+
case ConstraintUniqKey:
893+
return "UNIQUE KEY"
894+
case ConstraintUniqIndex:
895+
return "UNIQUE INDEX"
896+
case ConstraintForeignKey:
897+
return "FOREIGN KEY"
898+
case ConstraintFulltext:
899+
return "FULLTEXT"
900+
}
901+
return ""
902+
}
903+
775904
// Constraint is constraint for table definition.
776905
type Constraint struct {
777906
Type ConstraintType
@@ -780,11 +909,90 @@ type Constraint struct {
780909
Keys []ColIdent
781910
}
782911

912+
func (node Constraint) String() string {
913+
keys := []string{}
914+
for _, key := range node.Keys {
915+
keys = append(keys, fmt.Sprintf("`%v`", key))
916+
}
917+
name := ""
918+
if node.Name != "" {
919+
name = fmt.Sprintf("`%s`", node.Name)
920+
}
921+
return fmt.Sprintf("%s %s (%s)", node.Type.String(), name, strings.Join(keys, ", "))
922+
}
923+
924+
// ColumnOptionType is the type for ColumnOption.
925+
type ColumnOptionType int
926+
927+
const (
928+
ColumnOptionNoOption ColumnOptionType = iota
929+
ColumnOptionPrimaryKey
930+
ColumnOptionNotNull
931+
ColumnOptionAutoIncrement
932+
ColumnOptionDefaultValue
933+
ColumnOptionUniqKey
934+
ColumnOptionNull
935+
ColumnOptionOnUpdate // For Timestamp and Datetime only.
936+
ColumnOptionFulltext
937+
ColumnOptionComment
938+
ColumnOptionGenerated
939+
ColumnOptionReference
940+
)
941+
942+
func (o ColumnOptionType) String() string {
943+
switch o {
944+
case ColumnOptionPrimaryKey:
945+
return "PRIMARY KEY"
946+
case ColumnOptionNotNull:
947+
return "NOT NULL"
948+
case ColumnOptionAutoIncrement:
949+
return "AUTO_INCREMENT"
950+
case ColumnOptionDefaultValue:
951+
return "DEFAULT"
952+
case ColumnOptionUniqKey:
953+
return "UNIQUE KEY"
954+
case ColumnOptionNull:
955+
return "NULL"
956+
case ColumnOptionOnUpdate:
957+
return "ON UPDATE"
958+
case ColumnOptionFulltext:
959+
return "FULLTEXT"
960+
case ColumnOptionComment:
961+
return "COMMENT"
962+
case ColumnOptionGenerated:
963+
case ColumnOptionReference:
964+
default:
965+
}
966+
return ""
967+
}
968+
969+
// ColumnOption is used for parsing column constraint info from SQL.
970+
type ColumnOption struct {
971+
Type ColumnOptionType
972+
}
973+
783974
type ColumnDef struct {
784975
Name string
785976
Type string
786977
// Elems is the element list for enum and set type.
787-
Elems []string
978+
Elems []string
979+
Options []ColumnOptionType
980+
}
981+
982+
func (node ColumnDef) String() string {
983+
if len(node.Elems) > 0 {
984+
elems := strings.Join(node.Elems, ",")
985+
return fmt.Sprintf("`%s` %s (%s)", node.Name, node.Type, elems)
986+
}
987+
option := ""
988+
if len(node.Options) > 0 {
989+
options := []string{}
990+
for _, option := range node.Options {
991+
options = append(options, option.String())
992+
}
993+
option = " " + strings.Join(options, " ")
994+
}
995+
return fmt.Sprintf("`%s` %s%s", node.Name, node.Type, option)
788996
}
789997

790998
// Columns represents an insert column list.

sqlparser/type_converter.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ import (
77
func convertFromCreateTableStmt(stmt *ast.CreateTableStmt, ddl *DDL) Statement {
88
columns := []*ColumnDef{}
99
for _, col := range stmt.Cols {
10+
options := []ColumnOptionType{}
11+
for _, option := range col.Options {
12+
options = append(options, ColumnOptionType(option.Tp))
13+
}
1014
columns = append(columns, &ColumnDef{
11-
Name: col.Name.Name.String(),
12-
Type: col.Tp.String(),
13-
Elems: col.Tp.Elems,
15+
Name: col.Name.Name.String(),
16+
Type: col.Tp.String(),
17+
Elems: col.Tp.Elems,
18+
Options: options,
1419
})
1520
}
1621
constraints := []*Constraint{}
@@ -25,10 +30,19 @@ func convertFromCreateTableStmt(stmt *ast.CreateTableStmt, ddl *DDL) Statement {
2530
Keys: keys,
2631
})
2732
}
33+
options := []*TableOption{}
34+
for _, option := range stmt.Options {
35+
options = append(options, &TableOption{
36+
Type: TableOptionType(option.Tp),
37+
StrValue: option.StrValue,
38+
UintValue: option.UintValue,
39+
})
40+
}
2841
return &CreateTable{
2942
DDL: ddl,
3043
Columns: columns,
3144
Constraints: constraints,
45+
Options: options,
3246
}
3347
}
3448

0 commit comments

Comments
 (0)