Skip to content

Commit

Permalink
parser: implement Restore for CreateIndexStmt (pingcap#150)
Browse files Browse the repository at this point in the history
* add empty test case

* CreateIndexStmt restore implement

* remove useless function TestCreateIndexStmtRestore

* CreateIndexStmt error add index info

* combine trivial code of CreateIndexStmt.Restore

* add unique index case for CreateIndexStmt.Restore

* merge code from master
  • Loading branch information
firekillice authored and kennytm committed Jan 13, 2019
1 parent 5ccee6e commit 9b09429
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
31 changes: 30 additions & 1 deletion ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,36 @@ type CreateIndexStmt struct {

// Restore implements Node interface.
func (n *CreateIndexStmt) Restore(ctx *RestoreCtx) error {
return errors.New("Not implemented")
ctx.WriteKeyWord("CREATE ")
if n.Unique {
ctx.WriteKeyWord("UNIQUE ")
}
ctx.WriteKeyWord("INDEX ")
ctx.WriteName(n.IndexName)
ctx.WriteKeyWord(" ON ")
if err := n.Table.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore CreateIndexStmt.Table")
}

ctx.WritePlain(" (")
for i, indexColName := range n.IndexColNames {
if i != 0 {
ctx.WritePlain(", ")
}
if err := indexColName.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore CreateIndexStmt.IndexColNames: [%v]", i)
}
}
ctx.WritePlain(")")

if n.IndexOption.Tp != model.IndexTypeInvalid || n.IndexOption.KeyBlockSize > 0 || n.IndexOption.Comment != "" {
ctx.WritePlain(" ")
if err := n.IndexOption.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore CreateIndexStmt.IndexOption")
}
}

return nil
}

// Accept implements Node Accept interface.
Expand Down
Empty file modified ast/ddl_test.go
100644 → 100755
Empty file.
15 changes: 8 additions & 7 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1789,13 +1789,14 @@ func (s *testParserSuite) TestDDL(c *C) {
{"alter table t analyze partition a index b with 4 buckets", true, ""},

// For create index statement
{"CREATE INDEX idx ON t (a)", true, ""},
{"CREATE INDEX idx ON t (a) USING HASH", true, ""},
{"CREATE INDEX idx ON t (a) COMMENT 'foo'", true, ""},
{"CREATE INDEX idx ON t (a) USING HASH COMMENT 'foo'", true, ""},
{"CREATE INDEX idx ON t (a) LOCK=NONE", true, ""},
{"CREATE INDEX idx USING BTREE ON t (a) USING HASH COMMENT 'foo'", true, ""},
{"CREATE INDEX idx USING BTREE ON t (a)", true, ""},
{"CREATE INDEX idx ON t (a)", true, "CREATE INDEX `idx` ON `t` (`a`)"},
{"CREATE UNIQUE INDEX idx ON t (a)", true, "CREATE UNIQUE INDEX `idx` ON `t` (`a`)"},
{"CREATE INDEX idx ON t (a) USING HASH", true, "CREATE INDEX `idx` ON `t` (`a`) USING HASH"},
{"CREATE INDEX idx ON t (a) COMMENT 'foo'", true, "CREATE INDEX `idx` ON `t` (`a`) COMMENT 'foo'"},
{"CREATE INDEX idx ON t (a) USING HASH COMMENT 'foo'", true, "CREATE INDEX `idx` ON `t` (`a`) USING HASH COMMENT 'foo'"},
{"CREATE INDEX idx ON t (a) LOCK=NONE", true, "CREATE INDEX `idx` ON `t` (`a`)"},
{"CREATE INDEX idx USING BTREE ON t (a) USING HASH COMMENT 'foo'", true, "CREATE INDEX `idx` ON `t` (`a`) USING HASH COMMENT 'foo'"},
{"CREATE INDEX idx USING BTREE ON t (a)", true, "CREATE INDEX `idx` ON `t` (`a`) USING BTREE"},

//For dorp index statement
{"drop index a on t", true, "DROP INDEX `a` ON `t`"},
Expand Down

0 comments on commit 9b09429

Please sign in to comment.