From bd78bdd29017867fb4dd6f121fd90006f868f88f Mon Sep 17 00:00:00 2001 From: cdp <1> Date: Thu, 20 Dec 2018 16:29:05 +0800 Subject: [PATCH 01/11] implement Restore for IndexColName --- ast/ddl.go | 9 ++++++++- ast/ddl_test.go | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/ast/ddl.go b/ast/ddl.go index b1d382f20..38718873a 100644 --- a/ast/ddl.go +++ b/ast/ddl.go @@ -18,6 +18,7 @@ import ( "github.com/pingcap/parser/auth" "github.com/pingcap/parser/model" "github.com/pingcap/parser/types" + "strconv" ) var ( @@ -156,7 +157,13 @@ type IndexColName struct { // Restore implements Node interface. func (n *IndexColName) Restore(ctx *RestoreCtx) error { - return errors.New("Not implemented") + if err := n.Column.Restore(ctx); err != nil { + return errors.Annotate(err, "An error occurred while splicing IndexColName") + } + if n.Length > 0 { + ctx.WritePlain("(" + strconv.Itoa(n.Length) + ")") + } + return nil } // Accept implements Node Accept interface. diff --git a/ast/ddl_test.go b/ast/ddl_test.go index a7fbd0f64..f7d3d6a29 100644 --- a/ast/ddl_test.go +++ b/ast/ddl_test.go @@ -62,3 +62,26 @@ func (ts *testDDLSuite) TestDDLVisitorCover(c *C) { v.node.Accept(visitor1{}) } } + +type stmtStruct struct { + stmt string + extractNodeFunc func(node Node) Node +} + +func (ts *testDDLSuite) TestDDLIndexColNameRestore(c *C) { + stmtTestCase := []stmtStruct{ + {"CREATE TABLE hello2 (world VARCHAR(20), PRIMARY KEY idx_1 (%s))", func(node Node) Node { return node.(*CreateTableStmt).Constraints[0].Keys[0] }}, + {"CREATE TABLE hello2 (world VARCHAR(20), FULLTEXT KEY idx_1 (%s))", func(node Node) Node { return node.(*CreateTableStmt).Constraints[0].Keys[0] }}, + {"CREATE TABLE hello1 (world VARCHAR(20), INDEX idx_1 (%s))", func(node Node) Node { return node.(*CreateTableStmt).Constraints[0].Keys[0] }}, + {"CREATE TABLE hello1 (world VARCHAR(20), UNIQUE INDEX idx_1 (%s))", func(node Node) Node { return node.(*CreateTableStmt).Constraints[0].Keys[0] }}, + {"CREATE INDEX idx ON t (%s) USING HASH",func(node Node) Node { return node.(*CreateIndexStmt).IndexColNames[0] }}, + } + + for _, s := range stmtTestCase { + testCases := []NodeRestoreTestCase{ + {"world", "`world`"}, + {"world(2)", "`world`(2)"}, + } + RunNodeRestoreTest(c, testCases, s.stmt, s.extractNodeFunc) + } +} From ef3c50dc14d773e6bb0df1e6c5533b5597ab38fa Mon Sep 17 00:00:00 2001 From: cdp <1> Date: Thu, 20 Dec 2018 16:35:30 +0800 Subject: [PATCH 02/11] Add test case --- ast/ddl_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ast/ddl_test.go b/ast/ddl_test.go index f7d3d6a29..1cd59e23a 100644 --- a/ast/ddl_test.go +++ b/ast/ddl_test.go @@ -74,7 +74,10 @@ func (ts *testDDLSuite) TestDDLIndexColNameRestore(c *C) { {"CREATE TABLE hello2 (world VARCHAR(20), FULLTEXT KEY idx_1 (%s))", func(node Node) Node { return node.(*CreateTableStmt).Constraints[0].Keys[0] }}, {"CREATE TABLE hello1 (world VARCHAR(20), INDEX idx_1 (%s))", func(node Node) Node { return node.(*CreateTableStmt).Constraints[0].Keys[0] }}, {"CREATE TABLE hello1 (world VARCHAR(20), UNIQUE INDEX idx_1 (%s))", func(node Node) Node { return node.(*CreateTableStmt).Constraints[0].Keys[0] }}, - {"CREATE INDEX idx ON t (%s) USING HASH",func(node Node) Node { return node.(*CreateIndexStmt).IndexColNames[0] }}, + {"CREATE INDEX idx ON t (%s) USING HASH", func(node Node) Node { return node.(*CreateIndexStmt).IndexColNames[0] }}, + {"CREATE INDEX idx ON t (a) KEY_BLOCK_SIZE = 1232", func(node Node) Node { return node.(*CreateIndexStmt).IndexColNames[0] }}, + {"CREATE INDEX idx ON t (a) USING BTREE", func(node Node) Node { return node.(*CreateIndexStmt).IndexColNames[0] }}, + {"CREATE INDEX idx ON t (a) COMMENT 'foo'", func(node Node) Node { return node.(*CreateIndexStmt).IndexColNames[0] }}, } for _, s := range stmtTestCase { From 21582bfc8415e3e0e6497f067fd60366fb5d6c73 Mon Sep 17 00:00:00 2001 From: cdp <1> Date: Thu, 20 Dec 2018 16:39:25 +0800 Subject: [PATCH 03/11] Del other case --- ast/ddl_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/ast/ddl_test.go b/ast/ddl_test.go index 1cd59e23a..f0c1aac73 100644 --- a/ast/ddl_test.go +++ b/ast/ddl_test.go @@ -75,7 +75,6 @@ func (ts *testDDLSuite) TestDDLIndexColNameRestore(c *C) { {"CREATE TABLE hello1 (world VARCHAR(20), INDEX idx_1 (%s))", func(node Node) Node { return node.(*CreateTableStmt).Constraints[0].Keys[0] }}, {"CREATE TABLE hello1 (world VARCHAR(20), UNIQUE INDEX idx_1 (%s))", func(node Node) Node { return node.(*CreateTableStmt).Constraints[0].Keys[0] }}, {"CREATE INDEX idx ON t (%s) USING HASH", func(node Node) Node { return node.(*CreateIndexStmt).IndexColNames[0] }}, - {"CREATE INDEX idx ON t (a) KEY_BLOCK_SIZE = 1232", func(node Node) Node { return node.(*CreateIndexStmt).IndexColNames[0] }}, {"CREATE INDEX idx ON t (a) USING BTREE", func(node Node) Node { return node.(*CreateIndexStmt).IndexColNames[0] }}, {"CREATE INDEX idx ON t (a) COMMENT 'foo'", func(node Node) Node { return node.(*CreateIndexStmt).IndexColNames[0] }}, } From a97f5f08bcbe7dfda26328a025c01b09911f96b5 Mon Sep 17 00:00:00 2001 From: cdp <1> Date: Thu, 20 Dec 2018 16:43:43 +0800 Subject: [PATCH 04/11] Change test case --- ast/ddl_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ast/ddl_test.go b/ast/ddl_test.go index f0c1aac73..50ee6086f 100644 --- a/ast/ddl_test.go +++ b/ast/ddl_test.go @@ -75,8 +75,8 @@ func (ts *testDDLSuite) TestDDLIndexColNameRestore(c *C) { {"CREATE TABLE hello1 (world VARCHAR(20), INDEX idx_1 (%s))", func(node Node) Node { return node.(*CreateTableStmt).Constraints[0].Keys[0] }}, {"CREATE TABLE hello1 (world VARCHAR(20), UNIQUE INDEX idx_1 (%s))", func(node Node) Node { return node.(*CreateTableStmt).Constraints[0].Keys[0] }}, {"CREATE INDEX idx ON t (%s) USING HASH", func(node Node) Node { return node.(*CreateIndexStmt).IndexColNames[0] }}, - {"CREATE INDEX idx ON t (a) USING BTREE", func(node Node) Node { return node.(*CreateIndexStmt).IndexColNames[0] }}, - {"CREATE INDEX idx ON t (a) COMMENT 'foo'", func(node Node) Node { return node.(*CreateIndexStmt).IndexColNames[0] }}, + {"CREATE INDEX idx ON t (%s) USING BTREE", func(node Node) Node { return node.(*CreateIndexStmt).IndexColNames[0] }}, + {"CREATE INDEX idx ON t (%s) COMMENT 'foo'", func(node Node) Node { return node.(*CreateIndexStmt).IndexColNames[0] }}, } for _, s := range stmtTestCase { From 7e486e0400625e3257628f7ab59dd790094b84f9 Mon Sep 17 00:00:00 2001 From: cdp <1> Date: Fri, 21 Dec 2018 11:56:01 +0800 Subject: [PATCH 05/11] Change test case --- ast/ddl.go | 3 +-- ast/ddl_test.go | 21 ++++++--------------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/ast/ddl.go b/ast/ddl.go index 38718873a..e4601ceea 100644 --- a/ast/ddl.go +++ b/ast/ddl.go @@ -18,7 +18,6 @@ import ( "github.com/pingcap/parser/auth" "github.com/pingcap/parser/model" "github.com/pingcap/parser/types" - "strconv" ) var ( @@ -161,7 +160,7 @@ func (n *IndexColName) Restore(ctx *RestoreCtx) error { return errors.Annotate(err, "An error occurred while splicing IndexColName") } if n.Length > 0 { - ctx.WritePlain("(" + strconv.Itoa(n.Length) + ")") + ctx.WritePlainf("(%d)",n.Length) } return nil } diff --git a/ast/ddl_test.go b/ast/ddl_test.go index 50ee6086f..a84d5defd 100644 --- a/ast/ddl_test.go +++ b/ast/ddl_test.go @@ -69,21 +69,12 @@ type stmtStruct struct { } func (ts *testDDLSuite) TestDDLIndexColNameRestore(c *C) { - stmtTestCase := []stmtStruct{ - {"CREATE TABLE hello2 (world VARCHAR(20), PRIMARY KEY idx_1 (%s))", func(node Node) Node { return node.(*CreateTableStmt).Constraints[0].Keys[0] }}, - {"CREATE TABLE hello2 (world VARCHAR(20), FULLTEXT KEY idx_1 (%s))", func(node Node) Node { return node.(*CreateTableStmt).Constraints[0].Keys[0] }}, - {"CREATE TABLE hello1 (world VARCHAR(20), INDEX idx_1 (%s))", func(node Node) Node { return node.(*CreateTableStmt).Constraints[0].Keys[0] }}, - {"CREATE TABLE hello1 (world VARCHAR(20), UNIQUE INDEX idx_1 (%s))", func(node Node) Node { return node.(*CreateTableStmt).Constraints[0].Keys[0] }}, - {"CREATE INDEX idx ON t (%s) USING HASH", func(node Node) Node { return node.(*CreateIndexStmt).IndexColNames[0] }}, - {"CREATE INDEX idx ON t (%s) USING BTREE", func(node Node) Node { return node.(*CreateIndexStmt).IndexColNames[0] }}, - {"CREATE INDEX idx ON t (%s) COMMENT 'foo'", func(node Node) Node { return node.(*CreateIndexStmt).IndexColNames[0] }}, + testCases := []NodeRestoreTestCase{ + {"world", "`world`"}, + {"world(2)", "`world`(2)"}, } - - for _, s := range stmtTestCase { - testCases := []NodeRestoreTestCase{ - {"world", "`world`"}, - {"world(2)", "`world`(2)"}, - } - RunNodeRestoreTest(c, testCases, s.stmt, s.extractNodeFunc) + extractNodeFunc := func(node Node) Node { + return node.(*CreateIndexStmt).IndexColNames[0] } + RunNodeRestoreTest(c, testCases, "CREATE INDEX idx ON t (%s) USING HASH", extractNodeFunc) } From c6f11ab11f9120a0770311327601450808232c72 Mon Sep 17 00:00:00 2001 From: cdp <1> Date: Fri, 21 Dec 2018 14:26:03 +0800 Subject: [PATCH 06/11] Del --- ast/ddl_test.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/ast/ddl_test.go b/ast/ddl_test.go index a84d5defd..4045c8c54 100644 --- a/ast/ddl_test.go +++ b/ast/ddl_test.go @@ -63,11 +63,6 @@ func (ts *testDDLSuite) TestDDLVisitorCover(c *C) { } } -type stmtStruct struct { - stmt string - extractNodeFunc func(node Node) Node -} - func (ts *testDDLSuite) TestDDLIndexColNameRestore(c *C) { testCases := []NodeRestoreTestCase{ {"world", "`world`"}, From e0923d027b238039573098898a443172bb399ffb Mon Sep 17 00:00:00 2001 From: cdp <1> Date: Fri, 21 Dec 2018 17:45:23 +0800 Subject: [PATCH 07/11] Del --- ast/ddl_test.go | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/ast/ddl_test.go b/ast/ddl_test.go index 6f2364733..e71fe3a51 100644 --- a/ast/ddl_test.go +++ b/ast/ddl_test.go @@ -63,10 +63,6 @@ func (ts *testDDLSuite) TestDDLVisitorCover(c *C) { } } -<<<<<<< HEAD -======= - ->>>>>>> c1417b374e4f7b9a4181c1b0c59c52421d93d939 func (ts *testDDLSuite) TestDDLIndexColNameRestore(c *C) { testCases := []NodeRestoreTestCase{ {"world", "`world`"}, @@ -80,23 +76,18 @@ func (ts *testDDLSuite) TestDDLIndexColNameRestore(c *C) { func (ts *testDDLSuite) TestDDLIndexOption(c *C) { testCases := []NodeRestoreTestCase{ - {"key_block_size=16","KEY_BLOCK_SIZE=16"}, - {"USING HASH","USING HASH"}, - {"comment 'hello'","COMMENT 'hello'"}, - {"key_block_size=16 USING HASH","KEY_BLOCK_SIZE=16 USING HASH"}, - {"USING HASH KEY_BLOCK_SIZE=16","KEY_BLOCK_SIZE=16 USING HASH"}, - {"USING HASH COMMENT 'foo'","USING HASH COMMENT 'foo'"}, - {"COMMENT 'foo'","COMMENT 'foo'"}, - {"key_block_size = 32 using hash comment 'hello'","KEY_BLOCK_SIZE=32 USING HASH COMMENT 'hello'"}, - {"key_block_size=32 using btree comment 'hello'","KEY_BLOCK_SIZE=32 USING BTREE COMMENT 'hello'"}, + {"key_block_size=16", "KEY_BLOCK_SIZE=16"}, + {"USING HASH", "USING HASH"}, + {"comment 'hello'", "COMMENT 'hello'"}, + {"key_block_size=16 USING HASH", "KEY_BLOCK_SIZE=16 USING HASH"}, + {"USING HASH KEY_BLOCK_SIZE=16", "KEY_BLOCK_SIZE=16 USING HASH"}, + {"USING HASH COMMENT 'foo'", "USING HASH COMMENT 'foo'"}, + {"COMMENT 'foo'", "COMMENT 'foo'"}, + {"key_block_size = 32 using hash comment 'hello'", "KEY_BLOCK_SIZE=32 USING HASH COMMENT 'hello'"}, + {"key_block_size=32 using btree comment 'hello'", "KEY_BLOCK_SIZE=32 USING BTREE COMMENT 'hello'"}, } extractNodeFunc := func(node Node) Node { return node.(*CreateIndexStmt).IndexOption } RunNodeRestoreTest(c, testCases, "CREATE INDEX idx ON t (a) %s", extractNodeFunc) -<<<<<<< HEAD -} -======= } - ->>>>>>> c1417b374e4f7b9a4181c1b0c59c52421d93d939 From d941f239cc9463e0a11b6da32ba716d945145712 Mon Sep 17 00:00:00 2001 From: cdp <1> Date: Fri, 21 Dec 2018 18:50:59 +0800 Subject: [PATCH 08/11] format --- ast/ddl_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/ast/ddl_test.go b/ast/ddl_test.go index e71fe3a51..094c3d977 100644 --- a/ast/ddl_test.go +++ b/ast/ddl_test.go @@ -76,15 +76,15 @@ func (ts *testDDLSuite) TestDDLIndexColNameRestore(c *C) { func (ts *testDDLSuite) TestDDLIndexOption(c *C) { testCases := []NodeRestoreTestCase{ - {"key_block_size=16", "KEY_BLOCK_SIZE=16"}, - {"USING HASH", "USING HASH"}, - {"comment 'hello'", "COMMENT 'hello'"}, - {"key_block_size=16 USING HASH", "KEY_BLOCK_SIZE=16 USING HASH"}, - {"USING HASH KEY_BLOCK_SIZE=16", "KEY_BLOCK_SIZE=16 USING HASH"}, - {"USING HASH COMMENT 'foo'", "USING HASH COMMENT 'foo'"}, - {"COMMENT 'foo'", "COMMENT 'foo'"}, - {"key_block_size = 32 using hash comment 'hello'", "KEY_BLOCK_SIZE=32 USING HASH COMMENT 'hello'"}, - {"key_block_size=32 using btree comment 'hello'", "KEY_BLOCK_SIZE=32 USING BTREE COMMENT 'hello'"}, + {"key_block_size=16","KEY_BLOCK_SIZE=16"}, + {"USING HASH","USING HASH"}, + {"comment 'hello'","COMMENT 'hello'"}, + {"key_block_size=16 USING HASH","KEY_BLOCK_SIZE=16 USING HASH"}, + {"USING HASH KEY_BLOCK_SIZE=16","KEY_BLOCK_SIZE=16 USING HASH"}, + {"USING HASH COMMENT 'foo'","USING HASH COMMENT 'foo'"}, + {"COMMENT 'foo'","COMMENT 'foo'"}, + {"key_block_size = 32 using hash comment 'hello'","KEY_BLOCK_SIZE=32 USING HASH COMMENT 'hello'"}, + {"key_block_size=32 using btree comment 'hello'","KEY_BLOCK_SIZE=32 USING BTREE COMMENT 'hello'"}, } extractNodeFunc := func(node Node) Node { return node.(*CreateIndexStmt).IndexOption From 7c84898ebbfb230c5ca0b6b4d4fbd479c817718d Mon Sep 17 00:00:00 2001 From: cdp <1> Date: Mon, 24 Dec 2018 13:21:51 +0800 Subject: [PATCH 09/11] fix --- ast/ddl_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ast/ddl_test.go b/ast/ddl_test.go index e8ca095b8..079bdd818 100644 --- a/ast/ddl_test.go +++ b/ast/ddl_test.go @@ -63,7 +63,6 @@ func (ts *testDDLSuite) TestDDLVisitorCover(c *C) { } } - func (ts *testDDLSuite) TestDDLIndexColNameRestore(c *C) { testCases := []NodeRestoreTestCase{ {"world", "`world`"}, @@ -73,6 +72,7 @@ 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{ From dabea552a81bcdf8423b669465211f05ba457cfb Mon Sep 17 00:00:00 2001 From: cdp <1> Date: Mon, 24 Dec 2018 13:25:38 +0800 Subject: [PATCH 10/11] fmt --- ast/ddl.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ast/ddl.go b/ast/ddl.go index c9e8199b4..6a37a607c 100644 --- a/ast/ddl.go +++ b/ast/ddl.go @@ -160,7 +160,7 @@ func (n *IndexColName) Restore(ctx *RestoreCtx) error { return errors.Annotate(err, "An error occurred while splicing IndexColName") } if n.Length > 0 { - ctx.WritePlainf("(%d)",n.Length) + ctx.WritePlainf("(%d)", n.Length) } return nil } @@ -317,7 +317,7 @@ const ( ColumnOptionDefaultValue ColumnOptionUniqKey ColumnOptionNull - ColumnOptionOnUpdate // For Timestamp and Datetime only. + ColumnOptionOnUpdate // For Timestamp and Datetime only. ColumnOptionFulltext ColumnOptionComment ColumnOptionGenerated From 1efad7805d287d43e127e00083f6684481d3a240 Mon Sep 17 00:00:00 2001 From: cdp <1> Date: Mon, 24 Dec 2018 14:23:35 +0800 Subject: [PATCH 11/11] space --- ast/ddl.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ast/ddl.go b/ast/ddl.go index 6a37a607c..ab53f259c 100644 --- a/ast/ddl.go +++ b/ast/ddl.go @@ -317,7 +317,7 @@ const ( ColumnOptionDefaultValue ColumnOptionUniqKey ColumnOptionNull - ColumnOptionOnUpdate // For Timestamp and Datetime only. + ColumnOptionOnUpdate // For Timestamp and Datetime only. ColumnOptionFulltext ColumnOptionComment ColumnOptionGenerated