Skip to content

Commit

Permalink
server: fix insert zero timestamp bug with prepared statement (#7506)
Browse files Browse the repository at this point in the history
  • Loading branch information
lysu authored Aug 28, 2018
1 parent bc97e2d commit 5b689e8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion server/conn_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (

"github.com/juju/errors"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/hack"
"golang.org/x/net/context"
)
Expand Down Expand Up @@ -354,7 +355,7 @@ func parseStmtArgs(args []interface{}, boundParams [][]byte, nullBitmap, paramTy
pos++
switch length {
case 0:
args[i] = "0"
args[i] = types.ZeroDatetimeStr
case 4:
pos, args[i] = parseBinaryDate(pos, paramValues)
case 7:
Expand Down
3 changes: 2 additions & 1 deletion server/conn_stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/terror"
"github.com/pingcap/tidb/types"
)

func (ts ConnTestSuite) TestParseStmtArgs(c *C) {
Expand Down Expand Up @@ -120,7 +121,7 @@ func (ts ConnTestSuite) TestParseStmtArgs(c *C) {
[]byte{0x00},
},
nil,
"0",
types.ZeroDatetimeStr,
},
// Tests for time
{
Expand Down
13 changes: 13 additions & 0 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,19 @@ func (s *testSessionSuite) TestLastInsertID(c *C) {
c.Assert(lastInsertID+2, Equals, currLastInsertID)
}

func (s *testSessionSuite) TestPrepareZero(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(v timestamp)")
tk.MustExec("prepare s1 from 'insert into t (v) values (?)'")
tk.MustExec("set @v1='0'")
_, rs := tk.Exec("execute s1 using @v1")
c.Assert(rs, NotNil)
tk.MustExec("set @v2='" + types.ZeroDatetimeStr + "'")
tk.MustExec("execute s1 using @v2")
tk.MustQuery("select v from t").Check(testkit.Rows("0000-00-00 00:00:00"))
}

func (s *testSessionSuite) TestPrimaryKeyAutoIncrement(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists t")
Expand Down

0 comments on commit 5b689e8

Please sign in to comment.