Current section
Files
Jump to
Current section
Files
src/cake@adapter@sqlite.erl
-module(cake@adapter@sqlite).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([with_connection/2, with_memory_connection/1, read_query_to_prepared_statement/1, write_query_to_prepared_statement/1, execute_raw_sql/2, run_read_query/3, run_write_query/3, run_query/3]).
-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(
" 🎂Cake 🪶SQLite adapter which passes `PreparedStatement`s\n"
" to the `sqlight` library for execution.\n"
"\n"
).
-file("src/cake/adapter/sqlite.gleam", 21).
?DOC(
" Connection to a SQLite database.\n"
"\n"
" This is a thin wrapper around the `sqlight` library's `Connection` type.\n"
).
-spec with_connection(binary(), fun((sqlight:connection()) -> PMH)) -> PMH.
with_connection(Filename, Callback) ->
sqlight:with_connection(<<"file:"/utf8, Filename/binary>>, Callback).
-file("src/cake/adapter/sqlite.gleam", 32).
?DOC(
" Connection to an in-memory SQLite database.\n"
"\n"
" This is a thin wrapper around the `sqlight` library's `Connection` type.\n"
).
-spec with_memory_connection(fun((sqlight:connection()) -> PMI)) -> PMI.
with_memory_connection(Callback) ->
sqlight:with_connection(<<":memory:"/utf8>>, Callback).
-file("src/cake/adapter/sqlite.gleam", 38).
?DOC(" Convert a Cake `ReadQuery` to a `PreparedStatement`.\n").
-spec read_query_to_prepared_statement(cake@internal@read_query:read_query()) -> cake@internal@prepared_statement:prepared_statement().
read_query_to_prepared_statement(Query) ->
_pipe = Query,
cake@dialect@sqlite_dialect:read_query_to_prepared_statement(_pipe).
-file("src/cake/adapter/sqlite.gleam", 46).
?DOC(" Convert a Cake `WriteQuery` to a `PreparedStatement`.\n").
-spec write_query_to_prepared_statement(
cake@internal@write_query:write_query(any())
) -> cake@internal@prepared_statement:prepared_statement().
write_query_to_prepared_statement(Query) ->
_pipe = Query,
cake@dialect@sqlite_dialect:write_query_to_prepared_statement(_pipe).
-file("src/cake/adapter/sqlite.gleam", 107).
?DOC(" Execute a raw SQL query against an SQLite database.\n").
-spec execute_raw_sql(binary(), sqlight:connection()) -> {ok, nil} |
{error, sqlight:error()}.
execute_raw_sql(Sql_string, Db_connection) ->
_pipe = Sql_string,
sqlight:exec(_pipe, Db_connection).
-file("src/cake/adapter/sqlite.gleam", 114).
-spec cake_param_to_client_param(cake@param:param()) -> sqlight:value().
cake_param_to_client_param(Param) ->
case Param of
{bool_param, Param@1} ->
sqlight:bool(Param@1);
{float_param, Param@2} ->
sqlight:float(Param@2);
{int_param, Param@3} ->
sqlight:int(Param@3);
{string_param, Param@4} ->
sqlight:text(Param@4);
null_param ->
sqlight_ffi:null()
end.
-file("src/cake/adapter/sqlite.gleam", 54).
?DOC(" Run a Cake `ReadQuery` against an SQLite database.\n").
-spec run_read_query(
cake@internal@read_query:read_query(),
gleam@dynamic@decode:decoder(PML),
sqlight:connection()
) -> {ok, list(PML)} | {error, sqlight:error()}.
run_read_query(Query, Decoder, Db_connection) ->
Prepared_statement = begin
_pipe = Query,
read_query_to_prepared_statement(_pipe)
end,
Sql_string = begin
_pipe@1 = Prepared_statement,
cake:get_sql(_pipe@1)
end,
Db_params = begin
_pipe@2 = Prepared_statement,
_pipe@3 = cake:get_params(_pipe@2),
gleam@list:map(_pipe@3, fun cake_param_to_client_param/1)
end,
_pipe@4 = Sql_string,
sqlight:'query'(_pipe@4, Db_connection, Db_params, Decoder).
-file("src/cake/adapter/sqlite.gleam", 72).
?DOC(" Run a Cake `WriteQuery` against an SQLite database.\n").
-spec run_write_query(
cake@internal@write_query:write_query(any()),
gleam@dynamic@decode:decoder(PMS),
sqlight:connection()
) -> {ok, list(PMS)} | {error, sqlight:error()}.
run_write_query(Query, Decoder, Db_connection) ->
Prepared_statement = begin
_pipe = Query,
write_query_to_prepared_statement(_pipe)
end,
Sql_string = begin
_pipe@1 = Prepared_statement,
cake:get_sql(_pipe@1)
end,
Db_params = begin
_pipe@2 = Prepared_statement,
_pipe@3 = cake:get_params(_pipe@2),
gleam@list:map(_pipe@3, fun cake_param_to_client_param/1)
end,
_pipe@4 = Sql_string,
sqlight:'query'(_pipe@4, Db_connection, Db_params, Decoder).
-file("src/cake/adapter/sqlite.gleam", 92).
?DOC(
" Run a Cake `CakeQuery` against an SQLite database.\n"
"\n"
" This function is a wrapper around `run_read_query` and `run_write_query`.\n"
).
-spec run_query(
cake:cake_query(PMX),
gleam@dynamic@decode:decoder(PMX),
sqlight:connection()
) -> {ok, list(PMX)} | {error, sqlight:error()}.
run_query(Query, Decoder, Db_connection) ->
case Query of
{cake_read_query, Read_query} ->
_pipe = Read_query,
run_read_query(_pipe, Decoder, Db_connection);
{cake_write_query, Write_query} ->
_pipe@1 = Write_query,
run_write_query(_pipe@1, Decoder, Db_connection)
end.