Packages
cake
4.0.0
4.1.0
4.0.0
3.0.0
2.2.2
2.2.1
2.2.0
2.1.3
2.1.2
2.1.1
2.1.0
2.0.2
2.0.1
2.0.0
1.1.2
1.1.1
1.1.0
1.0.1
1.0.0
1.0.0-rc.0
retired
0.15.0
retired
0.14.0
retired
0.13.0
retired
0.12.0
retired
0.11.0
retired
0.10.1
retired
0.10.0
retired
0.9.2
retired
0.9.1
retired
0.9.0
retired
0.8.0
retired
0.7.0
retired
0.6.0
retired
0.5.0
retired
0.4.0
retired
0.3.0
retired
0.2.0
retired
0.1.0
retired
0.0.1
retired
π An SQL query builder for Gleam for SQL dialects πPostgreSQL, πͺΆSQLite, π¦MariaDB, and π¬MySQL
Current section
Files
Jump to
Current section
Files
src/cake/join.gleam
//// Functions to build `JOIN` clauses of SQL queries.
////
//// Tables, views and sub-queries can be joined together.
////
//// ## Supported join kinds
////
//// - `INNER JOIN`
//// - `LEFT JOIN`, inclusive, same as `LEFT OUTER JOIN`,
//// - `RIGHT JOIN`, inclusive, same as `RIGHT OUTER JOIN`,
//// - `FULL JOIN`, inclusive, same as `FULL OUTER JOIN`,
//// - `CROSS JOIN`
////
//// You can also build following joins using the provided query builder
//// functions:
////
//// - `SELF JOIN`: Use the same table, view, or sub-query with a different
//// alias.
//// - `EXCLUSIVE LEFT JOIN`: `WHERE b.key IS NULL`
//// - `EXCLUSIVE RIGHT JOIN`: `WHERE a.key IS NULL`
//// - `EXCLUSIVE FULL JOIN`: `WHERE a.key IS NULL OR b.key IS NULL`
////
import cake/internal/read_query.{
CrossJoin, CrossJoinLateral, FullJoin, InnerJoin, InnerJoinLateralOnTrue,
JoinSubQuery, JoinTable, LeftJoin, LeftJoinLateralOnTrue, RightJoin,
}
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// β read_query type re-exports β
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
pub type Join =
read_query.Join
pub type JoinTarget =
read_query.JoinTarget
pub type ReadQuery =
read_query.ReadQuery
pub type Where =
read_query.Where
/// Create a `JOIN` target from a table name.
///
pub fn table(table_name table_name: String) -> JoinTarget {
table_name |> JoinTable
}
/// Create a `JOIN` target from a sub-query.
///
pub fn sub_query(sub_query sub_query: ReadQuery) -> JoinTarget {
sub_query |> JoinSubQuery
}
/// Create an `INNER JOIN`.
///
pub fn inner(with with: JoinTarget, on on: Where, alias alias: String) -> Join {
with |> InnerJoin(alias:, on:)
}
/// Creates a `LEFT JOIN`.
///
/// Also called `LEFT OUTER JOIN`.
///
/// _Inclusive_ by default.
///
/// Set `on` to `WHERE a.key IS NULL` to make it _exclusive_.
///
pub fn left(with with: JoinTarget, on on: Where, alias alias: String) -> Join {
with |> LeftJoin(alias:, on:)
}
/// Creates a `RIGHT JOIN`.
///
/// Also called `RIGHT OUTER JOIN`.
///
/// _Inclusive_ by default.
///
/// Set `on` to `WHERE b.key IS NULL` to make it _exclusive_.
///
pub fn right(with with: JoinTarget, on on: Where, alias alias: String) -> Join {
with |> RightJoin(alias:, on:)
}
/// Creates a `FULL JOIN`.
///
/// Also called `FULL OUTER JOIN`.
///
/// _Inclusive_ by default.
///
/// Set `on` to `WHERE a.key IS NULL OR b.key IS NULL` to make it _exclusive_.
///
pub fn full(with with: JoinTarget, on on: Where, alias alias: String) -> Join {
with |> FullJoin(alias:, on:)
}
/// Creates a `CROSS JOIN`.
///
/// Also called _cartesian product_.
///
pub fn cross(with with: JoinTarget, alias alias: String) -> Join {
with |> CrossJoin(alias:)
}
/// Creates a `INNER JOIN LATERAL ... ON TRUE`.
///
/// β οΈβ οΈβ οΈ
///
/// CAUTION: `LATERAL` joins are not optimized by the query planner,
/// and can be very slow on large datasets, especially when the sub-query
/// returns many rows.
///
/// β οΈβ οΈβ οΈ
///
/// See <https://www.postgresql.org/docs/9.3/sql-select.html#SQL-FROM> for an
/// explanation on how `LATERAL` works.
///
/// Any filtering must be done in WHERE clauses as the JOIN ON clause is always
/// TRUE when calling this function.
///
/// NOTICE: `LATERAL` is supported by πPostgreSQL 9.3+ and recent π¬MySQL
/// versions.
///
pub fn inner_lateral(with with: JoinTarget, alias alias: String) -> Join {
with |> InnerJoinLateralOnTrue(alias:)
}
/// Creates a `LEFT JOIN LATERAL ... ON TRUE`.
///
/// β οΈβ οΈβ οΈ
///
/// CAUTION: `LATERAL` joins are not optimized by the query planner,
/// and can be very slow on large datasets, especially when the sub-query
/// returns many rows.
///
/// β οΈβ οΈβ οΈ
///
/// See <https://www.postgresql.org/docs/9.3/sql-select.html#SQL-FROM> for an
/// explanation on how `LATERAL` works.
///
/// Any filtering must be done in WHERE clauses as the JOIN ON clause is always
/// TRUE when calling this function.
///
/// NOTICE: `LATERAL` is supported by πPostgreSQL 9.3+ and recent π¬MySQL
/// versions.
///
pub fn left_lateral(with with: JoinTarget, alias alias: String) -> Join {
with |> LeftJoinLateralOnTrue(alias:)
}
/// Creates a `CROSS JOIN LATERAL`.
///
/// β οΈβ οΈβ οΈ
///
/// CAUTION: `LATERAL` joins are not optimized by the query planner,
/// and can be very slow on large datasets, especially when the sub-query
/// returns many rows.
///
/// β οΈβ οΈβ οΈ
///
/// See <https://www.postgresql.org/docs/9.3/sql-select.html#SQL-FROM> for an
/// explanation on how `LATERAL` works.
///
/// NOTICE: `LATERAL` is supported by πPostgreSQL 9.3+ and recent π¬MySQL
/// versions.
///
pub fn cross_lateral(with with: JoinTarget, alias alias: String) -> Join {
with |> CrossJoinLateral(alias:)
}