File tree Expand file tree Collapse file tree 2 files changed +40
-4
lines changed Expand file tree Collapse file tree 2 files changed +40
-4
lines changed Original file line number Diff line number Diff line change @@ -513,7 +513,8 @@ type DDL struct {
513
513
514
514
type CreateTable struct {
515
515
* DDL
516
- Columns []* ColumnDef
516
+ Columns []* ColumnDef
517
+ Constraints []* Constraint
517
518
}
518
519
519
520
// DDL strings.
@@ -757,6 +758,28 @@ func (node Nextval) WalkSubtree(visit Visit) error {
757
758
return Walk (visit , node .Expr )
758
759
}
759
760
761
+ type ConstraintType int
762
+
763
+ const (
764
+ ConstraintNoConstraint ConstraintType = iota
765
+ ConstraintPrimaryKey
766
+ ConstraintKey
767
+ ConstraintIndex
768
+ ConstraintUniq
769
+ ConstraintUniqKey
770
+ ConstraintUniqIndex
771
+ ConstraintForeignKey
772
+ ConstraintFulltext
773
+ )
774
+
775
+ // Constraint is constraint for table definition.
776
+ type Constraint struct {
777
+ Type ConstraintType
778
+ Name string
779
+ // Used for PRIMARY KEY, UNIQUE, ......
780
+ Keys []ColIdent
781
+ }
782
+
760
783
type ColumnDef struct {
761
784
Name string
762
785
Type string
Original file line number Diff line number Diff line change @@ -5,17 +5,30 @@ import (
5
5
)
6
6
7
7
func convertFromCreateTableStmt (stmt * ast.CreateTableStmt , ddl * DDL ) Statement {
8
- var columns []* ColumnDef
8
+ columns := []* ColumnDef {}
9
9
for _ , col := range stmt .Cols {
10
10
columns = append (columns , & ColumnDef {
11
11
Name : col .Name .Name .String (),
12
12
Type : col .Tp .String (),
13
13
Elems : col .Tp .Elems ,
14
14
})
15
15
}
16
+ constraints := []* Constraint {}
17
+ for _ , constraint := range stmt .Constraints {
18
+ keys := []ColIdent {}
19
+ for _ , key := range constraint .Keys {
20
+ keys = append (keys , NewColIdent (key .Column .Name .String ()))
21
+ }
22
+ constraints = append (constraints , & Constraint {
23
+ Type : ConstraintType (constraint .Tp ),
24
+ Name : constraint .Name ,
25
+ Keys : keys ,
26
+ })
27
+ }
16
28
return & CreateTable {
17
- DDL : ddl ,
18
- Columns : columns ,
29
+ DDL : ddl ,
30
+ Columns : columns ,
31
+ Constraints : constraints ,
19
32
}
20
33
}
21
34
You can’t perform that action at this time.
0 commit comments