Skip to content

Commit

Permalink
parser: fix compatibility for OnDelete,OnUpdate clauses (pingcap#393
Browse files Browse the repository at this point in the history
)

* fix

* fix check for ColumnOption

* add test for match

* use array
  • Loading branch information
leoppro authored and kennytm committed Jul 25, 2019
1 parent 2783e7a commit 8673dd4
Show file tree
Hide file tree
Showing 5 changed files with 7,011 additions and 6,884 deletions.
26 changes: 26 additions & 0 deletions ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@ func (n *IndexColName) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

// MatchType is the type for reference match type.
type MatchType int

// match type
const (
MatchNone MatchType = iota
MatchFull
MatchPartial
MatchSimple
)

// ReferenceDef is used for parsing foreign key reference option from SQL.
// See http://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html
type ReferenceDef struct {
Expand All @@ -231,6 +242,7 @@ type ReferenceDef struct {
IndexColNames []*IndexColName
OnDelete *OnDeleteOpt
OnUpdate *OnUpdateOpt
Match MatchType
}

// Restore implements Node interface.
Expand All @@ -251,6 +263,17 @@ func (n *ReferenceDef) Restore(ctx *RestoreCtx) error {
}
}
ctx.WritePlain(")")
if n.Match != MatchNone {
ctx.WriteKeyWord(" MATCH ")
switch n.Match {
case MatchFull:
ctx.WriteKeyWord("FULL")
case MatchPartial:
ctx.WriteKeyWord("PARTIAL")
case MatchSimple:
ctx.WriteKeyWord("SIMPLE")
}
}
if n.OnDelete.ReferOpt != ReferOptionNoOption {
ctx.WritePlain(" ")
if err := n.OnDelete.Restore(ctx); err != nil {
Expand Down Expand Up @@ -308,6 +331,7 @@ const (
ReferOptionCascade
ReferOptionSetNull
ReferOptionNoAction
ReferOptionSetDefault
)

// String implements fmt.Stringer interface.
Expand All @@ -321,6 +345,8 @@ func (r ReferOptionType) String() string {
return "SET NULL"
case ReferOptionNoAction:
return "NO ACTION"
case ReferOptionSetDefault:
return "SET DEFAULT"
}
return ""
}
Expand Down
3 changes: 3 additions & 0 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ var tokenMap = map[string]int{
"LONGTEXT": longtextType,
"LOW_PRIORITY": lowPriority,
"MASTER": master,
"MATCH": match,
"MAX": max,
"MAX_CONNECTIONS_PER_HOUR": maxConnectionsPerHour,
"MAX_EXECUTION_TIME": maxExecutionTime,
Expand Down Expand Up @@ -405,6 +406,7 @@ var tokenMap = map[string]int{
"OUTER": outer,
"PACK_KEYS": packKeys,
"PAGE": pageSym,
"PARTIAL": partial,
"PARTITION": partition,
"PARTITIONS": partitions,
"PASSWORD": password,
Expand Down Expand Up @@ -470,6 +472,7 @@ var tokenMap = map[string]int{
"SHARED": shared,
"SHOW": show,
"SIGNED": signed,
"SIMPLE": simple,
"SLAVE": slave,
"SLOW": slow,
"SMALLINT": smallIntType,
Expand Down
Loading

0 comments on commit 8673dd4

Please sign in to comment.