Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser, ast: add ColumnOptionCollate for all column types #260

Merged
merged 5 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add test in TestDDL for testing restore.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed, PTAL!

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