@@ -58,87 +58,107 @@ defmodule Sqlitex.Server do
58
58
- `stmt_cache_size: (positive_integer)` to override the default limit (20) of statements
59
59
that are cached when calling `prepare/3`.
60
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, db_timeout: 5_000`.
61
+ interactions with the database. This can also be set in `config.exs` as `config :sqlitex, db_timeout: 5_000`.
62
+ - `db_chunk_size: (positive_integer)` to override `:esqlite3`'s default chunk_size of 5000 rows
63
+ to read from native sqlite and send to erlang process in one bulk.
64
+ This can also be set in `config.exs` as `config :sqlitex, db_chunk_size: 5_000`.
63
65
"""
64
66
def start_link ( db_path , opts \\ [ ] ) do
65
67
stmt_cache_size = Keyword . get ( opts , :stmt_cache_size , 20 )
66
- timeout = Keyword . get ( opts , :db_timeout , Config . db_timeout ( ) )
67
- GenServer . start_link ( __MODULE__ , { db_path , stmt_cache_size , timeout } , opts )
68
+ config = [
69
+ db_timeout: Config . db_timeout ( opts ) ,
70
+ db_chunk_size: Config . db_chunk_size ( opts )
71
+ ]
72
+ GenServer . start_link ( __MODULE__ , { db_path , stmt_cache_size , config } , opts )
68
73
end
69
74
70
75
## GenServer callbacks
71
76
72
- def init ( { db_path , stmt_cache_size , timeout } )
77
+ def init ( { db_path , stmt_cache_size , config } )
73
78
when is_integer ( stmt_cache_size ) and stmt_cache_size > 0
74
79
do
75
- case Sqlitex . open ( db_path , [ db_timeout: timeout ] ) do
76
- { :ok , db } -> { :ok , { db , __MODULE__ . StatementCache . new ( db , stmt_cache_size ) , timeout } }
80
+ case Sqlitex . open ( db_path , config ) do
81
+ { :ok , db } -> { :ok , { db , __MODULE__ . StatementCache . new ( db , stmt_cache_size ) , config } }
77
82
{ :error , reason } -> { :stop , reason }
78
83
end
79
84
end
80
85
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 } }
86
+ def handle_call ( { :exec , sql } , _from , { db , stmt_cache , config } ) do
87
+ result = Sqlitex . exec ( db , sql , config )
88
+ { :reply , result , { db , stmt_cache , config } }
84
89
end
85
90
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 } }
91
+ def handle_call ( { :query , sql , opts } , _from , { db , stmt_cache , config } ) do
92
+ case query_impl ( sql , stmt_cache , Keyword . merge ( config , opts ) ) do
93
+ { :ok , result , new_cache } -> { :reply , { :ok , result } , { db , new_cache , config } }
94
+ err -> { :reply , err , { db , stmt_cache , config } }
90
95
end
91
96
end
92
97
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 } }
98
+ def handle_call ( { :query_rows , sql , opts } , _from , { db , stmt_cache , config } ) do
99
+ case query_rows_impl ( sql , stmt_cache , Keyword . merge ( config , opts ) ) do
100
+ { :ok , result , new_cache } -> { :reply , { :ok , result } , { db , new_cache , config } }
101
+ err -> { :reply , err , { db , stmt_cache , config } }
97
102
end
98
103
end
99
104
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 } }
105
+ def handle_call ( { :prepare , sql } , _from , { db , stmt_cache , config } ) do
106
+ case prepare_impl ( sql , stmt_cache , config ) do
107
+ { :ok , result , new_cache } -> { :reply , { :ok , result } , { db , new_cache , config } }
108
+ err -> { :reply , err , { db , stmt_cache , config } }
104
109
end
105
110
end
106
111
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 } }
112
+ def handle_call ( { :create_table , name , table_opts , cols } , _from , { db , stmt_cache , config } ) do
113
+ result = Sqlitex . create_table ( db , name , table_opts , cols , config )
114
+ { :reply , result , { db , stmt_cache , config } }
110
115
end
111
116
112
- def handle_call ( { :set_update_hook , pid , opts } , _from , { db , stmt_cache , timeout } ) do
113
- result = Sqlitex . set_update_hook ( db , pid , opts )
114
- { :reply , result , { db , stmt_cache , timeout } }
117
+ def handle_call ( { :set_update_hook , pid , opts } , _from , { db , stmt_cache , config } ) do
118
+ result = Sqlitex . set_update_hook ( db , pid , Keyword . merge ( config , opts ) )
119
+ { :reply , result , { db , stmt_cache , config } }
115
120
end
116
121
117
- def handle_cast ( :stop , { db , stmt_cache , timeout } ) do
118
- { :stop , :normal , { db , stmt_cache , timeout } }
122
+ def handle_cast ( :stop , { db , stmt_cache , config } ) do
123
+ { :stop , :normal , { db , stmt_cache , config } }
119
124
end
120
125
121
- def terminate ( _reason , { db , _stmt_cache , _timeout } ) do
122
- Sqlitex . close ( db )
126
+ def terminate ( _reason , { db , _stmt_cache , config } ) do
127
+ Sqlitex . close ( db , config )
123
128
:ok
124
129
end
125
130
126
131
## Public API
127
132
133
+ @ doc """
134
+ Same as `Sqlitex.exec/3` but using the shared db connections saved in the GenServer state.
135
+
136
+ Returns the results otherwise.
137
+ """
128
138
def exec ( pid , sql , opts \\ [ ] ) do
129
- GenServer . call ( pid , { :exec , sql } , timeout ( opts ) )
139
+ GenServer . call ( pid , { :exec , sql } , Config . call_timeout ( opts ) )
130
140
end
131
141
142
+ @ doc """
143
+ Same as `Sqlitex.Query.query/3` but using the shared db connections saved in the GenServer state.
144
+
145
+ Returns the results otherwise.
146
+ """
132
147
def query ( pid , sql , opts \\ [ ] ) do
133
- GenServer . call ( pid , { :query , sql , opts } , timeout ( opts ) )
148
+ GenServer . call ( pid , { :query , sql , opts } , Config . call_timeout ( opts ) )
134
149
end
135
150
151
+ @ doc """
152
+ Same as `Sqlitex.Query.query_rows/3` but using the shared db connections saved in the GenServer state.
153
+
154
+ Returns the results otherwise.
155
+ """
136
156
def query_rows ( pid , sql , opts \\ [ ] ) do
137
- GenServer . call ( pid , { :query_rows , sql , opts } , timeout ( opts ) )
157
+ GenServer . call ( pid , { :query_rows , sql , opts } , Config . call_timeout ( opts ) )
138
158
end
139
159
140
160
def set_update_hook ( server_pid , notification_pid , opts \\ [ ] ) do
141
- GenServer . call ( server_pid , { :set_update_hook , notification_pid , opts } , timeout ( opts ) )
161
+ GenServer . call ( server_pid , { :set_update_hook , notification_pid , opts } , Config . call_timeout ( opts ) )
142
162
end
143
163
144
164
@ doc """
@@ -160,7 +180,7 @@ defmodule Sqlitex.Server do
160
180
could not be prepared.
161
181
"""
162
182
def prepare ( pid , sql , opts \\ [ ] ) do
163
- GenServer . call ( pid , { :prepare , sql } , timeout ( opts ) )
183
+ GenServer . call ( pid , { :prepare , sql } , Config . call_timeout ( opts ) )
164
184
end
165
185
166
186
def create_table ( pid , name , table_opts \\ [ ] , cols ) do
@@ -173,30 +193,24 @@ defmodule Sqlitex.Server do
173
193
174
194
## Helpers
175
195
176
- defp query_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 ) ,
181
- { :ok , rows } <- Statement . fetch_all ( stmt , Keyword . get ( opts , :db_timeout , 5_000 ) , Keyword . get ( opts , :into , [ ] ) ) ,
196
+ defp query_impl ( sql , stmt_cache , opts ) do
197
+ with { % Cache { } = new_cache , stmt } <- Cache . prepare ( stmt_cache , sql , opts ) ,
198
+ { :ok , stmt } <- Statement . bind_values ( stmt , Keyword . get ( opts , :bind , [ ] ) , opts ) ,
199
+ { :ok , rows } <- Statement . fetch_all ( stmt , opts ) ,
182
200
do: { :ok , rows , new_cache }
183
201
end
184
202
185
- defp query_rows_impl ( sql , opts , stmt_cache , db_timeout ) do
186
- db_opts = [ db_timeout: db_timeout ]
187
-
188
- with { % Cache { } = new_cache , stmt } <- Cache . prepare ( stmt_cache , sql , db_opts ) ,
189
- { :ok , stmt } <- Statement . bind_values ( stmt , Keyword . get ( opts , :bind , [ ] ) , db_opts ) ,
190
- { :ok , rows } <- Statement . fetch_all ( stmt , Keyword . get ( opts , :db_timeout , 5_000 ) , :raw_list ) ,
203
+ defp query_rows_impl ( sql , stmt_cache , opts ) do
204
+ with { % Cache { } = new_cache , stmt } <- Cache . prepare ( stmt_cache , sql , opts ) ,
205
+ { :ok , stmt } <- Statement . bind_values ( stmt , Keyword . get ( opts , :bind , [ ] ) , opts ) ,
206
+ { :ok , rows } <- Statement . fetch_all ( stmt , Keyword . put ( opts , :into , :raw_list ) ) ,
191
207
do: { :ok ,
192
208
% { rows: rows , columns: stmt . column_names , types: stmt . column_types } ,
193
209
new_cache }
194
210
end
195
211
196
- defp prepare_impl ( sql , stmt_cache , db_timeout ) do
197
- with { % Cache { } = new_cache , stmt } <- Cache . prepare ( stmt_cache , sql , [ db_timeout: db_timeout ] ) ,
212
+ defp prepare_impl ( sql , stmt_cache , opts ) do
213
+ with { % Cache { } = new_cache , stmt } <- Cache . prepare ( stmt_cache , sql , opts ) ,
198
214
do: { :ok , % { columns: stmt . column_names , types: stmt . column_types } , new_cache }
199
215
end
200
-
201
- defp timeout ( kwopts ) , do: Keyword . get ( kwopts , :timeout , 5000 )
202
216
end
0 commit comments