@@ -58,87 +58,107 @@ defmodule Sqlitex.Server do
5858 - `stmt_cache_size: (positive_integer)` to override the default limit (20) of statements
5959 that are cached when calling `prepare/3`.
6060 - `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`.
6365 """
6466 def start_link ( db_path , opts \\ [ ] ) do
6567 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 )
6873 end
6974
7075 ## GenServer callbacks
7176
72- def init ( { db_path , stmt_cache_size , timeout } )
77+ def init ( { db_path , stmt_cache_size , config } )
7378 when is_integer ( stmt_cache_size ) and stmt_cache_size > 0
7479 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 } }
7782 { :error , reason } -> { :stop , reason }
7883 end
7984 end
8085
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 } }
8489 end
8590
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 } }
9095 end
9196 end
9297
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 } }
97102 end
98103 end
99104
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 } }
104109 end
105110 end
106111
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 } }
110115 end
111116
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 } }
115120 end
116121
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 } }
119124 end
120125
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 )
123128 :ok
124129 end
125130
126131 ## Public API
127132
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+ """
128138 def exec ( pid , sql , opts \\ [ ] ) do
129- GenServer . call ( pid , { :exec , sql } , timeout ( opts ) )
139+ GenServer . call ( pid , { :exec , sql } , Config . call_timeout ( opts ) )
130140 end
131141
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+ """
132147 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 ) )
134149 end
135150
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+ """
136156 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 ) )
138158 end
139159
140160 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 ) )
142162 end
143163
144164 @ doc """
@@ -160,7 +180,7 @@ defmodule Sqlitex.Server do
160180 could not be prepared.
161181 """
162182 def prepare ( pid , sql , opts \\ [ ] ) do
163- GenServer . call ( pid , { :prepare , sql } , timeout ( opts ) )
183+ GenServer . call ( pid , { :prepare , sql } , Config . call_timeout ( opts ) )
164184 end
165185
166186 def create_table ( pid , name , table_opts \\ [ ] , cols ) do
@@ -173,30 +193,24 @@ defmodule Sqlitex.Server do
173193
174194 ## Helpers
175195
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 ) ,
182200 do: { :ok , rows , new_cache }
183201 end
184202
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 ) ) ,
191207 do: { :ok ,
192208 % { rows: rows , columns: stmt . column_names , types: stmt . column_types } ,
193209 new_cache }
194210 end
195211
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 ) ,
198214 do: { :ok , % { columns: stmt . column_names , types: stmt . column_types } , new_cache }
199215 end
200-
201- defp timeout ( kwopts ) , do: Keyword . get ( kwopts , :timeout , 5000 )
202216end
0 commit comments