@@ -52,61 +52,67 @@ defmodule Sqlitex.Server do
52
52
Starts a SQLite Server (GenServer) instance.
53
53
54
54
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`.
57
62
"""
58
63
def start_link ( db_path , opts \\ [ ] ) do
59
64
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 )
61
67
end
62
68
63
69
## GenServer callbacks
64
70
65
- def init ( { db_path , stmt_cache_size } )
71
+ def init ( { db_path , stmt_cache_size , timeout } )
66
72
when is_integer ( stmt_cache_size ) and stmt_cache_size > 0
67
73
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 } }
70
76
{ :error , reason } -> { :stop , reason }
71
77
end
72
78
end
73
79
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 } }
77
83
end
78
84
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 } }
83
89
end
84
90
end
85
91
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 } }
90
96
end
91
97
end
92
98
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 } }
97
103
end
98
104
end
99
105
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 } }
103
109
end
104
110
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 } }
107
113
end
108
114
109
- def terminate ( _reason , { db , _stmt_cache } ) do
115
+ def terminate ( _reason , { db , _stmt_cache , _timeout } ) do
110
116
Sqlitex . close ( db )
111
117
:ok
112
118
end
@@ -157,24 +163,24 @@ defmodule Sqlitex.Server do
157
163
158
164
## Helpers
159
165
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 ) ,
163
169
{ :ok , rows } <- Statement . fetch_all ( stmt , Keyword . get ( opts , :into , [ ] ) ) ,
164
170
do: { :ok , rows , new_cache }
165
171
end
166
172
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 ) ,
170
176
{ :ok , rows } <- Statement . fetch_all ( stmt , :raw_list ) ,
171
177
do: { :ok ,
172
178
% { rows: rows , columns: stmt . column_names , types: stmt . column_types } ,
173
179
new_cache }
174
180
end
175
181
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 ) ,
178
184
do: { :ok , % { columns: stmt . column_names , types: stmt . column_types } , new_cache }
179
185
end
180
186
0 commit comments