|
| 1 | +defmodule Sqlite do |
| 2 | + @moduledoc """ |
| 3 | + SQLite3 driver for Elixir. |
| 4 | + """ |
| 5 | + |
| 6 | + alias Sqlite.Query |
| 7 | + |
| 8 | + defmodule Connection do |
| 9 | + @moduledoc false |
| 10 | + defstruct [ |
| 11 | + :pid, |
| 12 | + :ref, |
| 13 | + :database |
| 14 | + ] |
| 15 | + |
| 16 | + @typedoc false |
| 17 | + @type t :: %__MODULE__{ |
| 18 | + pid: GenServer.server(), |
| 19 | + ref: reference, |
| 20 | + database: Esqlite3.filename() |
| 21 | + } |
| 22 | + |
| 23 | + defimpl Inspect, for: __MODULE__ do |
| 24 | + def inspect(%{ref: ref}, _opts) when is_reference(ref) do |
| 25 | + String.replace(inspect(ref), "Reference", "Sqlite3") |
| 26 | + end |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + @typedoc "Connection identifier for your Sqlite instance." |
| 31 | + @opaque conn :: Connection.t() |
| 32 | + |
| 33 | + @doc """ |
| 34 | + Start the connection process and connect to sqlite. |
| 35 | + ## Options |
| 36 | + * `:database` -> Databse uri. |
| 37 | + ## Examples |
| 38 | + iex> {:ok, pid} = Sqlite.open(database: "sqlite.db") |
| 39 | + {:ok, #PID<0.69.0>} |
| 40 | + iex> {:ok, pid} = Sqlite.open(database: ":memory:") |
| 41 | + {:ok, #PID<0.69.0>} |
| 42 | + """ |
| 43 | + @spec open(Keyword.t(), GenServer.options()) :: {:ok, conn} | {:error, term} |
| 44 | + def open(opts, gen_server_opts \\ []) when is_list(opts) do |
| 45 | + opts = Sqlite.Utils.default_opts(opts) |
| 46 | + |
| 47 | + case GenServer.start_link(Sqlite.Server, opts, gen_server_opts) do |
| 48 | + {:ok, pid} -> |
| 49 | + conn = |
| 50 | + struct( |
| 51 | + Connection, |
| 52 | + pid: pid, |
| 53 | + database: opts[:database], |
| 54 | + ref: make_ref() |
| 55 | + ) |
| 56 | + |
| 57 | + {:ok, conn} |
| 58 | + {:error, reason} -> {:error, reason} |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + @doc """ |
| 63 | + Runs an (extended) query and returns the result as `{:ok, %Sqlite.Result{}}` |
| 64 | + or `{:error, %Sqlite.Error{}}` if there was a database error. Parameters can |
| 65 | + be set in the query as `$1` embedded in the query string. Parameters are given |
| 66 | + as a list of elixir values. See the README for information on how Sqlite |
| 67 | + encodes and decodes Elixir values by default. See `Sqlite.Result` for the |
| 68 | + result data. |
| 69 | + ## Examples |
| 70 | + Sqlite.query(conn, "CREATE TABLE posts (id serial, title text)", []) |
| 71 | + Sqlite.query(conn, "INSERT INTO posts (title) VALUES ('my title')", []) |
| 72 | + Sqlite.query(conn, "SELECT title FROM posts", []) |
| 73 | + Sqlite.query(conn, "SELECT id FROM posts WHERE title like $1", ["%my%"]) |
| 74 | + Sqlite.query(conn, "COPY posts TO STDOUT", []) |
| 75 | + """ |
| 76 | + @spec query(conn, iodata, list, Keyword.t()) :: |
| 77 | + {:ok, Sqlite.Result.t()} | {:error, Sqlite.Error.t()} |
| 78 | + def query(conn, statement, params, opts \\ []) do |
| 79 | + query = %Query{name: "", statement: statement} |
| 80 | + opts = opts |> defaults() |
| 81 | + |
| 82 | + GenServer.call(conn.pid, {:query, query, params, opts}, call_timeout(opts)) |
| 83 | + |> case do |
| 84 | + {:ok, %Sqlite.Result{}} = ok -> ok |
| 85 | + {:error, %Sqlite.Error{}} = ok -> ok |
| 86 | + err -> unexpected_response(err, :query) |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + @doc """ |
| 91 | + Runs an (extended) query and returns the result or raises `Sqlite.Error` if |
| 92 | + there was an error. See `query/3`. |
| 93 | + """ |
| 94 | + @spec query!(conn, iodata, list, Keyword.t()) :: Sqlite.Result.t() |
| 95 | + def query!(conn, statement, params, opts \\ []) do |
| 96 | + case query(conn, statement, params, opts) do |
| 97 | + {:ok, result} -> result |
| 98 | + {:error, reason} -> raise Sqlite.Error, %{reason: reason} |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + @doc """ |
| 103 | + Prepares an (extended) query and returns the result as |
| 104 | + `{:ok, %Sqlite.Query{}}` or `{:error, %Sqlite.Error{}}` if there was an |
| 105 | + error. Parameters can be set in the query as `$1` embedded in the query |
| 106 | + string. To execute the query call `execute/4`. To close the prepared query |
| 107 | + call `close/3`. See `Sqlite.Query` for the query data. |
| 108 | +
|
| 109 | + ## Examples |
| 110 | + Sqlite.prepare(conn, "", "CREATE TABLE posts (id serial, title text)") |
| 111 | + """ |
| 112 | + @spec prepare(conn, iodata, iodata, Keyword.t()) :: |
| 113 | + {:ok, Sqlite.Query.t()} | {:error, Sqlite.Error.t()} |
| 114 | + def prepare(conn, name, statement, opts \\ []) do |
| 115 | + query = %Query{name: name, statement: statement} |
| 116 | + opts = opts |> defaults() |
| 117 | + |
| 118 | + GenServer.call(conn.pid, {:prepare, query, opts}, call_timeout(opts)) |
| 119 | + |> case do |
| 120 | + {:ok, %Sqlite.Query{}} = ok -> ok |
| 121 | + {:error, %Sqlite.Error{}} = ok -> ok |
| 122 | + err -> unexpected_response(err, :prepare) |
| 123 | + end |
| 124 | + end |
| 125 | + |
| 126 | + @doc """ |
| 127 | + Prepares an (extended) query and returns the prepared query or raises |
| 128 | + `Sqlite.Error` if there was an error. See `prepare/4`. |
| 129 | + """ |
| 130 | + @spec prepare!(conn, iodata, iodata, Keyword.t()) :: Sqlite.Query.t() |
| 131 | + def prepare!(conn, name, statement, opts \\ []) do |
| 132 | + case prepare(conn, name, statement, opts) do |
| 133 | + {:ok, result} -> result |
| 134 | + {:error, reason} -> raise Sqlite.Error, %{reason: reason} |
| 135 | + end |
| 136 | + end |
| 137 | + |
| 138 | + @doc """ |
| 139 | + Runs an (extended) prepared query and returns the result as |
| 140 | + `{:ok, %Sqlite.Result{}}` or `{:error, %Sqlite.Error{}}` if there was an |
| 141 | + error. Parameters are given as part of the prepared query, `%Sqlite.Query{}`. |
| 142 | + See the README for information on how Sqlite encodes and decodes Elixir |
| 143 | + values by default. See `Sqlite.Query` for the query data and |
| 144 | + `Sqlite.Result` for the result data. |
| 145 | +
|
| 146 | + ## Examples |
| 147 | + query = Sqlite.prepare!(conn, "", "CREATE TABLE posts (id serial, title text)") |
| 148 | + Sqlite.execute(conn, query, []) |
| 149 | + query = Sqlite.prepare!(conn, "", "SELECT id FROM posts WHERE title like $1") |
| 150 | + Sqlite.execute(conn, query, ["%my%"]) |
| 151 | + """ |
| 152 | + @spec execute(conn, Sqlite.Query.t(), list, Keyword.t()) :: |
| 153 | + {:ok, Sqlite.Result.t()} | {:error, Sqlite.Error.t()} |
| 154 | + def execute(conn, query, params, opts \\ []) do |
| 155 | + opts = defaults(opts) |
| 156 | + |
| 157 | + GenServer.call(conn.pid, {:execute, query, params, opts}, call_timeout(opts)) |
| 158 | + |> case do |
| 159 | + {:ok, %Sqlite.Result{}} = ok -> ok |
| 160 | + {:error, %Sqlite.Error{}} = ok -> ok |
| 161 | + err -> unexpected_response(err, :execute) |
| 162 | + end |
| 163 | + end |
| 164 | + |
| 165 | + @doc """ |
| 166 | + Runs an (extended) prepared query and returns the result or raises |
| 167 | + `Sqlite.Error` if there was an error. See `execute/4`. |
| 168 | + """ |
| 169 | + @spec execute!(conn, Sqlite.Query.t(), list, Keyword.t()) :: Sqlite.Result.t() |
| 170 | + def execute!(conn, query, params, opts \\ []) do |
| 171 | + case execute(conn, query, params, opts) do |
| 172 | + {:ok, result} -> result |
| 173 | + {:error, reason} -> raise Sqlite.Error, %{reason: reason} |
| 174 | + end |
| 175 | + end |
| 176 | + |
| 177 | + @doc """ |
| 178 | + Closes an (extended) prepared query and returns `:ok` or |
| 179 | + `{:error, %Sqlite.Error{}}` if there was an error. Closing a query releases |
| 180 | + any resources held by sqlite3 for a prepared query with that name. See |
| 181 | + `Sqlite.Query` for the query data. |
| 182 | +
|
| 183 | + ## Examples |
| 184 | + query = Sqlite.prepare!(conn, "", "CREATE TABLE posts (id serial, title text)") |
| 185 | + Sqlite.close(conn, query) |
| 186 | + """ |
| 187 | + @spec close(conn, Sqlite.Query.t(), Keyword.t()) :: :ok | {:error, Sqlite.Error.t()} |
| 188 | + |
| 189 | + def close(conn, query \\ nil, opts \\ []) |
| 190 | + def close(conn, query, opts) do |
| 191 | + opts = defaults(opts) |
| 192 | + |
| 193 | + do_close = fn(conn, opts) -> |
| 194 | + GenServer.call(conn.pid, {:close, opts}, call_timeout(opts)) |
| 195 | + |> case do |
| 196 | + :ok -> :ok |
| 197 | + {:error, %Sqlite.Error{}} = ok -> ok |
| 198 | + err -> unexpected_response(err, :close) |
| 199 | + end |
| 200 | + end |
| 201 | + |
| 202 | + if query do |
| 203 | + case execute(query, [], opts) do |
| 204 | + {:error, reason} -> {:error, reason} |
| 205 | + {:ok, _result} -> do_close.(conn, opts) |
| 206 | + end |
| 207 | + else |
| 208 | + do_close.(conn, opts) |
| 209 | + end |
| 210 | + end |
| 211 | + |
| 212 | + @doc """ |
| 213 | + Closes an (extended) prepared query and returns `:ok` or raises |
| 214 | + `Sqlite.Error` if there was an error. See `close/3`. |
| 215 | + """ |
| 216 | + @spec close!(conn, Sqlite.Query.t(), Keyword.t()) :: :ok |
| 217 | + def close!(conn, query \\ nil, opts \\ []) do |
| 218 | + case close(conn, query, opts) do |
| 219 | + :ok -> :ok |
| 220 | + {:error, reason} -> raise Sqlite.Error, %{reason: reason} |
| 221 | + end |
| 222 | + end |
| 223 | + |
| 224 | + @spec call_timeout(Keyword.t()) :: timeout |
| 225 | + defp call_timeout(opts) do |
| 226 | + case Keyword.fetch!(opts, :timeout) do |
| 227 | + number when is_integer(number) -> number + 100 |
| 228 | + other -> other |
| 229 | + end |
| 230 | + end |
| 231 | + |
| 232 | + @spec defaults(Keyword.t()) :: Keyword.t() |
| 233 | + defp defaults(opts) do |
| 234 | + defaults = [ |
| 235 | + timeout: Application.get_env(:esqlite, :default_timeout, 5000) |
| 236 | + ] |
| 237 | + |
| 238 | + Keyword.merge(defaults, opts) |
| 239 | + end |
| 240 | + |
| 241 | + defp unexpected_response(err, fun) do |
| 242 | + raise "Unexpected response to #{fun}: #{inspect(err)}" |
| 243 | + end |
| 244 | +end |
0 commit comments