Skip to content

Commit

Permalink
Merge branch 'master' into feature/Resotre_IndexColName
Browse files Browse the repository at this point in the history
  • Loading branch information
feloxx authored Dec 24, 2018
2 parents 60498b3 + fd38d46 commit bb270e9
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
12 changes: 10 additions & 2 deletions ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,11 @@ type OnDeleteOpt struct {

// Restore implements Node interface.
func (n *OnDeleteOpt) Restore(ctx *RestoreCtx) error {
return errors.New("Not implemented")
if n.ReferOpt != ReferOptionNoOption {
ctx.WriteKeyWord("ON DELETE ")
ctx.WriteKeyWord(n.ReferOpt.String())
}
return nil
}

// Accept implements Node Accept interface.
Expand All @@ -284,7 +288,11 @@ type OnUpdateOpt struct {

// Restore implements Node interface.
func (n *OnUpdateOpt) Restore(ctx *RestoreCtx) error {
return errors.New("Not implemented")
if n.ReferOpt != ReferOptionNoOption {
ctx.WriteKeyWord("ON UPDATE ")
ctx.WriteKeyWord(n.ReferOpt.String())
}
return nil
}

// Accept implements Node Accept interface.
Expand Down
26 changes: 26 additions & 0 deletions ast/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (ts *testDDLSuite) TestDDLVisitorCover(c *C) {
}
}


func (ts *testDDLSuite) TestDDLIndexColNameRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"world", "`world`"},
Expand All @@ -72,6 +73,31 @@ func (ts *testDDLSuite) TestDDLIndexColNameRestore(c *C) {
return node.(*CreateIndexStmt).IndexColNames[0]
}
RunNodeRestoreTest(c, testCases, "CREATE INDEX idx ON t (%s) USING HASH", extractNodeFunc)

func (ts *testDDLSuite) TestDDLOnDeleteRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"on delete restrict", "ON DELETE RESTRICT"},
{"on delete CASCADE", "ON DELETE CASCADE"},
{"on delete SET NULL", "ON DELETE SET NULL"},
{"on delete no action", "ON DELETE NO ACTION"},
}
extractNodeFunc := func(node Node) Node {
return node.(*CreateTableStmt).Constraints[1].Refer.OnDelete
}
RunNodeRestoreTest(c, testCases, "CREATE TABLE child (id INT, parent_id INT, INDEX par_ind (parent_id), FOREIGN KEY (parent_id) REFERENCES parent(id) %s)", extractNodeFunc)
}

func (ts *testDDLSuite) TestDDLOnUpdateRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"ON UPDATE RESTRICT", "ON UPDATE RESTRICT"},
{"on update CASCADE", "ON UPDATE CASCADE"},
{"on update SET NULL", "ON UPDATE SET NULL"},
{"on update no action", "ON UPDATE NO ACTION"},
}
extractNodeFunc := func(node Node) Node {
return node.(*CreateTableStmt).Constraints[1].Refer.OnUpdate
}
RunNodeRestoreTest(c, testCases, "CREATE TABLE child ( id INT, parent_id INT, INDEX par_ind (parent_id), FOREIGN KEY (parent_id) REFERENCES parent(id) ON DELETE CASCADE %s )", extractNodeFunc)
}

func (ts *testDDLSuite) TestDDLIndexOption(c *C) {
Expand Down
13 changes: 12 additions & 1 deletion ast/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,18 @@ type RowExpr struct {

// Restore implements Node interface.
func (n *RowExpr) Restore(ctx *RestoreCtx) error {
return errors.New("Not implemented")
ctx.WriteKeyWord("ROW")
ctx.WritePlain("(")
for i, v := range n.Values {
if i != 0 {
ctx.WritePlain(",")
}
if err := v.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred when restore RowExpr.Values[%v]", i)
}
}
ctx.WritePlain(")")
return nil
}

// Format the ExprNode into a Writer.
Expand Down
13 changes: 13 additions & 0 deletions ast/expressions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,19 @@ func (tc *testExpressionsSuite) TestPatternRegexpExprRestore(c *C) {
RunNodeRestoreTest(c, testCases, "select %s", extractNodeFunc)
}

func (tc *testExpressionsSuite) TestRowExprRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"(1,2)", "ROW(1,2)"},
{"(col1,col2)", "ROW(`col1`,`col2`)"},
{"row(1,2)", "ROW(1,2)"},
{"row(col1,col2)", "ROW(`col1`,`col2`)"},
}
extractNodeFunc := func(node Node) Node {
return node.(*SelectStmt).Where.(*BinaryOperationExpr).L
}
RunNodeRestoreTest(c, testCases, "select 1 from t1 where %s = row(1,2)", extractNodeFunc)
}

func (tc *testExpressionsSuite) TestMaxValueExprRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"maxvalue", "MAXVALUE"},
Expand Down

0 comments on commit bb270e9

Please sign in to comment.