1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//! Ad-hoc joins.
//!
//! [`crate::query::QuerySet::select_related`] follows FK edges for you.
//! This module is the escape hatch: join any table on any predicate.
//! The predicate is a plain [`WhereExpr`], the same type `WHERE` uses,
//! so `and()` / `or()` / `Not` / function calls all work in the `ON`
//! clause.
//!
//! [`WhereExpr`]: crate::core::WhereExpr
//!
//! ```ignore
//! use rustango::core::joins::aliased;
//! use rustango::core::{JoinKind, Op, WhereExpr};
//!
//! // INNER JOIN comment AS c ON c.post_id = post.id AND c.is_approved = true
//! let on = WhereExpr::And(vec![
//! WhereExpr::ExprCompare {
//! lhs: aliased("c", "post_id"),
//! op: Op::Eq,
//! rhs: aliased("post", "id"),
//! },
//! WhereExpr::Predicate(rustango::core::Filter::new(
//! "is_approved",
//! Op::Eq,
//! rustango::core::SqlValue::Bool(true),
//! )),
//! ]);
//! Post::objects()
//! .join(Comment::SCHEMA, "c", JoinKind::Inner, on)
//! .fetch(&pool).await?;
//! ```
//!
//! ## Naming columns inside `on`
//!
//! - Bare `Filter` / `ColumnFilter` columns belong to the joined
//! alias, the one you passed to `.join(...)`.
//! - `aliased(alias, col)` emits `"<alias>"."<col>"`. Use it to point
//! back at the outer table or at an earlier join. The outer alias is
//! the outer model's `table` name.
//! - `WhereExpr::ExprCompare` takes an alias on both sides. Use it for
//! the column-on-column join condition.
//!
//! ## When to reach for ad-hoc joins
//!
//! | Need | Tool |
//! |---|---|
//! | Pull related rows along with the main row | `select_related` |
//! | Filter the main rows by a related-table predicate | `exists(...)` / `not_exists(...)` |
//! | Need both joined columns AND a custom join predicate | `join(...)` |
//! | One-shot anti-join | `not_exists(...)` |
//!
//! ## Dialect support
//!
//! `Inner` and `Left` work everywhere. `Right` is PG and MySQL only.
//! `Full` is PG only. The others raise
//! [`SqlError::JoinKindNotSupported`].
//!
//! [`SqlError::JoinKindNotSupported`]: crate::sql::SqlError::JoinKindNotSupported
use Expr;
use ;
use SqlValue;
/// Shorthand for [`Expr::AliasedColumn`]. The alias is either a join's
/// alias (the second argument to `.join(...)`) or the outer model's
/// `table` name.
/// Predicate builder for JOIN `on` clauses. Emits
/// `"<alias>"."<col>" <op> <value>`. Use it whenever the column you
/// filter on does not belong to the join's own alias.
///
/// # Why prefer this over a bare typed filter
///
/// Inside `on`, bare `Filter` columns are qualified with the joined
/// alias. So `Post::status.eq("draft").into()`, where `Post` is the
/// *outer* model, emits `"<joined_alias>"."status"`. The
/// `TypedFilter<Post>` loses its model tag at the `Into<WhereExpr>`
/// boundary, so the compiler cannot catch this.
///
/// `col_filter` sets the alias itself through
/// [`Expr::AliasedColumn`] + [`WhereExpr::ExprCompare`], so the SQL
/// matches the call site:
///
/// ```ignore
/// use rustango::core::joins::col_filter;
/// use rustango::core::Op;
///
/// // ON c.post_id = post.id AND post.status = 'draft'
/// let on = WhereExpr::And(vec![
/// WhereExpr::ExprCompare {
/// lhs: aliased("c", "post_id"),
/// op: Op::Eq,
/// rhs: aliased("post", "id"),
/// },
/// // Safe: explicitly qualifies to the outer table.
/// col_filter("post", "status", Op::Eq, "draft"),
/// ]);
/// ```
///
/// Only the comparison ops (`Eq`, `Ne`, `Lt`, `Lte`, `Gt`, `Gte`) make
/// sense here. Anything else fails with
/// [`SqlError::OpNotSupportedInDialect`] when the SQL is written. For
/// `IN` / `BETWEEN` / `IS NULL` on an aliased column, build the
/// `WhereExpr` by hand.
///
/// [`SqlError::OpNotSupportedInDialect`]: crate::sql::SqlError::OpNotSupportedInDialect