Skip to content

bug when interpolating params for multiStatements #426

Closed
@0xor1

Description

@0xor1

Issue description

When executing multiple statements with param interpolation mysql server complains about syntax of ? characters, it appears that when a query contains multiple statements that parameters are not injected beyond the first statement. This issue originates from this SO question

Example code

package main

import(
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
    "fmt"
)

const(
    sqlDriver = "mysql"
    sqlConnectionString = "usr:pwd@tcp(localhost:3306)/my_test_db?parseTime=true&loc=UTC&multiStatements=true"
)

func main(){
    db, _ := sql.Open(sqlDriver, sqlConnectionString)
    rows, err := db.Query(`SELECT "hello";`)
    if err != nil {
        fmt.Printf("single statements don't work with hard coded params, error: %v", err)
    }
    if rows != nil {
        rows.Close()
    }
    rows, err = db.Query(`SELECT "hello"; SELECT "world";`);
    if err != nil {
        fmt.Printf("multiple statements don't work with hard coded params, error: %v", err)
    }
    if rows != nil {
        rows.Close()
    }
    rows, err = db.Query(`SELECT ?;`, "hello")
    if err != nil {
        fmt.Printf("single statements don't work with interpolated params, error: %v", err)
    }
    if rows != nil {
        rows.Close()
    }
    rows, err = db.Query(`SELECT ?; SELECT ?;`, "hello", "world")
    if err != nil {
        fmt.Printf("multiple statements don't work with interpolated params, error: %v", err)
    }
    if rows != nil {
        rows.Close()
    }
}

produces output:

multiple statements don't work with interpolated params, error: Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT ?' at line 1

Configuration

Driver version (or git SHA): 267b128
Go version: 1.5.3
Server version: MySQL 5.7.10
Server OS: Windows 8.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions