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
49 changes: 49 additions & 0 deletions ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (
_ DDLNode = &DropDatabaseStmt{}
_ DDLNode = &DropIndexStmt{}
_ DDLNode = &DropTableStmt{}
_ DDLNode = &DropSequenceStmt{}
_ DDLNode = &RenameTableStmt{}
_ DDLNode = &TruncateTableStmt{}
_ DDLNode = &RepairTableStmt{}
Expand Down Expand Up @@ -1089,6 +1090,54 @@ func (n *DropTableStmt) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

// DropSequenceStmt is a statement to drop a Sequence.
type DropSequenceStmt struct {
ddlNode

IfExists bool
Sequences []*TableName
IsTemporary bool
}

// Restore implements Node interface.
func (n *DropSequenceStmt) Restore(ctx *RestoreCtx) error {
if n.IsTemporary {
ctx.WriteKeyWord("DROP TEMPORARY SEQUENCE ")
} else {
ctx.WriteKeyWord("DROP SEQUENCE ")
}
if n.IfExists {
ctx.WriteKeyWord("IF EXISTS ")
}
for i, sequence := range n.Sequences {
if i != 0 {
ctx.WritePlain(", ")
}
if err := sequence.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore DropSequenceStmt.Sequences[%d]", i)
}
}

return nil
}

// Accept implements Node Accept interface.
func (n *DropSequenceStmt) Accept(v Visitor) (Node, bool) {
newNode, skipChildren := v.Enter(n)
if skipChildren {
return v.Leave(newNode)
}
n = newNode.(*DropSequenceStmt)
for i, val := range n.Sequences {
node, ok := val.Accept(v)
if !ok {
return n, false
}
n.Sequences[i] = node.(*TableName)
}
return v.Leave(n)
}

// RenameTableStmt is a statement to rename a table.
// See http://dev.mysql.com/doc/refman/5.7/en/rename-table.html
type RenameTableStmt struct {
Expand Down
9 changes: 8 additions & 1 deletion ast/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ func (ts *testDDLSuite) TestAdminRepairTableRestore(c *C) {
RunNodeRestoreTest(c, testCases, "%s", extractNodeFunc)
}

func (ts *testDDLSuite) TestCreateSequenceRestore(c *C) {
func (ts *testDDLSuite) TestSequenceRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"create sequence seq", "CREATE SEQUENCE `seq`"},
{"create sequence if not exists seq", "CREATE SEQUENCE IF NOT EXISTS `seq`"},
Expand Down Expand Up @@ -564,6 +564,13 @@ func (ts *testDDLSuite) TestCreateSequenceRestore(c *C) {
{"create temporary sequence seq nocycle nocache maxvalue 1000 cache 1", "CREATE TEMPORARY SEQUENCE `seq` NOCYCLE NOCACHE MAXVALUE 1000 CACHE 1"},
{"create temporary sequence seq increment -1 no minvalue no maxvalue cache = 1", "CREATE TEMPORARY SEQUENCE `seq` INCREMENT BY -1 NO MINVALUE NO MAXVALUE CACHE 1"},
{"create temporary sequence if not exists seq increment 1 minvalue 0 nomaxvalue cache 100 nocycle noorder", "CREATE TEMPORARY SEQUENCE IF NOT EXISTS `seq` INCREMENT BY 1 MINVALUE 0 NO MAXVALUE CACHE 100 NOCYCLE NOORDER"},

// test drop sequence
{"drop sequence seq", "DROP SEQUENCE `seq`"},
{"drop sequence seq, seq2", "DROP SEQUENCE `seq`, `seq2`"},
{"drop sequence if exists seq, seq2", "DROP SEQUENCE IF EXISTS `seq`, `seq2`"},
{"drop temporary sequence if exists seq", "DROP TEMPORARY SEQUENCE IF EXISTS `seq`"},
{"drop temporary sequence sequence", "DROP TEMPORARY SEQUENCE `sequence`"},
}
extractNodeFunc := func(node Node) Node {
return node
Expand Down
Loading