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
use indexmap::IndexSet;
use toasty_core::stmt;
use crate::engine::{
exec,
mir::{self, LogicalPlan},
};
/// Performs an optimistic read-modify-write operation.
///
/// Used for conditional updates where the write only succeeds if the values
/// read have not been modified since reading. This is a fallback for databases
/// that do not support conditional updates in a single statement (e.g., SQLite,
/// MySQL without CTE support).
#[derive(Debug)]
pub(crate) struct ReadModifyWrite {
/// Nodes providing input arguments for the statements.
pub(crate) inputs: IndexSet<mir::NodeId>,
/// The read query that fetches current values.
pub(crate) read: stmt::Query,
/// The write statement to execute if the condition holds.
pub(crate) write: stmt::Statement,
/// The return type.
pub(crate) ty: stmt::Type,
}
impl ReadModifyWrite {
pub(crate) fn to_exec(
&self,
logical_plan: &LogicalPlan,
node: &mir::Node,
var_table: &mut exec::VarDecls,
) -> exec::ReadModifyWrite {
let input = self
.inputs
.iter()
.map(|input| logical_plan[input].var.get().unwrap())
.collect();
let var = var_table.register_var(self.ty.clone());
node.var.set(Some(var));
// When the write carries a `RETURNING`, the node's type is
// `List<Record>`; without one the type is `Unit` and the write reports
// only a row count.
let output_ty = mir::row_field_types(&self.ty);
exec::ReadModifyWrite {
input,
output: exec::Output {
var,
num_uses: node.num_uses.get(),
},
output_ty,
read: self.read.clone(),
write: self.write.clone(),
}
}
}
impl From<ReadModifyWrite> for mir::Node {
fn from(value: ReadModifyWrite) -> Self {
mir::Operation::ReadModifyWrite(Box::new(value)).into()
}
}