Skip to content

Commit f04be98

Browse files
Greg RychlewskiGreg Rychlewski
authored andcommitted
review comments
1 parent ff861cb commit f04be98

File tree

3 files changed

+32
-33
lines changed

3 files changed

+32
-33
lines changed

lib/myxql/connection.ex

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ defmodule MyXQL.Connection do
225225
end
226226

227227
other ->
228-
result(other, query, state)
228+
stream_result(other, query, state)
229229
end
230230
end
231231

@@ -245,7 +245,7 @@ defmodule MyXQL.Connection do
245245
end
246246

247247
other ->
248-
result(other, query, state)
248+
stream_result(other, query, state)
249249
end
250250
end
251251

@@ -271,6 +271,14 @@ defmodule MyXQL.Connection do
271271
end
272272

273273
## Internals
274+
defp stream_result({:error, :multiple_results}, _query, _state) do
275+
raise RuntimeError,
276+
"streaming stored procedures is not supported. Use MyXQL.query_many/4 and similar functions."
277+
end
278+
279+
defp stream_result(result, query, state) do
280+
result(result, query, state)
281+
end
274282

275283
defp result({:ok, ok_packet(status_flags: status_flags) = result}, query, state) do
276284
{:ok, query, format_result(result, state), put_status(state, status_flags)}

test/myxql_test.exs

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -199,29 +199,6 @@ defmodule MyXQLTest do
199199
)
200200
end
201201

202-
test "query_many!/4 with text", c do
203-
assert [%MyXQL.Result{rows: [[1]]}, %MyXQL.Result{rows: [[2]]}] =
204-
MyXQL.query_many!(c.conn, "SELECT 1; SELECT 2", [], query_type: :text)
205-
206-
assert [%MyXQL.Result{rows: [[1]]}] =
207-
MyXQL.query_many!(c.conn, "SELECT 1;", [], query_type: :text)
208-
209-
assert [%MyXQL.Result{num_rows: 0, rows: nil}] =
210-
MyXQL.query_many!(c.conn, "DROP TABLE IF EXISTS not_a_table;", [],
211-
query_type: :text
212-
)
213-
214-
assert [%MyXQL.Result{num_rows: 0, rows: nil}, %MyXQL.Result{rows: [[1]]}] =
215-
MyXQL.query_many!(c.conn, "DROP TABLE IF EXISTS not_a_table; SELECT 1;", [],
216-
query_type: :text
217-
)
218-
219-
assert [%MyXQL.Result{rows: [[1]]}, %MyXQL.Result{num_rows: 0, rows: nil}] =
220-
MyXQL.query_many!(c.conn, "SELECT 1; DROP TABLE IF EXISTS not_a_table;", [],
221-
query_type: :text
222-
)
223-
end
224-
225202
test "query_many/4 with text returning error", c do
226203
assert {:error, %MyXQL.Error{mysql: %{code: 1064}}} =
227204
MyXQL.query_many(c.conn, "SELECT 1; BADCOMMAND;", [], query_type: :text)
@@ -666,9 +643,6 @@ defmodule MyXQLTest do
666643
assert %MyXQL.Result{rows: nil} =
667644
MyXQL.query!(c.conn, "CALL single_ok_procedure()", [], query_type: :text)
668645

669-
assert %MyXQL.Result{rows: nil} =
670-
MyXQL.query!(c.conn, "CALL single_ok_procedure()", [], query_type: :text)
671-
672646
assert [%MyXQL.Result{rows: [[1]]}, %MyXQL.Result{num_rows: 0, rows: nil}] =
673647
MyXQL.query_many!(c.conn, "CALL one_resultset_one_ok_procedure()")
674648

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

671+
assert %MyXQL.Query{} = query = MyXQL.prepare!(c.conn, "", "CALL single_ok_procedure()")
672+
673+
assert %MyXQL.Result{rows: nil} = MyXQL.execute!(c.conn, query)
674+
697675
assert %MyXQL.Queries{} =
698-
query = MyXQL.prepare_many!(c.conn, "", "CALL one_resultset_one_ok_procedure()")
676+
query = MyXQL.prepare_many!(c.conn, "", "CALL one_resultset_one_ok_procedure()")
699677

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

712-
test "stream procedure with multiple results", c do
690+
test "stream stored procedure", c do
713691
statement = "CALL one_resultset_one_ok_procedure()"
714692

715-
assert_raise RuntimeError, ~r"returning multiple results is not supported", fn ->
693+
assert_raise RuntimeError, ~r"streaming stored procedures is not supported", fn ->
716694
MyXQL.transaction(c.conn, fn conn ->
717695
stream = MyXQL.stream(conn, statement, [], max_rows: 2)
718696
Enum.to_list(stream)
@@ -793,6 +771,17 @@ defmodule MyXQLTest do
793771
assert %MyXQL.Queries{} = query = MyXQL.prepare_many!(c.conn, "", "CALL multi_procedure()")
794772
assert :ok == MyXQL.close(c.conn, query)
795773
end
774+
775+
test "using stream/4 with a multiple result query that is not a stored procedure", c do
776+
statement = "SELECT 1; SELECT 2;"
777+
778+
assert_raise MyXQL.Error, ~r"\(1064\)", fn ->
779+
MyXQL.transaction(c.conn, fn conn ->
780+
stream = MyXQL.stream(conn, statement, [], max_rows: 2)
781+
Enum.to_list(stream)
782+
end)
783+
end
784+
end
796785
end
797786

798787
@tag :skip

test/test_helper.exs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,11 @@ defmodule TestHelper do
140140
my_char CHAR
141141
);
142142
143-
# This will only return the trailing ok_packet
143+
# This will only return the trailing ok packet
144144
# because the commands inside the stored procedure
145-
# do not return result sets.
145+
# do not return result sets. Commands inside of stored
146+
# procedures that return ok packets do not have their
147+
# results sent through the wire.
146148
DROP PROCEDURE IF EXISTS single_ok_procedure;
147149
DELIMITER $$
148150
CREATE PROCEDURE single_ok_procedure()

0 commit comments

Comments
 (0)