Skip to content

Add Exqlite.Sqlite3.interrupt/1. #282

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 3 commits into from
Apr 3, 2024
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
29 changes: 29 additions & 0 deletions c_src/sqlite3_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,34 @@ exqlite_set_log_hook(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
return make_atom(env, "ok");
}

///
/// @brief Interrupt a long-running query.
///
static ERL_NIF_TERM
exqlite_interrupt(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
assert(env);

connection_t* conn = NULL;

if (argc != 1) {
return enif_make_badarg(env);
}

if (!enif_get_resource(env, argv[0], connection_type, (void**)&conn)) {
return make_error_tuple(env, "invalid_connection");
}

// DB is already closed, nothing to do here
if (conn->db == NULL) {
return make_atom(env, "ok");
}

sqlite3_interrupt(conn->db);

return make_atom(env, "ok");
}

//
// Most of our nif functions are going to be IO bounded
//
Expand All @@ -1151,6 +1179,7 @@ static ErlNifFunc nif_funcs[] = {
{"enable_load_extension", 2, exqlite_enable_load_extension, ERL_NIF_DIRTY_JOB_IO_BOUND},
{"set_update_hook", 2, exqlite_set_update_hook, ERL_NIF_DIRTY_JOB_IO_BOUND},
{"set_log_hook", 1, exqlite_set_log_hook, ERL_NIF_DIRTY_JOB_IO_BOUND},
{"interrupt", 1, exqlite_interrupt, ERL_NIF_DIRTY_JOB_IO_BOUND},
};

ERL_NIF_INIT(Elixir.Exqlite.Sqlite3NIF, nif_funcs, on_load, NULL, NULL, on_unload)
7 changes: 7 additions & 0 deletions lib/exqlite/sqlite3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ defmodule Exqlite.Sqlite3 do
def close(nil), do: :ok
def close(conn), do: Sqlite3NIF.close(conn)

@doc """
Interrupt a long-running query.
"""
@spec interrupt(db() | nil) :: :ok | {:error, reason()}
def interrupt(nil), do: :ok
def interrupt(conn), do: Sqlite3NIF.interrupt(conn)

@doc """
Executes an sql script. Multiple stanzas can be passed at once.
"""
Expand Down
3 changes: 3 additions & 0 deletions lib/exqlite/sqlite3_nif.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ defmodule Exqlite.Sqlite3NIF do
@spec close(db()) :: :ok | {:error, reason()}
def close(_conn), do: :erlang.nif_error(:not_loaded)

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

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

Expand Down
30 changes: 30 additions & 0 deletions test/exqlite/sqlite3_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -547,4 +547,34 @@ defmodule Exqlite.Sqlite3Test do
refute_receive _anything_else
end
end

describe ".interrupt/1" do
test "double interrupting a connection" do
{:ok, conn} = Sqlite3.open(":memory:")

:ok = Sqlite3.interrupt(conn)
:ok = Sqlite3.interrupt(conn)
end

test "interrupting a nil connection" do
:ok = Sqlite3.interrupt(nil)
end

test "interrupting a long running query and able to close a connection" do
{:ok, conn} = Sqlite3.open(":memory:")

spawn(fn ->
:ok =
Sqlite3.execute(
conn,
"WITH RECURSIVE r(i) AS ( VALUES(0) UNION ALL SELECT i FROM r LIMIT 1000000000 ) SELECT i FROM r WHERE i = 1;"
)
end)

Process.sleep(100)
:ok = Sqlite3.interrupt(conn)

:ok = Sqlite3.close(conn)
end
end
end