-module(kvite@raw). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -define(FILEPATH, "src/kvite/raw.gleam"). -export([apply_pragmas/1, create_table/2, get/3, set/4, del/3, keys/2, truncate/2, begin_transaction/1, commit_transaction/1, cancel_transaction/1]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC( " ## Here be dragons!\n" "\n" " This modules contains functions for raw operations on a `sqlight.Connection` used by Kvite.\n" " The table must follow the Kvite schema of having a `key TEXT` and a `value BLOB` column.\n" "\n" " These functions are used internally by Kvite and are not intended for direct use by applications.\n" " Only use them as an escape hatch when you know what you're doing.\n" ). -file("src/kvite/raw.gleam", 15). -spec apply_pragmas(sqlight:connection()) -> {ok, nil} | {error, sqlight:error()}. apply_pragmas(Conn) -> gleam@result:'try'( begin _pipe = <<"PRAGMA main.synchronous = NORMAL;"/utf8>>, sqlight:exec(_pipe, Conn) end, fun(_) -> gleam@result:'try'( begin _pipe@1 = <<"PRAGMA main.journal_mode = WAL2;"/utf8>>, sqlight:exec(_pipe@1, Conn) end, fun(_) -> gleam@result:'try'( begin _pipe@2 = <<"PRAGMA main.auto_vacuum = INCREMENTAL;"/utf8>>, sqlight:exec(_pipe@2, Conn) end, fun(_) -> {ok, nil} end ) end ) end ). -file("src/kvite/raw.gleam", 33). -spec create_table(sqlight:connection(), binary()) -> {ok, nil} | {error, sqlight:error()}. create_table(Conn, Table) -> Sql = <<<<"CREATE TABLE IF NOT EXISTS "/utf8, Table/binary>>/binary, " (key TEXT PRIMARY KEY, value BLOB);"/utf8>>, _pipe = Sql, sqlight:exec(_pipe, Conn). -file("src/kvite/raw.gleam", 46). -spec get(sqlight:connection(), binary(), binary()) -> {ok, gleam@option:option(bitstring())} | {error, sqlight:error()}. get(Conn, Table, Key) -> gleam@result:'try'( begin _pipe = (<<<<"SELECT value FROM "/utf8, Table/binary>>/binary, " WHERE key = ?;"/utf8>>), sqlight:'query'( _pipe, Conn, [sqlight:text(Key)], begin gleam@dynamic@decode:field( 0, {decoder, fun gleam@dynamic@decode:decode_bit_array/1}, fun(Value) -> gleam@dynamic@decode:success(Value) end ) end ) end, fun(Rows) -> case Rows of [] -> {ok, none}; [Value@1] -> {ok, {some, Value@1}}; _ -> erlang:error(#{gleam_error => panic, message => <<"Too many rows returned"/utf8>>, file => <>, module => <<"kvite/raw"/utf8>>, function => <<"get"/utf8>>, line => 64}) end end ). -file("src/kvite/raw.gleam", 68). -spec set(sqlight:connection(), binary(), binary(), bitstring()) -> {ok, nil} | {error, sqlight:error()}. set(Conn, Table, Key, Value) -> gleam@result:'try'( begin _pipe = (<<<<"INSERT OR REPLACE INTO "/utf8, Table/binary>>/binary, " (key, value) VALUES (?, ?);"/utf8>>), sqlight:'query'( _pipe, Conn, [sqlight:text(Key), sqlight_ffi:coerce_blob(Value)], {decoder, fun gleam@dynamic@decode:decode_int/1} ) end, fun(_) -> {ok, nil} end ). -file("src/kvite/raw.gleam", 81). -spec del(sqlight:connection(), binary(), binary()) -> {ok, nil} | {error, sqlight:error()}. del(Conn, Table, Key) -> gleam@result:'try'( begin _pipe = (<<<<"DELETE FROM "/utf8, Table/binary>>/binary, " WHERE key = ?;"/utf8>>), sqlight:'query'( _pipe, Conn, [sqlight:text(Key)], {decoder, fun gleam@dynamic@decode:decode_int/1} ) end, fun(_) -> {ok, nil} end ). -file("src/kvite/raw.gleam", 93). -spec keys(sqlight:connection(), binary()) -> {ok, list(binary())} | {error, sqlight:error()}. keys(Conn, Table) -> _pipe = (<<<<"SELECT key FROM "/utf8, Table/binary>>/binary, ";"/utf8>>), sqlight:'query'( _pipe, Conn, [], begin gleam@dynamic@decode:field( 0, {decoder, fun gleam@dynamic@decode:decode_string/1}, fun(Key) -> gleam@dynamic@decode:success(Key) end ) end ). -file("src/kvite/raw.gleam", 104). -spec truncate(sqlight:connection(), binary()) -> {ok, nil} | {error, sqlight:error()}. truncate(Conn, Table) -> _pipe = (<<<<"DELETE FROM "/utf8, Table/binary>>/binary, ";"/utf8>>), sqlight:exec(_pipe, Conn). -file("src/kvite/raw.gleam", 112). -spec begin_transaction(sqlight:connection()) -> {ok, nil} | {error, sqlight:error()}. begin_transaction(Conn) -> _pipe = <<"BEGIN TRANSACTION;"/utf8>>, sqlight:exec(_pipe, Conn). -file("src/kvite/raw.gleam", 119). -spec commit_transaction(sqlight:connection()) -> {ok, nil} | {error, sqlight:error()}. commit_transaction(Conn) -> _pipe = <<"COMMIT TRANSACTION;"/utf8>>, sqlight:exec(_pipe, Conn). -file("src/kvite/raw.gleam", 126). -spec cancel_transaction(sqlight:connection()) -> {ok, nil} | {error, sqlight:error()}. cancel_transaction(Conn) -> _pipe = <<"ROLLBACK TRANSACTION;"/utf8>>, sqlight:exec(_pipe, Conn).