Skip to content
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

fix: binary type options #65

Merged
merged 4 commits into from
Apr 18, 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
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