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};

/// data for `Update` statements
#[derive(PartialEq, Debug, Clone)]
pub struct Update {
    /// if present then statement starts with BEGIN BATCH
    pub begin_batch: Option<BeginBatch>,
    /// the table name to update
    pub table_name: FQName,
    /// if present then the TTL Timestamp for the update
    pub using_ttl: Option<TtlTimestamp>,
    /// the column assignments for the update.
    pub assignments: Vec<AssignmentElement>,
    /// the where clause
    pub where_clause: Vec<RelationElement>,
    /// if present a list of key,values for the `IF` clause
    pub if_clause: Vec<RelationElement>,
    /// if true and `if_clause` is NONE then  `IF EXISTS` is added to the statement
    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()
            }
        )
    }
}

/// defines an assignment element comprising the column, the value, and an optional +/- value operator.
#[derive(PartialEq, Debug, Clone)]
pub struct AssignmentElement {
    /// the column to set the value for.
    pub name: IndexedColumn,
    /// the column value
    pub value: Operand,
    /// an optional +/- value
    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),
        }
    }
}

/// Defines the optional +/- value for an assignment
#[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),
        }
    }
}