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

body recursive fetch_all without Enum.reverse #205

Merged
merged 1 commit into from Jun 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 13 additions & 21 deletions lib/exqlite/sqlite3.ex
Expand Up @@ -118,7 +118,18 @@ defmodule Exqlite.Sqlite3 do

@spec fetch_all(db(), statement(), integer()) :: {:ok, [row()]} | {:error, reason()}
def fetch_all(conn, statement, chunk_size) do
fetch_all(conn, statement, chunk_size, [])
{:ok, try_fetch_all(conn, statement, chunk_size)}
catch
:throw, {:error, _reason} = error -> error
end

defp try_fetch_all(conn, statement, chunk_size) do
case multi_step(conn, statement, chunk_size) do
{:done, rows} -> rows
{:rows, rows} -> rows ++ try_fetch_all(conn, statement, chunk_size)
{:error, _reason} = error -> throw(error)
:busy -> throw({:error, "Database busy"})
end
end

@spec fetch_all(db(), statement()) :: {:ok, [row()]} | {:error, reason()}
Expand All @@ -129,26 +140,7 @@ defmodule Exqlite.Sqlite3 do
#
# For now this just works
chunk_size = Application.get_env(:exqlite, :default_chunk_size, 50)
fetch_all(conn, statement, chunk_size, [])
end

defp fetch_all(conn, statement, chunk_size, accum) do
case multi_step(conn, statement, chunk_size) do
{:done, rows} ->
case accum do
[] -> {:ok, rows}
accum -> {:ok, Enum.reverse(rows ++ accum)}
Copy link
Contributor Author

@ruslandoga ruslandoga Jun 1, 2022

Choose a reason for hiding this comment

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

rows should've been reversed here before concatenating, I think

Copy link
Member

Choose a reason for hiding this comment

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

You are right. They should have been reversed, before appending.

end

{:rows, rows} ->
fetch_all(conn, statement, chunk_size, Enum.reverse(rows) ++ accum)

{:error, reason} ->
{:error, reason}

:busy ->
{:error, "Database busy"}
end
fetch_all(conn, statement, chunk_size)
end

@doc """
Expand Down