-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shenli/set password #195
Shenli/set password #195
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -203,6 +203,9 @@ func (s *testParserSuite) TestParser0(c *C) { | |
// SET CHARACTER SET | ||
{"SET CHARACTER SET utf8mb4;", true}, | ||
{"SET CHARACTER SET 'utf8mb4';", true}, | ||
// Set password | ||
{"SET PASSWORD = 'password';", true}, | ||
{"SET PASSWORD FOR 'shenli'@'localhost' = 'password';", true}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think root may be better than your name...... |
||
|
||
// qualified select | ||
{"SELECT a.b.c FROM t", true}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ import ( | |
"github.com/ngaut/log" | ||
. "github.com/pingcap/check" | ||
"github.com/pingcap/tidb" | ||
mysql "github.com/pingcap/tidb/mysqldef" | ||
) | ||
|
||
func TestT(t *testing.T) { | ||
|
@@ -32,18 +33,20 @@ var _ = Suite(&testStmtSuite{}) | |
type testStmtSuite struct { | ||
dbName string | ||
|
||
testDB *sql.DB | ||
createDBSql string | ||
dropDBSql string | ||
useDBSql string | ||
createTableSql string | ||
insertSql string | ||
selectSql string | ||
testDB *sql.DB | ||
createDBSql string | ||
dropDBSql string | ||
useDBSql string | ||
createTableSql string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace Sql to Text would be better. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They are all SQL statements. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, SQL is structured query language, it doesn't means text. |
||
insertSql string | ||
selectSql string | ||
createMySQLDBSQL string | ||
createMySQLUserTableSQL string | ||
} | ||
|
||
func (s *testStmtSuite) SetUpTest(c *C) { | ||
log.SetLevelByString("error") | ||
s.dbName = "test" | ||
s.dbName = "teststmts" | ||
var err error | ||
s.testDB, err = sql.Open(tidb.DriverName, tidb.EngineGoLevelDBMemory+"/"+s.dbName+"/"+s.dbName) | ||
c.Assert(err, IsNil) | ||
|
@@ -59,6 +62,12 @@ func (s *testStmtSuite) SetUpTest(c *C) { | |
s.selectSql = `SELECT * from test limit 2;` | ||
mustExec(c, s.testDB, s.createDBSql) | ||
mustExec(c, s.testDB, s.useDBSql) | ||
|
||
s.createMySQLDBSQL = fmt.Sprintf("create database if not exists %s;", mysql.MySQLDB) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MySQLDBSQL looks wired. Could you rename createMySQLDBSQL to createDBText There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MySQL is the DB name. This is the system db. How about createSystemDB? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. createSystemDB is ok for me :) |
||
s.createMySQLUserTableSQL = mysql.CreateUserTable | ||
|
||
mustExec(c, s.testDB, s.createMySQLDBSQL) | ||
mustExec(c, s.testDB, s.createMySQLUserTableSQL) | ||
} | ||
|
||
func (s *testStmtSuite) TearDownTest(c *C) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,9 +22,14 @@ import ( | |
"github.com/pingcap/tidb/context" | ||
"github.com/pingcap/tidb/expression" | ||
"github.com/pingcap/tidb/expression/expressions" | ||
"github.com/pingcap/tidb/model" | ||
mysql "github.com/pingcap/tidb/mysqldef" | ||
"github.com/pingcap/tidb/parser/opcode" | ||
"github.com/pingcap/tidb/rset" | ||
"github.com/pingcap/tidb/rset/rsets" | ||
"github.com/pingcap/tidb/sessionctx/variable" | ||
"github.com/pingcap/tidb/stmt" | ||
"github.com/pingcap/tidb/table" | ||
"github.com/pingcap/tidb/util/format" | ||
) | ||
|
||
|
@@ -230,6 +235,30 @@ func (s *SetPwdStmt) SetText(text string) { | |
|
||
// Exec implements the stmt.Statement Exec interface. | ||
func (s *SetPwdStmt) Exec(ctx context.Context) (_ rset.Recordset, err error) { | ||
// TODO: finish this | ||
return nil, nil | ||
// If len(s.User) == 0, use CURRENT_USER() | ||
strs := strings.Split(s.User, "@") | ||
userName := strs[0] | ||
host := strs[1] | ||
// Update mysql.user | ||
r := &rsets.JoinRset{ | ||
Left: &rsets.TableSource{ | ||
Source: table.Ident{ | ||
Name: model.NewCIStr(mysql.UserTable), | ||
Schema: model.NewCIStr(mysql.MySQLDB), | ||
}, | ||
}, | ||
} | ||
asgn := expressions.Assignment{ | ||
ColName: "Password", | ||
Expr: expressions.Value{Val: s.Password}, | ||
} | ||
nameMatch := expressions.NewBinaryOperation(opcode.EQ, &expressions.Ident{CIStr: model.NewCIStr("User")}, &expressions.Value{Val: userName}) | ||
hostMatch := expressions.NewBinaryOperation(opcode.EQ, &expressions.Ident{CIStr: model.NewCIStr("Host")}, &expressions.Value{Val: host}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here not check name or host match, how about nameExpr and hostExpr? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean check the validation of name/host? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I only mean whether the variable names use nameExpr and hostExpr are better, for nameMatch and nameMatch sound like boolean variables. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I only mean whether the variable names use nameExpr and hostExpr are better, for nameMatch and nameMatch sound like boolean variables. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They are boolean expressions. For nameMatch means if "User" equals userName. |
||
where := expressions.NewBinaryOperation(opcode.AndAnd, nameMatch, hostMatch) | ||
st := &UpdateStmt{ | ||
TableRefs: r, | ||
List: []expressions.Assignment{asgn}, | ||
Where: where, | ||
} | ||
return st.Exec(ctx) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw, how do u think of building an update sql to excute it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't want to call parser for the second time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MySQL
End of .