Skip to content

Commit

Permalink
Changes to pointer API
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jul 10, 2023
1 parent 7069341 commit eb3443a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/adbc_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule Adbc.Connection do
def query_pointer(conn, query, params \\ [], fun)
when is_binary(query) and is_list(params) and is_function(fun) do
stream_lock(conn, {:query, query, params}, fn stream_ref, rows_affected ->
fun.(Adbc.Nif.adbc_arrow_array_stream_get_pointer(stream_ref), rows_affected)
{:ok, fun.(Adbc.Nif.adbc_arrow_array_stream_get_pointer(stream_ref), rows_affected)}
end)
end

Expand Down Expand Up @@ -209,7 +209,7 @@ defmodule Adbc.Connection do
case GenServer.call(conn, {:stream_lock, command}, :infinity) do
{:ok, conn, unlock_ref, stream_ref, rows_affected} ->
try do
fun.(stream_ref, rows_affected)
fun.(stream_ref, normalize_rows(rows_affected))
after
GenServer.cast(conn, {:unlock, unlock_ref})
end
Expand All @@ -219,7 +219,9 @@ defmodule Adbc.Connection do
end
end

defp stream_results(reference, -1), do: stream_results(reference, %{}, nil)
defp normalize_rows(-1), do: nil
defp normalize_rows(rows) when is_integer(rows) and rows >= 0, do: rows

defp stream_results(reference, num_rows), do: stream_results(reference, %{}, num_rows)

defp stream_results(reference, acc, num_rows) do
Expand Down Expand Up @@ -321,7 +323,7 @@ defmodule Adbc.Connection do

defp handle_command({name, args}, conn) do
with {:ok, stream_ref} <- apply(Adbc.Nif, name, [conn | args]) do
{:ok, stream_ref, nil}
{:ok, stream_ref, -1}
end
end

Expand Down
12 changes: 12 additions & 0 deletions test/adbc_connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ defmodule Adbc.Connection.Test do
end
end

describe "query_pointer" do
test "select", %{db: db} do
conn = start_supervised!({Connection, database: db})

assert {:ok, :from_pointer} =
Connection.query_pointer(conn, "SELECT 123 as num", fn
pointer, nil when is_integer(pointer) ->
:from_pointer
end)
end
end

describe "lock" do
test "serializes access", %{db: db} do
conn = start_supervised!({Connection, database: db})
Expand Down

0 comments on commit eb3443a

Please sign in to comment.