Current section
Files
Jump to
Current section
Files
src/gmysql.erl
-module(gmysql).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([default_config/0, connect/1, with_connection/2, exec/3, to_param/1, 'query'/5, with_transaction/3, close/1]).
-export_type([connection/0, connection_mode/0, connection_option/0, config/0, error/0, param/0, transaction_error/1]).
-type connection() :: any().
-type connection_mode() :: synchronous | asynchronous | lazy.
-type connection_option() :: {host, binary()} |
{port, integer()} |
{user, binary()} |
{password, binary()} |
{database, binary()} |
{connect_mode, connection_mode()} |
{connect_timeout, integer()} |
{keep_alive, integer()}.
-type config() :: {config,
binary(),
integer(),
gleam@option:option(binary()),
gleam@option:option(binary()),
binary(),
connection_mode(),
integer(),
integer()}.
-type error() :: {server_error, integer(), bitstring()} |
{unknown_error, gleam@dynamic:dynamic_()} |
{decode_error, list(gleam@dynamic:decode_error())}.
-type param() :: any().
-type transaction_error(FQU) :: {function_error, FQU} |
{other_error, gleam@dynamic:dynamic_()}.
-spec default_config() -> config().
default_config() ->
{config,
<<"localhost"/utf8>>,
3306,
none,
none,
<<"mysql"/utf8>>,
asynchronous,
1000,
1000}.
-spec config_to_connection_options(config()) -> list(connection_option()).
config_to_connection_options(Config) ->
_pipe = [{some, {host, erlang:element(2, Config)}},
{some, {port, erlang:element(3, Config)}},
gleam@option:map(
erlang:element(4, Config),
fun(Field@0) -> {user, Field@0} end
),
gleam@option:map(
erlang:element(5, Config),
fun(Field@0) -> {password, Field@0} end
),
{some, {database, erlang:element(6, Config)}},
{some, {connect_mode, erlang:element(7, Config)}},
{some, {connect_timeout, erlang:element(8, Config)}},
{some, {keep_alive, erlang:element(9, Config)}}],
gleam@option:values(_pipe).
-spec connect(config()) -> {ok, connection()} |
{error, gleam@dynamic:dynamic_()}.
connect(Config) ->
_pipe = config_to_connection_options(Config),
gmysql_ffi:connect(_pipe).
-spec with_connection(config(), fun((connection()) -> FRE)) -> {ok, FRE} |
{error, gleam@dynamic:dynamic_()}.
with_connection(Config, Function) ->
_pipe = config_to_connection_options(Config),
gmysql_ffi:with_connection(_pipe, Function).
-spec exec(connection(), binary(), integer()) -> {ok, nil} | {error, error()}.
exec(Connection, Query, Timeout) ->
gmysql_ffi:exec(Connection, Query, Timeout).
-spec to_param(any()) -> param().
to_param(Param) ->
gmysql_ffi:to_param(Param).
-spec 'query'(
connection(),
binary(),
list(param()),
integer(),
fun((gleam@dynamic:dynamic_()) -> {ok, FRO} |
{error, list(gleam@dynamic:decode_error())})
) -> {ok, FRO} | {error, error()}.
'query'(Connection, Query, Params, Timeout, Decoder) ->
case gmysql_ffi:'query'(Connection, Query, Params, Timeout) of
{error, Int} ->
{error, Int};
{ok, Dyn} ->
case Decoder(Dyn) of
{ok, Decoded} ->
{ok, Decoded};
{error, Decode_errors} ->
{error, {decode_error, Decode_errors}}
end
end.
-spec with_transaction(
connection(),
integer(),
fun((connection()) -> {ok, FRT} | {error, FRU})
) -> {ok, FRT} | {error, transaction_error(FRU)}.
with_transaction(Connection, Retries, Function) ->
gmysql_ffi:with_transaction(Connection, Retries, Function).
-spec close(connection()) -> nil.
close(Connection) ->
gmysql_ffi:close(Connection).