Skip to content

Adding before_disconnect hook #271

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

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
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
18 changes: 14 additions & 4 deletions lib/exqlite/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,18 @@ defmodule Exqlite.Connection do
:path,
:transaction_status,
:status,
:chunk_size
:chunk_size,
:before_disconnect
]

@type t() :: %__MODULE__{
db: Sqlite3.db(),
directory: String.t() | nil,
path: String.t(),
transaction_status: :idle | :transaction,
status: :idle | :busy
status: :idle | :busy,
chunk_size: integer(),
before_disconnect: (t -> any) | {module, atom, [any]} | nil
}

@type journal_mode() :: :delete | :truncate | :persist | :memory | :wal | :off
Expand Down Expand Up @@ -73,6 +76,7 @@ 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}

@impl true
@doc """
Expand Down Expand Up @@ -155,6 +159,7 @@ defmodule Exqlite.Connection do
"./priv/sqlite/\#{arch_dir}/vss0"
]
```
* `:before_disconnect` - A function to run before disconnect, either a 2-arity fun or `{module, function, args}` with the close reason and `t:Exqlite.Connection.t/0` prepended to `args` or `nil` (default: `nil`)

For more information about the options above, see [sqlite documentation][1]

Expand Down Expand Up @@ -190,7 +195,11 @@ defmodule Exqlite.Connection do
end

@impl true
def disconnect(_err, %__MODULE__{db: db}) do
def disconnect(err, %__MODULE__{db: db} = state) do
if state.before_disconnect != nil do
apply(state.before_disconnect, [err, state])
end

case Sqlite3.close(db) do
:ok -> :ok
{:error, reason} -> {:error, %Error{message: to_string(reason)}}
Expand Down Expand Up @@ -539,7 +548,8 @@ defmodule Exqlite.Connection do
path: database,
transaction_status: :idle,
status: :idle,
chunk_size: Keyword.get(options, :chunk_size)
chunk_size: Keyword.get(options, :chunk_size),
before_disconnect: Keyword.get(options, :before_disconnect, nil)
}

{:ok, state}
Expand Down
13 changes: 13 additions & 0 deletions test/exqlite/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ defmodule Exqlite.ConnectionTest do

assert :ok == Connection.disconnect(nil, conn)
end

test "executes before_disconnect before disconnecting" do
{:ok, conn} =
Connection.connect(
database: :memory,
before_disconnect: fn err, db ->
assert err
assert is_function(db.before_disconnect)
end
)

assert :ok == Connection.disconnect(true, conn)
end
end

describe ".handle_execute/4" do
Expand Down