Skip to content

Commit 564a809

Browse files
committed
support constraints
1 parent 2d4b3fd commit 564a809

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

sqlparser/ast.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,8 @@ type DDL struct {
513513

514514
type CreateTable struct {
515515
*DDL
516-
Columns []*ColumnDef
516+
Columns []*ColumnDef
517+
Constraints []*Constraint
517518
}
518519

519520
// DDL strings.
@@ -757,6 +758,28 @@ func (node Nextval) WalkSubtree(visit Visit) error {
757758
return Walk(visit, node.Expr)
758759
}
759760

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+
760783
type ColumnDef struct {
761784
Name string
762785
Type string

sqlparser/type_converter.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,30 @@ import (
55
)
66

77
func convertFromCreateTableStmt(stmt *ast.CreateTableStmt, ddl *DDL) Statement {
8-
var columns []*ColumnDef
8+
columns := []*ColumnDef{}
99
for _, col := range stmt.Cols {
1010
columns = append(columns, &ColumnDef{
1111
Name: col.Name.Name.String(),
1212
Type: col.Tp.String(),
1313
Elems: col.Tp.Elems,
1414
})
1515
}
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+
}
1628
return &CreateTable{
17-
DDL: ddl,
18-
Columns: columns,
29+
DDL: ddl,
30+
Columns: columns,
31+
Constraints: constraints,
1932
}
2033
}
2134

0 commit comments

Comments
 (0)