@@ -29,14 +29,21 @@ defmodule Sqlite do
2929 @ typedoc "Connection identifier for your Sqlite instance."
3030 @ opaque conn :: Connection . t ( )
3131
32+ @ default_start_opts [
33+ timeout: Application . get_env ( :esqlite , :default_timeout , 5000 )
34+ ]
35+
3236 @ doc """
3337 Start the connection process and connect to sqlite.
3438 ## Options
3539 * `: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)
3643 ## Examples
3744 iex> {:ok, pid} = Sqlite.open(database: "sqlite.db")
3845 {:ok, #PID<0.69.0>}
39- iex> {:ok, pid} = Sqlite.open(database: ":memory:")
46+ iex> {:ok, pid} = Sqlite.open(database: ":memory:", timeout: 6000 )
4047 {:ok, #PID<0.69.0>}
4148 """
4249 @ spec open ( Keyword . t ( ) , GenServer . options ( ) ) :: { :ok , conn } | { :error , term }
@@ -77,8 +84,8 @@ defmodule Sqlite do
7784 { :ok , Sqlite.Result . t ( ) } | { :error , Sqlite.Error . t ( ) }
7885 def query ( conn , sql , params , opts \\ [ ] ) do
7986 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 ) )
8289
8390 case r do
8491 { :ok , % Sqlite.Result { } } = ok -> ok
@@ -110,8 +117,8 @@ defmodule Sqlite do
110117 @ spec prepare ( conn , iodata , Keyword . t ( ) ) :: { :ok , Sqlite.Query . t ( ) } | { :error , Sqlite.Error . t ( ) }
111118 def prepare ( conn , sql , opts \\ [ ] ) do
112119 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 ) )
115122
116123 case r do
117124 { :ok , % Sqlite.Query { } } = ok -> ok
@@ -141,8 +148,8 @@ defmodule Sqlite do
141148 @ spec release_query ( conn , Sqlite.Query . t ( ) , Keyword . t ( ) ) :: :ok | { :error , Sqlite.Error . t ( ) }
142149 def release_query ( conn , query , opts \\ [ ] ) do
143150 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 ) )
146153
147154 case r do
148155 :ok -> :ok
@@ -186,8 +193,8 @@ defmodule Sqlite do
186193 { :ok , Sqlite.Result . t ( ) } | { :error , Sqlite.Error . t ( ) }
187194 def execute ( conn , query , params , opts \\ [ ] ) do
188195 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 ) )
191198
192199 case r do
193200 { :ok , % Sqlite.Result { } } = ok -> ok
@@ -239,8 +246,9 @@ defmodule Sqlite do
239246 Keyword . merge ( defaults , opts )
240247 end
241248
249+ @ doc false
242250 @ 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 )
245253 end
246254end
0 commit comments