Skip to content

Commit

Permalink
Fix set charset statement (pingcap#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjhuang2016 committed Jul 8, 2020
1 parent 3a18f1e commit e54f3d7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
10 changes: 7 additions & 3 deletions ast/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,11 @@ func (n *UseStmt) Accept(v Visitor) (Node, bool) {
}

const (
// SetNames is the const for set names/charset stmt.
// If VariableAssignment.Name == Names, it should be set names/charset stmt.
// SetNames is the const for set names stmt.
// If VariableAssignment.Name == Names, it should be set names stmt.
SetNames = "SetNAMES"
// SetCharset is the const for set charset stmt.
SetCharset = "SetCharset"
)

// VariableAssignment is a variable assignment struct.
Expand Down Expand Up @@ -591,11 +593,13 @@ func (n *VariableAssignment) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("SESSION")
}
ctx.WritePlain(".")
} else if n.Name != SetNames {
} else if n.Name != SetNames && n.Name != SetCharset {
ctx.WriteKeyWord("@")
}
if n.Name == SetNames {
ctx.WriteKeyWord("NAMES ")
} else if n.Name == SetCharset {
ctx.WriteKeyWord("CHARSET ")
} else {
ctx.WriteName(n.Name)
ctx.WritePlain("=")
Expand Down
2 changes: 1 addition & 1 deletion parser.go

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

2 changes: 1 addition & 1 deletion parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -8297,7 +8297,7 @@ VariableAssignment:
}
| CharsetKw CharsetNameOrDefault
{
$$ = &ast.VariableAssignment{Name: ast.SetNames, Value: $2}
$$ = &ast.VariableAssignment{Name: ast.SetCharset, Value: $2}
}

CharsetNameOrDefault:
Expand Down
12 changes: 6 additions & 6 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1011,8 +1011,8 @@ func (s *testParserSuite) TestDBAStmt(c *C) {
{"SET @@global.autocommit = default", true, "SET @@GLOBAL.`autocommit`=DEFAULT"},
{"SET @@session.autocommit = default", true, "SET @@SESSION.`autocommit`=DEFAULT"},
// SET CHARACTER SET
{"SET CHARACTER SET utf8mb4;", true, "SET NAMES 'utf8mb4'"},
{"SET CHARACTER SET 'utf8mb4';", true, "SET NAMES 'utf8mb4'"},
{"SET CHARACTER SET utf8mb4;", true, "SET CHARSET 'utf8mb4'"},
{"SET CHARACTER SET 'utf8mb4';", true, "SET CHARSET 'utf8mb4'"},
// set password
{"SET PASSWORD = 'password';", true, "SET PASSWORD='password'"},
{"SET PASSWORD FOR 'root'@'localhost' = 'password';", true, "SET PASSWORD FOR `root`@`localhost`='password'"},
Expand All @@ -1037,9 +1037,9 @@ func (s *testParserSuite) TestDBAStmt(c *C) {

// for set character set | name default
{"set names default", true, "SET NAMES DEFAULT"},
{"set character set default", true, "SET NAMES DEFAULT"},
{"set charset default", true, "SET NAMES DEFAULT"},
{"set char set default", true, "SET NAMES DEFAULT"},
{"set character set default", true, "SET CHARSET DEFAULT"},
{"set charset default", true, "SET CHARSET DEFAULT"},
{"set char set default", true, "SET CHARSET DEFAULT"},

{"set role `role1`", true, "SET ROLE `role1`@`%`"},
{"SET ROLE DEFAULT", true, "SET ROLE DEFAULT"},
Expand All @@ -1048,7 +1048,7 @@ func (s *testParserSuite) TestDBAStmt(c *C) {
{"SET DEFAULT ROLE administrator, developer TO `joe`@`10.0.0.1`", true, "SET DEFAULT ROLE `administrator`@`%`, `developer`@`%` TO `joe`@`10.0.0.1`"},
// for set names and set vars
{"set names utf8, @@session.sql_mode=1;", true, "SET NAMES 'utf8', @@SESSION.`sql_mode`=1"},
{"set @@session.sql_mode=1, names utf8, charset utf8;", true, "SET @@SESSION.`sql_mode`=1, NAMES 'utf8', NAMES 'utf8'"},
{"set @@session.sql_mode=1, names utf8, charset utf8;", true, "SET @@SESSION.`sql_mode`=1, NAMES 'utf8', CHARSET 'utf8'"},

// for set/show config
{"set config TIKV LOG.LEVEL='info'", true, "SET CONFIG TIKV LOG.LEVEL = 'info'"},
Expand Down

0 comments on commit e54f3d7

Please sign in to comment.