Skip to content

Commit

Permalink
*: add IF EXISTS and IF NOT EXISTS supported for MariaDB (pingcap…
Browse files Browse the repository at this point in the history
  • Loading branch information
csuzhangxc authored and tangenta committed Nov 18, 2019
1 parent 276b178 commit 42f0c4e
Show file tree
Hide file tree
Showing 4 changed files with 2,173 additions and 2,066 deletions.
52 changes: 52 additions & 0 deletions ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,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 @@ -633,8 +637,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 Down Expand Up @@ -670,6 +680,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 @@ -1184,6 +1197,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 @@ -1198,6 +1215,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 @@ -1643,6 +1663,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 @@ -1686,6 +1714,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 @@ -1715,6 +1746,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 @@ -1723,12 +1757,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 @@ -1740,6 +1783,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 @@ -1799,6 +1845,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 @@ -1819,6 +1868,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 42f0c4e

Please sign in to comment.