Skip to content

Commit 455a8be

Browse files
authored
Adds ability to set more limits for sqlite (#189)
- `:journal_size_limit` - `:soft_heap_limit` - `:hard_heap_limit`
1 parent 427c1da commit 455a8be

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

lib/exqlite/connection.ex

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ defmodule Exqlite.Connection do
9292
* `:chunk_size` - The chunk size for bulk fetching. Defaults to `50`.
9393
* `:key` - Optional key to set during database initialization. This PRAGMA
9494
is often used to set up database level encryption.
95+
* `:journal_size_limit` - The size limit in bytes of the journal.
96+
* `:soft_heap_limit` - The size limit in bytes for the heap limit.
97+
* `:hard_heap_limit` - The size limit in bytes for the heap.
9598
9699
For more information about the options above, see [sqlite documenation][1]
97100
@@ -351,6 +354,25 @@ defmodule Exqlite.Connection do
351354
end
352355
end
353356

357+
defp set_pragma_if_present(_db, _pragma, nil), do: :ok
358+
defp set_pragma_if_present(db, pragma, value), do: set_pragma(db, pragma, value)
359+
360+
defp set_journal_size_limit(db, options) do
361+
set_pragma_if_present(
362+
db,
363+
"journal_size_limit",
364+
Keyword.get(options, :journal_size_limit)
365+
)
366+
end
367+
368+
defp set_soft_heap_limit(db, options) do
369+
set_pragma_if_present(db, "soft_heap_limit", Keyword.get(options, :soft_heap_limit))
370+
end
371+
372+
defp set_hard_heap_limit(db, options) do
373+
set_pragma_if_present(db, "hard_heap_limit", Keyword.get(options, :hard_heap_limit))
374+
end
375+
354376
defp set_journal_mode(db, options) do
355377
maybe_set_pragma(db, "journal_mode", Pragma.journal_mode(options))
356378
end
@@ -414,7 +436,10 @@ defmodule Exqlite.Connection do
414436
:ok <- set_secure_delete(db, options),
415437
:ok <- set_wal_auto_check_point(db, options),
416438
:ok <- set_case_sensitive_like(db, options),
417-
:ok <- set_busy_timeout(db, options) do
439+
:ok <- set_busy_timeout(db, options),
440+
:ok <- set_journal_size_limit(db, options),
441+
:ok <- set_soft_heap_limit(db, options),
442+
:ok <- set_hard_heap_limit(db, options) do
418443
state = %__MODULE__{
419444
db: db,
420445
path: path,

test/exqlite/connection_test.exs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,51 @@ defmodule Exqlite.ConnectionTest do
3636

3737
File.rm(path)
3838
end
39+
40+
test "setting journal_size_limit" do
41+
path = Temp.path!()
42+
size_limit = 20 * 1024 * 1024
43+
{:ok, state} = Connection.connect(database: path, journal_size_limit: size_limit)
44+
45+
assert state.db
46+
47+
assert {:ok, ^size_limit} = get_pragma(state.db, :journal_size_limit)
48+
49+
File.rm(path)
50+
end
51+
52+
test "setting soft_heap_limit" do
53+
path = Temp.path!()
54+
size_limit = 20 * 1024 * 1024
55+
{:ok, state} = Connection.connect(database: path, soft_heap_limit: size_limit)
56+
57+
assert state.db
58+
59+
assert {:ok, ^size_limit} = get_pragma(state.db, :soft_heap_limit)
60+
61+
File.rm(path)
62+
end
63+
64+
test "setting hard_heap_limit" do
65+
path = Temp.path!()
66+
size_limit = 20 * 1024 * 1024
67+
{:ok, state} = Connection.connect(database: path, hard_heap_limit: size_limit)
68+
69+
assert state.db
70+
71+
assert {:ok, ^size_limit} = get_pragma(state.db, :hard_heap_limit)
72+
73+
File.rm(path)
74+
end
75+
end
76+
77+
defp get_pragma(db, pragma_name) do
78+
{:ok, statement} = Sqlite3.prepare(db, "PRAGMA #{pragma_name}")
79+
80+
case Sqlite3.fetch_all(db, statement) do
81+
{:ok, [[value]]} -> {:ok, value}
82+
_ -> :error
83+
end
3984
end
4085

4186
describe ".disconnect/2" do

0 commit comments

Comments
 (0)