123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560 |
- // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
- //
- // Copyright 2017 The Go-MySQL-Driver Authors. All rights reserved.
- //
- // This Source Code Form is subject to the terms of the Mozilla Public
- // License, v. 2.0. If a copy of the MPL was not distributed with this file,
- // You can obtain one at http://mozilla.org/MPL/2.0/.
- // +build go1.8
- package mysql
- import (
- "context"
- "database/sql"
- "database/sql/driver"
- "fmt"
- "reflect"
- "testing"
- "time"
- )
- // static interface implementation checks of mysqlConn
- var (
- _ driver.ConnBeginTx = &mysqlConn{}
- _ driver.ConnPrepareContext = &mysqlConn{}
- _ driver.ExecerContext = &mysqlConn{}
- _ driver.Pinger = &mysqlConn{}
- _ driver.QueryerContext = &mysqlConn{}
- )
- // static interface implementation checks of mysqlStmt
- var (
- _ driver.StmtExecContext = &mysqlStmt{}
- _ driver.StmtQueryContext = &mysqlStmt{}
- )
- func TestMultiResultSet(t *testing.T) {
- type result struct {
- values [][]int
- columns []string
- }
- // checkRows is a helper test function to validate rows containing 3 result
- // sets with specific values and columns. The basic query would look like this:
- //
- // SELECT 1 AS col1, 2 AS col2 UNION SELECT 3, 4;
- // SELECT 0 UNION SELECT 1;
- // SELECT 1 AS col1, 2 AS col2, 3 AS col3 UNION SELECT 4, 5, 6;
- //
- // to distinguish test cases the first string argument is put in front of
- // every error or fatal message.
- checkRows := func(desc string, rows *sql.Rows, dbt *DBTest) {
- expected := []result{
- {
- values: [][]int{{1, 2}, {3, 4}},
- columns: []string{"col1", "col2"},
- },
- {
- values: [][]int{{1, 2, 3}, {4, 5, 6}},
- columns: []string{"col1", "col2", "col3"},
- },
- }
- var res1 result
- for rows.Next() {
- var res [2]int
- if err := rows.Scan(&res[0], &res[1]); err != nil {
- dbt.Fatal(err)
- }
- res1.values = append(res1.values, res[:])
- }
- cols, err := rows.Columns()
- if err != nil {
- dbt.Fatal(desc, err)
- }
- res1.columns = cols
- if !reflect.DeepEqual(expected[0], res1) {
- dbt.Error(desc, "want =", expected[0], "got =", res1)
- }
- if !rows.NextResultSet() {
- dbt.Fatal(desc, "expected next result set")
- }
- // ignoring one result set
- if !rows.NextResultSet() {
- dbt.Fatal(desc, "expected next result set")
- }
- var res2 result
- cols, err = rows.Columns()
- if err != nil {
- dbt.Fatal(desc, err)
- }
- res2.columns = cols
- for rows.Next() {
- var res [3]int
- if err := rows.Scan(&res[0], &res[1], &res[2]); err != nil {
- dbt.Fatal(desc, err)
- }
- res2.values = append(res2.values, res[:])
- }
- if !reflect.DeepEqual(expected[1], res2) {
- dbt.Error(desc, "want =", expected[1], "got =", res2)
- }
- if rows.NextResultSet() {
- dbt.Error(desc, "unexpected next result set")
- }
- if err := rows.Err(); err != nil {
- dbt.Error(desc, err)
- }
- }
- runTestsWithMultiStatement(t, dsn, func(dbt *DBTest) {
- rows := dbt.mustQuery(`DO 1;
- SELECT 1 AS col1, 2 AS col2 UNION SELECT 3, 4;
- DO 1;
- SELECT 0 UNION SELECT 1;
- SELECT 1 AS col1, 2 AS col2, 3 AS col3 UNION SELECT 4, 5, 6;`)
- defer rows.Close()
- checkRows("query: ", rows, dbt)
- })
- runTestsWithMultiStatement(t, dsn, func(dbt *DBTest) {
- queries := []string{
- `
- DROP PROCEDURE IF EXISTS test_mrss;
- CREATE PROCEDURE test_mrss()
- BEGIN
- DO 1;
- SELECT 1 AS col1, 2 AS col2 UNION SELECT 3, 4;
- DO 1;
- SELECT 0 UNION SELECT 1;
- SELECT 1 AS col1, 2 AS col2, 3 AS col3 UNION SELECT 4, 5, 6;
- END
- `,
- `
- DROP PROCEDURE IF EXISTS test_mrss;
- CREATE PROCEDURE test_mrss()
- BEGIN
- SELECT 1 AS col1, 2 AS col2 UNION SELECT 3, 4;
- SELECT 0 UNION SELECT 1;
- SELECT 1 AS col1, 2 AS col2, 3 AS col3 UNION SELECT 4, 5, 6;
- END
- `,
- }
- defer dbt.mustExec("DROP PROCEDURE IF EXISTS test_mrss")
- for i, query := range queries {
- dbt.mustExec(query)
- stmt, err := dbt.db.Prepare("CALL test_mrss()")
- if err != nil {
- dbt.Fatalf("%v (i=%d)", err, i)
- }
- defer stmt.Close()
- for j := 0; j < 2; j++ {
- rows, err := stmt.Query()
- if err != nil {
- dbt.Fatalf("%v (i=%d) (j=%d)", err, i, j)
- }
- checkRows(fmt.Sprintf("prepared stmt query (i=%d) (j=%d): ", i, j), rows, dbt)
- }
- }
- })
- }
- func TestMultiResultSetNoSelect(t *testing.T) {
- runTestsWithMultiStatement(t, dsn, func(dbt *DBTest) {
- rows := dbt.mustQuery("DO 1; DO 2;")
- defer rows.Close()
- if rows.Next() {
- dbt.Error("unexpected row")
- }
- if rows.NextResultSet() {
- dbt.Error("unexpected next result set")
- }
- if err := rows.Err(); err != nil {
- dbt.Error("expected nil; got ", err)
- }
- })
- }
- // tests if rows are set in a proper state if some results were ignored before
- // calling rows.NextResultSet.
- func TestSkipResults(t *testing.T) {
- runTests(t, dsn, func(dbt *DBTest) {
- rows := dbt.mustQuery("SELECT 1, 2")
- defer rows.Close()
- if !rows.Next() {
- dbt.Error("expected row")
- }
- if rows.NextResultSet() {
- dbt.Error("unexpected next result set")
- }
- if err := rows.Err(); err != nil {
- dbt.Error("expected nil; got ", err)
- }
- })
- }
- func TestPingContext(t *testing.T) {
- runTests(t, dsn, func(dbt *DBTest) {
- ctx, cancel := context.WithCancel(context.Background())
- cancel()
- if err := dbt.db.PingContext(ctx); err != context.Canceled {
- dbt.Errorf("expected context.Canceled, got %v", err)
- }
- })
- }
- func TestContextCancelExec(t *testing.T) {
- runTests(t, dsn, func(dbt *DBTest) {
- dbt.mustExec("CREATE TABLE test (v INTEGER)")
- ctx, cancel := context.WithCancel(context.Background())
- // Delay execution for just a bit until db.ExecContext has begun.
- defer time.AfterFunc(100*time.Millisecond, cancel).Stop()
- // This query will be canceled.
- startTime := time.Now()
- if _, err := dbt.db.ExecContext(ctx, "INSERT INTO test VALUES (SLEEP(1))"); err != context.Canceled {
- dbt.Errorf("expected context.Canceled, got %v", err)
- }
- if d := time.Since(startTime); d > 500*time.Millisecond {
- dbt.Errorf("too long execution time: %s", d)
- }
- // Wait for the INSERT query has done.
- time.Sleep(time.Second)
- // Check how many times the query is executed.
- var v int
- if err := dbt.db.QueryRow("SELECT COUNT(*) FROM test").Scan(&v); err != nil {
- dbt.Fatalf("%s", err.Error())
- }
- if v != 1 { // TODO: need to kill the query, and v should be 0.
- dbt.Errorf("expected val to be 1, got %d", v)
- }
- // Context is already canceled, so error should come before execution.
- if _, err := dbt.db.ExecContext(ctx, "INSERT INTO test VALUES (1)"); err == nil {
- dbt.Error("expected error")
- } else if err.Error() != "context canceled" {
- dbt.Fatalf("unexpected error: %s", err)
- }
- // The second insert query will fail, so the table has no changes.
- if err := dbt.db.QueryRow("SELECT COUNT(*) FROM test").Scan(&v); err != nil {
- dbt.Fatalf("%s", err.Error())
- }
- if v != 1 {
- dbt.Errorf("expected val to be 1, got %d", v)
- }
- })
- }
- func TestContextCancelQuery(t *testing.T) {
- runTests(t, dsn, func(dbt *DBTest) {
- dbt.mustExec("CREATE TABLE test (v INTEGER)")
- ctx, cancel := context.WithCancel(context.Background())
- // Delay execution for just a bit until db.ExecContext has begun.
- defer time.AfterFunc(100*time.Millisecond, cancel).Stop()
- // This query will be canceled.
- startTime := time.Now()
- if _, err := dbt.db.QueryContext(ctx, "INSERT INTO test VALUES (SLEEP(1))"); err != context.Canceled {
- dbt.Errorf("expected context.Canceled, got %v", err)
- }
- if d := time.Since(startTime); d > 500*time.Millisecond {
- dbt.Errorf("too long execution time: %s", d)
- }
- // Wait for the INSERT query has done.
- time.Sleep(time.Second)
- // Check how many times the query is executed.
- var v int
- if err := dbt.db.QueryRow("SELECT COUNT(*) FROM test").Scan(&v); err != nil {
- dbt.Fatalf("%s", err.Error())
- }
- if v != 1 { // TODO: need to kill the query, and v should be 0.
- dbt.Errorf("expected val to be 1, got %d", v)
- }
- // Context is already canceled, so error should come before execution.
- if _, err := dbt.db.QueryContext(ctx, "INSERT INTO test VALUES (1)"); err != context.Canceled {
- dbt.Errorf("expected context.Canceled, got %v", err)
- }
- // The second insert query will fail, so the table has no changes.
- if err := dbt.db.QueryRow("SELECT COUNT(*) FROM test").Scan(&v); err != nil {
- dbt.Fatalf("%s", err.Error())
- }
- if v != 1 {
- dbt.Errorf("expected val to be 1, got %d", v)
- }
- })
- }
- func TestContextCancelQueryRow(t *testing.T) {
- runTests(t, dsn, func(dbt *DBTest) {
- dbt.mustExec("CREATE TABLE test (v INTEGER)")
- dbt.mustExec("INSERT INTO test VALUES (1), (2), (3)")
- ctx, cancel := context.WithCancel(context.Background())
- rows, err := dbt.db.QueryContext(ctx, "SELECT v FROM test")
- if err != nil {
- dbt.Fatalf("%s", err.Error())
- }
- // the first row will be succeed.
- var v int
- if !rows.Next() {
- dbt.Fatalf("unexpected end")
- }
- if err := rows.Scan(&v); err != nil {
- dbt.Fatalf("%s", err.Error())
- }
- cancel()
- // make sure the driver recieve cancel request.
- time.Sleep(100 * time.Millisecond)
- if rows.Next() {
- dbt.Errorf("expected end, but not")
- }
- if err := rows.Err(); err != context.Canceled {
- dbt.Errorf("expected context.Canceled, got %v", err)
- }
- })
- }
- func TestContextCancelPrepare(t *testing.T) {
- runTests(t, dsn, func(dbt *DBTest) {
- ctx, cancel := context.WithCancel(context.Background())
- cancel()
- if _, err := dbt.db.PrepareContext(ctx, "SELECT 1"); err != context.Canceled {
- dbt.Errorf("expected context.Canceled, got %v", err)
- }
- })
- }
- func TestContextCancelStmtExec(t *testing.T) {
- runTests(t, dsn, func(dbt *DBTest) {
- dbt.mustExec("CREATE TABLE test (v INTEGER)")
- ctx, cancel := context.WithCancel(context.Background())
- stmt, err := dbt.db.PrepareContext(ctx, "INSERT INTO test VALUES (SLEEP(1))")
- if err != nil {
- dbt.Fatalf("unexpected error: %v", err)
- }
- // Delay execution for just a bit until db.ExecContext has begun.
- defer time.AfterFunc(100*time.Millisecond, cancel).Stop()
- // This query will be canceled.
- startTime := time.Now()
- if _, err := stmt.ExecContext(ctx); err != context.Canceled {
- dbt.Errorf("expected context.Canceled, got %v", err)
- }
- if d := time.Since(startTime); d > 500*time.Millisecond {
- dbt.Errorf("too long execution time: %s", d)
- }
- // Wait for the INSERT query has done.
- time.Sleep(time.Second)
- // Check how many times the query is executed.
- var v int
- if err := dbt.db.QueryRow("SELECT COUNT(*) FROM test").Scan(&v); err != nil {
- dbt.Fatalf("%s", err.Error())
- }
- if v != 1 { // TODO: need to kill the query, and v should be 0.
- dbt.Errorf("expected val to be 1, got %d", v)
- }
- })
- }
- func TestContextCancelStmtQuery(t *testing.T) {
- runTests(t, dsn, func(dbt *DBTest) {
- dbt.mustExec("CREATE TABLE test (v INTEGER)")
- ctx, cancel := context.WithCancel(context.Background())
- stmt, err := dbt.db.PrepareContext(ctx, "INSERT INTO test VALUES (SLEEP(1))")
- if err != nil {
- dbt.Fatalf("unexpected error: %v", err)
- }
- // Delay execution for just a bit until db.ExecContext has begun.
- defer time.AfterFunc(100*time.Millisecond, cancel).Stop()
- // This query will be canceled.
- startTime := time.Now()
- if _, err := stmt.QueryContext(ctx); err != context.Canceled {
- dbt.Errorf("expected context.Canceled, got %v", err)
- }
- if d := time.Since(startTime); d > 500*time.Millisecond {
- dbt.Errorf("too long execution time: %s", d)
- }
- // Wait for the INSERT query has done.
- time.Sleep(time.Second)
- // Check how many times the query is executed.
- var v int
- if err := dbt.db.QueryRow("SELECT COUNT(*) FROM test").Scan(&v); err != nil {
- dbt.Fatalf("%s", err.Error())
- }
- if v != 1 { // TODO: need to kill the query, and v should be 0.
- dbt.Errorf("expected val to be 1, got %d", v)
- }
- })
- }
- func TestContextCancelBegin(t *testing.T) {
- runTests(t, dsn, func(dbt *DBTest) {
- dbt.mustExec("CREATE TABLE test (v INTEGER)")
- ctx, cancel := context.WithCancel(context.Background())
- tx, err := dbt.db.BeginTx(ctx, nil)
- if err != nil {
- dbt.Fatal(err)
- }
- // Delay execution for just a bit until db.ExecContext has begun.
- defer time.AfterFunc(100*time.Millisecond, cancel).Stop()
- // This query will be canceled.
- startTime := time.Now()
- if _, err := tx.ExecContext(ctx, "INSERT INTO test VALUES (SLEEP(1))"); err != context.Canceled {
- dbt.Errorf("expected context.Canceled, got %v", err)
- }
- if d := time.Since(startTime); d > 500*time.Millisecond {
- dbt.Errorf("too long execution time: %s", d)
- }
- // Transaction is canceled, so expect an error.
- switch err := tx.Commit(); err {
- case sql.ErrTxDone:
- // because the transaction has already been rollbacked.
- // the database/sql package watches ctx
- // and rollbacks when ctx is canceled.
- case context.Canceled:
- // the database/sql package rollbacks on another goroutine,
- // so the transaction may not be rollbacked depending on goroutine scheduling.
- default:
- dbt.Errorf("expected sql.ErrTxDone or context.Canceled, got %v", err)
- }
- // Context is canceled, so cannot begin a transaction.
- if _, err := dbt.db.BeginTx(ctx, nil); err != context.Canceled {
- dbt.Errorf("expected context.Canceled, got %v", err)
- }
- })
- }
- func TestContextBeginIsolationLevel(t *testing.T) {
- runTests(t, dsn, func(dbt *DBTest) {
- dbt.mustExec("CREATE TABLE test (v INTEGER)")
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- tx1, err := dbt.db.BeginTx(ctx, &sql.TxOptions{
- Isolation: sql.LevelRepeatableRead,
- })
- if err != nil {
- dbt.Fatal(err)
- }
- tx2, err := dbt.db.BeginTx(ctx, &sql.TxOptions{
- Isolation: sql.LevelReadCommitted,
- })
- if err != nil {
- dbt.Fatal(err)
- }
- _, err = tx1.ExecContext(ctx, "INSERT INTO test VALUES (1)")
- if err != nil {
- dbt.Fatal(err)
- }
- var v int
- row := tx2.QueryRowContext(ctx, "SELECT COUNT(*) FROM test")
- if err := row.Scan(&v); err != nil {
- dbt.Fatal(err)
- }
- // Because writer transaction wasn't commited yet, it should be available
- if v != 0 {
- dbt.Errorf("expected val to be 0, got %d", v)
- }
- err = tx1.Commit()
- if err != nil {
- dbt.Fatal(err)
- }
- row = tx2.QueryRowContext(ctx, "SELECT COUNT(*) FROM test")
- if err := row.Scan(&v); err != nil {
- dbt.Fatal(err)
- }
- // Data written by writer transaction is already commited, it should be selectable
- if v != 1 {
- dbt.Errorf("expected val to be 1, got %d", v)
- }
- tx2.Commit()
- })
- }
- func TestContextBeginReadOnly(t *testing.T) {
- runTests(t, dsn, func(dbt *DBTest) {
- dbt.mustExec("CREATE TABLE test (v INTEGER)")
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- tx, err := dbt.db.BeginTx(ctx, &sql.TxOptions{
- ReadOnly: true,
- })
- if _, ok := err.(*MySQLError); ok {
- dbt.Skip("It seems that your MySQL does not support READ ONLY transactions")
- return
- } else if err != nil {
- dbt.Fatal(err)
- }
- // INSERT queries fail in a READ ONLY transaction.
- _, err = tx.ExecContext(ctx, "INSERT INTO test VALUES (1)")
- if _, ok := err.(*MySQLError); !ok {
- dbt.Errorf("expected MySQLError, got %v", err)
- }
- // SELECT queries can be executed.
- var v int
- row := tx.QueryRowContext(ctx, "SELECT COUNT(*) FROM test")
- if err := row.Scan(&v); err != nil {
- dbt.Fatal(err)
- }
- if v != 0 {
- dbt.Errorf("expected val to be 0, got %d", v)
- }
- if err := tx.Commit(); err != nil {
- dbt.Fatal(err)
- }
- })
- }
|