Skip to content

Commit

Permalink
[parser] parser: implement Restore for VariableAssignment, SetStmt an…
Browse files Browse the repository at this point in the history
…d SetPwdStmt (#199)
  • Loading branch information
exialin authored and ti-chi-bot committed Oct 9, 2021
1 parent 000ed8d commit 86be1ca
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 47 deletions.
50 changes: 47 additions & 3 deletions parser/ast/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,33 @@ type VariableAssignment struct {

// Restore implements Node interface.
func (n *VariableAssignment) Restore(ctx *RestoreCtx) error {
return errors.New("Not implemented")
if n.IsSystem {
ctx.WritePlain("@@")
if n.IsGlobal {
ctx.WriteKeyWord("GLOBAL")
} else {
ctx.WriteKeyWord("SESSION")
}
ctx.WritePlain(".")
} else if n.Name != SetNames {
ctx.WriteKeyWord("@")
}
if n.Name == SetNames {
ctx.WriteKeyWord("NAMES ")
} else {
ctx.WriteName(n.Name)
ctx.WritePlain("=")
}
if err := n.Value.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore VariableAssignment.Value")
}
if n.ExtendValue != nil {
ctx.WriteKeyWord(" COLLATE ")
if err := n.ExtendValue.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore VariableAssignment.ExtendValue")
}
}
return nil
}

// Accept implements Node interface.
Expand Down Expand Up @@ -548,7 +574,16 @@ type SetStmt struct {

// Restore implements Node interface.
func (n *SetStmt) Restore(ctx *RestoreCtx) error {
return errors.New("Not implemented")
ctx.WriteKeyWord("SET ")
for i, v := range n.Variables {
if i != 0 {
ctx.WritePlain(", ")
}
if err := v.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore SetStmt.Variables[%d]", i)
}
}
return nil
}

// Accept implements Node Accept interface.
Expand Down Expand Up @@ -600,7 +635,16 @@ type SetPwdStmt struct {

// Restore implements Node interface.
func (n *SetPwdStmt) Restore(ctx *RestoreCtx) error {
return errors.New("Not implemented")
ctx.WriteKeyWord("SET PASSWORD")
if n.User != nil {
ctx.WriteKeyWord(" FOR ")
if err := n.User.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore SetPwdStmt.User")
}
}
ctx.WritePlain("=")
ctx.WriteString(n.Password)
return nil
}

