src/libsql.erl

-module(libsql).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/libsql.gleam").
-export([error_code_to_int/1, error_code_from_int/1, open/1, open_remote/2, close/1, with_connection/2, with_remote_connection/3, open_replica/3, with_replica_connection/4, open_synced_database/3, with_synced_database/4, sync/1, replication_index/1, exec/2, exec_batch/3, 'begin'/1, commit/1, rollback/1, transaction/2, 'query'/4, query_named/4, query_first/4, query_first_named/4, query_one/4, query_one_named/4, changes/1, total_changes/1, last_insert_rowid/1, interrupt/1, prepare/2, exec_prepared/2, query_prepared/3, finalize/1, with_statement/3, null/0, nullable/2, int/1, float/1, text/1, blob/1, bool/1, decode_bool/0]).
-export_type([connection/0, statement/0, value/0, error/0, error_code/0, replicated/0]).

-type connection() :: any().

-type statement() :: any().

-type value() :: any().

-type error() :: {libsql_error, error_code(), binary(), integer()}.

-type error_code() :: abort |
    auth |
    busy |
    cantopen |
    constraint |
    corrupt |
    done |
    empty |
    generic_error |
    format |
    full |
    internal |
    interrupt |
    ioerr |
    locked |
    mismatch |
    misuse |
    nolfs |
    nomem |
    notadb |
    notfound |
    notice |
    generic_ok |
    perm |
    protocol |
    range |
    readonly |
    row |
    schema |
    toobig |
    warning |
    abort_rollback |
    auth_user |
    busy_recovery |
    busy_snapshot |
    busy_timeout |
    cantopen_convpath |
    cantopen_dirtywal |
    cantopen_fullpath |
    cantopen_isdir |
    cantopen_notempdir |
    cantopen_symlink |
    constraint_check |
    constraint_commithook |
    constraint_datatype |
    constraint_foreignkey |
    constraint_function |
    constraint_notnull |
    constraint_pinned |
    constraint_primarykey |
    constraint_rowid |
    constraint_trigger |
    constraint_unique |
    constraint_vtab |
    corrupt_index |
    corrupt_sequence |
    corrupt_vtab |
    error_missing_collseq |
    error_retry |
    error_snapshot |
    ioerr_access |
    ioerr_auth |
    ioerr_begin_atomic |
    ioerr_blocked |
    ioerr_checkreservedlock |
    ioerr_close |
    ioerr_commit_atomic |
    ioerr_convpath |
    ioerr_corruptfs |
    ioerr_data |
    ioerr_delete |
    ioerr_delete_noent |
    ioerr_dir_close |
    ioerr_dir_fsync |
    ioerr_fstat |
    ioerr_fsync |
    ioerr_gettemppath |
    ioerr_lock |
    ioerr_mmap |
    ioerr_nomem |
    ioerr_rdlock.

-type replicated() :: {replicated, gleam@option:option(integer()), integer()}.

