@@ -29,14 +29,21 @@ defmodule Sqlite do
29
29
@ typedoc "Connection identifier for your Sqlite instance."
30
30
@ opaque conn :: Connection . t ( )
31
31
32
+ @ default_start_opts [
33
+ timeout: Application . get_env ( :esqlite , :default_timeout , 5000 )
34
+ ]
35
+
32
36
@ doc """
33
37
Start the connection process and connect to sqlite.
34
38
## Options
35
39
* `:database` -> Databse uri.
40
+ * `:timeout` -> Max amount of time for commands to take. (default: 5000)
41
+ ## GenServer opts
42
+ These get passed directly to [GenServer](GenServer.html)
36
43
## Examples
37
44
iex> {:ok, pid} = Sqlite.open(database: "sqlite.db")
38
45
{:ok, #PID<0.69.0>}
39
- iex> {:ok, pid} = Sqlite.open(database: ":memory:")
46
+ iex> {:ok, pid} = Sqlite.open(database: ":memory:", timeout: 6000 )
40
47
{:ok, #PID<0.69.0>}
41
48
"""
42
49
@ spec open ( Keyword . t ( ) , GenServer . options ( ) ) :: { :ok , conn } | { :error , term }
@@ -77,8 +84,8 @@ defmodule Sqlite do
77
84
{ :ok , Sqlite.Result . t ( ) } | { :error , Sqlite.Error . t ( ) }
78
85
def query ( conn , sql , params , opts \\ [ ] ) do
79
86
opts = opts |> defaults ( )
80
-
81
- r = GenServer . call ( conn . pid , { :query , sql , params , opts } , call_timeout ( opts ) )
87
+ call = { :query , sql , params , opts }
88
+ r = GenServer . call ( conn . pid , call , call_timeout ( opts ) )
82
89
83
90
case r do
84
91
{ :ok , % Sqlite.Result { } } = ok -> ok
@@ -110,8 +117,8 @@ defmodule Sqlite do
110
117
@ spec prepare ( conn , iodata , Keyword . t ( ) ) :: { :ok , Sqlite.Query . t ( ) } | { :error , Sqlite.Error . t ( ) }
111
118
def prepare ( conn , sql , opts \\ [ ] ) do
112
119
opts = opts |> defaults ( )
113
-
114
- r = GenServer . call ( conn . pid , { :prepare , sql , opts } , call_timeout ( opts ) )
120
+ call = { :prepare , sql , opts }
121
+ r = GenServer . call ( conn . pid , call , call_timeout ( opts ) )
115
122
116
123
case r do
117
124
{ :ok , % Sqlite.Query { } } = ok -> ok
@@ -141,8 +148,8 @@ defmodule Sqlite do
141
148
@ spec release_query ( conn , Sqlite.Query . t ( ) , Keyword . t ( ) ) :: :ok | { :error , Sqlite.Error . t ( ) }
142
149
def release_query ( conn , query , opts \\ [ ] ) do
143
150
opts = opts |> defaults ( )
144
-
145
- r = GenServer . call ( conn . pid , { :release_query , query , opts } , call_timeout ( opts ) )
151
+ call = { :release_query , query , opts }
152
+ r = GenServer . call ( conn . pid , call , call_timeout ( opts ) )
146
153
147
154
case r do
148
155
:ok -> :ok
@@ -186,8 +193,8 @@ defmodule Sqlite do
186
193
{ :ok , Sqlite.Result . t ( ) } | { :error , Sqlite.Error . t ( ) }
187
194
def execute ( conn , query , params , opts \\ [ ] ) do
188
195
opts = defaults ( opts )
189
-
190
- r = GenServer . call ( conn . pid , { :execute , query , params , opts } , call_timeout ( opts ) )
196
+ call = { :execute , query , params , opts }
197
+ r = GenServer . call ( conn . pid , call , call_timeout ( opts ) )
191
198
192
199
case r do
193
200
{ :ok , % Sqlite.Result { } } = ok -> ok
@@ -239,8 +246,9 @@ defmodule Sqlite do
239
246
Keyword . merge ( defaults , opts )
240
247
end
241
248
249
+ @ doc false
242
250
@ spec default_opts ( Keyword . t ( ) ) :: Keyword . t ( )
243
- def default_opts ( opts ) do
244
- Keyword . merge ( [ timeout: Application . get_env ( :esqlite , :default_timeout , 5000 ) ] , opts )
251
+ defp default_opts ( opts ) do
252
+ Keyword . merge ( @ default_start_opts , opts )
245
253
end
246
254
end
0 commit comments