-module(cake@query@combined). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([to_query/1, union/2, union_many/3, union_all/2, union_all_many/3, except/2, except_many/3, except_all/2, except_all_many/3, intersect/2, intersect_many/3, intersect_all/2, intersect_all_many/3, get_queries/1, limit/2, get_limit/1, offset/2, get_offset/1, order_asc/2, order_asc_nulls_first/2, order_asc_replace/2, order_asc_nulls_first_replace/2, order_desc/2, order_desc_nulls_first/2, order_desc_replace/2, order_desc_nulls_first_replace/2, order/3, order_replace/3, epilog/2, no_epilog/1]). -export_type([direction/0]). -type direction() :: asc | desc. -spec to_query(cake@internal@query:combined()) -> cake@internal@query:'query'(). to_query(Qry) -> _pipe = Qry, {combined_query, _pipe}. -spec union(cake@internal@query:select(), cake@internal@query:select()) -> cake@internal@query:combined(). union(Qry_a, Qry_b) -> _pipe = union_distinct, cake@internal@query:combined_query_new(_pipe, [Qry_a, Qry_b]). -spec union_many( cake@internal@query:select(), cake@internal@query:select(), list(cake@internal@query:select()) ) -> cake@internal@query:combined(). union_many(Qry_a, Qry_b, Mr_qrys) -> _pipe = union_distinct, cake@internal@query:combined_query_new(_pipe, [Qry_a, Qry_b | Mr_qrys]). -spec union_all(cake@internal@query:select(), cake@internal@query:select()) -> cake@internal@query:combined(). union_all(Qry_a, Qry_b) -> _pipe = union_all, cake@internal@query:combined_query_new(_pipe, [Qry_a, Qry_b]). -spec union_all_many( cake@internal@query:select(), cake@internal@query:select(), list(cake@internal@query:select()) ) -> cake@internal@query:combined(). union_all_many(Qry_a, Qry_b, Mr_qrys) -> _pipe = union_all, cake@internal@query:combined_query_new(_pipe, [Qry_a, Qry_b | Mr_qrys]). -spec except(cake@internal@query:select(), cake@internal@query:select()) -> cake@internal@query:combined(). except(Qry_a, Qry_b) -> _pipe = except_distinct, cake@internal@query:combined_query_new(_pipe, [Qry_a, Qry_b]). -spec except_many( cake@internal@query:select(), cake@internal@query:select(), list(cake@internal@query:select()) ) -> cake@internal@query:combined(). except_many(Qry_a, Qry_b, Mr_qrys) -> _pipe = except_distinct, cake@internal@query:combined_query_new(_pipe, [Qry_a, Qry_b | Mr_qrys]). -spec except_all(cake@internal@query:select(), cake@internal@query:select()) -> cake@internal@query:combined(). except_all(Qry_a, Qry_b) -> _pipe = except_all, cake@internal@query:combined_query_new(_pipe, [Qry_a, Qry_b]). -spec except_all_many( cake@internal@query:select(), cake@internal@query:select(), list(cake@internal@query:select()) ) -> cake@internal@query:combined(). except_all_many(Qry_a, Qry_b, Mr_qrys) -> _pipe = except_all, cake@internal@query:combined_query_new(_pipe, [Qry_a, Qry_b | Mr_qrys]). -spec intersect(cake@internal@query:select(), cake@internal@query:select()) -> cake@internal@query:combined(). intersect(Qry_a, Qry_b) -> _pipe = intersect_distinct, cake@internal@query:combined_query_new(_pipe, [Qry_a, Qry_b]). -spec intersect_many( cake@internal@query:select(), cake@internal@query:select(), list(cake@internal@query:select()) ) -> cake@internal@query:combined(). intersect_many(Qry_a, Qry_b, Mr_qrys) -> _pipe = intersect_distinct, cake@internal@query:combined_query_new(_pipe, [Qry_a, Qry_b | Mr_qrys]). -spec intersect_all(cake@internal@query:select(), cake@internal@query:select()) -> cake@internal@query:combined(). intersect_all(Qry_a, Qry_b) -> _pipe = intersect_all, cake@internal@query:combined_query_new(_pipe, [Qry_a, Qry_b]). -spec intersect_all_many( cake@internal@query:select(), cake@internal@query:select(), list(cake@internal@query:select()) ) -> cake@internal@query:combined(). intersect_all_many(Qry_a, Qry_b, Mr_qrys) -> _pipe = intersect_all, cake@internal@query:combined_query_new(_pipe, [Qry_a, Qry_b | Mr_qrys]). -spec get_queries(cake@internal@query:combined()) -> list(cake@internal@query:select()). get_queries(Qry) -> erlang:element(3, Qry). -spec limit(cake@internal@query:combined(), integer()) -> cake@internal@query:combined(). limit(Qry, Lmt) -> Lmt@1 = begin _pipe = Lmt, cake@internal@query:limit_new(_pipe) end, erlang:setelement(4, Qry, Lmt@1). -spec get_limit(cake@internal@query:combined()) -> cake@internal@query:limit(). get_limit(Qry) -> erlang:element(4, Qry). -spec offset(cake@internal@query:combined(), integer()) -> cake@internal@query:combined(). offset(Qry, Offst) -> Offst@1 = begin _pipe = Offst, cake@internal@query:offset_new(_pipe) end, erlang:setelement(5, Qry, Offst@1). -spec get_offset(cake@internal@query:combined()) -> cake@internal@query:offset(). get_offset(Qry) -> erlang:element(5, Qry). -spec map_order_by_direction_constructor(direction()) -> cake@internal@query:order_by_direction(). map_order_by_direction_constructor(In) -> case In of asc -> asc; desc -> desc end. -spec order_asc(cake@internal@query:combined(), binary()) -> cake@internal@query:combined(). order_asc(Qry, Ordb) -> _pipe = Qry, cake@internal@query:combined_order_by( _pipe, {order_by, [{order_by_column, Ordb, asc}]}, true ). -spec order_asc_nulls_first(cake@internal@query:combined(), binary()) -> cake@internal@query:combined(). order_asc_nulls_first(Qry, Ordb) -> _pipe = Qry, cake@internal@query:combined_order_by( _pipe, {order_by, [{order_by_column, Ordb, asc_nulls_first}]}, true ). -spec order_asc_replace(cake@internal@query:combined(), binary()) -> cake@internal@query:combined(). order_asc_replace(Qry, Ordb) -> _pipe = Qry, cake@internal@query:combined_order_by( _pipe, {order_by, [{order_by_column, Ordb, asc}]}, false ). -spec order_asc_nulls_first_replace(cake@internal@query:combined(), binary()) -> cake@internal@query:combined(). order_asc_nulls_first_replace(Qry, Ordb) -> _pipe = Qry, cake@internal@query:combined_order_by( _pipe, {order_by, [{order_by_column, Ordb, asc_nulls_first}]}, false ). -spec order_desc(cake@internal@query:combined(), binary()) -> cake@internal@query:combined(). order_desc(Qry, Ordb) -> _pipe = Qry, cake@internal@query:combined_order_by( _pipe, {order_by, [{order_by_column, Ordb, desc}]}, true ). -spec order_desc_nulls_first(cake@internal@query:combined(), binary()) -> cake@internal@query:combined(). order_desc_nulls_first(Qry, Ordb) -> _pipe = Qry, cake@internal@query:combined_order_by( _pipe, {order_by, [{order_by_column, Ordb, desc_nulls_first}]}, true ). -spec order_desc_replace(cake@internal@query:combined(), binary()) -> cake@internal@query:combined(). order_desc_replace(Qry, Ordb) -> _pipe = Qry, cake@internal@query:combined_order_by( _pipe, {order_by, [{order_by_column, Ordb, desc}]}, false ). -spec order_desc_nulls_first_replace(cake@internal@query:combined(), binary()) -> cake@internal@query:combined(). order_desc_nulls_first_replace(Qry, Ordb) -> _pipe = Qry, cake@internal@query:combined_order_by( _pipe, {order_by, [{order_by_column, Ordb, desc_nulls_first}]}, false ). -spec order(cake@internal@query:combined(), binary(), direction()) -> cake@internal@query:combined(). order(Qry, Ordb, Dir) -> Dir@1 = begin _pipe = Dir, map_order_by_direction_constructor(_pipe) end, _pipe@1 = Qry, cake@internal@query:combined_order_by( _pipe@1, {order_by, [{order_by_column, Ordb, Dir@1}]}, true ). -spec order_replace(cake@internal@query:combined(), binary(), direction()) -> cake@internal@query:combined(). order_replace(Qry, Ordb, Dir) -> Dir@1 = begin _pipe = Dir, map_order_by_direction_constructor(_pipe) end, _pipe@1 = Qry, cake@internal@query:combined_order_by( _pipe@1, {order_by, [{order_by_column, Ordb, Dir@1}]}, false ). -spec epilog(cake@internal@query:combined(), binary()) -> cake@internal@query:combined(). epilog(Qry, Eplg) -> Eplg@1 = begin _pipe = Eplg, gleam@string:trim(_pipe) end, case Eplg@1 of <<""/utf8>> -> erlang:setelement(7, Qry, no_epilog); _ -> erlang:setelement( 7, Qry, begin _pipe@1 = (<<" "/utf8, Eplg@1/binary>>), {epilog, _pipe@1} end ) end. -spec no_epilog(cake@internal@query:combined()) -> cake@internal@query:combined(). no_epilog(Qry) -> erlang:setelement(7, Qry, no_epilog).