Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a Go Mysql driver exist that supports multiple statements within a single string?

Tags:

mysql

go

driver

I'm trying to find a MySql driver that i can use with Go which supports issuing multiple SQL statements in one call. For example i might wish to create a database using the following SQL:

DROP SCHEMA IF EXISTS foo;
CREATE SCHEMA IF NOT EXISTS foo;

In languages such as PHP you can just place both SQL statements in one string and execute it in one go, like this:

$db = new PDO(...);
$db->query("DROP SCHEMA IF EXISTS foo; CREATE SCHEMA IF NOT EXISTS foo;");

The reason i need this is because i have SQL dumps (from mysqldump) i'd like to apply programmatically to various databases.

I'm looking for the same functionality in Go but it seems all the different drivers don't support it, which, frankly, is shocking to me.

Go-MySQL-Driver
https://github.com/go-sql-driver/mysql
This seems to be the most used driver for Go.

package main

import "database/sql"
import "log"
import _ "github.com/go-sql-driver/mysql"

func main() {

    db, err := sql.Open("mysql", "user:password@(127.0.0.1:3306)/")
    if err != nil {
        log.Println(err)
    }

    sql := "DROP SCHEMA IF EXISTS foo; CREATE SCHEMA IF NOT EXISTS foo;"
    _, err = db.Exec(sql)
    if err != nil {
        log.Println(err)
    }

    db.Close()
}

output:

2015/02/16 18:58:08 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 'CREATE SCHEMA IF NOT EXISTS foo' at line 1

MyMySQL
https://github.com/ziutek/mymysql
This is another popular driver.

package main

import "database/sql"
import "log"
import _ "github.com/ziutek/mymysql/godrv"

func main() {

    db, err := sql.Open("mymysql", "database/user/password")
    if err != nil {
        log.Println(err)
    }

    sql := "DROP SCHEMA IF EXISTS foo; CREATE SCHEMA IF NOT EXISTS foo;"
    _, err = db.Exec(sql)
    if err != nil {
        log.Println(err)
    }

    sql = "USE DATABASE foo;"
    _, err = db.Exec(sql) // <-- error
    if err != nil {
        log.Println(err)
    }

    db.Close()
}

output:

2015/02/16 18:58:08 packet sequence error

Does anyone know of any MySql driver compatible with Go that can handle multiple statements in one string like these?

like image 843
Gary Willoughby Avatar asked Feb 16 '15 19:02

Gary Willoughby


People also ask

How do I run multiple SQL statements in MySQL?

MySQL also supports the execution of a string containing multiple statements separated by semicolon ( ; ) characters. This capability is enabled by special options that are specified either when you connect to the server with mysql_real_connect() or after connecting by calling mysql_set_server_option() .

How do you run multiple queries in a single prepared statement?

Demonstrating execution of multiple SQL commands on a database simultaneously using the addBatch() and executeBatch() commands of JDBC. The addBatch() command is used to queue the SQL statements and executeBatch() command is used to execute the queued SQL statements all at once.

What is go in MySQL?

The "go" keyword in Sybase ASE and SQL Server is a batch delimiter. It signals the end of a batch. The semicolon implemented by both SQL Server and MySQL are statement delimiters (terminating an individual statement).

How run multiple MySQL queries in PHP?

Multiple statements or multi queries must be executed with mysqli::multi_query(). The individual statements of the statement string are separated by semicolon. Then, all result sets returned by the executed statements must be fetched.


2 Answers

the github.com/go-sql-driver/mysql can be configured to accept multiple statements with the multiStatements=true connection parameter.

The documentation clearly states why you should be careful doing it. See https://github.com/go-sql-driver/mysql

like image 197
mkm Avatar answered Sep 20 '22 03:09

mkm


Adding an example for the answer from @ithkuil regarding multiStatements for the go-sql-driver package for reference. (I didn't have enough rep to add as a comment).

The parameter for multiStatements is added onto the dataSourceName string for your sql.Open call. e.g.

db, err := sql.Open("mysql", "user:pass@tcp(localhost:3306)/dbname?multiStatements=true")

It's recommended that you not use such a db handler for processing user input, but it works great for processing known sql files.

like image 37
Slotheroo Avatar answered Sep 19 '22 03:09

Slotheroo