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
159
160
161
162
163
164
165
166
167
168
//! Ad-hoc joins — issue #80.
//!
//! Where [`crate::core::QuerySet::select_related`] follows FK edges
//! automatically, this module adds a SQLAlchemy-shape escape hatch
//! for joining against an arbitrary table with an arbitrary predicate.
//! The predicate accepts the same [`WhereExpr`] machinery that powers
//! `WHERE` clauses, so `and()` / `or()` / `Not` / function calls /
//! sub-conditions all compose freely inside the JOIN `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 {
//! column: "is_approved",
//! op: Op::Eq,
//! value: rustango::core::SqlValue::Bool(true),
//! }),
//! ]);
//! Post::objects()
//! .join(Comment::SCHEMA, "c", JoinKind::Inner, on)
//! .fetch(&pool).await?;
//! ```
//!
//! ## Column qualification inside `on`
//!
//! - **Bare `Filter` / `ColumnFilter` columns** resolve to the joined
//! alias (i.e. the `<alias>` you passed to `.join(...)`). That's
//! the natural reading when most of the predicate is about the
//! joined table.
//! - **`aliased(alias, col)`** emits `"<alias>"."<col>"` explicitly,
//! for cross-references back to the outer table or to a previously
//! joined alias. Use the outer model's `table` name as the alias
//! when referring to the outer side.
//! - **`WhereExpr::ExprCompare`** lets both sides carry their own
//! alias via `aliased(...)`. Use this for the column-on-column
//! join condition.
//!
//! ## When to reach for ad-hoc joins
//!
//! | Need | Tool |
//! |---|---|
//! | Pull related rows along with the main row (Django shape) | `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-portability
//!
//! - `Inner` / `Left` — every dialect.
//! - `Right` — PG + MySQL only. SQLite raises
//! [`SqlError::JoinKindNotSupported`].
//! - `Full` — PG only. MySQL + SQLite raise
//! [`SqlError::JoinKindNotSupported`].
//!
//! [`SqlError::JoinKindNotSupported`]: crate::sql::SqlError::JoinKindNotSupported
use Expr;
use ;
use SqlValue;
/// Shorthand for [`Expr::AliasedColumn`]. The alias can be a join's
/// explicit alias (the second arg to `.join(...)`) or the outer
/// model's `table` name when referring back to the outer side.
/// Safe predicate-builder for JOIN `on` clauses: emits
/// `"<alias>"."<col>" <op> <value>`. Use this whenever filtering
/// inside an `on` predicate against a column whose table isn't the
/// join's default alias.
///
/// # Why prefer this over a bare typed filter
///
/// Inside an `on` predicate, the writer auto-qualifies bare
/// `Filter` columns to the joined alias. If you write
/// `Post::status.eq("draft").into()` — where `Post` is the *outer*
/// model, not the joined one — the writer emits
/// `"<joined_alias>"."status"`, **not** `"<post_table>"."status"`.
/// The `TypedFilter<Post>` loses its model tag at the
/// `Into<WhereExpr>` boundary, so the compiler can't catch this.
///
/// `col_filter` forces an explicit alias on the LHS by routing
/// through [`Expr::AliasedColumn`] + [`WhereExpr::ExprCompare`], so
/// the emitted SQL matches the call site verbatim:
///
/// ```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 binary-comparison ops (`Eq`, `Ne`, `Lt`, `Lte`, `Gt`, `Gte`)
/// are meaningful here — other ops surface as
/// [`SqlError::OpNotSupportedInDialect`] at emit time. For `IN` /
/// `BETWEEN` / `IS NULL` against an aliased column, drop into a
/// hand-rolled `WhereExpr` until a richer helper ships.
///
/// [`SqlError::OpNotSupportedInDialect`]: crate::sql::SqlError::OpNotSupportedInDialect