diff --git a/lib/exqlite.ex b/lib/exqlite.ex index af42a7f5..df5f704a 100644 --- a/lib/exqlite.ex +++ b/lib/exqlite.ex @@ -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 diff --git a/lib/exqlite/connection.ex b/lib/exqlite/connection.ex index 261c4b54..d08b4bb3 100644 --- a/lib/exqlite/connection.ex +++ b/lib/exqlite/connection.ex @@ -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 @@ -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 """ @@ -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 @@ -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}}