Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 68 additions & 3 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -807,9 +807,10 @@ func (node *PartitionDefinition) walkSubtree(visit Visit) error {

// TableSpec describes the structure of a table from a CREATE TABLE statement
type TableSpec struct {
Columns []*ColumnDefinition
Indexes []*IndexDefinition
Options string
Columns []*ColumnDefinition
Indexes []*IndexDefinition
Constraints []*ConstraintDefinition
Options string
}

// Format formats the node.
Expand All @@ -825,6 +826,9 @@ func (ts *TableSpec) Format(buf *TrackedBuffer) {
for _, idx := range ts.Indexes {
buf.Myprintf(",\n\t%v", idx)
}
for _, c := range ts.Constraints {
buf.Myprintf(",\n\t%v", c)
}

buf.Myprintf("\n)%s", strings.Replace(ts.Options, ", ", ",\n ", -1))
}
Expand All @@ -839,6 +843,11 @@ func (ts *TableSpec) AddIndex(id *IndexDefinition) {
ts.Indexes = append(ts.Indexes, id)
}

// AddConstraint appends the given index to the list in the spec
func (ts *TableSpec) AddConstraint(cd *ConstraintDefinition) {
ts.Constraints = append(ts.Constraints, cd)
}

func (ts *TableSpec) walkSubtree(visit Visit) error {
if ts == nil {
return nil
Expand All @@ -856,6 +865,12 @@ func (ts *TableSpec) walkSubtree(visit Visit) error {
}
}

for _, n := range ts.Constraints {
if err := Walk(visit, n); err != nil {
return err
}
}

return nil
}

Expand Down Expand Up @@ -1278,6 +1293,56 @@ func (node VindexParam) walkSubtree(visit Visit) error {
)
}

// ConstraintDefinition describes a constraint in a CREATE TABLE statement
type ConstraintDefinition struct {
Name string
Details ConstraintInfo
}

// ConstraintInfo details a constraint in a CREATE TABLE statement
type ConstraintInfo interface {
SQLNode
constraintInfo()
}

// Format formats the node.
func (c *ConstraintDefinition) Format(buf *TrackedBuffer) {
if c.Name != "" {
buf.Myprintf("constraint %s ", c.Name)
}
c.Details.Format(buf)
}

func (c *ConstraintDefinition) walkSubtree(visit Visit) error {
return Walk(visit, c.Details)
}

// ForeignKeyDefinition describes a foreign key in a CREATE TABLE statement
type ForeignKeyDefinition struct {
Source Columns
ReferencedTable TableName
ReferencedColumns Columns
}

var _ ConstraintInfo = &ForeignKeyDefinition{}

// Format formats the node.
func (f *ForeignKeyDefinition) Format(buf *TrackedBuffer) {
buf.Myprintf("foreign key %v references %v %v", f.Source, f.ReferencedTable, f.ReferencedColumns)
}

func (f *ForeignKeyDefinition) constraintInfo() {}

func (f *ForeignKeyDefinition) walkSubtree(visit Visit) error {
if err := Walk(visit, f.Source); err != nil {
return err
}
if err := Walk(visit, f.ReferencedTable); err != nil {
return err
}
return Walk(visit, f.ReferencedColumns)
}

// Show represents a show statement.
type Show struct {
Type string
Expand Down
11 changes: 11 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1839,6 +1839,17 @@ func TestCreateTable(t *testing.T) {
" key by_email (email(10), username)\n" +
")",

// foreign keys
"create table t (\n" +
" id int auto_increment,\n" +
" username varchar,\n" +
" k int,\n" +
" Z int,\n" +
" primary key (id, username),\n" +
" key by_email (email(10), username),\n" +
" constraint second_ibfk_1 foreign key (k, j) references simple (a, b)\n" +
")",

// table options
"create table t (\n" +
" id int auto_increment\n" +
Expand Down
Loading