// SecureText implements SensitiveStatement interface.
Expand Down
88 changes: 44 additions & 44 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,60 +693,60 @@ func (s *testParserSuite) TestDBAStmt(c *C) {
{"load stats '/tmp/stats.json'", true, "LOAD STATS '/tmp/stats.json'"},
// set
// user defined
{"SET @ = 1", true, ""},
{"SET @' ' = 1", true, ""},
{"SET @ = 1", true, "SET @``=1"},
{"SET @' ' = 1", true, "SET @` `=1"},
{"SET @! = 1", false, ""},
{"SET @1 = 1", true, ""},
{"SET @a = 1", true, ""},
{"SET @b := 1", true, ""},
{"SET @.c = 1", true, ""},
{"SET @_d = 1", true, ""},
{"SET @_e._$. = 1", true, ""},
{"SET @1 = 1", true, "SET @`1`=1"},
{"SET @a = 1", true, "SET @`a`=1"},
{"SET @b := 1", true, "SET @`b`=1"},
{"SET @.c = 1", true, "SET @`.c`=1"},
{"SET @_d = 1", true, "SET @`_d`=1"},
{"SET @_e._$. = 1", true, "SET @`_e._$.`=1"},
{"SET @~f = 1", false, ""},
{"SET @`g,` = 1", true, ""},
{"SET @`g,` = 1", true, "SET @`g,`=1"},
// session system variables
{"SET SESSION autocommit = 1", true, ""},
{"SET @@session.autocommit = 1", true, ""},
{"SET @@SESSION.autocommit = 1", true, ""},
{"SET @@GLOBAL.GTID_PURGED = '123'", true, ""},
{"SET @MYSQLDUMP_TEMP_LOG_BIN = @@SESSION.SQL_LOG_BIN", true, ""},
{"SET LOCAL autocommit = 1", true, ""},
{"SET @@local.autocommit = 1", true, ""},
{"SET @@autocommit = 1", true, ""},
{"SET autocommit = 1", true, ""},
{"SET SESSION autocommit = 1", true, "SET @@SESSION.`autocommit`=1"},
{"SET @@session.autocommit = 1", true, "SET @@SESSION.`autocommit`=1"},
{"SET @@SESSION.autocommit = 1", true, "SET @@SESSION.`autocommit`=1"},
{"SET @@GLOBAL.GTID_PURGED = '123'", true, "SET @@GLOBAL.`gtid_purged`='123'"},
{"SET @MYSQLDUMP_TEMP_LOG_BIN = @@SESSION.SQL_LOG_BIN", true, "SET @`MYSQLDUMP_TEMP_LOG_BIN`=@@SESSION.`sql_log_bin`"},
{"SET LOCAL autocommit = 1", true, "SET @@SESSION.`autocommit`=1"},
{"SET @@local.autocommit = 1", true, "SET @@SESSION.`autocommit`=1"},
{"SET @@autocommit = 1", true, "SET @@SESSION.`autocommit`=1"},
{"SET autocommit = 1", true, "SET @@SESSION.`autocommit`=1"},
// global system variables
{"SET GLOBAL autocommit = 1", true, ""},
{"SET @@global.autocommit = 1", true, ""},
{"SET GLOBAL autocommit = 1", true, "SET @@GLOBAL.`autocommit`=1"},
{"SET @@global.autocommit = 1", true, "SET @@GLOBAL.`autocommit`=1"},
// set default value
{"SET @@global.autocommit = default", true, ""},
{"SET @@session.autocommit = default", true, ""},
{"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 CHARACTER SET 'utf8mb4';", true, ""},
{"SET CHARACTER SET utf8mb4;", true, "SET NAMES 'utf8mb4'"},
{"SET CHARACTER SET 'utf8mb4';", true, "SET NAMES 'utf8mb4'"},
// set password
{"SET PASSWORD = 'password';", true, ""},
{"SET PASSWORD FOR 'root'@'localhost' = 'password';", true, ""},
{"SET PASSWORD = 'password';", true, "SET PASSWORD='password'"},
{"SET PASSWORD FOR 'root'@'localhost' = 'password';", true, "SET PASSWORD FOR `root`@`localhost`='password'"},
// SET TRANSACTION Syntax
{"SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ", true, ""},
{"SET GLOBAL TRANSACTION ISOLATION LEVEL REPEATABLE READ", true, ""},
{"SET SESSION TRANSACTION READ WRITE", true, ""},
{"SET SESSION TRANSACTION READ ONLY", true, ""},
{"SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED", true, ""},
{"SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED", true, ""},
{"SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE", true, ""},
{"SET TRANSACTION ISOLATION LEVEL REPEATABLE READ", true, ""},
{"SET TRANSACTION READ WRITE", true, ""},
{"SET TRANSACTION READ ONLY", true, ""},
{"SET TRANSACTION ISOLATION LEVEL READ COMMITTED", true, ""},
{"SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED", true, ""},
{"SET TRANSACTION ISOLATION LEVEL SERIALIZABLE", true, ""},
{"SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ", true, "SET @@SESSION.`tx_isolation`='REPEATABLE-READ'"},
{"SET GLOBAL TRANSACTION ISOLATION LEVEL REPEATABLE READ", true, "SET @@GLOBAL.`tx_isolation`='REPEATABLE-READ'"},
{"SET SESSION TRANSACTION READ WRITE", true, "SET @@SESSION.`tx_read_only`='0'"},
{"SET SESSION TRANSACTION READ ONLY", true, "SET @@SESSION.`tx_read_only`='1'"},
{"SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED", true, "SET @@SESSION.`tx_isolation`='READ-COMMITTED'"},
{"SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED", true, "SET @@SESSION.`tx_isolation`='READ-UNCOMMITTED'"},
{"SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE", true, "SET @@SESSION.`tx_isolation`='SERIALIZABLE'"},
{"SET TRANSACTION ISOLATION LEVEL REPEATABLE READ", true, "SET @@SESSION.`tx_isolation_one_shot`='REPEATABLE-READ'"},
{"SET TRANSACTION READ WRITE", true, "SET @@SESSION.`tx_read_only`='0'"},
{"SET TRANSACTION READ ONLY", true, "SET @@SESSION.`tx_read_only`='1'"},
{"SET TRANSACTION ISOLATION LEVEL READ COMMITTED", true, "SET @@SESSION.`tx_isolation_one_shot`='READ-COMMITTED'"},
{"SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED", true, "SET @@SESSION.`tx_isolation_one_shot`='READ-UNCOMMITTED'"},
{"SET TRANSACTION ISOLATION LEVEL SERIALIZABLE", true, "SET @@SESSION.`tx_isolation_one_shot`='SERIALIZABLE'"},
// for set names
{"set names utf8", true, ""},
{"set names utf8 collate utf8_unicode_ci", true, ""},
{"set names binary", true, ""},
{"set names utf8", true, "SET NAMES 'utf8'"},
{"set names utf8 collate utf8_unicode_ci", true, "SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'"},
{"set names binary", true, "SET NAMES 'binary'"},
// for set names and set vars
{"set names utf8, @@session.sql_mode=1;", true, ""},
{"set @@session.sql_mode=1, names utf8, charset utf8;", true, ""},
{"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'"},

// for FLUSH statement
{"flush no_write_to_binlog tables tbl1 with read lock", true, ""},
Expand Down

0 comments on commit 86be1ca

Please sign in to comment.