[parser] fix alter table convert to charset default syntax (#566)

This commit is contained in:
sim41
2019-11-25 11:00:52 +08:00
committed by Ti Chi Robot
parent c47b3b66dc
commit 570f2f00a6
4 changed files with 3895 additions and 3851 deletions

View File

@ -1692,7 +1692,11 @@ func (n *TableOption) Restore(ctx *RestoreCtx) error {
case TableOptionCharset:
ctx.WriteKeyWord("DEFAULT CHARACTER SET ")
ctx.WritePlain("= ")
ctx.WriteKeyWord(n.StrValue)
if n.Default {
ctx.WriteKeyWord("DEFAULT")
} else {
ctx.WriteKeyWord(n.StrValue)
}
case TableOptionCollate:
ctx.WriteKeyWord("DEFAULT COLLATE ")
ctx.WritePlain("= ")
@ -2110,9 +2114,16 @@ func (n *AlterTableSpec) Restore(ctx *RestoreCtx) error {
n.Options[0].Tp == TableOptionCharset &&
n.Options[1].Tp == TableOptionCollate:
ctx.WriteKeyWord("CONVERT TO CHARACTER SET ")
ctx.WriteKeyWord(n.Options[0].StrValue)
if n.Options[0].Default {
ctx.WriteKeyWord("DEFAULT")
} else {
ctx.WriteKeyWord(n.Options[0].StrValue)
}
ctx.WriteKeyWord(" COLLATE ")
ctx.WriteKeyWord(n.Options[1].StrValue)
case n.Options[0].Tp == TableOptionCharset &&
n.Options[0].Default:
ctx.WriteKeyWord("CONVERT TO CHARACTER SET DEFAULT")
default:
for i, opt := range n.Options {
if i != 0 {

File diff suppressed because it is too large Load Diff

View File

@ -1328,6 +1328,17 @@ AlterTableSpec:
}
$$ = op
}
| "CONVERT" "TO" CharsetKw "DEFAULT" OptCollate
{
op := &ast.AlterTableSpec{
Tp: ast.AlterTableOption,
Options:[]*ast.TableOption{{Tp: ast.TableOptionCharset, Default: true}},
}
if $5 != "" {
op.Options = append(op.Options, &ast.TableOption{Tp: ast.TableOptionCollate, StrValue: $5.(string)})
}
$$ = op
}
| "ADD" ColumnKeywordOpt IfNotExists ColumnDef ColumnPosition
{
$$ = &ast.AlterTableSpec{

View File

@ -2351,6 +2351,13 @@ func (s *testParserSuite) TestDDL(c *C) {
{"ALTER TABLE t CONVERT TO CHARSET UTF8;", true, "ALTER TABLE `t` DEFAULT CHARACTER SET = UTF8"},
{"ALTER TABLE t CONVERT TO CHARACTER SET UTF8 COLLATE UTF8_BIN;", true, "ALTER TABLE `t` CONVERT TO CHARACTER SET UTF8 COLLATE UTF8_BIN"},
{"ALTER TABLE t CONVERT TO CHARSET UTF8 COLLATE UTF8_BIN;", true, "ALTER TABLE `t` CONVERT TO CHARACTER SET UTF8 COLLATE UTF8_BIN"},
// alter table convert to character set default, issue #498
{"alter table d_n.t_n convert to character set default", true, "ALTER TABLE `d_n`.`t_n` CONVERT TO CHARACTER SET DEFAULT"},
{"alter table d_n.t_n convert to charset default", true, "ALTER TABLE `d_n`.`t_n` CONVERT TO CHARACTER SET DEFAULT"},
{"alter table d_n.t_n convert to char set default", true, "ALTER TABLE `d_n`.`t_n` CONVERT TO CHARACTER SET DEFAULT"},
{"alter table d_n.t_n convert to character set default collate utf8mb4_0900_ai_ci", true, "ALTER TABLE `d_n`.`t_n` CONVERT TO CHARACTER SET DEFAULT COLLATE UTF8MB4_0900_AI_CI"},
{"ALTER TABLE t FORCE", true, "ALTER TABLE `t` FORCE /* AlterTableForce is not supported */ "},
{"ALTER TABLE t DROP INDEX;", false, "ALTER TABLE `t` DROP INDEX"},
{"ALTER TABLE t DROP INDEX a", true, "ALTER TABLE `t` DROP INDEX `a`"},