-file("src/libsql.gleam", 105).
-spec error_code_to_int(error_code()) -> integer().
error_code_to_int(Error) ->
    case Error of
        generic_error ->
            1;

        abort ->
            4;

        auth ->
            23;

        busy ->
            5;

        cantopen ->
            14;

        constraint ->
            19;

        corrupt ->
            11;

        done ->
            101;

        empty ->
            16;

        format ->
            24;

        full ->
            13;

        internal ->
            2;

        interrupt ->
            9;

        ioerr ->
            10;

        locked ->
            6;

        mismatch ->
            20;

        misuse ->
            21;

        nolfs ->
            22;

        nomem ->
            7;

        notadb ->
            26;

        notfound ->
            12;

        notice ->
            27;

        generic_ok ->
            0;

        perm ->
            3;

        protocol ->
            15;

        range ->
            25;

        readonly ->
            8;

        row ->
            100;

        schema ->
            17;

        toobig ->
            18;

        warning ->
            28;

        abort_rollback ->
            516;

        auth_user ->
            279;

        busy_recovery ->
            261;

        busy_snapshot ->
            517;

        busy_timeout ->
            773;

        cantopen_convpath ->
            1038;

        cantopen_dirtywal ->
            1294;

        cantopen_fullpath ->
            782;

        cantopen_isdir ->
            526;

        cantopen_notempdir ->
            270;

        cantopen_symlink ->
            1550;

        constraint_check ->
            275;

        constraint_commithook ->
            531;

        constraint_datatype ->
            3091;

        constraint_foreignkey ->
            787;

        constraint_function ->
            1043;

        constraint_notnull ->
            1299;

        constraint_pinned ->
            2835;

        constraint_primarykey ->
            1555;

        constraint_rowid ->
            2579;

        constraint_trigger ->
            1811;

        constraint_unique ->
            2067;

        constraint_vtab ->
            2323;

        corrupt_index ->
            779;

        corrupt_sequence ->
            523;

        corrupt_vtab ->
            267;

        error_missing_collseq ->
            257;

        error_retry ->
            513;

        error_snapshot ->
            769;

        ioerr_access ->
            3338;

        ioerr_auth ->
            7178;

        ioerr_begin_atomic ->
            7434;

        ioerr_blocked ->
            2826;

        ioerr_checkreservedlock ->
            3594;

        ioerr_close ->
            4106;

        ioerr_commit_atomic ->
            7690;

        ioerr_convpath ->
            6666;

        ioerr_corruptfs ->
            8458;

        ioerr_data ->
            8202;

        ioerr_delete ->
            2570;

        ioerr_delete_noent ->
            5898;

        ioerr_dir_close ->
            4362;

        ioerr_dir_fsync ->
            1290;

        ioerr_fstat ->
            1802;

        ioerr_fsync ->
            1034;

        ioerr_gettemppath ->
            6410;

        ioerr_lock ->
            3850;

        ioerr_mmap ->
            6154;

        ioerr_nomem ->
            3082;

        ioerr_rdlock ->
            2314
    end.

-file("src/libsql.gleam", 191).
-spec error_code_from_int(integer()) -> error_code().
error_code_from_int(Code) ->
    case Code of
        4 ->
            abort;

        23 ->
            auth;

        5 ->
            busy;

        14 ->
            cantopen;

        19 ->
            constraint;

        11 ->
            corrupt;

        101 ->
            done;

        16 ->
            empty;

        1 ->
            generic_error;

        24 ->
            format;

        13 ->
            full;

        2 ->
            internal;

        9 ->
            interrupt;

        10 ->
            ioerr;

        6 ->
            locked;

        20 ->
            mismatch;

        21 ->
            misuse;

        22 ->
            nolfs;

        7 ->
            nomem;

        26 ->
            notadb;

        12 ->
            notfound;

        27 ->
            notice;

        0 ->
            generic_ok;

        3 ->
            perm;

        15 ->
            protocol;

        25 ->
            range;

        8 ->
            readonly;

        100 ->
            row;

        17 ->
            schema;

        18 ->
            toobig;

        28 ->
            warning;

        516 ->
            abort_rollback;

        279 ->
            auth_user;

        261 ->
            busy_recovery;

        517 ->
            busy_snapshot;

        773 ->
            busy_timeout;

        1038 ->
            cantopen_convpath;

        1294 ->
            cantopen_dirtywal;

        782 ->
            cantopen_fullpath;

        526 ->
            cantopen_isdir;

        270 ->
            cantopen_notempdir;

        1550 ->
            cantopen_symlink;

        275 ->
            constraint_check;

        531 ->
            constraint_commithook;

        3091 ->
            constraint_datatype;

        787 ->
            constraint_foreignkey;

        1043 ->
            constraint_function;

        1299 ->
            constraint_notnull;

        2835 ->
            constraint_pinned;

        1555 ->
            constraint_primarykey;

        2579 ->
            constraint_rowid;

        1811 ->
            constraint_trigger;

        2067 ->
            constraint_unique;

        2323 ->
            constraint_vtab;

        779 ->
            corrupt_index;

        523 ->
            corrupt_sequence;

        267 ->
            corrupt_vtab;

        257 ->
            error_missing_collseq;

        513 ->
            error_retry;

        769 ->
            error_snapshot;

        3338 ->
            ioerr_access;

        7178 ->
            ioerr_auth;

        7434 ->
            ioerr_begin_atomic;

        2826 ->
            ioerr_blocked;

        3594 ->
            ioerr_checkreservedlock;

        4106 ->
            ioerr_close;

        7690 ->
            ioerr_commit_atomic;

        6666 ->
            ioerr_convpath;

        8458 ->
            ioerr_corruptfs;

        8202 ->
            ioerr_data;

        2570 ->
            ioerr_delete;

        5898 ->
            ioerr_delete_noent;

        4362 ->
            ioerr_dir_close;

        1290 ->
            ioerr_dir_fsync;

        1802 ->
            ioerr_fstat;

        1034 ->
            ioerr_fsync;

        6410 ->
            ioerr_gettemppath;

        3850 ->
            ioerr_lock;

        6154 ->
            ioerr_mmap;

        3082 ->
            ioerr_nomem;

        2314 ->
            ioerr_rdlock;

        _ ->
            generic_error
    end.

