Skip to content

Commit

Permalink
parser : support admin repair table syntax (#547)
Browse files Browse the repository at this point in the history
* support admin repair table

* fix comment

* add ddl test

* fix depency
  • Loading branch information
AilinKid authored and kennytm committed Oct 21, 2019
1 parent 6fc90a9 commit 646d641
Show file tree
Hide file tree
Showing 6 changed files with 4,775 additions and 4,691 deletions.
41 changes: 41 additions & 0 deletions ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (
_ DDLNode = &DropTableStmt{}
_ DDLNode = &RenameTableStmt{}
_ DDLNode = &TruncateTableStmt{}
_ DDLNode = &RepairTableStmt{}

_ Node = &AlterTableSpec{}
_ Node = &ColumnDef{}
Expand Down Expand Up @@ -1559,6 +1560,46 @@ func (n *CleanupTableLockStmt) Restore(ctx *RestoreCtx) error {
return nil
}

// RepairTableStmt is a statement to repair tableInfo.
type RepairTableStmt struct {
ddlNode
Table *TableName
CreateStmt *CreateTableStmt
}

// Accept implements Node Accept interface.
func (n *RepairTableStmt) Accept(v Visitor) (Node, bool) {
newNode, skipChildren := v.Enter(n)
if skipChildren {
return v.Leave(newNode)
}
n = newNode.(*RepairTableStmt)
node, ok := n.Table.Accept(v)
if !ok {
return n, false
}
n.Table = node.(*TableName)
node, ok = n.CreateStmt.Accept(v)
if !ok {
return n, false
}
n.CreateStmt = node.(*CreateTableStmt)
return v.Leave(n)
}

// Restore implements Node interface.
func (n *RepairTableStmt) Restore(ctx *RestoreCtx) error {
ctx.WriteKeyWord("ADMIN REPAIR TABLE ")
if err := n.Table.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore RepairTableStmt.table : [%v]", n.Table)
}
ctx.WritePlain(" ")
if err := n.CreateStmt.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore RepairTableStmt.createStmt : [%v]", n.CreateStmt)
}
return nil
}

// TableOptionType is the type for TableOption
type TableOptionType int

Expand Down
12 changes: 12 additions & 0 deletions ast/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,15 @@ func (ts *testDDLSuite) TestAlterTableSpecRestore(c *C) {
}
RunNodeRestoreTest(c, testCases, "ALTER TABLE t %s", extractNodeFunc)
}

func (ts *testDDLSuite) TestAdminRepairTableRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"ADMIN REPAIR TABLE t CREATE TABLE t (a int)", "ADMIN REPAIR TABLE `t` CREATE TABLE `t` (`a` INT)"},
{"ADMIN REPAIR TABLE t CREATE TABLE t (a char(1), b int)", "ADMIN REPAIR TABLE `t` CREATE TABLE `t` (`a` CHAR(1),`b` INT)"},
{"ADMIN REPAIR TABLE t CREATE TABLE t (a TINYINT UNSIGNED)", "ADMIN REPAIR TABLE `t` CREATE TABLE `t` (`a` TINYINT UNSIGNED)"},
}
extractNodeFunc := func(node Node) Node {
return node
}
RunNodeRestoreTest(c, testCases, "%s", extractNodeFunc)
}
2 changes: 2 additions & 0 deletions model/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const (
ActionModifySchemaCharsetAndCollate ActionType = 26
ActionLockTable ActionType = 27
ActionUnlockTable ActionType = 28
ActionRepairTable ActionType = 29
)

// AddIndexStr is a string related to the operation of "add index".
Expand Down Expand Up @@ -92,6 +93,7 @@ var actionMap = map[ActionType]string{
ActionModifySchemaCharsetAndCollate: "modify schema charset and collate",
ActionLockTable: "lock table",
ActionUnlockTable: "unlock table",
ActionRepairTable: "repair table",
}

// String return current ddl action in string
Expand Down
Loading

0 comments on commit 646d641

Please sign in to comment.