|
| 1 | +// +build go1.8 |
| 2 | + |
| 3 | +package mysql |
| 4 | + |
| 5 | +import ( |
| 6 | + "database/sql" |
| 7 | + "fmt" |
| 8 | + "reflect" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestMultiResultSet(t *testing.T) { |
| 13 | + type result struct { |
| 14 | + values [][]int |
| 15 | + columns []string |
| 16 | + } |
| 17 | + |
| 18 | + // checkRows is a helper test function to validate rows containing 3 result |
| 19 | + // sets with specific values and columns. The basic query would look like this: |
| 20 | + // |
| 21 | + // SELECT 1 AS col1, 2 AS col2 UNION SELECT 3, 4; |
| 22 | + // SELECT 0 UNION SELECT 1; |
| 23 | + // SELECT 1 AS col1, 2 AS col2, 3 AS col3 UNION SELECT 4, 5, 6; |
| 24 | + // |
| 25 | + // to distinguish test cases the first string argument is put in front of |
| 26 | + // every error or fatal message. |
| 27 | + checkRows := func(desc string, rows *sql.Rows, dbt *DBTest) { |
| 28 | + expected := []result{ |
| 29 | + { |
| 30 | + values: [][]int{{1, 2}, {3, 4}}, |
| 31 | + columns: []string{"col1", "col2"}, |
| 32 | + }, |
| 33 | + { |
| 34 | + values: [][]int{{1, 2, 3}, {4, 5, 6}}, |
| 35 | + columns: []string{"col1", "col2", "col3"}, |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + var res1 result |
| 40 | + for rows.Next() { |
| 41 | + var res [2]int |
| 42 | + if err := rows.Scan(&res[0], &res[1]); err != nil { |
| 43 | + dbt.Fatal(err) |
| 44 | + } |
| 45 | + res1.values = append(res1.values, res[:]) |
| 46 | + } |
| 47 | + |
| 48 | + cols, err := rows.Columns() |
| 49 | + if err != nil { |
| 50 | + dbt.Fatal(desc, err) |
| 51 | + } |
| 52 | + res1.columns = cols |
| 53 | + |
| 54 | + if !reflect.DeepEqual(expected[0], res1) { |
| 55 | + dbt.Error(desc, "want =", expected[0], "got =", res1) |
| 56 | + } |
| 57 | + |
| 58 | + if !rows.NextResultSet() { |
| 59 | + dbt.Fatal(desc, "expected next result set") |
| 60 | + } |
| 61 | + |
| 62 | + // ignoring one result set |
| 63 | + |
| 64 | + if !rows.NextResultSet() { |
| 65 | + dbt.Fatal(desc, "expected next result set") |
| 66 | + } |
| 67 | + |
| 68 | + var res2 result |
| 69 | + cols, err = rows.Columns() |
| 70 | + if err != nil { |
| 71 | + dbt.Fatal(desc, err) |
| 72 | + } |
| 73 | + res2.columns = cols |
| 74 | + |
| 75 | + for rows.Next() { |
| 76 | + var res [3]int |
| 77 | + if err := rows.Scan(&res[0], &res[1], &res[2]); err != nil { |
| 78 | + dbt.Fatal(desc, err) |
| 79 | + } |
| 80 | + res2.values = append(res2.values, res[:]) |
| 81 | + } |
| 82 | + |
| 83 | + if !reflect.DeepEqual(expected[1], res2) { |
| 84 | + dbt.Error(desc, "want =", expected[1], "got =", res2) |
| 85 | + } |
| 86 | + |
| 87 | + if rows.NextResultSet() { |
| 88 | + dbt.Error(desc, "unexpected next result set") |
| 89 | + } |
| 90 | + |
| 91 | + if err := rows.Err(); err != nil { |
| 92 | + dbt.Error(desc, err) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + runTestsWithMultiStatement(t, dsn, func(dbt *DBTest) { |
| 97 | + rows := dbt.mustQuery(`DO 1; |
| 98 | + SELECT 1 AS col1, 2 AS col2 UNION SELECT 3, 4; |
| 99 | + DO 1; |
| 100 | + SELECT 0 UNION SELECT 1; |
| 101 | + SELECT 1 AS col1, 2 AS col2, 3 AS col3 UNION SELECT 4, 5, 6;`) |
| 102 | + defer rows.Close() |
| 103 | + checkRows("query: ", rows, dbt) |
| 104 | + }) |
| 105 | + |
| 106 | + runTestsWithMultiStatement(t, dsn, func(dbt *DBTest) { |
| 107 | + queries := []string{ |
| 108 | + ` |
| 109 | + DROP PROCEDURE IF EXISTS test_mrss; |
| 110 | + CREATE PROCEDURE test_mrss() |
| 111 | + BEGIN |
| 112 | + DO 1; |
| 113 | + SELECT 1 AS col1, 2 AS col2 UNION SELECT 3, 4; |
| 114 | + DO 1; |
| 115 | + SELECT 0 UNION SELECT 1; |
| 116 | + SELECT 1 AS col1, 2 AS col2, 3 AS col3 UNION SELECT 4, 5, 6; |
| 117 | + END |
| 118 | + `, |
| 119 | + ` |
| 120 | + DROP PROCEDURE IF EXISTS test_mrss; |
| 121 | + CREATE PROCEDURE test_mrss() |
| 122 | + BEGIN |
| 123 | + SELECT 1 AS col1, 2 AS col2 UNION SELECT 3, 4; |
| 124 | + SELECT 0 UNION SELECT 1; |
| 125 | + SELECT 1 AS col1, 2 AS col2, 3 AS col3 UNION SELECT 4, 5, 6; |
| 126 | + END |
| 127 | + `, |
| 128 | + } |
| 129 | + |
| 130 | + defer dbt.mustExec("DROP PROCEDURE IF EXISTS test_mrss") |
| 131 | + |
| 132 | + for i, query := range queries { |
| 133 | + dbt.mustExec(query) |
| 134 | + |
| 135 | + stmt, err := dbt.db.Prepare("CALL test_mrss()") |
| 136 | + if err != nil { |
| 137 | + dbt.Fatalf("%v (i=%d)", err, i) |
| 138 | + } |
| 139 | + defer stmt.Close() |
| 140 | + |
| 141 | + for j := 0; j < 2; j++ { |
| 142 | + rows, err := stmt.Query() |
| 143 | + if err != nil { |
| 144 | + dbt.Fatalf("%v (i=%d) (j=%d)", err, i, j) |
| 145 | + } |
| 146 | + checkRows(fmt.Sprintf("prepared stmt query (i=%d) (j=%d): ", i, j), rows, dbt) |
| 147 | + } |
| 148 | + } |
| 149 | + }) |
| 150 | +} |
| 151 | + |
| 152 | +func TestMultiResultSetNoSelect(t *testing.T) { |
| 153 | + runTestsWithMultiStatement(t, dsn, func(dbt *DBTest) { |
| 154 | + rows := dbt.mustQuery("DO 1; DO 2;") |
| 155 | + defer rows.Close() |
| 156 | + |
| 157 | + if rows.Next() { |
| 158 | + dbt.Error("unexpected row") |
| 159 | + } |
| 160 | + |
| 161 | + if rows.NextResultSet() { |
| 162 | + dbt.Error("unexpected next result set") |
| 163 | + } |
| 164 | + |
| 165 | + if err := rows.Err(); err != nil { |
| 166 | + dbt.Error("expected nil; got ", err) |
| 167 | + } |
| 168 | + }) |
| 169 | +} |
| 170 | + |
| 171 | +// tests if rows are set in a proper state if some results were ignored before |
| 172 | +// calling rows.NextResultSet. |
| 173 | +func TestSkipResults(t *testing.T) { |
| 174 | + runTests(t, dsn, func(dbt *DBTest) { |
| 175 | + rows := dbt.mustQuery("SELECT 1, 2") |
| 176 | + defer rows.Close() |
| 177 | + |
| 178 | + if !rows.Next() { |
| 179 | + dbt.Error("expected row") |
| 180 | + } |
| 181 | + |
| 182 | + if rows.NextResultSet() { |
| 183 | + dbt.Error("unexpected next result set") |
| 184 | + } |
| 185 | + |
| 186 | + if err := rows.Err(); err != nil { |
| 187 | + dbt.Error("expected nil; got ", err) |
| 188 | + } |
| 189 | + }) |
| 190 | +} |
0 commit comments