-file("src/libsql.gleam", 287).
-spec open(binary()) -> {ok, connection()} | {error, error()}.
open(Path) ->
    libsql_ffi:open(Path).

-file("src/libsql.gleam", 291).
-spec open_remote(binary(), binary()) -> {ok, connection()} | {error, error()}.
open_remote(Url, Token) ->
    libsql_ffi:open_remote(Url, Token).

-file("src/libsql.gleam", 295).
-spec close(connection()) -> {ok, nil} | {error, error()}.
close(Connection) ->
    libsql_ffi:close(Connection).

-file("src/libsql.gleam", 299).
-spec with_connection(binary(), fun((connection()) -> DLF)) -> DLF.
with_connection(Path, F) ->
    Connection@1 = case open(Path) of
        {ok, Connection} -> Connection;
        _assert_fail ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"libsql"/utf8>>,
                        function => <<"with_connection"/utf8>>,
                        line => 300,
                        value => _assert_fail,
                        start => 5996,
                        'end' => 6034,
                        pattern_start => 6007,
                        pattern_end => 6021})
    end,
    Value = F(Connection@1),
    case close(Connection@1) of
        {ok, _} -> nil;
        _assert_fail@1 ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"libsql"/utf8>>,
                        function => <<"with_connection"/utf8>>,
                        line => 302,
                        value => _assert_fail@1,
                        start => 6065,
                        'end' => 6101,
                        pattern_start => 6076,
                        pattern_end => 6081})
    end,
    Value.

-file("src/libsql.gleam", 306).
-spec with_remote_connection(binary(), binary(), fun((connection()) -> DLG)) -> DLG.
with_remote_connection(Url, Token, F) ->
    Connection@1 = case open_remote(Url, Token) of
        {ok, Connection} -> Connection;
        _assert_fail ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"libsql"/utf8>>,
                        function => <<"with_remote_connection"/utf8>>,
                        line => 307,
                        value => _assert_fail,
                        start => 6204,
                        'end' => 6255,
                        pattern_start => 6215,
                        pattern_end => 6229})
    end,
    Value = F(Connection@1),
    case close(Connection@1) of
        {ok, _} -> nil;
        _assert_fail@1 ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"libsql"/utf8>>,
                        function => <<"with_remote_connection"/utf8>>,
                        line => 309,
                        value => _assert_fail@1,
                        start => 6286,
                        'end' => 6322,
                        pattern_start => 6297,
                        pattern_end => 6302})
    end,
    Value.

