Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion lib/exqlite.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ defmodule Exqlite do
end

@spec execute(DBConnection.conn(), Query.t(), list(), list()) ::
{:ok, Result.t()} | {:error, Error.t()}
{:ok, DBConnection.Query.t(), Result.t()} | {:error, Error.t()}
def execute(conn, query, params, opts \\ []) do
DBConnection.execute(conn, query, params, opts)
end
Expand Down
24 changes: 5 additions & 19 deletions lib/exqlite/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule Exqlite.Connection do
transaction_status: :idle | :transaction,
status: :idle | :busy,
chunk_size: integer(),
before_disconnect: (t -> any) | {module, atom, [any]} | nil
before_disconnect: (Exception.t(), t -> any) | {module, atom, [any]} | nil
}

@type journal_mode() :: :delete | :truncate | :persist | :memory | :wal | :off
Expand Down Expand Up @@ -81,7 +81,8 @@ defmodule Exqlite.Connection do
| {:hard_heap_limit, integer()}
| {:key, String.t()}
| {:custom_pragmas, [{keyword(), integer() | boolean() | String.t()}]}
| {:before_disconnect, (t -> any) | {module, atom, [any]} | nil}
| {:before_disconnect,
(Exception.t(), t -> any) | {module, atom, [any]} | nil}

@impl true
@doc """
Expand Down Expand Up @@ -374,7 +375,7 @@ defmodule Exqlite.Connection do
def handle_declare(%Query{} = query, params, opts, state) do
# We emulate cursor functionality by just using a prepared statement and
# step through it. Thus we just return the query ref as the cursor.
with {:ok, query} <- prepare_no_cache(query, opts, state),
with {:ok, query} <- prepare(query, opts, state),
{:ok, query} <- bind_params(query, params, state) do
{:ok, query, query.ref, state}
end
Expand Down Expand Up @@ -627,25 +628,10 @@ defmodule Exqlite.Connection do
end
end

# Attempt to retrieve the cached query, if it doesn't exist, we'll prepare one
# and cache it for later.
defp prepare(%Query{statement: statement} = query, options, state) do
query = maybe_put_command(query, options)

with {:ok, ref} <- Sqlite3.prepare(state.db, IO.iodata_to_binary(statement)),
query <- %{query | ref: ref} do
{:ok, query}
else
{:error, reason} ->
{:error, %Error{message: to_string(reason), statement: statement}, state}
end
end

# Prepare a query and do not cache it.
defp prepare_no_cache(%Query{statement: statement} = query, options, state) do
query = maybe_put_command(query, options)

case Sqlite3.prepare(state.db, statement) do
case Sqlite3.prepare(state.db, IO.iodata_to_binary(statement)) do
{:ok, ref} ->
{:ok, %{query | ref: ref}}

Expand Down
Loading