Skip to content

Commit

Permalink
Merge pull request #43 from pingcap/shenli/check-prepare-bool
Browse files Browse the repository at this point in the history
tidb: Convert bool type prepared statement args to int8
  • Loading branch information
ngaut committed Sep 7, 2015
2 parents 222ea14 + cd01fa1 commit 82ee778
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
10 changes: 9 additions & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/db"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/types"
)

// Session context
Expand Down Expand Up @@ -150,7 +151,14 @@ func (s *session) PrepareStmt(sql string) (stmtID uint32, paramCount int, fields
func checkArgs(args ...interface{}) error {
for i, v := range args {
switch v.(type) {
case nil, bool, float32, float64, string,
case bool:
// We do not handle bool as int8 in tidb.
vv, err := types.ToBool(v)
if err != nil {
return errors.Trace(err)
}
args[i] = vv
case nil, float32, float64, string,
int8, int16, int32, int64, int,
uint8, uint16, uint32, uint64, uint,
[]byte, time.Duration, time.Time:
Expand Down
10 changes: 10 additions & 0 deletions tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,16 @@ func (s *testSessionSuite) TestPrimaryKeyAutoincrement(c *C) {
row, err = rs.FirstRow()
c.Assert(err, IsNil)
match(c, row, id, "abc", 1)
// Check for pass bool param to tidb prepared statement
mustExecSQL(c, se, "drop table if exists t")
mustExecSQL(c, se, "create table t (id tiny)")
mustExecSQL(c, se, "insert t values (?)", true)
rs = mustExecSQL(c, se, "select * from t")
c.Assert(rs, NotNil)
row, err = rs.FirstRow()
c.Assert(err, IsNil)
match(c, row, int8(1))

mustExecSQL(c, se, s.dropDBSQL)
}

Expand Down

0 comments on commit 82ee778

Please sign in to comment.