-file("src/libsql.gleam", 316).
-spec open_replica(binary(), binary(), binary()) -> {ok, connection()} |
    {error, error()}.
open_replica(Path, Url, Token) ->
    libsql_ffi:open_replica(Path, Url, Token).

-file("src/libsql.gleam", 324).
-spec with_replica_connection(
    binary(),
    binary(),
    binary(),
    fun((connection()) -> DLL)
) -> DLL.
with_replica_connection(Path, Url, Token, F) ->
    Connection@1 = case open_replica(Path, Url, Token) of
        {ok, Connection} -> Connection;
        _assert_fail ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"libsql"/utf8>>,
                        function => <<"with_replica_connection"/utf8>>,
                        line => 330,
                        value => _assert_fail,
                        start => 6718,
                        'end' => 6776,
                        pattern_start => 6729,
                        pattern_end => 6743})
    end,
    Value = F(Connection@1),
    case close(Connection@1) of
        {ok, _} -> nil;
        _assert_fail@1 ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"libsql"/utf8>>,
                        function => <<"with_replica_connection"/utf8>>,
                        line => 332,
                        value => _assert_fail@1,
                        start => 6807,
                        'end' => 6843,
                        pattern_start => 6818,
                        pattern_end => 6823})
    end,
    Value.

-file("src/libsql.gleam", 339).
-spec open_synced_database(binary(), binary(), binary()) -> {ok, connection()} |
    {error, error()}.
open_synced_database(Path, Url, Token) ->
    libsql_ffi:open_synced_database(Path, Url, Token).

-file("src/libsql.gleam", 343).
-spec with_synced_database(
    binary(),
    binary(),
    binary(),
    fun((connection()) -> DLQ)
) -> DLQ.
with_synced_database(Path, Url, Token, F) ->
    Connection@1 = case open_synced_database(Path, Url, Token) of
        {ok, Connection} -> Connection;
        _assert_fail ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"libsql"/utf8>>,
                        function => <<"with_synced_database"/utf8>>,
                        line => 349,
                        value => _assert_fail,
                        start => 7259,
                        'end' => 7325,
                        pattern_start => 7270,
                        pattern_end => 7284})
    end,
    Value = F(Connection@1),
    case close(Connection@1) of
        {ok, _} -> nil;
        _assert_fail@1 ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"libsql"/utf8>>,
                        function => <<"with_synced_database"/utf8>>,
                        line => 351,
                        value => _assert_fail@1,
                        start => 7356,
                        'end' => 7392,
                        pattern_start => 7367,
                        pattern_end => 7372})
    end,
    Value.

-file("src/libsql.gleam", 365).
-spec sync(connection()) -> {ok, replicated()} | {error, error()}.
sync(Connection) ->
    gleam@result:'try'(
        libsql_ffi:sync(Connection),
        fun(Result) ->
            {Frame_no_raw, Frames_synced} = Result,
            Frame_no = case Frame_no_raw of
                -1 ->
                    none;

                N ->
                    {some, N}
            end,
            {ok, {replicated, Frame_no, Frames_synced}}
        end
    ).

-file("src/libsql.gleam", 375).
-spec replication_index(connection()) -> {ok, gleam@option:option(integer())} |
    {error, error()}.
replication_index(Connection) ->
    gleam@result:'try'(
        libsql_ffi:replication_index(Connection),
        fun(Raw) ->
            case gleam@dynamic@decode:run(
                Raw,
                gleam@dynamic@decode:optional(
                    {decoder, fun gleam@dynamic@decode:decode_int/1}
                )
            ) of
                {ok, Val} ->
                    {ok, Val};

                {error, _} ->
                    {ok, none}
            end
        end
    ).

-file("src/libsql.gleam", 385).
-spec exec(binary(), connection()) -> {ok, nil} | {error, error()}.
exec(Sql, Connection) ->
    libsql_ffi:exec(Sql, Connection).

