From e54f3d768ec8b3faed3d04fa4d3cc796ae3c8017 Mon Sep 17 00:00:00 2001 From: wjHuang Date: Fri, 15 May 2020 14:34:35 +0800 Subject: [PATCH] Fix `set charset` statement (#836) --- ast/misc.go | 10 +++++++--- parser.go | 2 +- parser.y | 2 +- parser_test.go | 12 ++++++------ 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/ast/misc.go b/ast/misc.go index 7f481a241..6f669486b 100755 --- a/ast/misc.go +++ b/ast/misc.go @@ -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. @@ -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("=") diff --git a/parser.go b/parser.go index 8f65e362d..85991606a 100644 --- a/parser.go +++ b/parser.go @@ -15445,7 +15445,7 @@ yynewstate: } case 1484: { - parser.yyVAL.item = &ast.VariableAssignment{Name: ast.SetNames, Value: yyS[yypt-0].expr} + parser.yyVAL.item = &ast.VariableAssignment{Name: ast.SetCharset, Value: yyS[yypt-0].expr} } case 1485: { diff --git a/parser.y b/parser.y index 5fe4f4097..e679bde35 100644 --- a/parser.y +++ b/parser.y @@ -8297,7 +8297,7 @@ VariableAssignment: } | CharsetKw CharsetNameOrDefault { - $$ = &ast.VariableAssignment{Name: ast.SetNames, Value: $2} + $$ = &ast.VariableAssignment{Name: ast.SetCharset, Value: $2} } CharsetNameOrDefault: diff --git a/parser_test.go b/parser_test.go index 7565252c4..b8c1bd01b 100644 --- a/parser_test.go +++ b/parser_test.go @@ -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'"}, @@ -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"}, @@ -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'"},