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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use crate::stmt::{Node, Statement, Visit, VisitMut};
use super::{Expr, Projection};
use std::{collections::BTreeMap, ops};
/// An ordered map of field assignments for an [`Update`](super::Update) statement.
///
/// Each entry maps a field projection (identifying which field to change) to an
/// [`Assignment`] (how to change it). The entries are ordered by projection,
/// allowing range queries over prefixes.
///
/// # Examples
///
/// ```ignore
/// use toasty_core::stmt::{Assignments, Expr, Projection};
///
/// let mut assignments = Assignments::default();
/// assert!(assignments.is_empty());
///
/// assignments.set(Projection::single(0), Expr::null());
/// assert_eq!(assignments.len(), 1);
/// ```
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Assignments {
/// Map from field projection to assignment. The projection may reference an
/// application-level model field or a lowered table column. Supports both
/// single-step (e.g., `[0]`) and multi-step projections (e.g., `[0, 1]`
/// for nested fields).
assignments: BTreeMap<Projection, Assignment>,
}
/// A field assignment within an [`Update`](super::Update) statement.
///
/// Each variant carries the expression providing the value for the operation.
/// Multiple operations on the same field are represented via [`Batch`](Assignment::Batch).
///
/// # Examples
///
/// ```ignore
/// use toasty_core::stmt::{Assignment, Expr};
///
/// let assignment = Assignment::Set(Expr::null());
/// assert!(assignment.is_set());
/// ```
#[derive(Debug, Clone, PartialEq)]
pub enum Assignment {
/// Set a field, replacing the current value.
Set(Expr),
/// Insert one or more values into a set field.
Insert(Expr),
/// Remove one or more values from a set field.
Remove(Expr),
/// Multiple assignments on the same field.
Batch(Vec<Assignment>),
}
impl Statement {
/// Returns this statement's assignments if it is an `Update`.
pub fn assignments(&self) -> Option<&Assignments> {
match self {
Statement::Update(update) => Some(&update.assignments),
_ => None,
}
}
}
impl Assignments {
/// Creates an empty `Assignments`.
pub fn new() -> Self {
Self {
assignments: BTreeMap::new(),
}
}
/// Returns `true` if there are no assignments.
pub fn is_empty(&self) -> bool {
self.assignments.is_empty()
}
/// Returns the number of assigned projections (keys).
pub fn len(&self) -> usize {
self.assignments.len()
}
/// Returns `true` if an assignment exists for the given projection.
///
/// The `key` accepts any type that implements `AsRef<[usize]>`:
/// - [`Projection`] — look up by projection directly
/// - `&[usize]` — a slice of field indices (e.g., `&[1, 2]`)
/// - `[usize; N]` — a fixed-size array (e.g., `[1]`, `[1, 2]`).
/// Integer literals infer as `usize` from the `AsRef<[usize]>` bound,
/// so `&[1]` works without a suffix.
pub fn contains<Q>(&self, key: &Q) -> bool
where
Q: ?Sized + AsRef<[usize]>,
{
self.assignments.contains_key(key.as_ref())
}
/// Returns a reference to the assignment for the given projection, if any.
///
/// The `key` accepts any type that implements `AsRef<[usize]>`:
/// - [`Projection`] — look up by projection directly
/// - `&[usize]` — a slice of field indices (e.g., `&[1, 2]`)
/// - `[usize; N]` — a fixed-size array (e.g., `[1]`, `[1, 2]`).
/// Integer literals infer as `usize` from the `AsRef<[usize]>` bound,
/// so `&[1]` works without a suffix.
pub fn get<Q>(&self, key: &Q) -> Option<&Assignment>
where
Q: ?Sized + AsRef<[usize]>,
{
self.assignments.get(key.as_ref())
}
/// Returns a mutable reference to the assignment for the given projection.
///
/// The `key` accepts any type that implements `AsRef<[usize]>`:
/// - [`Projection`] — look up by projection directly
/// - `&[usize]` — a slice of field indices (e.g., `&[1, 2]`)
/// - `[usize; N]` — a fixed-size array (e.g., `[1]`, `[1, 2]`).
/// Integer literals infer as `usize` from the `AsRef<[usize]>` bound,
/// so `&[1]` works without a suffix.
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut Assignment>
where
Q: ?Sized + AsRef<[usize]>,
{
self.assignments.get_mut(key.as_ref())
}
/// Sets a field to the given expression value, replacing any existing
/// assignment for that projection.
pub fn set<Q>(&mut self, key: Q, expr: impl Into<Expr>)
where
Q: Into<Projection>,
{
let key = key.into();
self.assignments.insert(key, Assignment::Set(expr.into()));
}
/// Removes the assignment for the given projection, if any.
///
/// The `key` accepts any type that implements `AsRef<[usize]>`:
/// - [`Projection`] — look up by projection directly
/// - `&[usize]` — a slice of field indices (e.g., `&[1, 2]`)
/// - `[usize; N]` — a fixed-size array (e.g., `[1]`, `[1, 2]`).
/// Integer literals infer as `usize` from the `AsRef<[usize]>` bound,
/// so `&[1]` works without a suffix.
pub fn unset<Q>(&mut self, key: &Q)
where
Q: ?Sized + AsRef<[usize]>,
{
self.assignments.remove(key.as_ref());
}
/// Insert a value into a set. The expression should evaluate to a single
/// value that is inserted into the set.
pub fn insert<Q>(&mut self, key: Q, expr: impl Into<Expr>)
where
Q: Into<Projection>,
{
let key = key.into();
let new = Assignment::Insert(expr.into());
self.assignments
.entry(key)
.and_modify(|existing| existing.push(new.clone()))
.or_insert(new);
}
/// Adds a `Remove` assignment for the given projection.
pub fn remove<Q>(&mut self, key: Q, expr: impl Into<Expr>)
where
Q: Into<Projection>,
{
let key = key.into();
let new = Assignment::Remove(expr.into());
self.assignments
.entry(key)
.and_modify(|existing| existing.push(new.clone()))
.or_insert(new);
}
/// Removes and returns the assignment for the given projection.
///
/// The `key` accepts any type that implements `AsRef<[usize]>`:
/// - [`Projection`] — look up by projection directly
/// - `&[usize]` — a slice of field indices (e.g., `&[1, 2]`)
/// - `[usize; N]` — a fixed-size array (e.g., `[1]`, `[1, 2]`).
/// Integer literals infer as `usize` from the `AsRef<[usize]>` bound,
/// so `&[1]` works without a suffix.
pub fn take<Q>(&mut self, key: &Q) -> Option<Assignment>
where
Q: ?Sized + AsRef<[usize]>,
{
self.assignments.remove(key.as_ref())
}
/// Returns an iterator over the assignment projections (keys).
pub fn keys(&self) -> impl Iterator<Item = &Projection> + '_ {
self.assignments.keys()
}
/// Returns an iterator over `(projection, assignment)` pairs.
pub fn iter(&self) -> impl Iterator<Item = (&Projection, &Assignment)> + '_ {
self.assignments.iter()
}
/// Returns a mutable iterator over `(projection, assignment)` pairs.
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&Projection, &mut Assignment)> + '_ {
self.assignments.iter_mut()
}
}
impl IntoIterator for Assignments {
type Item = (Projection, Assignment);
type IntoIter = std::collections::btree_map::IntoIter<Projection, Assignment>;
fn into_iter(self) -> Self::IntoIter {
self.assignments.into_iter()
}
}
impl<'a> IntoIterator for &'a Assignments {
type Item = (&'a Projection, &'a Assignment);
type IntoIter = std::collections::btree_map::Iter<'a, Projection, Assignment>;
fn into_iter(self) -> Self::IntoIter {
self.assignments.iter()
}
}
/// Indexes into the assignments by projection. Panics if no assignment exists
/// for the given key.
///
/// The index accepts any type that implements `AsRef<[usize]>`:
/// [`Projection`], `&[usize]`, or `[usize; N]` arrays.
impl<Q> ops::Index<Q> for Assignments
where
Q: AsRef<[usize]>,
{
type Output = Assignment;
fn index(&self, index: Q) -> &Self::Output {
match self.assignments.get(index.as_ref()) {
Some(ret) => ret,
None => panic!("no assignment for projection"),
}
}
}
/// Mutably indexes into the assignments by projection. Panics if no assignment
/// exists for the given key.
///
/// The index accepts any type that implements `AsRef<[usize]>`:
/// [`Projection`], `&[usize]`, or `[usize; N]` arrays.
impl<Q> ops::IndexMut<Q> for Assignments
where
Q: AsRef<[usize]>,
{
fn index_mut(&mut self, index: Q) -> &mut Self::Output {
match self.assignments.get_mut(index.as_ref()) {
Some(ret) => ret,
None => panic!("no assignment for projection"),
}
}
}
impl Assignment {
/// Returns `true` if this is the `Set` variant.
pub fn is_set(&self) -> bool {
matches!(self, Self::Set(_))
}
/// Returns `true` if this is the `Remove` variant.
pub fn is_remove(&self) -> bool {
matches!(self, Self::Remove(_))
}
/// Appends another assignment, converting to `Batch` if needed.
pub fn push(&mut self, other: Assignment) {
match self {
Self::Batch(entries) => entries.push(other),
_ => {
let prev = std::mem::replace(self, Assignment::Batch(Vec::new()));
if let Assignment::Batch(entries) = self {
entries.push(prev);
entries.push(other);
}
}
}
}
}
impl Node for Assignment {
fn visit<V: Visit>(&self, mut visit: V) {
visit.visit_assignment(self);
}
fn visit_mut<V: VisitMut>(&mut self, mut visit: V) {
visit.visit_assignment_mut(self);
}
}