Skip to content

Commit

Permalink
fix: binary type options (#65)
Browse files Browse the repository at this point in the history
* fix: incorrect data when getting binary type options
  • Loading branch information
cocoa-xu authored Apr 18, 2024
1 parent 7a05043 commit 510e4a5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
15 changes: 10 additions & 5 deletions c_src/adbc_nif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1128,14 +1128,19 @@ static ERL_NIF_TERM adbc_set_option(ErlNifEnv *env, const ERL_NIF_TERM argv[], S
struct AdbcError adbc_error{};
AdbcStatusCode code;
if (type == "string" || type == "binary") {
std::string value;
if (!erlang::nif::get(env, argv[3], value)) {
return enif_make_badarg(env);
}
if (type == "string") {
std::string value;
if (!erlang::nif::get(env, argv[3], value)) {
return enif_make_badarg(env);
}
code = set_string(&resource->val, key.c_str(), value.c_str(), &adbc_error);
} else {
code = set_bytes(&resource->val, key.c_str(), (const uint8_t *)value.data(), value.length(), &adbc_error);
ErlNifBinary bin;
ERL_NIF_TERM ret = enif_inspect_binary(env, argv[3], &bin);
if (!ret) {
return enif_make_badarg(env);
}
code = set_bytes(&resource->val, key.c_str(), bin.data, bin.size, &adbc_error);
}
} else if (type == "integer") {
int64_t value;
Expand Down
20 changes: 10 additions & 10 deletions lib/adbc_database.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ defmodule Adbc.Database do
end

@doc """
Set option for the connection.
Set option for the database.
- If `value` is an atom or a string, then corresponding string option will be set.
- If `value` is a `{:binary, binary()}`-tuple, then corresponding binary option will be set.
Expand All @@ -105,22 +105,22 @@ defmodule Adbc.Database do
atom() | {:binary, binary()} | String.t() | number()
) ::
:ok | {:error, String.t()}
def set_option(conn, key, value)
def set_option(db, key, value)

def set_option(conn, key, value) when is_pid(conn) and (is_atom(value) or is_binary(value)) do
Adbc.Helper.option(conn, :adbc_database_set_option, [:string, key, value])
def set_option(db, key, value) when is_pid(db) and (is_atom(value) or is_binary(value)) do
Adbc.Helper.option(db, :adbc_database_set_option, [:string, key, value])
end

def set_option(conn, key, {:binary, value}) when is_pid(conn) and is_binary(value) do
Adbc.Helper.option(conn, :adbc_database_set_option, [:binary, key, value])
def set_option(db, key, {:binary, value}) when is_pid(db) and is_binary(value) do
Adbc.Helper.option(db, :adbc_database_set_option, [:binary, key, value])
end

def set_option(conn, key, value) when is_pid(conn) and is_integer(value) do
Adbc.Helper.option(conn, :adbc_database_set_option, [:integer, key, value])
def set_option(db, key, value) when is_pid(db) and is_integer(value) do
Adbc.Helper.option(db, :adbc_database_set_option, [:integer, key, value])
end

def set_option(conn, key, value) when is_pid(conn) and is_float(value) do
Adbc.Helper.option(conn, :adbc_database_set_option, [:float, key, value])
def set_option(db, key, value) when is_pid(db) and is_float(value) do
Adbc.Helper.option(db, :adbc_database_set_option, [:float, key, value])
end

defp driver_default_options(:duckdb), do: [entrypoint: "duckdb_adbc_init"]
Expand Down
3 changes: 3 additions & 0 deletions lib/adbc_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ defmodule Adbc.Helper do
:ok ->
:ok

{:ok, value} ->
{:ok, value}

{:error, reason} ->
{:error, error_to_exception(reason)}
end
Expand Down

0 comments on commit 510e4a5

Please sign in to comment.