@@ -15,7 +15,7 @@ defmodule Sqlitex.Query do
1515 * `bind` - If your query has parameters in it, you should provide the options
1616 to bind as a list.
1717 * `into` - The collection to put results into. This defaults to a list.
18- * `timeout ` - The timeout (in ms) to apply to each of the underlying SQLite operations. Defaults
18+ * `db_timeout ` - The timeout (in ms) to apply to each of the underlying SQLite operations. Defaults
1919 to 5000, or to `Application.get_env(:sqlitex, :esqlite3_timeout)` if set.
2020
2121 ## Returns
@@ -30,7 +30,10 @@ defmodule Sqlitex.Query do
3030 @ spec query ( Sqlitex . connection , String . t | charlist ) :: { :ok , [ [ ] ] } | { :error , term ( ) }
3131 @ spec query ( Sqlitex . connection , String . t | charlist , [ { atom , term } ] ) :: { :ok , [ [ ] ] } | { :error , term ( ) }
3232 def query ( db , sql , opts \\ [ ] ) do
33- do_query ( db , sql , opts , Keyword . get ( opts , :timeout , nil ) )
33+ with { :ok , stmt } <- Statement . prepare ( db , sql , opts ) ,
34+ { :ok , stmt } <- Statement . bind_values ( stmt , Keyword . get ( opts , :bind , [ ] ) , opts ) ,
35+ { :ok , res } <- Statement . fetch_all ( stmt , Keyword . get ( opts , :into , [ ] ) ) ,
36+ do: { :ok , res }
3437 end
3538
3639 @ doc """
@@ -39,27 +42,14 @@ defmodule Sqlitex.Query do
3942 Returns the results otherwise.
4043 """
4144 @ spec query! ( Sqlitex . connection , String . t | charlist ) :: [ [ ] ]
42- @ spec query! ( Sqlitex . connection , String . t | charlist , [ bind: [ ] , into: Enum . t , timeout : integer ( ) ] ) :: [ Enum . t ]
45+ @ spec query! ( Sqlitex . connection , String . t | charlist , [ bind: [ ] , into: Enum . t , db_timeout : integer ( ) ] ) :: [ Enum . t ]
4346 def query! ( db , sql , opts \\ [ ] ) do
4447 case query ( db , sql , opts ) do
4548 { :error , reason } -> raise Sqlitex.QueryError , reason: reason
4649 { :ok , results } -> results
4750 end
4851 end
4952
50- defp do_query ( db , sql , opts , nil ) do
51- with { :ok , stmt } <- Statement . prepare ( db , sql ) ,
52- { :ok , stmt } <- Statement . bind_values ( stmt , Keyword . get ( opts , :bind , [ ] ) ) ,
53- { :ok , res } <- Statement . fetch_all ( stmt , Keyword . get ( opts , :into , [ ] ) ) ,
54- do: { :ok , res }
55- end
56- defp do_query ( db , sql , opts , timeout ) do
57- with { :ok , stmt } <- Statement . prepare ( db , sql , timeout ) ,
58- { :ok , stmt } <- Statement . bind_values ( stmt , Keyword . get ( opts , :bind , [ ] ) , timeout ) ,
59- { :ok , res } <- Statement . fetch_all ( stmt , Keyword . get ( opts , :into , [ ] ) ) ,
60- do: { :ok , res }
61- end
62-
6353 @ doc """
6454 Runs a query and returns the results as a list of rows each represented as
6555 a list of column values.
@@ -74,7 +64,7 @@ defmodule Sqlitex.Query do
7464
7565 * `bind` - If your query has parameters in it, you should provide the options
7666 to bind as a list.
77- * `timeout ` - The timeout (in ms) to apply to each of the underlying SQLite operations. Defaults
67+ * `db_timeout ` - The timeout (in ms) to apply to each of the underlying SQLite operations. Defaults
7868 to 5000, or to `Application.get_env(:sqlitex, :esqlite3_timeout)` if set.
7969
8070 ## Returns
@@ -83,9 +73,12 @@ defmodule Sqlitex.Query do
8373 """
8474
8575 @ spec query_rows ( Sqlitex . connection , String . t | charlist ) :: { :ok , % { } } | Sqlitex . sqlite_error
86- @ spec query_rows ( Sqlitex . connection , String . t | charlist , [ bind: [ ] , timeout : integer ( ) ] ) :: { :ok , % { } } | Sqlitex . sqlite_error
76+ @ spec query_rows ( Sqlitex . connection , String . t | charlist , [ bind: [ ] , db_timeout : integer ( ) ] ) :: { :ok , % { } } | Sqlitex . sqlite_error
8777 def query_rows ( db , sql , opts \\ [ ] ) do
88- do_query_rows ( db , sql , opts , Keyword . get ( opts , :timeout , nil ) )
78+ with { :ok , stmt } <- Statement . prepare ( db , sql , opts ) ,
79+ { :ok , stmt } <- Statement . bind_values ( stmt , Keyword . get ( opts , :bind , [ ] ) , opts ) ,
80+ { :ok , rows } <- Statement . fetch_all ( stmt , :raw_list ) ,
81+ do: { :ok , % { rows: rows , columns: stmt . column_names , types: stmt . column_types } }
8982 end
9083
9184 @ doc """
@@ -94,24 +87,11 @@ defmodule Sqlitex.Query do
9487 Returns the results otherwise.
9588 """
9689 @ spec query_rows! ( Sqlitex . connection , String . t | charlist ) :: % { }
97- @ spec query_rows! ( Sqlitex . connection , String . t | charlist , [ bind: [ ] , timeout : integer ( ) ] ) :: % { }
90+ @ spec query_rows! ( Sqlitex . connection , String . t | charlist , [ bind: [ ] , db_timeout : integer ( ) ] ) :: % { }
9891 def query_rows! ( db , sql , opts \\ [ ] ) do
9992 case query_rows ( db , sql , opts ) do
10093 { :error , reason } -> raise Sqlitex.QueryError , reason: reason
10194 { :ok , results } -> results
10295 end
10396 end
104-
105- defp do_query_rows ( db , sql , opts , nil ) do
106- with { :ok , stmt } <- Statement . prepare ( db , sql ) ,
107- { :ok , stmt } <- Statement . bind_values ( stmt , Keyword . get ( opts , :bind , [ ] ) ) ,
108- { :ok , rows } <- Statement . fetch_all ( stmt , :raw_list ) ,
109- do: { :ok , % { rows: rows , columns: stmt . column_names , types: stmt . column_types } }
110- end
111- defp do_query_rows ( db , sql , opts , timeout ) do
112- with { :ok , stmt } <- Statement . prepare ( db , sql , timeout ) ,
113- { :ok , stmt } <- Statement . bind_values ( stmt , Keyword . get ( opts , :bind , [ ] ) , timeout ) ,
114- { :ok , rows } <- Statement . fetch_all ( stmt , :raw_list ) ,
115- do: { :ok , % { rows: rows , columns: stmt . column_names , types: stmt . column_types } }
116- end
11797end
0 commit comments