Skip to content

Commit

Permalink
Don't translate string to charlist before sending to NIF (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
warmwaffles authored Dec 3, 2021
1 parent 2c3138a commit cd22706
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
4 changes: 0 additions & 4 deletions c_src/sqlite3_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,6 @@ exqlite_execute(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
return make_error_tuple(env, "sql_not_iolist");
}

enif_inspect_iolist_as_binary(env,
enif_make_list2(env, argv[1], eos),
&bin);

rc = sqlite3_exec(conn->db, (char*)bin.data, NULL, NULL, NULL);
if (rc != SQLITE_OK) {
return make_sqlite3_error_tuple(env, rc, conn->db);
Expand Down
2 changes: 1 addition & 1 deletion lib/exqlite/sqlite3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ defmodule Exqlite.Sqlite3 do
"""
@spec execute(db(), String.t()) :: :ok | {:error, reason()}
def execute(conn, sql) do
case Sqlite3NIF.execute(conn, String.to_charlist(sql)) do
case Sqlite3NIF.execute(conn, sql) do
:ok -> :ok
{:error, reason} -> {:error, reason}
_ -> {:error, "unhandled error"}
Expand Down
12 changes: 12 additions & 0 deletions test/exqlite/sqlite3_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ defmodule Exqlite.Sqlite3Test do
:ok =
Sqlite3.execute(conn, "insert into things(content) VALUES ('this is content')")
end

test "handles unicode characters" do
{:ok, conn} = Sqlite3.open(":memory:")

:ok =
Exqlite.Sqlite3.execute(
conn,
"create table test (id integer primary key, stuff text)"
)

:ok = Exqlite.Sqlite3.execute(conn, "insert into test (stuff) values ('😝')")
end
end

describe ".prepare/3" do
Expand Down

0 comments on commit cd22706

Please sign in to comment.