Skip to content

Commit

Permalink
parser, ast: add ColumnOptionCollate for all column types (pingcap#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
bb7133 authored and winkyao committed Apr 4, 2019
1 parent 1f3c393 commit 5ff8337
Show file tree
Hide file tree
Showing 5 changed files with 4,424 additions and 4,389 deletions.
10 changes: 9 additions & 1 deletion ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ const (
ColumnOptionComment
ColumnOptionGenerated
ColumnOptionReference
ColumnOptionCollate
)

// ColumnOption is used for parsing column constraint info from SQL.
Expand All @@ -365,7 +366,8 @@ type ColumnOption struct {
// Stored is only for ColumnOptionGenerated, default is false.
Stored bool
// Refer is used for foreign key.
Refer *ReferenceDef
Refer *ReferenceDef
StrValue string
}

// Restore implements Node interface.
Expand Down Expand Up @@ -416,6 +418,12 @@ func (n *ColumnOption) Restore(ctx *RestoreCtx) error {
if err := n.Refer.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while splicing ColumnOption ReferenceDef")
}
case ColumnOptionCollate:
if n.StrValue == "" {
return errors.New("Empty ColumnOption COLLATE")
}
ctx.WriteKeyWord("COLLATE ")
ctx.WritePlain(n.StrValue)
default:
return errors.New("An error occurred while splicing ColumnOption")
}
Expand Down
4 changes: 4 additions & 0 deletions ast/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func (ts *testDDLSuite) TestDDLColumnOptionRestore(c *C) {
{"generated always as(id + 1) virtual", "GENERATED ALWAYS AS(`id`+1) VIRTUAL"},
{"generated always as(id + 1) stored", "GENERATED ALWAYS AS(`id`+1) STORED"},
{"REFERENCES parent(id)", "REFERENCES `parent`(`id`)"},
{"COLLATE utf8_bin", "COLLATE utf8_bin"},
}
extractNodeFunc := func(node Node) Node {
return node.(*CreateTableStmt).Cols[0].Options[0]
Expand Down Expand Up @@ -238,6 +239,7 @@ func (ts *testDDLSuite) TestDDLColumnDefRestore(c *C) {
{"id text charset UTF8", "`id` TEXT CHARACTER SET UTF8"},
{"id varchar(50) collate UTF8MB4_CZECH_CI", "`id` VARCHAR(50) COLLATE UTF8MB4_CZECH_CI"},
{"id varchar(50) collate utf8", "`id` VARCHAR(50) COLLATE utf8"},
{"id varchar(50) collate utf8 collate utf8mb4_bin", "`id` VARCHAR(50) COLLATE utf8 COLLATE utf8mb4_bin"},
{"c1 char(10) character set LATIN1 collate latin1_german1_ci", "`c1` CHAR(10) CHARACTER SET LATIN1 COLLATE latin1_german1_ci"},

{"id int(11) PRIMARY KEY", "`id` INT(11) PRIMARY KEY"},
Expand All @@ -248,6 +250,8 @@ func (ts *testDDLSuite) TestDDLColumnDefRestore(c *C) {
{"id INT(11) DEFAULT '10'", "`id` INT(11) DEFAULT '10'"},
{"id INT(11) DEFAULT 1.1", "`id` INT(11) DEFAULT 1.1"},
{"id INT(11) UNIQUE KEY", "`id` INT(11) UNIQUE KEY"},
{"id INT(11) COLLATE ascii_bin", "`id` INT(11) COLLATE ascii_bin"},
{"id INT(11) collate ascii_bin collate utf8_bin", "`id` INT(11) COLLATE ascii_bin COLLATE utf8_bin"},
{"id INT(11) on update CURRENT_TIMESTAMP", "`id` INT(11) ON UPDATE CURRENT_TIMESTAMP()"},
{"id INT(11) comment 'hello'", "`id` INT(11) COMMENT 'hello'"},
{"id INT(11) generated always as(id + 1)", "`id` INT(11) GENERATED ALWAYS AS(`id`+1) VIRTUAL"},
Expand Down
Loading

0 comments on commit 5ff8337

Please sign in to comment.