Skip to content

Correct typespecs #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/exqlite/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ defmodule Exqlite.Connection do
end
end

@spec maybe_changes(Sqlite3.db(), Query.t()) :: integer() | nil
defp maybe_changes(db, %Query{command: command})
when command in [:update, :insert, :delete] do
case Sqlite3.changes(db) do
Expand Down
19 changes: 9 additions & 10 deletions lib/exqlite/sqlite3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule Exqlite.Sqlite3 do
@type db() :: reference()
@type statement() :: reference()
@type reason() :: atom() | String.t()
@type row() :: []
@type row() :: list()

@doc """
Opens a new sqlite database at the Path provided.
Expand All @@ -46,7 +46,6 @@ defmodule Exqlite.Sqlite3 do
case Sqlite3NIF.execute(conn, String.to_charlist(sql)) do
:ok -> :ok
{:error, reason} -> {:error, reason}
_ -> {:error, "unhandled error"}
end
end

Expand All @@ -57,7 +56,7 @@ defmodule Exqlite.Sqlite3 do

See: https://sqlite.org/c3ref/changes.html
"""
@spec changes(db()) :: {:ok, integer()}
@spec changes(db()) :: {:ok, integer()} | {:error, reason()}
def changes(conn), do: Sqlite3NIF.changes(conn)

@spec prepare(db(), String.t()) :: {:ok, statement()} | {:error, reason()}
Expand All @@ -68,29 +67,29 @@ defmodule Exqlite.Sqlite3 do
@spec bind(db(), statement(), nil) :: :ok | {:error, reason()}
def bind(conn, statement, nil), do: bind(conn, statement, [])

@spec bind(db(), statement(), []) :: :ok | {:error, reason()}
@spec bind(db(), statement(), list()) :: :ok | {:error, reason()}
def bind(conn, statement, args) do
Sqlite3NIF.bind(conn, statement, Enum.map(args, &convert/1))
end

@spec columns(db(), statement()) :: {:ok, []} | {:error, reason()}
@spec columns(db(), statement()) :: {:ok, [binary()]} | {:error, reason()}
def columns(conn, statement), do: Sqlite3NIF.columns(conn, statement)

@spec step(db(), statement()) :: :done | :busy | {:row, []}
@spec step(db(), statement()) :: :done | :busy | {:row, [row()]} | {:error, reason()}
def step(conn, statement), do: Sqlite3NIF.step(conn, statement)

@spec multi_step(db(), statement()) :: :busy | {:rows, [row()]} | {:done, [row()]}
@spec multi_step(db(), statement()) :: :busy | {:rows, [row()]} | {:done, [row()]} | {:error, reason()}
def multi_step(conn, statement) do
chunk_size = Application.get_env(:exqlite, :default_chunk_size, 50)
multi_step(conn, statement, chunk_size)
end

@spec multi_step(db(), statement(), integer()) ::
:busy | {:rows, [row()]} | {:done, [row()]}
:busy | {:rows, [row()]} | {:done, [row()]} | {:error, reason()}
def multi_step(conn, statement, chunk_size) do
case Sqlite3NIF.multi_step(conn, statement, chunk_size) do
:busy ->
{:error, "Database busy"}
:busy

{:error, reason} ->
{:error, reason}
Expand Down Expand Up @@ -187,7 +186,7 @@ defmodule Exqlite.Sqlite3 do
@doc """
Allow loading native extensions.
"""
@spec enable_load_extension(db(), boolean) :: :ok | {:error, any}
@spec enable_load_extension(db(), boolean()) :: :ok | {:error, reason()}
def enable_load_extension(conn, flag) do
if flag do
Sqlite3NIF.enable_load_extension(conn, 1)
Expand Down
13 changes: 7 additions & 6 deletions lib/exqlite/sqlite3_nif.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Exqlite.Sqlite3NIF do
@type db() :: reference()
@type statement() :: reference()
@type reason() :: :atom | String.Chars.t()
@type row() :: list()

def load_nif() do
path = :filename.join(:code.priv_dir(:exqlite), 'sqlite3_nif')
Expand All @@ -25,24 +26,24 @@ defmodule Exqlite.Sqlite3NIF do
@spec execute(db(), String.Chars.t()) :: :ok | {:error, reason()}
def execute(_conn, _sql), do: :erlang.nif_error(:not_loaded)

@spec changes(db()) :: {:ok, integer()}
@spec changes(db()) :: {:ok, integer()} | {:error, reason()}
def changes(_conn), do: :erlang.nif_error(:not_loaded)

@spec prepare(db(), String.Chars.t()) :: {:ok, statement()} | {:error, reason()}
def prepare(_conn, _sql), do: :erlang.nif_error(:not_loaded)

@spec bind(db(), statement(), []) ::
@spec bind(db(), statement(), list()) ::
:ok | {:error, reason()} | {:error, {atom(), any()}}
def bind(_conn, _statement, _args), do: :erlang.nif_error(:not_loaded)

@spec step(db(), statement()) :: :done | :busy | {:row, []}
@spec step(db(), statement()) :: :done | :busy | {:row, [row()]} | {:error, reason()}
def step(_conn, _statement), do: :erlang.nif_error(:not_loaded)

@spec multi_step(db(), statement(), integer()) ::
:busy | {:rows, [[]]} | {:done, [[]]}
:busy | {:rows, [row()]} | {:done, [row()]} | {:error, reason()}
def multi_step(_conn, _statement, _chunk_size), do: :erlang.nif_error(:not_loaded)

@spec columns(db(), statement()) :: {:ok, []} | {:error, reason()}
@spec columns(db(), statement()) :: {:ok, list(binary())} | {:error, reason()}
def columns(_conn, _statement), do: :erlang.nif_error(:not_loaded)

@spec last_insert_rowid(db()) :: {:ok, integer()}
Expand All @@ -60,7 +61,7 @@ defmodule Exqlite.Sqlite3NIF do
@spec release(db(), statement()) :: :ok | {:error, reason()}
def release(_conn, _statement), do: :erlang.nif_error(:not_loaded)

@spec enable_load_extension(db(), boolean()) :: :ok | {:error, reason()}
@spec enable_load_extension(db(), integer()) :: :ok | {:error, reason()}
def enable_load_extension(_conn, _flag), do: :erlang.nif_error(:not_loaded)

# TODO: add statement inspection tooling https://sqlite.org/c3ref/expanded_sql.html
Expand Down