Skip to content
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

fixes for query_many and its variants #152

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/myxql/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ defmodule MyXQL.Connection do
end

other ->
result(other, query, state)
stream_result(other, query, state)
end
end

Expand All @@ -245,7 +245,7 @@ defmodule MyXQL.Connection do
end

other ->
result(other, query, state)
stream_result(other, query, state)
end
end

Expand All @@ -271,6 +271,14 @@ defmodule MyXQL.Connection do
end

## Internals
defp stream_result({:error, :multiple_results}, _query, _state) do
raise RuntimeError,
"streaming stored procedures is not supported. Use MyXQL.query_many/4 and similar functions."
end
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wojtekmach The idea here is that when streaming you can only get this type of error when using a stored procedure. The text query separated by semi-colons returns a syntax error, which I put a test for here: f04be98#diff-25c00486ed7c3364911a13625fb201869b4221fc96834062c8abe5f099d334daR775


defp stream_result(result, query, state) do
result(result, query, state)
end

defp result({:ok, ok_packet(status_flags: status_flags) = result}, query, state) do
{:ok, query, format_result(result, state), put_status(state, status_flags)}
Expand Down
47 changes: 18 additions & 29 deletions test/myxql_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -199,29 +199,6 @@ defmodule MyXQLTest do
)
end

test "query_many!/4 with text", c do
assert [%MyXQL.Result{rows: [[1]]}, %MyXQL.Result{rows: [[2]]}] =
MyXQL.query_many!(c.conn, "SELECT 1; SELECT 2", [], query_type: :text)

assert [%MyXQL.Result{rows: [[1]]}] =
MyXQL.query_many!(c.conn, "SELECT 1;", [], query_type: :text)

assert [%MyXQL.Result{num_rows: 0, rows: nil}] =
MyXQL.query_many!(c.conn, "DROP TABLE IF EXISTS not_a_table;", [],
query_type: :text
)

assert [%MyXQL.Result{num_rows: 0, rows: nil}, %MyXQL.Result{rows: [[1]]}] =
MyXQL.query_many!(c.conn, "DROP TABLE IF EXISTS not_a_table; SELECT 1;", [],
query_type: :text
)

assert [%MyXQL.Result{rows: [[1]]}, %MyXQL.Result{num_rows: 0, rows: nil}] =
MyXQL.query_many!(c.conn, "SELECT 1; DROP TABLE IF EXISTS not_a_table;", [],
query_type: :text
)
end

test "query_many/4 with text returning error", c do
assert {:error, %MyXQL.Error{mysql: %{code: 1064}}} =
MyXQL.query_many(c.conn, "SELECT 1; BADCOMMAND;", [], query_type: :text)
Expand Down Expand Up @@ -666,9 +643,6 @@ defmodule MyXQLTest do
assert %MyXQL.Result{rows: nil} =
MyXQL.query!(c.conn, "CALL single_ok_procedure()", [], query_type: :text)

assert %MyXQL.Result{rows: nil} =
MyXQL.query!(c.conn, "CALL single_ok_procedure()", [], query_type: :text)

assert [%MyXQL.Result{rows: [[1]]}, %MyXQL.Result{num_rows: 0, rows: nil}] =
MyXQL.query_many!(c.conn, "CALL one_resultset_one_ok_procedure()")

Expand All @@ -694,8 +668,12 @@ defmodule MyXQLTest do
%MyXQL.Result{num_rows: 0, rows: nil}
]} = MyXQL.prepare_execute_many!(c.conn, "", "CALL multi_procedure()")

assert %MyXQL.Query{} = query = MyXQL.prepare!(c.conn, "", "CALL single_ok_procedure()")

assert %MyXQL.Result{rows: nil} = MyXQL.execute!(c.conn, query)

assert %MyXQL.Queries{} =
query = MyXQL.prepare_many!(c.conn, "", "CALL one_resultset_one_ok_procedure()")
query = MyXQL.prepare_many!(c.conn, "", "CALL one_resultset_one_ok_procedure()")

assert [%MyXQL.Result{rows: [[1]]}, %MyXQL.Result{num_rows: 0, rows: nil}] =
MyXQL.execute_many!(c.conn, query)
Expand All @@ -709,10 +687,10 @@ defmodule MyXQLTest do
] = MyXQL.execute_many!(c.conn, query)
end

test "stream procedure with multiple results", c do
test "stream stored procedure", c do
statement = "CALL one_resultset_one_ok_procedure()"

assert_raise RuntimeError, ~r"returning multiple results is not supported", fn ->
assert_raise RuntimeError, ~r"streaming stored procedures is not supported", fn ->
MyXQL.transaction(c.conn, fn conn ->
stream = MyXQL.stream(conn, statement, [], max_rows: 2)
Enum.to_list(stream)
Expand Down Expand Up @@ -793,6 +771,17 @@ defmodule MyXQLTest do
assert %MyXQL.Queries{} = query = MyXQL.prepare_many!(c.conn, "", "CALL multi_procedure()")
assert :ok == MyXQL.close(c.conn, query)
end

test "using stream/4 with a multiple result query that is not a stored procedure", c do
statement = "SELECT 1; SELECT 2;"

assert_raise MyXQL.Error, ~r"\(1064\)", fn ->
MyXQL.transaction(c.conn, fn conn ->
stream = MyXQL.stream(conn, statement, [], max_rows: 2)
Enum.to_list(stream)
end)
end
end
end

@tag :skip
Expand Down
6 changes: 4 additions & 2 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,11 @@ defmodule TestHelper do
my_char CHAR
);

# This will only return the trailing ok_packet
# This will only return the trailing ok packet
# because the commands inside the stored procedure
# do not return result sets.
# do not return result sets. Commands inside of stored
# procedures that return ok packets do not have their
# results sent through the wire.
DROP PROCEDURE IF EXISTS single_ok_procedure;
DELIMITER $$
CREATE PROCEDURE single_ok_procedure()
Expand Down