-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add Multi-Results support #537
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
// +build go1.8 | ||
|
||
package mysql | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
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) | ||
} | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why next result set is unexpected?
What happens for
DO 1; DO 2; DO 3; SELECT 42
?I expect
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I followed the example from the std:
https://tip.golang.org/pkg/database/sql/#example_DB_Query_multipleResultSets
which seems to ignore the empty results sets.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I got it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So what would happen if I have
SELECT 1 FROM DUAL WHERE 1 = 0; SELECT 1,2 FROM DUAL WHERE 1 = 1;
? That is the first query returns no rows, but the second query does.I would expect
That is, I might not know which query has no resultset. So, I have to go through them all to map processing them correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joegrasse, sorry - "empty results sets" was inaccurate. Result set is present if column count > 0.
So, in the example that you provided it would work as you expected - it would return two result sets, first would be empty, the other one would have one row.