Skip to content

Accept a String or charstring for database open #290

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
Sep 13, 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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: CI
on:
push:
branches:
- "*"
- "main"
pull_request:
branches:
- "*"
Expand Down
10 changes: 5 additions & 5 deletions c_src/sqlite3_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <sqlite3.h>

#define MAX_ATOM_LENGTH 255
#define MAX_PATHNAME 512

static ErlNifResourceType* connection_type = NULL;
static ErlNifResourceType* statement_type = NULL;
Expand Down Expand Up @@ -197,23 +196,24 @@ exqlite_open(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
connection_t* conn = NULL;
sqlite3* db = NULL;
ErlNifMutex* mutex = NULL;
char filename[MAX_PATHNAME];
ERL_NIF_TERM result;
ErlNifBinary bin;

ERL_NIF_TERM eos = enif_make_int(env, 0);

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

size = enif_get_string(env, argv[0], filename, MAX_PATHNAME, ERL_NIF_LATIN1);
if (size <= 0) {
if (!enif_inspect_iolist_as_binary(env, enif_make_list2(env, argv[0], eos), &bin)) {
return make_error_tuple(env, "invalid_filename");
}

if (!enif_get_int(env, argv[1], &flags)) {
return make_error_tuple(env, "invalid_flags");
}

rc = sqlite3_open_v2(filename, &db, flags, NULL);
rc = sqlite3_open_v2((char*)bin.data, &db, flags, NULL);
if (rc != SQLITE_OK) {
return make_error_tuple(env, "database_open_failed");
}
Expand Down
6 changes: 4 additions & 2 deletions lib/exqlite/sqlite3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ defmodule Exqlite.Sqlite3 do
the database if it doesn't already exist. Defaults to `:readwrite`.
Note: [:readwrite, :nomutex] is not recommended.
"""
@spec open(String.t(), [open_opt()]) :: {:ok, db()} | {:error, reason()}
@spec open(String.t() | String.Chars.t(), [open_opt()]) ::
{:ok, db()}
| {:error, reason()}
def open(path, opts \\ []) do
mode = Keyword.get(opts, :mode, :readwrite)
Sqlite3NIF.open(String.to_charlist(path), flags_from_mode(mode))
Sqlite3NIF.open(path, flags_from_mode(mode))
end

defp flags_from_mode(:nomutex) do
Expand Down
10 changes: 10 additions & 0 deletions test/exqlite/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@
File.rm(path)
end

test "connects to a file with an accented character" do
path = Temp.path!(prefix: "databasé")
{:ok, state} = Connection.connect(database: path)

assert state.path == path
assert state.db

File.rm(path)
end

test "connects to a file from URL" do
path = Temp.path!()

Expand Down Expand Up @@ -324,7 +334,7 @@
assert query.statement
end

test "users table does not exist" do

Check failure on line 337 in test/exqlite/connection_test.exs

View workflow job for this annotation

GitHub Actions / Test Elixir 1.16, OTP 26, OS windows-2019

test .handle_prepare/3 users table does not exist (Exqlite.ConnectionTest)
{:ok, conn} = Connection.connect(database: :memory)

{:error, error, _state} =
Expand Down
Loading