@@ -21,6 +21,7 @@ import (
21
21
"encoding/hex"
22
22
"encoding/json"
23
23
"errors"
24
+ "fmt"
24
25
"strings"
25
26
26
27
"github.com/knocknote/vitess-sqlparser/sqltypes"
@@ -511,10 +512,118 @@ type DDL struct {
511
512
IfExists bool
512
513
}
513
514
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
+
514
594
type CreateTable struct {
515
595
* DDL
516
596
Columns []* ColumnDef
517
597
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
+ }
518
627
}
519
628
520
629
// DDL strings.
@@ -528,8 +637,6 @@ const (
528
637
// Format formats the node.
529
638
func (node * DDL ) Format (buf * TrackedBuffer ) {
530
639
switch node .Action {
531
- case CreateStr :
532
- buf .Myprintf ("%s table %v" , node .Action , node .NewName )
533
640
case DropStr :
534
641
exists := ""
535
642
if node .IfExists {
@@ -772,6 +879,28 @@ const (
772
879
ConstraintFulltext
773
880
)
774
881
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
+
775
904
// Constraint is constraint for table definition.
776
905
type Constraint struct {
777
906
Type ConstraintType
@@ -780,11 +909,90 @@ type Constraint struct {
780
909
Keys []ColIdent
781
910
}
782
911
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
+
783
974
type ColumnDef struct {
784
975
Name string
785
976
Type string
786
977
// 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 )
788
996
}
789
997
790
998
// Columns represents an insert column list.
0 commit comments