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
use crate::begin_batch::BeginBatch;
use crate::common::{FQName, Operand, RelationElement, TtlTimestamp};
use crate::delete::IndexedColumn;
use itertools::Itertools;
use std::fmt::{Display, Formatter};
#[derive(PartialEq, Debug, Clone)]
pub struct Update {
pub begin_batch: Option<BeginBatch>,
pub table_name: FQName,
pub using_ttl: Option<TtlTimestamp>,
pub assignments: Vec<AssignmentElement>,
pub where_clause: Vec<RelationElement>,
pub if_clause: Vec<RelationElement>,
pub if_exists: bool,
}
impl Display for Update {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}UPDATE {}{} SET {} WHERE {}{}",
self.begin_batch
.as_ref()
.map_or("".to_string(), |x| x.to_string()),
self.table_name,
self.using_ttl
.as_ref()
.map_or("".to_string(), |x| x.to_string()),
self.assignments.iter().map(|a| a.to_string()).join(", "),
self.where_clause.iter().join(" AND "),
if !self.if_clause.is_empty() {
format!(" IF {}", self.if_clause.iter().join(" AND "))
} else if self.if_exists {
" IF EXISTS".to_string()
} else {
"".to_string()
}
)
}
}
#[derive(PartialEq, Debug, Clone)]
pub struct AssignmentElement {
pub name: IndexedColumn,
pub value: Operand,
pub operator: Option<AssignmentOperator>,
}
impl Display for AssignmentElement {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match &self.operator {
Some(x) => write!(f, "{} = {}{}", self.name, self.value, x),
None => write!(f, "{} = {}", self.name, self.value),
}
}
}
#[derive(PartialEq, Debug, Clone)]
pub enum AssignmentOperator {
Plus(Operand),
Minus(Operand),
}
impl Display for AssignmentOperator {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
AssignmentOperator::Plus(op) => write!(f, " + {}", op),
AssignmentOperator::Minus(op) => write!(f, " - {}", op),
}
}
}