Skip to content

Commit

Permalink
*: add IF EXISTS and IF NOT EXISTS supported for MariaDB (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
csuzhangxc authored and tiancaiamao committed Jun 13, 2019
1 parent 3b36f86 commit d2cf607
Show file tree
Hide file tree
Showing 4 changed files with 2,436 additions and 2,327 deletions.
52 changes: 52 additions & 0 deletions ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,10 @@ const (
type Constraint struct {
node

// only supported by MariaDB 10.0.2+ (ADD {INDEX|KEY}, ADD FOREIGN KEY),
// see https://mariadb.com/kb/en/library/alter-table/
IfNotExists bool

Tp ConstraintType
Name string

Expand All @@ -587,8 +591,14 @@ func (n *Constraint) Restore(ctx *RestoreCtx) error {
ctx.WriteKeyWord("PRIMARY KEY")
case ConstraintKey:
ctx.WriteKeyWord("KEY")
if n.IfNotExists {
ctx.WriteKeyWord(" IF NOT EXISTS")
}
case ConstraintIndex:
ctx.WriteKeyWord("INDEX")
if n.IfNotExists {
ctx.WriteKeyWord(" IF NOT EXISTS")
}
case ConstraintUniq:
ctx.WriteKeyWord("UNIQUE")
case ConstraintUniqKey:
Expand All @@ -606,6 +616,9 @@ func (n *Constraint) Restore(ctx *RestoreCtx) error {
ctx.WritePlain(" ")
}
ctx.WriteKeyWord("FOREIGN KEY ")
if n.IfNotExists {
ctx.WriteKeyWord("IF NOT EXISTS ")
}
} else if n.Name != "" {
ctx.WritePlain(" ")
ctx.WriteName(n.Name)
Expand Down Expand Up @@ -1120,6 +1133,10 @@ func (n *CreateViewStmt) Accept(v Visitor) (Node, bool) {
type CreateIndexStmt struct {
ddlNode

// only supported by MariaDB 10.0.2+,
// see https://mariadb.com/kb/en/library/create-index/
IfNotExists bool

IndexName string
Table *TableName
Unique bool
Expand All @@ -1134,6 +1151,9 @@ func (n *CreateIndexStmt) Restore(ctx *RestoreCtx) error {
ctx.WriteKeyWord("UNIQUE ")
}
ctx.WriteKeyWord("INDEX ")
if n.IfNotExists {
ctx.WriteKeyWord("IF NOT EXISTS ")
}
ctx.WriteName(n.IndexName)
ctx.WriteKeyWord(" ON ")
if err := n.Table.Restore(ctx); err != nil {
Expand Down Expand Up @@ -1641,6 +1661,14 @@ func (a AlterAlgorithm) String() string {
type AlterTableSpec struct {
node

// only supported by MariaDB 10.0.2+ (DROP COLUMN, CHANGE COLUMN, MODIFY COLUMN, DROP INDEX, DROP FOREIGN KEY, DROP PARTITION)
// see https://mariadb.com/kb/en/library/alter-table/
IfExists bool

// only supported by MariaDB 10.0.2+ (ADD COLUMN, ADD PARTITION)
// see https://mariadb.com/kb/en/library/alter-table/
IfNotExists bool

Tp AlterTableType
Name string
Constraint *Constraint
Expand Down Expand Up @@ -1684,6 +1712,9 @@ func (n *AlterTableSpec) Restore(ctx *RestoreCtx) error {
}
case AlterTableAddColumns:
ctx.WriteKeyWord("ADD COLUMN ")
if n.IfNotExists {
ctx.WriteKeyWord("IF NOT EXISTS ")
}
if n.Position != nil && len(n.NewColumns) == 1 {
if err := n.NewColumns[0].Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore AlterTableSpec.NewColumns[%d]", 0)
Expand Down Expand Up @@ -1713,6 +1744,9 @@ func (n *AlterTableSpec) Restore(ctx *RestoreCtx) error {
}
case AlterTableDropColumn:
ctx.WriteKeyWord("DROP COLUMN ")
if n.IfExists {
ctx.WriteKeyWord("IF EXISTS ")
}
if err := n.OldColumnName.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore AlterTableSpec.OldColumnName")
}
Expand All @@ -1721,12 +1755,21 @@ func (n *AlterTableSpec) Restore(ctx *RestoreCtx) error {
ctx.WriteKeyWord("DROP PRIMARY KEY")
case AlterTableDropIndex:
ctx.WriteKeyWord("DROP INDEX ")
if n.IfExists {
ctx.WriteKeyWord("IF EXISTS ")
}
ctx.WriteName(n.Name)
case AlterTableDropForeignKey:
ctx.WriteKeyWord("DROP FOREIGN KEY ")
if n.IfExists {
ctx.WriteKeyWord("IF EXISTS ")
}
ctx.WriteName(n.Name)
case AlterTableModifyColumn:
ctx.WriteKeyWord("MODIFY COLUMN ")
if n.IfExists {
ctx.WriteKeyWord("IF EXISTS ")
}
if err := n.NewColumns[0].Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore AlterTableSpec.NewColumns[0]")
}
Expand All @@ -1738,6 +1781,9 @@ func (n *AlterTableSpec) Restore(ctx *RestoreCtx) error {
}
case AlterTableChangeColumn:
ctx.WriteKeyWord("CHANGE COLUMN ")
if n.IfExists {
ctx.WriteKeyWord("IF EXISTS ")
}
if err := n.OldColumnName.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore AlterTableSpec.OldColumnName")
}
Expand Down Expand Up @@ -1788,6 +1834,9 @@ func (n *AlterTableSpec) Restore(ctx *RestoreCtx) error {
ctx.WritePlain(" /* AlterTableForce is not supported */ ")
case AlterTableAddPartitions:
ctx.WriteKeyWord("ADD PARTITION")
if n.IfNotExists {
ctx.WriteKeyWord(" IF NOT EXISTS")
}
if n.PartDefinitions != nil {
ctx.WritePlain(" (")
for i, def := range n.PartDefinitions {
Expand All @@ -1808,6 +1857,9 @@ func (n *AlterTableSpec) Restore(ctx *RestoreCtx) error {
ctx.WritePlainf("%d", n.Num)
case AlterTableDropPartition:
ctx.WriteKeyWord("DROP PARTITION ")
if n.IfExists {
ctx.WriteKeyWord("IF EXISTS ")
}
for i, name := range n.PartitionNames {
if i != 0 {
ctx.WritePlain(",")
Expand Down
Loading

0 comments on commit d2cf607

Please sign in to comment.