Skip to content

Commit

Permalink
serssion, server: fix the correctness of the query field in the DDL j…
Browse files Browse the repository at this point in the history
  • Loading branch information
zimulala authored and sre-bot committed Mar 18, 2020
1 parent f7e6943 commit 4188903
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
6 changes: 0 additions & 6 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1030,12 +1030,6 @@ func (s *session) SetProcessInfo(sql string, t time.Time, command byte, maxExecu
}

func (s *session) executeStatement(ctx context.Context, connID uint64, stmtNode ast.StmtNode, stmt sqlexec.Statement, recordSets []sqlexec.RecordSet, inMulitQuery bool) ([]sqlexec.RecordSet, error) {
s.SetValue(sessionctx.QueryString, stmt.OriginText())
if _, ok := stmtNode.(ast.DDLNode); ok {
s.SetValue(sessionctx.LastExecuteDDL, true)
} else {
s.ClearValue(sessionctx.LastExecuteDDL)
}
logStmt(stmtNode, s.sessionVars)
startTime := time.Now()
recordSet, err := runStmt(ctx, s, stmt)
Expand Down
15 changes: 15 additions & 0 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,21 @@ func (s *testSessionSuite) TestQueryString(c *C) {
tk.MustExec("create table mutil1 (a int);create table multi2 (a int)")
queryStr := tk.Se.Value(sessionctx.QueryString)
c.Assert(queryStr, Equals, "create table multi2 (a int)")

// Test execution of DDL through the "ExecutePreparedStmt" interface.
_, err := tk.Se.Execute(context.Background(), "use test;")
c.Assert(err, IsNil)
_, err = tk.Se.Execute(context.Background(), "CREATE TABLE t (id bigint PRIMARY KEY, age int)")
c.Assert(err, IsNil)
_, err = tk.Se.Execute(context.Background(), "show create table t")
c.Assert(err, IsNil)
id, _, _, err := tk.Se.PrepareStmt("CREATE TABLE t2(id bigint PRIMARY KEY, age int)")
c.Assert(err, IsNil)
params := []types.Datum{}
_, err = tk.Se.ExecutePreparedStmt(context.Background(), id, params)
c.Assert(err, IsNil)
qs := tk.Se.Value(sessionctx.QueryString)
c.Assert(qs.(string), Equals, "CREATE TABLE t2(id bigint PRIMARY KEY, age int)")
}

func (s *testSessionSuite) TestAffectedRows(c *C) {
Expand Down
7 changes: 7 additions & 0 deletions session/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ func runStmt(ctx context.Context, sctx sessionctx.Context, s sqlexec.Statement)
span1.LogKV("sql", s.OriginText())
defer span1.Finish()
}
sctx.SetValue(sessionctx.QueryString, s.OriginText())
if _, ok := s.(*executor.ExecStmt).StmtNode.(ast.DDLNode); ok {
sctx.SetValue(sessionctx.LastExecuteDDL, true)
} else {
sctx.ClearValue(sessionctx.LastExecuteDDL)
}

se := sctx.(*session)
sessVars := se.GetSessionVars()
// Save origTxnCtx here to avoid it reset in the transaction retry.
Expand Down

0 comments on commit 4188903

Please sign in to comment.