Skip to content
This repository was archived by the owner on Mar 19, 2021. It is now read-only.

Commit 04717a4

Browse files
committed
use db_timeout as Application config name
Also adds the @SPEC's back in for the lower-arity version of functions
1 parent 10eeb21 commit 04717a4

File tree

6 files changed

+22
-22
lines changed

6 files changed

+22
-22
lines changed

lib/sqlitex.ex

+7-5
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,25 @@ defmodule Sqlitex do
3535
following in your `config.exs`:
3636
3737
```
38-
config :sqlitex,
39-
esqlite3_timeout: 10_000 # or other positive integer number of ms
38+
config :sqlitex, db_timeout: 10_000 # or other positive integer number of ms
4039
```
4140
"""
4241

4342
alias Sqlitex.Config
4443

44+
@spec close(connection) :: :ok
4545
@spec close(connection, Keyword.t) :: :ok
4646
def close(db, opts \\ []) do
47-
timeout = Keyword.get(opts, :db_timeout, Config.esqlite3_timeout())
47+
timeout = Keyword.get(opts, :db_timeout, Config.db_timeout())
4848
:esqlite3.close(db, timeout)
4949
end
5050

51+
@spec open(charlist | String.t) :: {:ok, connection} | {:error, {atom, charlist}}
5152
@spec open(charlist | String.t, Keyword.t) :: {:ok, connection} | {:error, {atom, charlist}}
5253
def open(path, opts \\ [])
5354
def open(path, opts) when is_binary(path), do: open(string_to_charlist(path), opts)
5455
def open(path, opts) do
55-
timeout = Keyword.get(opts, :db_timeout, Config.esqlite3_timeout())
56+
timeout = Keyword.get(opts, :db_timeout, Config.db_timeout())
5657
:esqlite3.open(path, timeout)
5758
end
5859

@@ -63,9 +64,10 @@ defmodule Sqlitex do
6364
res
6465
end
6566

67+
@spec exec(connection, string_or_charlist) :: :ok | sqlite_error
6668
@spec exec(connection, string_or_charlist, Keyword.t) :: :ok | sqlite_error
6769
def exec(db, sql, opts \\ []) do
68-
timeout = Keyword.get(opts, :db_timeout, Config.esqlite3_timeout())
70+
timeout = Keyword.get(opts, :db_timeout, Config.db_timeout())
6971
:esqlite3.exec(sql, db, timeout)
7072
end
7173

lib/sqlitex/config.ex

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
defmodule Sqlitex.Config do
22
@moduledoc false
33

4-
def esqlite3_timeout do
5-
Application.get_env(:sqlitex, :esqlite3_timeout, default_esqlite3_timeout())
4+
def db_timeout do
5+
Application.get_env(:sqlitex, :db_timeout, 5_000)
66
end
7-
8-
def default_esqlite3_timeout, do: 5_000
97
end

lib/sqlitex/query.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ defmodule Sqlitex.Query do
1616
to bind as a list.
1717
* `into` - The collection to put results into. This defaults to a list.
1818
* `db_timeout` - The timeout (in ms) to apply to each of the underlying SQLite operations. Defaults
19-
to 5000, or to `Application.get_env(:sqlitex, :esqlite3_timeout)` if set.
19+
to `Application.get_env(:sqlitex, :db_timeout)` or `5000`ms if not configured.
2020
2121
## Returns
2222
* [results...] on success
@@ -65,7 +65,7 @@ defmodule Sqlitex.Query do
6565
* `bind` - If your query has parameters in it, you should provide the options
6666
to bind as a list.
6767
* `db_timeout` - The timeout (in ms) to apply to each of the underlying SQLite operations. Defaults
68-
to 5000, or to `Application.get_env(:sqlitex, :esqlite3_timeout)` if set.
68+
to `Application.get_env(:sqlitex, :db_timeout)` or `5000`ms if not configured.
6969
7070
## Returns
7171
* {:ok, %{rows: [[1, 2], [2, 3]], columns: [:a, :b], types: [:INTEGER, :INTEGER]}} on success

lib/sqlitex/server.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ defmodule Sqlitex.Server do
5959
that are cached when calling `prepare/3`.
6060
- `db_timeout: (positive_integer)` to override `:esqlite3`'s default timeout of 5000 ms for
6161
interactions with the database. This can also be set in `config.exs` as
62-
`config :sqlitex, esqlite3_timeout: 5_000`.
62+
`config :sqlitex, db_timeout: 5_000`.
6363
"""
6464
def start_link(db_path, opts \\ []) do
6565
stmt_cache_size = Keyword.get(opts, :stmt_cache_size, 20)
66-
timeout = Keyword.get(opts, :db_timeout, Config.esqlite3_timeout())
66+
timeout = Keyword.get(opts, :db_timeout, Config.db_timeout())
6767
GenServer.start_link(__MODULE__, {db_path, stmt_cache_size, timeout}, opts)
6868
end
6969