-file("src/libsql.gleam", 392).
-spec exec_batch(binary(), connection(), list(list(value()))) -> {ok, nil} |
    {error, error()}.
exec_batch(Sql, Connection, Batches) ->
    libsql_ffi:exec_batch(Sql, Connection, Batches).

-file("src/libsql.gleam", 400).
-spec 'begin'(connection()) -> {ok, nil} | {error, error()}.
'begin'(Connection) ->
    exec(<<"BEGIN"/utf8>>, Connection).

-file("src/libsql.gleam", 404).
-spec commit(connection()) -> {ok, nil} | {error, error()}.
commit(Connection) ->
    exec(<<"COMMIT"/utf8>>, Connection).

-file("src/libsql.gleam", 408).
-spec rollback(connection()) -> {ok, nil} | {error, error()}.
rollback(Connection) ->
    exec(<<"ROLLBACK"/utf8>>, Connection).

-file("src/libsql.gleam", 412).
-spec transaction(connection(), fun(() -> {ok, DMQ} | {error, error()})) -> {ok,
        DMQ} |
    {error, error()}.
transaction(Connection, F) ->
    gleam@result:'try'('begin'(Connection), fun(_) -> case F() of
                {ok, Value} ->
                    gleam@result:'try'(
                        commit(Connection),
                        fun(_) -> {ok, Value} end
                    );

                {error, E} ->
                    _ = rollback(Connection),
                    {error, E}
            end end).

-file("src/libsql.gleam", 682).
-spec decode_error(list(gleam@dynamic@decode:decode_error())) -> error().
decode_error(Errors) ->
    {Expected@1, Actual@1, Path@1} = case Errors of
        [{decode_error, Expected, Actual, Path} | _] -> {Expected, Actual, Path};
        _assert_fail ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"libsql"/utf8>>,
                        function => <<"decode_error"/utf8>>,
                        line => 683,
                        value => _assert_fail,
                        start => 15358,
                        'end' => 15426,
                        pattern_start => 15369,
                        pattern_end => 15417})
    end,
    Path@2 = gleam@string:join(Path@1, <<"."/utf8>>),
    Message = <<<<<<<<<<"Decoder failed, expected "/utf8, Expected@1/binary>>/binary,
                    ", got "/utf8>>/binary,
                Actual@1/binary>>/binary,
            " in "/utf8>>/binary,
        Path@2/binary>>,
    {libsql_error, generic_error, Message, -1}.

-file("src/libsql.gleam", 429).
-spec 'query'(
    binary(),
    connection(),
    list(value()),
    gleam@dynamic@decode:decoder(DMW)
) -> {ok, list(DMW)} | {error, error()}.
'query'(Sql, Connection, Arguments, Decoder) ->
    gleam@result:'try'(
        libsql_ffi:'query'(Sql, Connection, Arguments),
        fun(Rows) ->
            gleam@result:'try'(
                begin
                    _pipe = gleam@list:try_map(
                        Rows,
                        fun(Row) -> gleam@dynamic@decode:run(Row, Decoder) end
                    ),
                    gleam@result:map_error(_pipe, fun decode_error/1)
                end,
                fun(Rows@1) -> {ok, Rows@1} end
            )
        end
    ).

-file("src/libsql.gleam", 443).
-spec query_named(
    binary(),
    connection(),
    list({binary(), value()}),
    gleam@dynamic@decode:decoder(DNC)
) -> {ok, list(DNC)} | {error, error()}.
query_named(Sql, Connection, Arguments, Decoder) ->
    gleam@result:'try'(
        libsql_ffi:query_named(Sql, Connection, Arguments),
        fun(Rows) ->
            gleam@result:'try'(
                begin
                    _pipe = gleam@list:try_map(
                        Rows,
                        fun(Row) -> gleam@dynamic@decode:run(Row, Decoder) end
                    ),
                    gleam@result:map_error(_pipe, fun decode_error/1)
                end,
                fun(Rows@1) -> {ok, Rows@1} end
            )
        end
    ).

