@@ -36,15 +36,18 @@ defmodule Exqlite.Connection do
36
36
:path ,
37
37
:transaction_status ,
38
38
:status ,
39
- :chunk_size
39
+ :chunk_size ,
40
+ :before_disconnect
40
41
]
41
42
42
43
@ type t ( ) :: % __MODULE__ {
43
44
db: Sqlite3 . db ( ) ,
44
45
directory: String . t ( ) | nil ,
45
46
path: String . t ( ) ,
46
47
transaction_status: :idle | :transaction ,
47
- status: :idle | :busy
48
+ status: :idle | :busy ,
49
+ chunk_size: integer ( ) ,
50
+ before_disconnect: ( t -> any ) | { module , atom , [ any ] } | nil
48
51
}
49
52
50
53
@ type journal_mode ( ) :: :delete | :truncate | :persist | :memory | :wal | :off
@@ -73,6 +76,7 @@ defmodule Exqlite.Connection do
73
76
| { :hard_heap_limit , integer ( ) }
74
77
| { :key , String . t ( ) }
75
78
| { :custom_pragmas , [ { keyword ( ) , integer ( ) | boolean ( ) | String . t ( ) } ] }
79
+ | { :before_disconnect , ( t -> any ) | { module , atom , [ any ] } | nil }
76
80
77
81
@ impl true
78
82
@ doc """
@@ -155,6 +159,9 @@ defmodule Exqlite.Connection do
155
159
"./priv/sqlite/\# {arch_dir}/vss0"
156
160
]
157
161
```
162
+ * `:before_disconnect` - A function to run before disconnect, either a
163
+ 2-arity fun or `{module, function, args}` with the close reason and
164
+ `t:Exqlite.Connection.t/0` prepended to `args` or `nil` (default: `nil`)
158
165
159
166
For more information about the options above, see [sqlite documentation][1]
160
167
@@ -190,7 +197,11 @@ defmodule Exqlite.Connection do
190
197
end
191
198
192
199
@ impl true
193
- def disconnect ( _err , % __MODULE__ { db: db } ) do
200
+ def disconnect ( err , % __MODULE__ { db: db } = state ) do
201
+ if state . before_disconnect != nil do
202
+ apply ( state . before_disconnect , [ err , state ] )
203
+ end
204
+
194
205
case Sqlite3 . close ( db ) do
195
206
:ok -> :ok
196
207
{ :error , reason } -> { :error , % Error { message: to_string ( reason ) } }
@@ -539,7 +550,8 @@ defmodule Exqlite.Connection do
539
550
path: database ,
540
551
transaction_status: :idle ,
541
552
status: :idle ,
542
- chunk_size: Keyword . get ( options , :chunk_size )
553
+ chunk_size: Keyword . get ( options , :chunk_size ) ,
554
+ before_disconnect: Keyword . get ( options , :before_disconnect , nil )
543
555
}
544
556
545
557
{ :ok , state }
0 commit comments