Skip to content

Commit

Permalink
Merge pull request #326 from hanchuanchuan/update-charset-convert
Browse files Browse the repository at this point in the history
update: 优化Alter Table字符集设置和转换语法的不同处理
  • Loading branch information
hanchuanchuan authored Mar 23, 2021
2 parents 6bf4262 + fb9662f commit 29e1502
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 14 deletions.
22 changes: 19 additions & 3 deletions ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,11 @@ const (
OnDuplicateKeyHandlingReplace
)

const (
TableOptionCharsetWithoutConvertTo uint64 = 0
TableOptionCharsetWithConvertTo uint64 = 1
)

// TableOption is used for parsing table option from SQL.
type TableOption struct {
Tp TableOptionType
Expand All @@ -1497,9 +1502,17 @@ func (n *TableOption) Restore(ctx *RestoreCtx) error {
ctx.WritePlain("''")
}
case TableOptionCharset:
ctx.WriteKeyWord("DEFAULT CHARACTER SET ")
ctx.WritePlain("= ")
if n.UintValue == TableOptionCharsetWithConvertTo {
ctx.WriteKeyWord("CONVERT TO ")
} else {
ctx.WriteKeyWord("DEFAULT ")
}
ctx.WriteKeyWord("CHARACTER SET ")
if n.UintValue == TableOptionCharsetWithoutConvertTo {
ctx.WriteKeyWord("= ")
}
ctx.WriteKeyWord(n.StrValue)

case TableOptionCollate:
ctx.WriteKeyWord("DEFAULT COLLATE ")
ctx.WritePlain("= ")
Expand Down Expand Up @@ -1865,7 +1878,10 @@ func (n *AlterTableSpec) Restore(ctx *RestoreCtx) error {
case len(n.Options) == 2 &&
n.Options[0].Tp == TableOptionCharset &&
n.Options[1].Tp == TableOptionCollate:
ctx.WriteKeyWord("CONVERT TO CHARACTER SET ")
if n.Options[0].UintValue == TableOptionCharsetWithConvertTo {
ctx.WriteKeyWord("CONVERT TO ")
}
ctx.WriteKeyWord("CHARACTER SET ")
ctx.WriteKeyWord(n.Options[0].StrValue)
ctx.WriteKeyWord(" COLLATE ")
ctx.WriteKeyWord(n.Options[1].StrValue)
Expand Down
4 changes: 2 additions & 2 deletions ast/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ func (ts *testDDLSuite) TestAlterTableSpecRestore(c *C) {

{"shard_row_id_bits 1", "SHARD_ROW_ID_BITS = 1"},
{"shard_row_id_bits = 1", "SHARD_ROW_ID_BITS = 1"},
{"CONVERT TO CHARACTER SET utf8", "DEFAULT CHARACTER SET = UTF8"},
{"CONVERT TO CHARSET utf8", "DEFAULT CHARACTER SET = UTF8"},
{"CONVERT TO CHARACTER SET utf8", "CONVERT TO CHARACTER SET UTF8"},
{"CONVERT TO CHARSET utf8", "CONVERT TO CHARACTER SET UTF8"},
{"CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin", "CONVERT TO CHARACTER SET UTF8 COLLATE UTF8_BIN"},
{"CONVERT TO CHARSET utf8 COLLATE utf8_bin", "CONVERT TO CHARACTER SET UTF8 COLLATE UTF8_BIN"},
{"ADD COLUMN (a SMALLINT UNSIGNED)", "ADD COLUMN (`a` SMALLINT UNSIGNED)"},
Expand Down
9 changes: 6 additions & 3 deletions parser/parser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,9 @@ AlterTableSpec:
{
op := &ast.AlterTableSpec{
Tp: ast.AlterTableOption,
Options: []*ast.TableOption{{Tp: ast.TableOptionCharset, StrValue: $4.(string)}},
Options: []*ast.TableOption{{Tp: ast.TableOptionCharset,
StrValue: $4.(string),
UintValue: ast.TableOptionCharsetWithConvertTo}},
}
if $5 != "" {
op.Options = append(op.Options, &ast.TableOption{Tp: ast.TableOptionCollate, StrValue: $5.(string)})
Expand Down Expand Up @@ -6918,7 +6920,8 @@ TableOption:
}
| DefaultKwdOpt CharsetKw EqOpt CharsetName
{
$$ = &ast.TableOption{Tp: ast.TableOptionCharset, StrValue: $4.(string)}
$$ = &ast.TableOption{Tp: ast.TableOptionCharset, StrValue: $4.(string),
UintValue: ast.TableOptionCharsetWithoutConvertTo}
}
| DefaultKwdOpt "COLLATE" EqOpt CollationName
{
Expand Down
3 changes: 1 addition & 2 deletions session/inception_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,8 @@ func (s *MyRecordSets) All() []*Record {
func (s *MyRecordSets) Next() *Record {
if s.cursor == s.count {
return nil
} else {
s.cursor++
}
s.cursor++
return s.records[s.cursor-1]
}

Expand Down
7 changes: 5 additions & 2 deletions session/session_inception.go
Original file line number Diff line number Diff line change
Expand Up @@ -3152,8 +3152,11 @@ func (s *session) checkAlterTable(node *ast.AlterTableStmt, sql string) {
case ast.AlterTableOption:
for _, opt := range alter.Options {
switch opt.Tp {
case ast.TableOptionCharset,
ast.TableOptionCollate,
case ast.TableOptionCharset:
if opt.UintValue == ast.TableOptionCharsetWithoutConvertTo {
ignoreOsc = true
}
case ast.TableOptionCollate,
ast.TableOptionComment:
ignoreOsc = true
}
Expand Down
5 changes: 5 additions & 0 deletions session/session_inception_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3069,6 +3069,11 @@ func (s *testSessionIncSuite) TestGetAlterTablePostPart(c *C) {
},
{
"alter table tb_archery default character set utf8 collate utf8_bin;",
"CHARACTER SET UTF8 COLLATE UTF8_BIN",
"CHARACTER SET UTF8 COLLATE UTF8_BIN",
},
{
"alter table tb_archery convert to character set utf8 collate utf8_bin;",
"CONVERT TO CHARACTER SET UTF8 COLLATE UTF8_BIN",
"CONVERT TO CHARACTER SET UTF8 COLLATE UTF8_BIN",
},
Expand Down

0 comments on commit 29e1502

Please sign in to comment.