-file("src/libsql.gleam", 457).
-spec query_first(
    binary(),
    connection(),
    list(value()),
    gleam@dynamic@decode:decoder(DNI)
) -> {ok, gleam@option:option(DNI)} | {error, error()}.
query_first(Sql, Connection, Arguments, Decoder) ->
    gleam@result:'try'(
        'query'(Sql, Connection, Arguments, Decoder),
        fun(Rows) -> case Rows of
                [] ->
                    {ok, none};

                [First | _] ->
                    {ok, {some, First}}
            end end
    ).

-file("src/libsql.gleam", 470).
-spec query_first_named(
    binary(),
    connection(),
    list({binary(), value()}),
    gleam@dynamic@decode:decoder(DNO)
) -> {ok, gleam@option:option(DNO)} | {error, error()}.
query_first_named(Sql, Connection, Arguments, Decoder) ->
    gleam@result:'try'(
        query_named(Sql, Connection, Arguments, Decoder),
        fun(Rows) -> case Rows of
                [] ->
                    {ok, none};

                [First | _] ->
                    {ok, {some, First}}
            end end
    ).

-file("src/libsql.gleam", 483).
-spec query_one(
    binary(),
    connection(),
    list(value()),
    gleam@dynamic@decode:decoder(DNU)
) -> {ok, DNU} | {error, error()}.
query_one(Sql, Connection, Arguments, Decoder) ->
    gleam@result:'try'(
        'query'(Sql, Connection, Arguments, Decoder),
        fun(Rows) -> case Rows of
                [One] ->
                    {ok, One};

                [] ->
                    {error,
                        {libsql_error,
                            generic_error,
                            <<"Query returned 0 rows, expected exactly 1"/utf8>>,
                            -1}};

                _ ->
                    {error,
                        {libsql_error,
                            generic_error,
                            <<"Query returned multiple rows, expected exactly 1"/utf8>>,
                            -1}}
            end end
    ).

-file("src/libsql.gleam", 507).
-spec query_one_named(
    binary(),
    connection(),
    list({binary(), value()}),
    gleam@dynamic@decode:decoder(DNZ)
) -> {ok, DNZ} | {error, error()}.
query_one_named(Sql, Connection, Arguments, Decoder) ->
    gleam@result:'try'(
        query_named(Sql, Connection, Arguments, Decoder),
        fun(Rows) -> case Rows of
                [One] ->
                    {ok, One};

                [] ->
                    {error,
                        {libsql_error,
                            generic_error,
                            <<"Query returned 0 rows, expected exactly 1"/utf8>>,
                            -1}};

                _ ->
                    {error,
                        {libsql_error,
                            generic_error,
                            <<"Query returned multiple rows, expected exactly 1"/utf8>>,
                            -1}}
            end end
    ).

-file("src/libsql.gleam", 543).
-spec changes(connection()) -> {ok, integer()} | {error, error()}.
changes(Connection) ->
    libsql_ffi:changes(Connection).

-file("src/libsql.gleam", 547).
-spec total_changes(connection()) -> {ok, integer()} | {error, error()}.
total_changes(Connection) ->
    libsql_ffi:total_changes(Connection).

-file("src/libsql.gleam", 551).
-spec last_insert_rowid(connection()) -> {ok, integer()} | {error, error()}.
last_insert_rowid(Connection) ->
    libsql_ffi:last_insert_rowid(Connection).

-file("src/libsql.gleam", 555).
-spec interrupt(connection()) -> {ok, nil} | {error, error()}.
interrupt(Connection) ->
    libsql_ffi:interrupt(Connection).

