-module(cake@adapter@sqlite). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/cake/adapter/sqlite.gleam"). -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", 24). ?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()) -> QSV)) -> QSV. with_connection(Filename, Callback) -> sqlight:with_connection(<<"file:"/utf8, Filename/binary>>, Callback). -file("src/cake/adapter/sqlite.gleam", 35). ?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()) -> QSW)) -> QSW. with_memory_connection(Callback) -> sqlight:with_connection(<<":memory:"/utf8>>, Callback). -file("src/cake/adapter/sqlite.gleam", 41). ?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", 49). ?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", 110). ?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", 117). -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(); {date_param, Param@5} -> {date, Year, Month, Day} = Param@5, Year@1 = begin _pipe = Year, _pipe@1 = erlang:integer_to_binary(_pipe), gleam@string:pad_start(_pipe@1, 4, <<"0"/utf8>>) end, Month@1 = begin _pipe@2 = Month, _pipe@3 = gleam@time@calendar:month_to_int(_pipe@2), _pipe@4 = erlang:integer_to_binary(_pipe@3), gleam@string:pad_start(_pipe@4, 2, <<"0"/utf8>>) end, Day@1 = begin _pipe@5 = Day, _pipe@6 = erlang:integer_to_binary(_pipe@5), gleam@string:pad_start(_pipe@6, 2, <<"0"/utf8>>) end, Date = <<<<<<<>/binary, Month@1/binary>>/binary, "-"/utf8>>/binary, Day@1/binary>>, _pipe@7 = Date, sqlight:text(_pipe@7) end. -file("src/cake/adapter/sqlite.gleam", 57). ?DOC(" Run a Cake `ReadQuery` against an SQLite database.\n"). -spec run_read_query( cake@internal@read_query:read_query(), gleam@dynamic@decode:decoder(QSZ), sqlight:connection() ) -> {ok, list(QSZ)} | {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", 75). ?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(QTG), sqlight:connection() ) -> {ok, list(QTG)} | {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", 95). ?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(QTL), gleam@dynamic@decode:decoder(QTL), sqlight:connection() ) -> {ok, list(QTL)} | {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.