@@ -47,66 +47,73 @@ defmodule Sqlitex.Server do
4747
4848 alias Sqlitex.Statement
4949 alias Sqlitex.Server.StatementCache , as: Cache
50+ alias Sqlitex.Config
5051
5152 @ doc """
5253 Starts a SQLite Server (GenServer) instance.
5354
5455 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`.
56+ you can also specify:
57+
58+ - `stmt_cache_size: (positive_integer)` to override the default limit (20) of statements
59+ that are cached when calling `prepare/3`.
60+ - `db_timeout: (positive_integer)` to override `:esqlite3`'s default timeout of 5000 ms for
61+ interactions with the database. This can also be set in `config.exs` as
62+ `config :sqlitex, esqlite3_timeout: 5_000`.
5763 """
5864 def start_link ( db_path , opts \\ [ ] ) do
5965 stmt_cache_size = Keyword . get ( opts , :stmt_cache_size , 20 )
60- GenServer . start_link ( __MODULE__ , { db_path , stmt_cache_size } , opts )
66+ timeout = Keyword . get ( opts , :db_timeout , Config . esqlite3_timeout ( ) )
67+ GenServer . start_link ( __MODULE__ , { db_path , stmt_cache_size , timeout } , opts )
6168 end
6269
6370 ## GenServer callbacks
6471
65- def init ( { db_path , stmt_cache_size } )
72+ def init ( { db_path , stmt_cache_size , timeout } )
6673 when is_integer ( stmt_cache_size ) and stmt_cache_size > 0
6774 do
68- case Sqlitex . open ( db_path ) do
69- { :ok , db } -> { :ok , { db , __MODULE__ . StatementCache . new ( db , stmt_cache_size ) } }
75+ case Sqlitex . open ( db_path , [ db_timeout: timeout ] ) do
76+ { :ok , db } -> { :ok , { db , __MODULE__ . StatementCache . new ( db , stmt_cache_size ) , timeout } }
7077 { :error , reason } -> { :stop , reason }
7178 end
7279 end
7380
74- def handle_call ( { :exec , sql } , _from , { db , stmt_cache } ) do
75- result = Sqlitex . exec ( db , sql )
76- { :reply , result , { db , stmt_cache } }
81+ def handle_call ( { :exec , sql } , _from , { db , stmt_cache , timeout } ) do
82+ result = Sqlitex . exec ( db , sql , [ db_timeout: timeout ] )
83+ { :reply , result , { db , stmt_cache , timeout } }
7784 end
7885
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 } }
86+ def handle_call ( { :query , sql , opts } , _from , { db , stmt_cache , timeout } ) do
87+ case query_impl ( sql , opts , stmt_cache , timeout ) do
88+ { :ok , result , new_cache } -> { :reply , { :ok , result } , { db , new_cache , timeout } }
89+ err -> { :reply , err , { db , stmt_cache , timeout } }
8390 end
8491 end
8592
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 } }
93+ def handle_call ( { :query_rows , sql , opts } , _from , { db , stmt_cache , timeout } ) do
94+ case query_rows_impl ( sql , opts , stmt_cache , timeout ) do
95+ { :ok , result , new_cache } -> { :reply , { :ok , result } , { db , new_cache , timeout } }
96+ err -> { :reply , err , { db , stmt_cache , timeout } }
9097 end
9198 end
9299
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 } }
100+ def handle_call ( { :prepare , sql } , _from , { db , stmt_cache , timeout } ) do
101+ case prepare_impl ( sql , stmt_cache , timeout ) do
102+ { :ok , result , new_cache } -> { :reply , { :ok , result } , { db , new_cache , timeout } }
103+ err -> { :reply , err , { db , stmt_cache , timeout } }
97104 end
98105 end
99106
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 } }
107+ def handle_call ( { :create_table , name , table_opts , cols } , _from , { db , stmt_cache , timeout } ) do
108+ result = Sqlitex . create_table ( db , name , table_opts , cols , [ db_timeout: timeout ] )
109+ { :reply , result , { db , stmt_cache , timeout } }
103110 end
104111
105- def handle_cast ( :stop , { db , stmt_cache } ) do
106- { :stop , :normal , { db , stmt_cache } }
112+ def handle_cast ( :stop , { db , stmt_cache , timeout } ) do
113+ { :stop , :normal , { db , stmt_cache , timeout } }
107114 end
108115
109- def terminate ( _reason , { db , _stmt_cache } ) do
116+ def terminate ( _reason , { db , _stmt_cache , _timeout } ) do
110117 Sqlitex . close ( db )
111118 :ok
112119 end
@@ -157,24 +164,28 @@ defmodule Sqlitex.Server do
157164
158165 ## Helpers
159166
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 , [ ] ) ) ,
167+ defp query_impl ( sql , opts , stmt_cache , db_timeout ) do
168+ db_opts = [ db_timeout: db_timeout ]
169+
170+ with { % Cache { } = new_cache , stmt } <- Cache . prepare ( stmt_cache , sql , db_opts ) ,
171+ { :ok , stmt } <- Statement . bind_values ( stmt , Keyword . get ( opts , :bind , [ ] ) , db_opts ) ,
163172 { :ok , rows } <- Statement . fetch_all ( stmt , Keyword . get ( opts , :into , [ ] ) ) ,
164173 do: { :ok , rows , new_cache }
165174 end
166175
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 , [ ] ) ) ,
176+ defp query_rows_impl ( sql , opts , stmt_cache , db_timeout ) do
177+ db_opts = [ db_timeout: db_timeout ]
178+
179+ with { % Cache { } = new_cache , stmt } <- Cache . prepare ( stmt_cache , sql , db_opts ) ,
180+ { :ok , stmt } <- Statement . bind_values ( stmt , Keyword . get ( opts , :bind , [ ] ) , db_opts ) ,
170181 { :ok , rows } <- Statement . fetch_all ( stmt , :raw_list ) ,
171182 do: { :ok ,
172183 % { rows: rows , columns: stmt . column_names , types: stmt . column_types } ,
173184 new_cache }
174185 end
175186
176- defp prepare_impl ( sql , stmt_cache ) do
177- with { % Cache { } = new_cache , stmt } <- Cache . prepare ( stmt_cache , sql ) ,
187+ defp prepare_impl ( sql , stmt_cache , db_timeout ) do
188+ with { % Cache { } = new_cache , stmt } <- Cache . prepare ( stmt_cache , sql , [ db_timeout: db_timeout ] ) ,
178189 do: { :ok , % { columns: stmt . column_names , types: stmt . column_types } , new_cache }
179190 end
180191
0 commit comments