Skip to content

support prepared statements with more than 32 parameters #210

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

Merged
merged 5 commits into from
Jan 31, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ New Features:

- Logging of critical errors is configurable with `SetLogger`

Bugfixes:

- Allow more than 32 parameters in prepared statements


## Version 1.1 (2013-11-02)

Changes:
Expand Down
24 changes: 24 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,30 @@ func TestStmtMultiRows(t *testing.T) {
})
}

// Regression test for
// * more than 32 NULL parameters (issue 209)
// * more parameters than fit into the buffer (issue 201)
func TestPreparedManyCols(t *testing.T) {
const numParams = defaultBufSize
runTests(t, dsn, func(dbt *DBTest) {
query := "SELECT ?" + strings.Repeat(",?", numParams-1)
stmt, err := dbt.db.Prepare(query)
if err != nil {
dbt.Fatal(err)
}
defer stmt.Close()
// create more parameters than fit into the buffer
// which will take nil-values
params := make([]interface{}, numParams)
rows, err := stmt.Query(params...)
if err != nil {
stmt.Close()
dbt.Fatal(err)
}
defer rows.Close()
})
}

func TestConcurrent(t *testing.T) {
if enabled, _ := readBool(os.Getenv("MYSQL_TEST_CONCURRENT")); !enabled {
t.Skip("MYSQL_TEST_CONCURRENT env var not set")
Expand Down
46 changes: 29 additions & 17 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
)
}

const minPktLen = 4 + 1 + 4 + 1 + 4
mc := stmt.mc

// Reset packet-sequence
Expand All @@ -758,7 +759,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
var data []byte

if len(args) == 0 {
data = mc.buf.takeBuffer(4 + 1 + 4 + 1 + 4)
data = mc.buf.takeBuffer(minPktLen)
} else {
data = mc.buf.takeCompleteBuffer()
}
Expand Down Expand Up @@ -787,34 +788,50 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
data[13] = 0x00

if len(args) > 0 {
// NULL-bitmap [(len(args)+7)/8 bytes]
nullMask := uint64(0)

pos := 4 + 1 + 4 + 1 + 4 + ((len(args) + 7) >> 3)
pos := minPktLen

var nullMask []byte
if maskLen, typesLen := (len(args)+7)/8, 1+2*len(args); pos+maskLen+typesLen >= len(data) {
// buffer has to be extended but we don't know by how much so
// we depend on append after all data with known sizes fit.
// We stop at that because we deal with a lot of columns here
// which makes the required allocation size hard to guess.
tmp := make([]byte, pos+maskLen+typesLen)
copy(tmp[:pos], data[:pos])
data = tmp
nullMask = data[pos : pos+maskLen]
pos += maskLen
} else {
nullMask = data[pos : pos+maskLen]
for i := 0; i < maskLen; i++ {
nullMask[i] = 0
}
pos += maskLen
}

// newParameterBoundFlag 1 [1 byte]
data[pos] = 0x01
pos++

// type of each parameter [len(args)*2 bytes]
paramTypes := data[pos:]
pos += (len(args) << 1)
pos += len(args) * 2

// value of each parameter [n bytes]
paramValues := data[pos:pos]
valuesCap := cap(paramValues)

for i := range args {
for i, arg := range args {
// build NULL-bitmap
if args[i] == nil {
nullMask |= 1 << uint(i)
if arg == nil {
nullMask[i/8] |= 1 << (uint(i) & 7)
paramTypes[i+i] = fieldTypeNULL
paramTypes[i+i+1] = 0x00
continue
}

// cache types and values
switch v := args[i].(type) {
switch v := arg.(type) {
case int64:
paramTypes[i+i] = fieldTypeLongLong
paramTypes[i+i+1] = 0x00
Expand Down Expand Up @@ -877,7 +894,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
}

// Handle []byte(nil) as a NULL value
nullMask |= 1 << uint(i)
nullMask[i/8] |= 1 << (uint(i) & 7)
paramTypes[i+i] = fieldTypeNULL
paramTypes[i+i+1] = 0x00

Expand Down Expand Up @@ -913,7 +930,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
paramValues = append(paramValues, val...)

default:
return fmt.Errorf("Can't convert type: %T", args[i])
return fmt.Errorf("Can't convert type: %T", arg)
}
}

Expand All @@ -926,11 +943,6 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {

pos += len(paramValues)
data = data[:pos]

// Convert nullMask to bytes
for i, max := 0, (stmt.paramCount+7)>>3; i < max; i++ {
data[i+14] = byte(nullMask >> uint(i<<3))
}
}

return mc.writePacket(data)
Expand Down