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
51 changes: 49 additions & 2 deletions ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ const (
ColumnOptionGenerated
ColumnOptionReference
ColumnOptionCollate
ColumnOptionCheck
)

var (
Expand All @@ -417,6 +418,8 @@ type ColumnOption struct {
// Refer is used for foreign key.
Refer *ReferenceDef
StrValue string
// Enforced is only for Check, default is true.
Enforced bool
}

// Restore implements Node interface.
Expand Down Expand Up @@ -473,6 +476,18 @@ func (n *ColumnOption) Restore(ctx *RestoreCtx) error {
}
ctx.WriteKeyWord("COLLATE ")
ctx.WritePlain(n.StrValue)
case ColumnOptionCheck:
ctx.WriteKeyWord("CHECK")
ctx.WritePlain("(")
if err := n.Expr.Restore(ctx); err != nil {
return errors.Trace(err)
}
ctx.WritePlain(")")
if n.Enforced {
ctx.WriteKeyWord(" ENFORCED")
} else {
ctx.WriteKeyWord(" NOT ENFORCED")
}
default:
return errors.New("An error occurred while splicing ColumnOption")
}
Expand Down Expand Up @@ -562,6 +577,7 @@ const (
ConstraintUniqIndex
ConstraintForeignKey
ConstraintFulltext
ConstraintCheck
)

// Constraint is constraint for table definition.
Expand All @@ -580,6 +596,10 @@ type Constraint struct {
Refer *ReferenceDef // Used for foreign key.

Option *IndexOption // Index Options

Expr ExprNode // Used for Check

Enforced bool // Used for Check
}

// Restore implements Node interface.
Expand Down Expand Up @@ -607,6 +627,24 @@ func (n *Constraint) Restore(ctx *RestoreCtx) error {
ctx.WriteKeyWord("UNIQUE INDEX")
case ConstraintFulltext:
ctx.WriteKeyWord("FULLTEXT")
case ConstraintCheck:
if n.Name != "" {
ctx.WriteKeyWord("CONSTRAINT ")
ctx.WriteName(n.Name)
ctx.WritePlain(" ")
}
ctx.WriteKeyWord("CHECK")
ctx.WritePlain("(")
if err := n.Expr.Restore(ctx); err != nil {
return errors.Trace(err)
}
ctx.WritePlain(") ")
if n.Enforced {
ctx.WriteKeyWord("ENFORCED")
} else {
ctx.WriteKeyWord("NOT ENFORCED")
}
return nil
}

if n.Tp == ConstraintForeignKey {
Expand Down Expand Up @@ -1852,8 +1890,17 @@ func (n *AlterTableSpec) Restore(ctx *RestoreCtx) error {
}
if len(n.NewColumns[0].Options) == 1 {
ctx.WriteKeyWord("SET DEFAULT ")
if err := n.NewColumns[0].Options[0].Expr.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore AlterTableSpec.NewColumns[0].Options[0].Expr")
expr := n.NewColumns[0].Options[0].Expr
if valueExpr, ok := expr.(ValueExpr); ok {
if err := valueExpr.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore AlterTableSpec.NewColumns[0].Options[0].Expr")
}
} else {
ctx.WritePlain("(")
if err := expr.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore AlterTableSpec.NewColumns[0].Options[0].Expr")
}
ctx.WritePlain(")")
}
} else {
ctx.WriteKeyWord(" DROP DEFAULT")
Expand Down
1 change: 1 addition & 0 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ var tokenMap = map[string]int{
"ENABLE": enable,
"ENCLOSED": enclosed,
"END": end,
"ENFORCED": enforced,
"ENGINE": engine,
"ENGINES": engines,
"ENUM": enum,
Expand Down
Loading