Skip to content

Commit d39a700

Browse files
authored
Accept a String or charstring for database open (#290)
* Only run ci on push to main and all PRs * Accept a String or charstring for database open Closes #278 Closes #277
1 parent 34332ea commit d39a700

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: CI
33
on:
44
push:
55
branches:
6-
- "*"
6+
- "main"
77
pull_request:
88
branches:
99
- "*"

c_src/sqlite3_nif.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <sqlite3.h>
1212

1313
#define MAX_ATOM_LENGTH 255
14-
#define MAX_PATHNAME 512
1514

1615
static ErlNifResourceType* connection_type = NULL;
1716
static ErlNifResourceType* statement_type = NULL;
@@ -197,23 +196,24 @@ exqlite_open(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
197196
connection_t* conn = NULL;
198197
sqlite3* db = NULL;
199198
ErlNifMutex* mutex = NULL;
200-
char filename[MAX_PATHNAME];
201199
ERL_NIF_TERM result;
200+
ErlNifBinary bin;
201+
202+
ERL_NIF_TERM eos = enif_make_int(env, 0);
202203

203204
if (argc != 2) {
204205
return enif_make_badarg(env);
205206
}
206207

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

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

216-
rc = sqlite3_open_v2(filename, &db, flags, NULL);
216+
rc = sqlite3_open_v2((char*)bin.data, &db, flags, NULL);
217217
if (rc != SQLITE_OK) {
218218
return make_error_tuple(env, "database_open_failed");
219219
}

lib/exqlite/sqlite3.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ defmodule Exqlite.Sqlite3 do
3535
the database if it doesn't already exist. Defaults to `:readwrite`.
3636
Note: [:readwrite, :nomutex] is not recommended.
3737
"""
38-
@spec open(String.t(), [open_opt()]) :: {:ok, db()} | {:error, reason()}
38+
@spec open(String.t() | String.Chars.t(), [open_opt()]) ::
39+
{:ok, db()}
40+
| {:error, reason()}
3941
def open(path, opts \\ []) do
4042
mode = Keyword.get(opts, :mode, :readwrite)
41-
Sqlite3NIF.open(String.to_charlist(path), flags_from_mode(mode))
43+
Sqlite3NIF.open(path, flags_from_mode(mode))
4244
end
4345

4446
defp flags_from_mode(:nomutex) do

test/exqlite/connection_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ defmodule Exqlite.ConnectionTest do
3737
File.rm(path)
3838
end
3939

40+
test "connects to a file with an accented character" do
41+
path = Temp.path!(prefix: "databasé")
42+
{:ok, state} = Connection.connect(database: path)
43+
44+
assert state.path == path
45+
assert state.db
46+
47+
File.rm(path)
48+
end
49+
4050
test "connects to a file from URL" do
4151
path = Temp.path!()
4252

0 commit comments

Comments
 (0)