-file("src/libsql.gleam", 574).
-spec prepare(binary(), connection()) -> {ok, statement()} | {error, error()}.
prepare(Sql, Connection) ->
    libsql_ffi:prepare(Sql, Connection).

-file("src/libsql.gleam", 578).
-spec exec_prepared(statement(), list(value())) -> {ok, nil} | {error, error()}.
exec_prepared(Statement, Params) ->
    libsql_ffi:exec_prepared(Params, Statement).

-file("src/libsql.gleam", 585).
-spec query_prepared(
    statement(),
    list(value()),
    gleam@dynamic@decode:decoder(DPK)
) -> {ok, list(DPK)} | {error, error()}.
query_prepared(Statement, Params, Decoder) ->
    gleam@result:'try'(
        libsql_ffi:query_prepared(Params, Statement),
        fun(Rows) ->
            gleam@result:'try'(
                begin
                    _pipe = gleam@list:try_map(
                        Rows,
                        fun(Row) -> gleam@dynamic@decode:run(Row, Decoder) end
                    ),
                    gleam@result:map_error(_pipe, fun decode_error/1)
                end,
                fun(Rows@1) -> {ok, Rows@1} end
            )
        end
    ).

-file("src/libsql.gleam", 598).
-spec finalize(statement()) -> {ok, nil} | {error, error()}.
finalize(Statement) ->
    libsql_ffi:finalize(Statement).

-file("src/libsql.gleam", 602).
-spec with_statement(binary(), connection(), fun((statement()) -> DPR)) -> DPR.
with_statement(Sql, Connection, F) ->
    Statement@1 = case prepare(Sql, Connection) of
        {ok, Statement} -> Statement;
        _assert_fail ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"libsql"/utf8>>,
                        function => <<"with_statement"/utf8>>,
                        line => 607,
                        value => _assert_fail,
                        start => 13856,
                        'end' => 13907,
                        pattern_start => 13867,
                        pattern_end => 13880})
    end,
    Value = F(Statement@1),
    case finalize(Statement@1) of
        {ok, _} -> nil;
        _assert_fail@1 ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"libsql"/utf8>>,
                        function => <<"with_statement"/utf8>>,
                        line => 609,
                        value => _assert_fail@1,
                        start => 13937,
                        'end' => 13975,
                        pattern_start => 13948,
                        pattern_end => 13953})
    end,
    Value.

-file("src/libsql.gleam", 669).
-spec null() -> value().
null() ->
    libsql_ffi:null().

-file("src/libsql.gleam", 633).
-spec nullable(fun((DQD) -> value()), gleam@option:option(DQD)) -> value().
nullable(Inner_type, Value) ->
    case Value of
        {some, Value@1} ->
            Inner_type(Value@1);

        none ->
            null()
    end.

-file("src/libsql.gleam", 640).
-spec int(integer()) -> value().
int(Value) ->
    libsql_ffi:coerce_value(Value).

-file("src/libsql.gleam", 644).
-spec float(float()) -> value().
float(Value) ->
    libsql_ffi:coerce_value(Value).

-file("src/libsql.gleam", 648).
-spec text(binary()) -> value().
text(Value) ->
    libsql_ffi:coerce_value(Value).

-file("src/libsql.gleam", 655).
-spec blob(bitstring()) -> value().
blob(Value) ->
    libsql_ffi:coerce_blob(Value).

-file("src/libsql.gleam", 659).
-spec bool(boolean()) -> value().
bool(Value) ->
    int(case Value of
            true ->
                1;

            false ->
                0
        end).

-file("src/libsql.gleam", 673).
-spec decode_bool() -> gleam@dynamic@decode:decoder(boolean()).
decode_bool() ->
    gleam@dynamic@decode:then(
        {decoder, fun gleam@dynamic@decode:decode_int/1},
        fun(B) -> case B of
                0 ->
                    gleam@dynamic@decode:success(false);

                _ ->
                    gleam@dynamic@decode:success(true)
            end end
    ).