-module(cake@internal@write_query). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([to_insert_query/1, update_to_write_query/1, delete_to_write_query/1, to_prepared_statement/3]). -export_type([write_query/1, returning/0, insert/1, insert_into_table/0, insert_columns/0, insert_modifier/0, insert_source/1, insert_row/0, insert_value/0, insert_conflict_strategy/1, insert_confict_target/0, update/1, update_modifier/0, update_table/0, update_sets/0, update_set/0, delete/1, delete_modifier/0, delete_table/0, delete_using/0]). -type write_query(PXV) :: {insert_query, insert(PXV)} | {update_query, update(PXV)} | {delete_query, delete(PXV)}. -type returning() :: no_returning | {returning, list(binary())}. -type insert(PXW) :: {insert, insert_into_table(), insert_columns(), insert_modifier(), insert_source(PXW), insert_conflict_strategy(PXW), returning(), cake@internal@query:epilog(), cake@internal@query:comment()}. -type insert_into_table() :: {insert_into_table, binary()}. -type insert_columns() :: {insert_columns, list(binary())}. -type insert_modifier() :: no_insert_modifier | {insert_modifier, binary()}. -type insert_source(PXX) :: insert_source_default | {insert_source_records, list(PXX), fun((PXX) -> insert_row())} | {insert_source_rows, list(insert_row())} | {insert_source_query, cake@internal@query:'query'()}. -type insert_row() :: {insert_row, list(insert_value())}. -type insert_value() :: {insert_param, binary(), cake@param:param()} | {insert_default, binary()}. -type insert_conflict_strategy(PXY) :: insert_conflict_error | {insert_conflict_ignore, insert_confict_target(), cake@internal@query:where()} | {insert_conflict_update, insert_confict_target(), cake@internal@query:where(), update(PXY)}. -type insert_confict_target() :: {insert_conflict_target, list(binary())} | {insert_conflict_target_constraint, binary()}. -type update(PXZ) :: {update, update_table(), update_modifier(), update_sets(), cake@internal@query:from(), cake@internal@query:joins(), cake@internal@query:where(), returning(), cake@internal@query:epilog(), cake@internal@query:comment()} | {gleam_phantom, PXZ}. -type update_modifier() :: no_update_modifier | {update_modifier, binary()}. -type update_table() :: {update_table, binary()}. -type update_sets() :: {update_sets, list(update_set())}. -type update_set() :: {update_param_set, binary(), cake@param:param()} | {update_expression_set, list(binary()), binary()} | {update_sub_query_set, list(binary()), cake@internal@query:'query'()}. -type delete(PYA) :: {delete, delete_modifier(), delete_table(), delete_using(), cake@internal@query:where(), returning(), cake@internal@query:epilog(), cake@internal@query:comment()} | {gleam_phantom, PYA}. -type delete_modifier() :: no_delete_modifier | {delete_modifier, binary()}. -type delete_table() :: {delete_table, binary()}. -type delete_using() :: no_delete_using | {delete_using, list(cake@internal@query:from())}. -spec returning_apply( cake@internal@prepared_statement:prepared_statement(), returning() ) -> cake@internal@prepared_statement:prepared_statement(). returning_apply(Prp_stm, Rtrn) -> case Rtrn of no_returning -> Prp_stm; {returning, Cols} -> _pipe = Prp_stm, _pipe@1 = cake@internal@prepared_statement:append_sql( _pipe, <<" RETURNING "/utf8>> ), cake@internal@prepared_statement:append_sql( _pipe@1, begin _pipe@2 = Cols, gleam@string:join(_pipe@2, <<", "/utf8>>) end ) end. -spec to_insert_query(insert(PYF)) -> write_query(PYF). to_insert_query(Insert) -> _pipe = Insert, {insert_query, _pipe}. -spec insert_modifier_apply( cake@internal@prepared_statement:prepared_statement(), insert_modifier() ) -> cake@internal@prepared_statement:prepared_statement(). insert_modifier_apply(Prp_stm, Isrt_mdfr) -> case Isrt_mdfr of no_insert_modifier -> Prp_stm; {insert_modifier, Mdfr} -> _pipe = Prp_stm, cake@internal@prepared_statement:append_sql( _pipe, <<" "/utf8, Mdfr/binary>> ) end. -spec row_apply( cake@internal@prepared_statement:prepared_statement(), list(insert_value()) ) -> cake@internal@prepared_statement:prepared_statement(). row_apply(New_prp_stm, Row) -> _pipe = Row, gleam@list:fold( _pipe, New_prp_stm, fun(New_prp_stm_inner, Insert_value) -> case Insert_value of {insert_param, _, Param} -> case New_prp_stm_inner =:= New_prp_stm of true -> _pipe@1 = New_prp_stm_inner, cake@internal@prepared_statement:append_param( _pipe@1, Param ); false -> _pipe@2 = New_prp_stm_inner, _pipe@3 = cake@internal@prepared_statement:append_sql( _pipe@2, <<", "/utf8>> ), cake@internal@prepared_statement:append_param( _pipe@3, Param ) end; {insert_default, _} -> case New_prp_stm_inner =:= New_prp_stm of true -> _pipe@4 = New_prp_stm_inner, cake@internal@prepared_statement:append_sql( _pipe@4, <<"DEFAULT"/utf8>> ); false -> _pipe@5 = New_prp_stm_inner, cake@internal@prepared_statement:append_sql( _pipe@5, <<", DEFAULT"/utf8>> ) end end end ). -spec insert_from_params_apply( cake@internal@prepared_statement:prepared_statement(), list(PYL), fun((PYL) -> insert_row()) ) -> cake@internal@prepared_statement:prepared_statement(). insert_from_params_apply(Prp_stm, Src, Cstr) -> Prp_stm@1 = begin _pipe = Prp_stm, cake@internal@prepared_statement:append_sql(_pipe, <<" ("/utf8>>) end, Prp_stm@2 = begin _pipe@1 = Src, gleam@list:fold( _pipe@1, Prp_stm@1, fun(New_prp_stm, Rcrd) -> {insert_row, Row} = begin _pipe@2 = Rcrd, Cstr(_pipe@2) end, case New_prp_stm =:= Prp_stm@1 of true -> _pipe@3 = New_prp_stm, row_apply(_pipe@3, Row); false -> _pipe@4 = New_prp_stm, _pipe@5 = cake@internal@prepared_statement:append_sql( _pipe@4, <<"), ("/utf8>> ), row_apply(_pipe@5, Row) end end ) end, Prp_stm@3 = begin _pipe@6 = Prp_stm@2, cake@internal@prepared_statement:append_sql(_pipe@6, <<")"/utf8>>) end, Prp_stm@3. -spec insert_from_values_apply( cake@internal@prepared_statement:prepared_statement(), list(insert_row()) ) -> cake@internal@prepared_statement:prepared_statement(). insert_from_values_apply(Prp_stm, Src) -> Prp_stm@1 = begin _pipe = Prp_stm, cake@internal@prepared_statement:append_sql(_pipe, <<" ("/utf8>>) end, Prp_stm@2 = begin _pipe@1 = Src, gleam@list:fold( _pipe@1, Prp_stm@1, fun(New_prp_stm, Row) -> {insert_row, Row@1} = Row, case New_prp_stm =:= Prp_stm@1 of true -> _pipe@2 = New_prp_stm, row_apply(_pipe@2, Row@1); false -> _pipe@3 = New_prp_stm, _pipe@4 = cake@internal@prepared_statement:append_sql( _pipe@3, <<"), ("/utf8>> ), row_apply(_pipe@4, Row@1) end end ) end, Prp_stm@3 = begin _pipe@5 = Prp_stm@2, cake@internal@prepared_statement:append_sql(_pipe@5, <<")"/utf8>>) end, Prp_stm@3. -spec insert_from_query_apply( cake@internal@prepared_statement:prepared_statement(), cake@internal@query:'query'() ) -> cake@internal@prepared_statement:prepared_statement(). insert_from_query_apply(Prp_stm, Qry) -> _pipe = Prp_stm, _pipe@1 = cake@internal@prepared_statement:append_sql(_pipe, <<" ("/utf8>>), _pipe@2 = cake@internal@query:apply(_pipe@1, Qry), cake@internal@prepared_statement:append_sql(_pipe@2, <<")"/utf8>>). -spec on_conflict_target_apply( cake@internal@prepared_statement:prepared_statement(), insert_confict_target() ) -> cake@internal@prepared_statement:prepared_statement(). on_conflict_target_apply(Prp_stm, Cflt_trgt) -> case Cflt_trgt of {insert_conflict_target, Cols} -> _pipe = Prp_stm, cake@internal@prepared_statement:append_sql( _pipe, begin _pipe@1 = Cols, gleam@string:join(_pipe@1, <<", "/utf8>>) end ); {insert_conflict_target_constraint, Cnstrnt} -> _pipe@2 = Prp_stm, cake@internal@prepared_statement:append_sql(_pipe@2, Cnstrnt) end. -spec update_to_write_query(update(PYW)) -> write_query(PYW). update_to_write_query(Insert) -> _pipe = Insert, {update_query, _pipe}. -spec update_modifier_apply( cake@internal@prepared_statement:prepared_statement(), update_modifier() ) -> cake@internal@prepared_statement:prepared_statement(). update_modifier_apply(Prp_stm, Updt_mdfr) -> case Updt_mdfr of no_update_modifier -> Prp_stm; {update_modifier, Mdfr} -> _pipe = Prp_stm, cake@internal@prepared_statement:append_sql( _pipe, <<" "/utf8, Mdfr/binary>> ) end. -spec update_sets_apply( cake@internal@prepared_statement:prepared_statement(), list(update_set()) ) -> cake@internal@prepared_statement:prepared_statement(). update_sets_apply(Prp_stm, Updt_sts) -> Apply_columns = fun(New_prp_stm, Cols) -> case Cols of [] -> _pipe = New_prp_stm, cake@internal@prepared_statement:append_sql(_pipe, <<" "/utf8>>); [Col] -> _pipe@1 = New_prp_stm, cake@internal@prepared_statement:append_sql( _pipe@1, <