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

Commit 0d0fae6

Browse files
committed
Improve documentation for prepared statement cache, per code review suggestion. (Good idea!)
1 parent 6924417 commit 0d0fae6

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

lib/sqlitex/server.ex

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ defmodule Sqlitex.Server do
1919
{:ok, %{rows: [[1, 1], [2, 2]], columns: [:a, :b], types: [:INTEGER, :INTEGER]}}
2020
iex> Sqlitex.Server.prepare(:example, "SELECT * FROM t")
2121
{:ok, %{columns: [:a, :b], types: [:INTEGER, :INTEGER]}}
22+
# Subsequent queries using this exact statement will now operate more efficiently
23+
# because this statement has been cached.
24+
iex> Sqlitex.Server.prepare(:example, "INVALID SQL")
25+
{:error, {:sqlite_error, 'near "INVALID": syntax error'}}
2226
iex> Sqlitex.Server.stop(:example)
2327
:ok
2428
iex> :timer.sleep(10) # wait for the process to exit asynchronously
@@ -44,6 +48,13 @@ defmodule Sqlitex.Server do
4448
alias Sqlitex.Statement
4549
alias Sqlitex.Server.StatementCache, as: Cache
4650

51+
@doc """
52+
Starts a SQLite Server (GenServer) instance.
53+
54+
In addition to the options that are typically provided to `GenServer.start_link/3`,
55+
you can also specify `stmt_cache_size: (positive_integer)` to override the default
56+
limit (20) of statements that are cached when calling `prepare/3`.
57+
"""
4758
def start_link(db_path, opts \\ []) do
4859
stmt_cache_size = Keyword.get(opts, :stmt_cache_size, 20)
4960
GenServer.start_link(__MODULE__, {db_path, stmt_cache_size}, opts)
@@ -114,6 +125,24 @@ defmodule Sqlitex.Server do
114125
GenServer.call(pid, {:query_rows, sql, opts}, timeout(opts))
115126
end
116127

128+
@doc """
129+
Prepares a SQL statement for future use.
130+
131+
This causes a call to [`sqlite3_prepare_v2`](https://sqlite.org/c3ref/prepare.html)
132+
to be executed in the Server process. To protect the reference to the corresponding
133+
[`sqlite3_stmt` struct](https://sqlite.org/c3ref/stmt.html) from misuse in other
134+
processes, that reference is not passed back. Instead, prepared statements are
135+
cached in the Server process. If a subsequent call to `query/3` or `query_rows/3`
136+
is made with a matching SQL statement, the prepared statement is reused.
137+
138+
Prepared statements are purged from the cache when the cache exceeds a pre-set
139+
limit (20 statements by default).
140+
141+
Returns summary information about the prepared statement
142+
`{:ok, %{columns: [:column1_name, :column2_name,... ], types: [:column1_type, ...]}}`
143+
on success or `{:error, {:reason_code, 'SQLite message'}}` if the statement
144+
could not be prepared.
145+
"""
117146
def prepare(pid, sql, opts \\ []) do
118147
GenServer.call(pid, {:prepare, sql}, timeout(opts))
119148
end

0 commit comments

Comments
 (0)