-module(galchemy@sql@postgres). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src\\galchemy\\sql\\postgres.gleam"). -export([to_pog_value/1, with_params/2, to_query_from_compiled/1, to_query_with/2, to_query/1, execute_with_config/3, execute/2, execute_with_decoder_config/4, execute_with/3]). -export_type([postgres_error/0]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. -type postgres_error() :: {compile_error, galchemy@sql@compiler:compile_error()} | {query_error, pog:query_error()}. -file("src\\galchemy\\sql\\postgres.gleam", 14). ?DOC(" Converts an SQL AST value into a `pog` parameter value.\n"). -spec to_pog_value(galchemy@ast@expression:sql_value()) -> pog:value(). to_pog_value(Value) -> case Value of {text, V} -> pog_ffi:coerce(V); {int, V@1} -> pog_ffi:coerce(V@1); {float, V@2} -> pog_ffi:coerce(V@2); {bool, V@3} -> pog_ffi:coerce(V@3); {bytea, V@4} -> pog_ffi:coerce(V@4); {uuid, V@5} -> pog_ffi:coerce(V@5); {numeric, V@6} -> pog_ffi:coerce(V@6); {json, V@7} -> pog_ffi:coerce(V@7); {jsonb, V@8} -> pog_ffi:coerce(V@8); {enum, _, V@9} -> pog_ffi:coerce(V@9); {array, V@10} -> pog:array(fun to_pog_value/1, V@10); {timestamp, V@11} -> pog:timestamp(V@11); {date, V@12} -> pog:calendar_date(V@12); {time_of_day, V@13} -> pog:calendar_time_of_day(V@13); null -> pog_ffi:null() end. -file("src\\galchemy\\sql\\postgres.gleam", 43). ?DOC(" Recursively appends parameters to `pog.Query`.\n"). -spec with_params_loop( pog:'query'(NAU), list(galchemy@ast@expression:sql_value()) ) -> pog:'query'(NAU). with_params_loop(Query, Params) -> case Params of [] -> Query; [Param | Rest] -> Next_query = pog:parameter(Query, to_pog_value(Param)), with_params_loop(Next_query, Rest) end. -file("src\\galchemy\\sql\\postgres.gleam", 35). ?DOC(" Adds parameters to a `pog` query while preserving their original order.\n"). -spec with_params(pog:'query'(NAQ), list(galchemy@ast@expression:sql_value())) -> pog:'query'(NAQ). with_params(Query, Params) -> with_params_loop(Query, Params). -file("src\\galchemy\\sql\\postgres.gleam", 57). ?DOC(" Builds a `pog` query from already compiled SQL.\n"). -spec to_query_from_compiled(galchemy@sql@compiler:compiled_query()) -> pog:'query'(nil). to_query_from_compiled(Compiled) -> {compiled_query, Sql, Params} = Compiled, _pipe = pog:'query'(Sql), with_params(_pipe, Params). -file("src\\galchemy\\sql\\postgres.gleam", 72). -spec to_query_with( galchemy@ast@query:'query'(), galchemy@sql@compiler:compiler_config() ) -> {ok, pog:'query'(nil)} | {error, galchemy@sql@compiler:compile_error()}. to_query_with(Query, Config) -> case galchemy@sql@compiler:compile_with(Query, Config) of {ok, Compiled} -> {ok, to_query_from_compiled(Compiled)}; {error, Error} -> {error, Error} end. -file("src\\galchemy\\sql\\postgres.gleam", 66). ?DOC(" Compiles an AST query and converts it into a `pog` query.\n"). -spec to_query(galchemy@ast@query:'query'()) -> {ok, pog:'query'(nil)} | {error, galchemy@sql@compiler:compile_error()}. to_query(Query) -> to_query_with(Query, galchemy@sql@compiler:default_config()). -file("src\\galchemy\\sql\\postgres.gleam", 90). -spec execute_with_config( galchemy@ast@query:'query'(), galchemy@sql@compiler:compiler_config(), pog:connection() ) -> {ok, pog:returned(nil)} | {error, postgres_error()}. execute_with_config(Query, Config, Connection) -> case to_query_with(Query, Config) of {ok, Compiled_query} -> case pog:execute(Compiled_query, Connection) of {ok, Returned} -> {ok, Returned}; {error, Error} -> {error, {query_error, Error}} end; {error, Error@1} -> {error, {compile_error, Error@1}} end. -file("src\\galchemy\\sql\\postgres.gleam", 83). ?DOC(" Executes an AST query without row decoding.\n"). -spec execute(galchemy@ast@query:'query'(), pog:connection()) -> {ok, pog:returned(nil)} | {error, postgres_error()}. execute(Query, Connection) -> execute_with_config( Query, galchemy@sql@compiler:default_config(), Connection ). -file("src\\galchemy\\sql\\postgres.gleam", 120). -spec execute_with_decoder_config( galchemy@ast@query:'query'(), gleam@dynamic@decode:decoder(NBQ), galchemy@sql@compiler:compiler_config(), pog:connection() ) -> {ok, pog:returned(NBQ)} | {error, postgres_error()}. execute_with_decoder_config(Query, Decoder, Config, Connection) -> case to_query_with(Query, Config) of {ok, Compiled_query} -> Query_with_decoder = pog:returning(Compiled_query, Decoder), case pog:execute(Query_with_decoder, Connection) of {ok, Returned} -> {ok, Returned}; {error, Error} -> {error, {query_error, Error}} end; {error, Error@1} -> {error, {compile_error, Error@1}} end. -file("src\\galchemy\\sql\\postgres.gleam", 107). ?DOC(" Executes an AST query using a row decoder.\n"). -spec execute_with( galchemy@ast@query:'query'(), gleam@dynamic@decode:decoder(NBL), pog:connection() ) -> {ok, pog:returned(NBL)} | {error, postgres_error()}. execute_with(Query, Decoder, Connection) -> execute_with_decoder_config( Query, Decoder, galchemy@sql@compiler:default_config(), Connection ).