Skip to content

Commit 43a9376

Browse files
AilinKidtangenta
authored andcommitted
parser : support drop sequence syntax. (#680)
1 parent d3b2972 commit 43a9376

5 files changed

Lines changed: 5089 additions & 4995 deletions

File tree

ast/ddl.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var (
3333
_ DDLNode = &DropDatabaseStmt{}
3434
_ DDLNode = &DropIndexStmt{}
3535
_ DDLNode = &DropTableStmt{}
36+
_ DDLNode = &DropSequenceStmt{}
3637
_ DDLNode = &RenameTableStmt{}
3738
_ DDLNode = &TruncateTableStmt{}
3839
_ DDLNode = &RepairTableStmt{}
@@ -1089,6 +1090,54 @@ func (n *DropTableStmt) Accept(v Visitor) (Node, bool) {
10891090
return v.Leave(n)
10901091
}
10911092

1093+
// DropSequenceStmt is a statement to drop a Sequence.
1094+
type DropSequenceStmt struct {
1095+
ddlNode
1096+
1097+
IfExists bool
1098+
Sequences []*TableName
1099+
IsTemporary bool
1100+
}
1101+
1102+
// Restore implements Node interface.
1103+
func (n *DropSequenceStmt) Restore(ctx *RestoreCtx) error {
1104+
if n.IsTemporary {
1105+
ctx.WriteKeyWord("DROP TEMPORARY SEQUENCE ")
1106+
} else {
1107+
ctx.WriteKeyWord("DROP SEQUENCE ")
1108+
}
1109+
if n.IfExists {
1110+
ctx.WriteKeyWord("IF EXISTS ")
1111+
}
1112+
for i, sequence := range n.Sequences {
1113+
if i != 0 {
1114+
ctx.WritePlain(", ")
1115+
}
1116+
if err := sequence.Restore(ctx); err != nil {
1117+
return errors.Annotatef(err, "An error occurred while restore DropSequenceStmt.Sequences[%d]", i)
1118+
}
1119+
}
1120+
1121+
return nil
1122+
}
1123+
1124+
// Accept implements Node Accept interface.
1125+
func (n *DropSequenceStmt) Accept(v Visitor) (Node, bool) {
1126+
newNode, skipChildren := v.Enter(n)
1127+
if skipChildren {
1128+
return v.Leave(newNode)
1129+
}
1130+
n = newNode.(*DropSequenceStmt)
1131+
for i, val := range n.Sequences {
1132+
node, ok := val.Accept(v)
1133+
if !ok {
1134+
return n, false
1135+
}
1136+
n.Sequences[i] = node.(*TableName)
1137+
}
1138+
return v.Leave(n)
1139+
}
1140+
10921141
// RenameTableStmt is a statement to rename a table.
10931142
// See http://dev.mysql.com/doc/refman/5.7/en/rename-table.html
10941143
type RenameTableStmt struct {

ast/ddl_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ func (ts *testDDLSuite) TestAdminRepairTableRestore(c *C) {
531531
RunNodeRestoreTest(c, testCases, "%s", extractNodeFunc)
532532
}
533533

534-
func (ts *testDDLSuite) TestCreateSequenceRestore(c *C) {
534+
func (ts *testDDLSuite) TestSequenceRestore(c *C) {
535535
testCases := []NodeRestoreTestCase{
536536
{"create sequence seq", "CREATE SEQUENCE `seq`"},
537537
{"create sequence if not exists seq", "CREATE SEQUENCE IF NOT EXISTS `seq`"},
@@ -564,6 +564,13 @@ func (ts *testDDLSuite) TestCreateSequenceRestore(c *C) {
564564
{"create temporary sequence seq nocycle nocache maxvalue 1000 cache 1", "CREATE TEMPORARY SEQUENCE `seq` NOCYCLE NOCACHE MAXVALUE 1000 CACHE 1"},
565565
{"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"},
566566
{"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"},
567+
568+
// test drop sequence
569+
{"drop sequence seq", "DROP SEQUENCE `seq`"},
570+
{"drop sequence seq, seq2", "DROP SEQUENCE `seq`, `seq2`"},
571+
{"drop sequence if exists seq, seq2", "DROP SEQUENCE IF EXISTS `seq`, `seq2`"},
572+
{"drop temporary sequence if exists seq", "DROP TEMPORARY SEQUENCE IF EXISTS `seq`"},
573+
{"drop temporary sequence sequence", "DROP TEMPORARY SEQUENCE `sequence`"},
567574
}
568575
extractNodeFunc := func(node Node) Node {
569576
return node

0 commit comments

Comments
 (0)