Skip to content

Commit 2c2365f

Browse files
Greg RychlewskiGreg Rychlewski
authored andcommitted
handle insert/update/delete responses in query_many
1 parent 1f370f7 commit 2c2365f

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

lib/myxql/connection.ex

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -324,23 +324,36 @@ defmodule MyXQL.Connection do
324324
defp result({:ok, resultsets}, query, state) when is_list(resultsets) do
325325
{results, status_flags} =
326326
Enum.reduce(resultsets, {[], nil}, fn resultset, {results, newest_status_flags} ->
327-
resultset(
328-
column_defs: column_defs,
329-
num_rows: num_rows,
330-
rows: rows,
331-
status_flags: status_flags,
332-
num_warnings: num_warnings
333-
) = resultset
334-
335-
columns = Enum.map(column_defs, &elem(&1, 1))
336-
337-
result = %Result{
338-
connection_id: state.client.connection_id,
339-
columns: columns,
340-
num_rows: num_rows,
341-
rows: rows,
342-
num_warnings: num_warnings
343-
}
327+
{result, status_flags} =
328+
case resultset do
329+
resultset(
330+
column_defs: column_defs,
331+
num_rows: num_rows,
332+
rows: rows,
333+
status_flags: status_flags,
334+
num_warnings: num_warnings
335+
) ->
336+
{%Result{
337+
connection_id: state.client.connection_id,
338+
columns: Enum.map(column_defs, &elem(&1, 1)),
339+
num_rows: num_rows,
340+
rows: rows,
341+
num_warnings: num_warnings
342+
}, status_flags}
343+
344+
ok_packet(
345+
last_insert_id: last_insert_id,
346+
affected_rows: affected_rows,
347+
status_flags: status_flags,
348+
num_warnings: num_warnings
349+
) ->
350+
{%Result{
351+
connection_id: state.client.connection_id,
352+
last_insert_id: last_insert_id,
353+
num_rows: affected_rows,
354+
num_warnings: num_warnings
355+
}, status_flags}
356+
end
344357

345358
# Keep status flags from the last query. The resultsets
346359
# are given to this function in reverse order, so it is the first one.

test/myxql_test.exs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,15 @@ defmodule MyXQLTest do
208208

209209
assert {:ok, [%MyXQL.Result{rows: [[1]]}]} =
210210
MyXQL.query_many(c.conn, "SELECT 1;", [], query_type: :text)
211+
212+
assert {:ok, [%MyXQL.Result{num_rows: 1}]} =
213+
MyXQL.query_many(c.conn, "INSERT INTO integers VALUES (1);", [], query_type: :text)
214+
215+
assert {:ok, [%MyXQL.Result{num_rows: 0}]} =
216+
MyXQL.query_many(c.conn, "UPDATE integers SET x = x + 1 WHERE x = 0;", [], query_type: :text)
217+
218+
assert {:ok, [%MyXQL.Result{num_rows: 1}]} =
219+
MyXQL.query_many(c.conn, "DELETE FROM integers WHERE x = 1;", [], query_type: :text)
211220
end
212221

213222
test "query_many!/4 with text", c do

0 commit comments

Comments
 (0)