@@ -52,61 +52,67 @@ defmodule Sqlitex.Server do
5252 Starts a SQLite Server (GenServer) instance.
5353
5454 In addition to the options that are typically provided to `GenServer.start_link/3`,
55- you can also specify `stmt_cache_size: (positive_integer)` to override the default
56- limit (20) of statements that are cached when calling `prepare/3`.
55+ you can also specify:
56+
57+ - `stmt_cache_size: (positive_integer)` to override the default limit (20) of statements
58+ that are cached when calling `prepare/3`.
59+ - `esqlite3_timeout: (positive_integer)` to override `:esqlite3`'s default timeout of 5000 ms for
60+ interactions with the database. This can also be set in `config.exs` as
61+ `config :sqlitex, esqlite3_timeout: 5_000`.
5762 """
5863 def start_link ( db_path , opts \\ [ ] ) do
5964 stmt_cache_size = Keyword . get ( opts , :stmt_cache_size , 20 )
60- GenServer . start_link ( __MODULE__ , { db_path , stmt_cache_size } , opts )
65+ timeout = Keyword . get ( opts , :esqlite3_timeout , Application . get_env ( :sqlitex , :esqlite3_timeout , 5_000 ) )
66+ GenServer . start_link ( __MODULE__ , { db_path , stmt_cache_size , timeout } , opts )
6167 end
6268
6369 ## GenServer callbacks
6470
65- def init ( { db_path , stmt_cache_size } )
71+ def init ( { db_path , stmt_cache_size , timeout } )
6672 when is_integer ( stmt_cache_size ) and stmt_cache_size > 0
6773 do
68- case Sqlitex . open ( db_path ) do
69- { :ok , db } -> { :ok , { db , __MODULE__ . StatementCache . new ( db , stmt_cache_size ) } }
74+ case Sqlitex . open ( db_path , timeout ) do
75+ { :ok , db } -> { :ok , { db , __MODULE__ . StatementCache . new ( db , stmt_cache_size ) , timeout } }
7076 { :error , reason } -> { :stop , reason }
7177 end
7278 end
7379
74- def handle_call ( { :exec , sql } , _from , { db , stmt_cache } ) do
75- result = Sqlitex . exec ( db , sql )
76- { :reply , result , { db , stmt_cache } }
80+ def handle_call ( { :exec , sql } , _from , { db , stmt_cache , timeout } ) do
81+ result = Sqlitex . exec ( db , sql , timeout )
82+ { :reply , result , { db , stmt_cache , timeout } }
7783 end
7884
79- def handle_call ( { :query , sql , opts } , _from , { db , stmt_cache } ) do
80- case query_impl ( sql , opts , stmt_cache ) do
81- { :ok , result , new_cache } -> { :reply , { :ok , result } , { db , new_cache } }
82- err -> { :reply , err , { db , stmt_cache } }
85+ def handle_call ( { :query , sql , opts } , _from , { db , stmt_cache , timeout } ) do
86+ case query_impl ( sql , opts , stmt_cache , timeout ) do
87+ { :ok , result , new_cache } -> { :reply , { :ok , result } , { db , new_cache , timeout } }
88+ err -> { :reply , err , { db , stmt_cache , timeout } }
8389 end
8490 end
8591
86- def handle_call ( { :query_rows , sql , opts } , _from , { db , stmt_cache } ) do
87- case query_rows_impl ( sql , opts , stmt_cache ) do
88- { :ok , result , new_cache } -> { :reply , { :ok , result } , { db , new_cache } }
89- err -> { :reply , err , { db , stmt_cache } }
92+ def handle_call ( { :query_rows , sql , opts } , _from , { db , stmt_cache , timeout } ) do
93+ case query_rows_impl ( sql , opts , stmt_cache , timeout ) do
94+ { :ok , result , new_cache } -> { :reply , { :ok , result } , { db , new_cache , timeout } }
95+ err -> { :reply , err , { db , stmt_cache , timeout } }
9096 end
9197 end
9298
93- def handle_call ( { :prepare , sql } , _from , { db , stmt_cache } ) do
94- case prepare_impl ( sql , stmt_cache ) do
95- { :ok , result , new_cache } -> { :reply , { :ok , result } , { db , new_cache } }
96- err -> { :reply , err , { db , stmt_cache } }
99+ def handle_call ( { :prepare , sql } , _from , { db , stmt_cache , timeout } ) do
100+ case prepare_impl ( sql , stmt_cache , timeout ) do
101+ { :ok , result , new_cache } -> { :reply , { :ok , result } , { db , new_cache , timeout } }
102+ err -> { :reply , err , { db , stmt_cache , timeout } }
97103 end
98104 end
99105
100- def handle_call ( { :create_table , name , table_opts , cols } , _from , { db , stmt_cache } ) do
101- result = Sqlitex . create_table ( db , name , table_opts , cols )
102- { :reply , result , { db , stmt_cache } }
106+ def handle_call ( { :create_table , name , table_opts , cols } , _from , { db , stmt_cache , timeout } ) do
107+ result = Sqlitex . create_table ( db , name , table_opts , cols , timeout )
108+ { :reply , result , { db , stmt_cache , timeout } }
103109 end
104110
105- def handle_cast ( :stop , { db , stmt_cache } ) do
106- { :stop , :normal , { db , stmt_cache } }
111+ def handle_cast ( :stop , { db , stmt_cache , timeout } ) do
112+ { :stop , :normal , { db , stmt_cache , timeout } }
107113 end
108114
109- def terminate ( _reason , { db , _stmt_cache } ) do
115+ def terminate ( _reason , { db , _stmt_cache , _timeout } ) do
110116 Sqlitex . close ( db )
111117 :ok
112118 end
@@ -157,24 +163,24 @@ defmodule Sqlitex.Server do
157163
158164 ## Helpers
159165
160- defp query_impl ( sql , opts , stmt_cache ) do
161- with { % Cache { } = new_cache , stmt } <- Cache . prepare ( stmt_cache , sql ) ,
162- { :ok , stmt } <- Statement . bind_values ( stmt , Keyword . get ( opts , :bind , [ ] ) ) ,
166+ defp query_impl ( sql , opts , stmt_cache , esqlite3_timeout ) do
167+ with { % Cache { } = new_cache , stmt } <- Cache . prepare ( stmt_cache , sql , esqlite3_timeout ) ,
168+ { :ok , stmt } <- Statement . bind_values ( stmt , Keyword . get ( opts , :bind , [ ] ) , esqlite3_timeout ) ,
163169 { :ok , rows } <- Statement . fetch_all ( stmt , Keyword . get ( opts , :into , [ ] ) ) ,
164170 do: { :ok , rows , new_cache }
165171 end
166172
167- defp query_rows_impl ( sql , opts , stmt_cache ) do
168- with { % Cache { } = new_cache , stmt } <- Cache . prepare ( stmt_cache , sql ) ,
169- { :ok , stmt } <- Statement . bind_values ( stmt , Keyword . get ( opts , :bind , [ ] ) ) ,
173+ defp query_rows_impl ( sql , opts , stmt_cache , esqlite3_timeout ) do
174+ with { % Cache { } = new_cache , stmt } <- Cache . prepare ( stmt_cache , sql , esqlite3_timeout ) ,
175+ { :ok , stmt } <- Statement . bind_values ( stmt , Keyword . get ( opts , :bind , [ ] ) , esqlite3_timeout ) ,
170176 { :ok , rows } <- Statement . fetch_all ( stmt , :raw_list ) ,
171177 do: { :ok ,
172178 % { rows: rows , columns: stmt . column_names , types: stmt . column_types } ,
173179 new_cache }
174180 end
175181
176- defp prepare_impl ( sql , stmt_cache ) do
177- with { % Cache { } = new_cache , stmt } <- Cache . prepare ( stmt_cache , sql ) ,
182+ defp prepare_impl ( sql , stmt_cache , esqlite3_timeout ) do
183+ with { % Cache { } = new_cache , stmt } <- Cache . prepare ( stmt_cache , sql , esqlite3_timeout ) ,
178184 do: { :ok , % { columns: stmt . column_names , types: stmt . column_types } , new_cache }
179185 end
180186
0 commit comments