Skip to content

Commit

Permalink
order by handling for unions
Browse files Browse the repository at this point in the history
  • Loading branch information
inoas committed Apr 30, 2024
1 parent 06befd2 commit eb9c23c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/cake.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn run_dummy_union_all() {
// |> query.select_query_order_asc("name")

let union_query =
query.combined_except_all_query_new([select_query_a, select_query_b])
query.combined_union_all_query_new([select_query_a, select_query_b])
// |> query.union_set_limit(1)
|> query.query_combined_wrap
|> iox.dbg
Expand Down
58 changes: 26 additions & 32 deletions src/cake/internal/query.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ pub type CombinedKind {
Union
UnionAll
Except
// ExceptAll Does not work on SQLite
ExceptAll
Intersect
// IntersectAll Does not work on SQLite
IntersectAll
}

Expand All @@ -37,6 +39,7 @@ pub type CombinedQuery {
kind: CombinedKind,
select_queries: List(SelectQuery),
limit_offset: LimitOffsetPart,
// TODO: before adding epilog to combined, fix it for selects with a custom type
// order_by: List(OrderByPart),
// Epilog allows you to append raw SQL to the end of queries.
// You should never put raw user data into epilog.
Expand All @@ -50,69 +53,60 @@ pub type CombinedQuery {
pub fn combined_union_query_new(
select_queries slct_qrys: List(SelectQuery),
) -> CombinedQuery {
CombinedQuery(
kind: Union,
select_queries: slct_qrys,
limit_offset: NoLimitOffset,
epilog: NoEpilogPart,
)
Union |> combined_query_query(slct_qrys)
}

pub fn combined_union_all_query_new(
select_queries slct_qrys: List(SelectQuery),
) -> CombinedQuery {
CombinedQuery(
kind: UnionAll,
select_queries: slct_qrys,
limit_offset: NoLimitOffset,
epilog: NoEpilogPart,
)
UnionAll |> combined_query_query(slct_qrys)
}

pub fn combined_except_query_new(
select_queries slct_qrys: List(SelectQuery),
) -> CombinedQuery {
CombinedQuery(
kind: Except,
select_queries: slct_qrys,
limit_offset: NoLimitOffset,
epilog: NoEpilogPart,
)
Except |> combined_query_query(slct_qrys)
}

pub fn combined_except_all_query_new(
select_queries slct_qrys: List(SelectQuery),
) -> CombinedQuery {
CombinedQuery(
kind: ExceptAll,
select_queries: slct_qrys,
limit_offset: NoLimitOffset,
epilog: NoEpilogPart,
)
ExceptAll |> combined_query_query(slct_qrys)
}

pub fn combined_intersect_query_new(
select_queries slct_qrys: List(SelectQuery),
) -> CombinedQuery {
CombinedQuery(
kind: Intersect,
select_queries: slct_qrys,
limit_offset: NoLimitOffset,
epilog: NoEpilogPart,
)
Intersect |> combined_query_query(slct_qrys)
}

pub fn combined_intersect_all_query_new(
select_queries slct_qrys: List(SelectQuery),
) -> CombinedQuery {
IntersectAll |> combined_query_query(slct_qrys)
}

fn combined_query_query(
kind: CombinedKind,
select_queries: List(SelectQuery),
) -> CombinedQuery {
let select_queries =
combined_query_remove_order_by_from_selects(select_queries)
CombinedQuery(
kind: IntersectAll,
select_queries: slct_qrys,
kind: kind,
select_queries: select_queries,
limit_offset: NoLimitOffset,
epilog: NoEpilogPart,
)
}

fn combined_query_remove_order_by_from_selects(
select_queries slct_qrys: List(SelectQuery),
) -> List(SelectQuery) {
slct_qrys
|> list.map(fn(slct_qry) { SelectQuery(..slct_qry, order_by: []) })
}

pub fn combined_get_select_queries(
combined_query cq: CombinedQuery,
) -> List(SelectQuery) {
Expand Down

0 comments on commit eb9c23c

Please sign in to comment.