Skip to main content

src/gdo@driver.erl

-module(gdo@driver).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gdo/driver.gleam").
-export([name/1, capabilities/1, connection_target_name/1, identifier/1, supports/2, connect/2, prepare/3, close/2, exec/3, query_all/3, 'begin'/2, commit/2, rollback/2, last_insert_id/2]).
-export_type([capability/0, driver/0, authentication/0, transport_security/0, network_endpoint/0, connection_target/0, driver_connection_state/0, driver_statement_state/0, driver_contract/0]).

-type capability() :: supports_transactions |
    supports_last_insert_id |
    supports_positional_parameters |
    supports_named_parameters |
    supports_embedded_connections |
    supports_network_connections |
    supports_authentication |
    supports_tls_configuration.

-type driver() :: sqlite.

-type authentication() :: no_authentication |
    {username_and_password, binary(), binary()}.

-type transport_security() :: disable_tls | prefer_tls | require_tls.

-type network_endpoint() :: {network_endpoint,
        binary(),
        integer(),
        binary(),
        authentication(),
        transport_security()}.

-type connection_target() :: {embedded_database, binary()} |
    {server_database, network_endpoint()}.

-type driver_connection_state() :: {sqlite_connection_state,
        binary(),
        sqlight:connection(),
        gleam@option:option(integer())}.

-type driver_statement_state() :: {sqlite_statement_state,
        sqlight:connection(),
        binary()}.

-type driver_contract() :: {driver_contract,
        fun((connection_target()) -> {ok, driver_connection_state()} |
            {error, gdo@error:error()}),
        fun((driver_connection_state()) -> {ok, nil} |
            {error, gdo@error:error()}),
        fun((driver_connection_state(), binary()) -> {ok,
                driver_statement_state()} |
            {error, gdo@error:error()}),
        fun((driver_statement_state(), list(gdo@value:param())) -> {ok,
                gdo@result:execution_result()} |
            {error, gdo@error:error()}),
        fun((driver_statement_state(), list(gdo@value:param())) -> {ok,
                gdo@result:query_result()} |
            {error, gdo@error:error()}),
        fun((driver_connection_state()) -> {ok, driver_connection_state()} |
            {error, gdo@error:error()}),
        fun((driver_connection_state()) -> {ok, driver_connection_state()} |
            {error, gdo@error:error()}),
        fun((driver_connection_state()) -> {ok, driver_connection_state()} |
            {error, gdo@error:error()}),
        fun((driver_connection_state()) -> gleam@option:option(integer()))}.

-file("src/gdo/driver.gleam", 78).
-spec name(driver()) -> binary().
name(Driver) ->
    case Driver of
        sqlite ->
            <<"sqlite"/utf8>>
    end.

-file("src/gdo/driver.gleam", 84).
-spec capabilities(driver()) -> list(capability()).
capabilities(Driver) ->
    case Driver of
        sqlite ->
            [supports_transactions,
                supports_last_insert_id,
                supports_positional_parameters,
                supports_named_parameters,
                supports_embedded_connections]
    end.

-file("src/gdo/driver.gleam", 96).
-spec connection_target_name(connection_target()) -> binary().
connection_target_name(Target) ->
    case Target of
        {embedded_database, _} ->
            <<"embedded"/utf8>>;

        {server_database, _} ->
            <<"server"/utf8>>
    end.

-file("src/gdo/driver.gleam", 103).
-spec identifier(connection_target()) -> binary().
identifier(Target) ->
    case Target of
        {embedded_database, Path} ->
            Path;

        {server_database, {network_endpoint, _, _, Database, _, _}} ->
            Database
    end.

-file("src/gdo/driver.gleam", 110).
-spec supports(driver(), capability()) -> boolean().
supports(Driver, Capability) ->
    gleam@list:any(
        capabilities(Driver),
        fun(Current) -> Current =:= Capability end
    ).

-file("src/gdo/driver.gleam", 114).
-spec connect(driver_contract(), connection_target()) -> {ok,
        driver_connection_state()} |
    {error, gdo@error:error()}.
connect(Contract, Target) ->
    {driver_contract, Connect, _, _, _, _, _, _, _, _} = Contract,
    Connect(Target).

-file("src/gdo/driver.gleam", 122).
-spec prepare(driver_contract(), driver_connection_state(), binary()) -> {ok,
        driver_statement_state()} |
    {error, gdo@error:error()}.
prepare(Contract, Connection_state, Sql) ->
    {driver_contract, _, _, Prepare, _, _, _, _, _, _} = Contract,
    Prepare(Connection_state, Sql).

-file("src/gdo/driver.gleam", 131).
-spec close(driver_contract(), driver_connection_state()) -> {ok, nil} |
    {error, gdo@error:error()}.
close(Contract, Connection_state) ->
    {driver_contract, _, Close, _, _, _, _, _, _, _} = Contract,
    Close(Connection_state).

-file("src/gdo/driver.gleam", 139).
-spec exec(driver_contract(), driver_statement_state(), list(gdo@value:param())) -> {ok,
        gdo@result:execution_result()} |
    {error, gdo@error:error()}.
exec(Contract, Statement_state, Params) ->
    {driver_contract, _, _, _, Exec, _, _, _, _, _} = Contract,
    Exec(Statement_state, Params).

-file("src/gdo/driver.gleam", 148).
-spec query_all(
    driver_contract(),
    driver_statement_state(),
    list(gdo@value:param())
) -> {ok, gdo@result:query_result()} | {error, gdo@error:error()}.
query_all(Contract, Statement_state, Params) ->
    {driver_contract, _, _, _, _, Query_all, _, _, _, _} = Contract,
    Query_all(Statement_state, Params).

-file("src/gdo/driver.gleam", 157).
-spec 'begin'(driver_contract(), driver_connection_state()) -> {ok,
        driver_connection_state()} |
    {error, gdo@error:error()}.
'begin'(Contract, Connection_state) ->
    {driver_contract, _, _, _, _, _, Begin, _, _, _} = Contract,
    Begin(Connection_state).

-file("src/gdo/driver.gleam", 165).
-spec commit(driver_contract(), driver_connection_state()) -> {ok,
        driver_connection_state()} |
    {error, gdo@error:error()}.
commit(Contract, Connection_state) ->
    {driver_contract, _, _, _, _, _, _, Commit, _, _} = Contract,
    Commit(Connection_state).

-file("src/gdo/driver.gleam", 173).
-spec rollback(driver_contract(), driver_connection_state()) -> {ok,
        driver_connection_state()} |
    {error, gdo@error:error()}.
rollback(Contract, Connection_state) ->
    {driver_contract, _, _, _, _, _, _, _, Rollback, _} = Contract,
    Rollback(Connection_state).

-file("src/gdo/driver.gleam", 181).
-spec last_insert_id(driver_contract(), driver_connection_state()) -> gleam@option:option(integer()).
last_insert_id(Contract, Connection_state) ->
    {driver_contract, _, _, _, _, _, _, _, _, Last_insert_id} = Contract,
    Last_insert_id(Connection_state).