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
use indexmap::IndexSet;
use toasty_core::{
schema::db::{ColumnId, TableId},
stmt,
};
use crate::engine::{
exec,
mir::{self, LogicalPlan},
};
/// Updates records by primary key.
///
/// Used with NoSQL drivers to update records given a list of primary key values.
///
/// Keys are always specified as an input node, whether a [`Const`] or the
/// output of a dependent operation.
#[derive(Debug)]
pub(crate) struct UpdateByKey {
/// The node producing the list of primary keys to update.
pub(crate) input: mir::NodeId,
/// The table to update records in.
pub(crate) table: TableId,
/// The field assignments to apply.
pub(crate) assignments: stmt::Assignments,
/// Optional additional filter applied before update.
pub(crate) filter: Option<stmt::Expr>,
/// Optional condition for optimistic locking.
pub(crate) condition: Option<stmt::Expr>,
/// The columns to return for each updated row. Empty when the update
/// returns only an affected-row count.
pub(crate) columns: IndexSet<stmt::ExprReference>,
/// The return type.
pub(crate) ty: stmt::Type,
}
impl UpdateByKey {
pub(crate) fn to_exec(
&self,
logical_plan: &LogicalPlan,
node: &mir::Node,
var_table: &mut exec::VarDecls,
) -> exec::UpdateByKey {
let input = logical_plan[self.input].var.get().unwrap();
let output = var_table.register_var(node.ty().clone());
node.var.set(Some(output));
let returning = if self.ty.is_unit() {
None
} else {
Some(
self.columns
.iter()
.map(|expr_reference| {
let stmt::ExprReference::Column(expr_column) = expr_reference else {
todo!()
};
debug_assert_eq!(expr_column.nesting, 0);
debug_assert_eq!(expr_column.table, 0);
ColumnId {
table: self.table,
index: expr_column.column,
}
})
.collect(),
)
};
exec::UpdateByKey {
input,
output: exec::Output {
var: output,
num_uses: node.num_uses.get(),
},
table: self.table,
assignments: self.assignments.clone(),
filter: self.filter.clone(),
condition: self.condition.clone(),
returning,
}
}
}
impl From<UpdateByKey> for mir::Node {
fn from(value: UpdateByKey) -> Self {
mir::Operation::UpdateByKey(value).into()
}
}