lib/sqlitex/statement.ex

+6-6
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ defmodule Sqlitex.Statement do
8585
8686
Also accepts the following keyword options:
8787
88-
* `db_timeout` - The time in ms allowed for the statement to run. Defaults to 5000, or the :esqlite3_timeout value in Application env.
88+
* `db_timeout` - The time in ms allowed for the statement to run. Defaults to 5000, or the :db_timeout value in Application env.
8989
9090
## Returns
9191
9292
* `{:ok, statement}` on success
9393
* See `:esqlite3.prepare` for errors.
9494
"""
9595
def prepare(db, sql, opts \\ []) do
96-
timeout = Keyword.get(opts, :db_timeout, Config.esqlite3_timeout)
96+
timeout = Keyword.get(opts, :db_timeout, Config.db_timeout())
9797

9898
with {:ok, stmt} <- do_prepare(db, sql, timeout),
9999
{:ok, stmt} <- get_column_names(stmt, timeout),
@@ -124,7 +124,7 @@ defmodule Sqlitex.Statement do
124124
125125
Also accepts the following keyword options:
126126
127-
* `db_timeout` - The time in ms allowed for the statement to run. Defaults to 5000, or the :esqlite3_timeout value in Application env.
127+
* `db_timeout` - The time in ms allowed for the statement to run. Defaults to 5000, or the :db_timeout value in Application env.
128128
129129
## Returns
130130
@@ -142,7 +142,7 @@ defmodule Sqlitex.Statement do
142142
* `%Decimal` - Converted into a number.
143143
"""
144144
def bind_values(statement, values, opts \\ []) do
145-
timeout = Keyword.get(opts, :db_timeout, Config.esqlite3_timeout)
145+
timeout = Keyword.get(opts, :db_timeout, Config.db_timeout())
146146

147147
case :esqlite3.bind(statement.statement, translate_bindings(values), timeout) do
148148
{:error, _} = error -> error
@@ -215,15 +215,15 @@ defmodule Sqlitex.Statement do
215215
216216
Also accepts the following keyword options:
217217
218-
* `db_timeout` - The time in ms allowed for the statement to run. Defaults to 5000, or the :esqlite3_timeout value in Application env.
218+
* `db_timeout` - The time in ms allowed for the statement to run. Defaults to 5000, or the :db_timeout value in Application env.
219219
220220
## Returns
221221
222222
* `:ok`
223223
* `{:error, error}`
224224
"""
225225
def exec(statement, opts \\ []) do
226-
timeout = Keyword.get(opts, :db_timeout, Config.esqlite3_timeout)
226+
timeout = Keyword.get(opts, :db_timeout, Config.db_timeout())
227227

228228
case :esqlite3.step(statement.statement, timeout) do
229229
# esqlite3.step returns some odd values, so lets translate them:

test/server/statement_cache_test.exs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ defmodule Sqlitex.Server.StatementCacheTest do
1010
cache = S.new(db, 3)
1111
assert %S{cached_stmts: %{}, db: _, limit: 3, lru: [], size: 0} = cache
1212

13-
{cache, stmt1a} = S.prepare(cache, "SELECT 42", [db_timeout: 5_000])
13+
{cache, stmt1a} = S.prepare(cache, "SELECT 42")
1414
assert %Stmt{column_names: [:"42"], column_types: [nil]} = stmt1a
1515

1616
{cache, stmt2a} = S.prepare(cache, "SELECT 43", [db_timeout: 5_000])
1717
assert %Stmt{column_names: [:"43"], column_types: [nil]} = stmt2a
1818

19-
{cache, stmt3} = S.prepare(cache, "SELECT 44", [db_timeout: 5_000])
19+
{cache, stmt3} = S.prepare(cache, "SELECT 44")
2020
assert %Stmt{column_names: [:"44"], column_types: [nil]} = stmt3
2121

2222
{cache, stmt1b} = S.prepare(cache, "SELECT 42", [db_timeout: 5_000])
2323
assert stmt1a == stmt1b # shouldn't have been purged
2424

25-
{cache, stmt4} = S.prepare(cache, "SELECT 353", [db_timeout: 5_000])
25+
{cache, stmt4} = S.prepare(cache, "SELECT 353")
2626
assert %Stmt{column_names: [:"353"], column_types: [nil]} = stmt4
2727

2828
{_cache, stmt2b} = S.prepare(cache, "SELECT 42", [db_timeout: 5_000])

0 commit comments

Comments
 (0)