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

Handle insert/update/delete responses in query_many #194

Merged
Merged
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
Next Next commit
handle insert/update/delete responses in query_many
Greg Rychlewski authored and Greg Rychlewski committed Aug 20, 2024
commit 2c2365f77992a2110ed6c77772c208c1b930afee
47 changes: 30 additions & 17 deletions lib/myxql/connection.ex
Original file line number Diff line number Diff line change
@@ -324,23 +324,36 @@ defmodule MyXQL.Connection do
defp result({:ok, resultsets}, query, state) when is_list(resultsets) do
{results, status_flags} =
Enum.reduce(resultsets, {[], nil}, fn resultset, {results, newest_status_flags} ->
resultset(
column_defs: column_defs,
num_rows: num_rows,
rows: rows,
status_flags: status_flags,
num_warnings: num_warnings
) = resultset

columns = Enum.map(column_defs, &elem(&1, 1))

result = %Result{
connection_id: state.client.connection_id,
columns: columns,
num_rows: num_rows,
rows: rows,
num_warnings: num_warnings
}
{result, status_flags} =
case resultset do
resultset(
column_defs: column_defs,
num_rows: num_rows,
rows: rows,
status_flags: status_flags,
num_warnings: num_warnings
) ->
{%Result{
connection_id: state.client.connection_id,
columns: Enum.map(column_defs, &elem(&1, 1)),
num_rows: num_rows,
rows: rows,
num_warnings: num_warnings
}, status_flags}

ok_packet(
last_insert_id: last_insert_id,
affected_rows: affected_rows,
status_flags: status_flags,
num_warnings: num_warnings
) ->
{%Result{
connection_id: state.client.connection_id,
last_insert_id: last_insert_id,
num_rows: affected_rows,
num_warnings: num_warnings
}, status_flags}
end

# Keep status flags from the last query. The resultsets
# are given to this function in reverse order, so it is the first one.
9 changes: 9 additions & 0 deletions test/myxql_test.exs
Original file line number Diff line number Diff line change
@@ -208,6 +208,15 @@ defmodule MyXQLTest do

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

assert {:ok, [%MyXQL.Result{num_rows: 1}]} =
MyXQL.query_many(c.conn, "INSERT INTO integers VALUES (1);", [], query_type: :text)
Copy link
Member

Choose a reason for hiding this comment

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

could you assert on columns: too?


assert {:ok, [%MyXQL.Result{num_rows: 0}]} =
MyXQL.query_many(c.conn, "UPDATE integers SET x = x + 1 WHERE x = 0;", [], query_type: :text)

assert {:ok, [%MyXQL.Result{num_rows: 1}]} =
MyXQL.query_many(c.conn, "DELETE FROM integers WHERE x = 1;", [], query_type: :text)
end

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