Skip to content

Commit

Permalink
add support for repair table stmt (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnh2 authored May 24, 2023
1 parent 6c840d5 commit 14fbd14
Show file tree
Hide file tree
Showing 5 changed files with 4,680 additions and 4,610 deletions.
53 changes: 48 additions & 5 deletions ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var (
_ DDLNode = &RenameTableStmt{}
_ DDLNode = &TruncateTableStmt{}
_ DDLNode = &RepairTableStmt{}
_ DDLNode = &AdminRepairTableStmt{}
_ DDLNode = &OptimizeTableStmt{}
_ DDLNode = &CheckTableStmt{}

Expand Down Expand Up @@ -1987,20 +1988,20 @@ func (c *CheckTableStmt) Accept(v Visitor) (Node, bool) {
return v.Leave(c)
}

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

// Accept implements Node Accept interface.
func (n *RepairTableStmt) Accept(v Visitor) (Node, bool) {
func (n *AdminRepairTableStmt) Accept(v Visitor) (Node, bool) {
newNode, skipChildren := v.Enter(n)
if skipChildren {
return v.Leave(newNode)
}
n = newNode.(*RepairTableStmt)
n = newNode.(*AdminRepairTableStmt)
node, ok := n.Table.Accept(v)
if !ok {
return n, false
Expand All @@ -2015,7 +2016,7 @@ func (n *RepairTableStmt) Accept(v Visitor) (Node, bool) {
}

// Restore implements Node interface.
func (n *RepairTableStmt) Restore(ctx *format.RestoreCtx) error {
func (n *AdminRepairTableStmt) Restore(ctx *format.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)
Expand All @@ -2027,6 +2028,48 @@ func (n *RepairTableStmt) Restore(ctx *format.RestoreCtx) error {
return nil
}

// RepairTableStmt is a statement to repair tableInfo.
// See https://dev.mysql.com/doc/refman/8.0/en/repair-table.html
type RepairTableStmt struct {
ddlNode
Tables []*TableName
Quick bool
}

// 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)
for i := range n.Tables {
node, ok := n.Tables[i].Accept(v)
if !ok {
return n, false
}
n.Tables[i] = node.(*TableName)
}
return v.Leave(n)
}

// Restore implements Node interface.
func (n *RepairTableStmt) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("REPAIR TABLE ")
for i, v := range n.Tables {
if i != 0 {
ctx.WritePlain(", ")
}
if err := v.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore RepairTableStmt.Tables[%d]", i)
}
}
if n.Quick {
ctx.WritePlain(" QUICK")
}
return nil
}

// PlacementOptionType is the type for PlacementOption
type PlacementOptionType int

Expand Down
1 change: 1 addition & 0 deletions ast/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestDDLVisitorCover(t *testing.T) {
{&CheckTableStmt{Tables: []*TableName{{}, {}}}, 0, 0},
{&RenameTableStmt{TableToTables: []*TableToTable{}}, 0, 0},
{&TruncateTableStmt{Table: &TableName{}}, 0, 0},
{&RepairTableStmt{Tables: []*TableName{}}, 0, 0},

// TODO: cover children
{&AlterTableStmt{Table: &TableName{}, Specs: []*AlterTableSpec{alterTableSpec}}, 0, 0},
Expand Down
Loading

0 comments on commit 14fbd14

Please sign in to comment.