Current section

Files

Jump to
gmysql src gmysql.erl
Raw

src/gmysql.erl

-module(gmysql).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([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, 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 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 connect(list(connection_option())) -> {ok, connection()} |
{error, gleam@dynamic:dynamic_()}.
connect(Options) ->
gmysql_ffi:connect(Options).
-spec with_connection(list(connection_option()), fun((connection()) -> FQZ)) -> {ok,
FQZ} |
{error, gleam@dynamic:dynamic_()}.
with_connection(Options, Function) ->
gmysql_ffi:with_connection(Options, 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, FRJ} |
{error, list(gleam@dynamic:decode_error())})
) -> {ok, FRJ} | {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, FRO} | {error, FRP})
) -> {ok, FRO} | {error, transaction_error(FRP)}.
with_transaction(Connection, Retries, Function) ->
gmysql_ffi:with_transaction(Connection, Retries, Function).
-spec close(connection()) -> nil.
close(Connection) ->
gmysql_ffi:close(Connection).