cql3_parser/
insert.rs

1use crate::begin_batch::BeginBatch;
2use crate::common::{FQName, Identifier, Operand, TtlTimestamp};
3use itertools::Itertools;
4use std::collections::BTreeMap;
5use std::fmt::{Display, Formatter};
6
7/// the data for insert statements.
8#[derive(PartialEq, Debug, Clone)]
9pub struct Insert {
10    /// if set the statement starts with `BEGIN BATCH`
11    pub begin_batch: Option<BeginBatch>,
12    /// the table name
13    pub table_name: FQName,
14    /// an the list of of column names to insert into.
15    pub columns: Vec<Identifier>,
16    /// the `VALUES` to insert
17    pub values: InsertValues,
18    /// if set the timestamp for `USING TTL`
19    pub using_ttl: Option<TtlTimestamp>,
20    /// if true then `IF NOT EXISTS` is added to the statement
21    pub if_not_exists: bool,
22}
23
24impl Insert {
25    /// return a sorted map of column names to Operands.
26    pub fn get_value_map(&self) -> BTreeMap<Identifier, &Operand> {
27        let mut result = BTreeMap::new();
28        match &self.values {
29            InsertValues::Values(operands) => {
30                // if there is a column mismatch we have a problem so
31                // return an empty list
32                if self.columns.len() == operands.len() {
33                    for (i, operand) in operands.iter().enumerate() {
34                        result.insert(self.columns[i].clone(), operand);
35                    }
36                }
37            }
38            InsertValues::Json(_) => {}
39        }
40        result
41    }
42}
43impl Display for Insert {
44    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
45        write!(
46            f,
47            "{}INSERT INTO {} ({}) {}{}{}",
48            self.begin_batch
49                .as_ref()
50                .map_or("".to_string(), |x| x.to_string()),
51            self.table_name,
52            self.columns.iter().map(|c| c.to_string()).join(", "),
53            self.values,
54            if self.if_not_exists {
55                " IF NOT EXISTS"
56            } else {
57                ""
58            },
59            self.using_ttl
60                .as_ref()
61                .map_or("".to_string(), |x| x.to_string()),
62        )
63    }
64}
65
66/// The structure that describs the values to insert.
67#[derive(PartialEq, Debug, Clone)]
68pub enum InsertValues {
69    /// this is the standard list of values.
70    Values(Vec<Operand>),
71    /// this option allows JSON string to define the values.
72    Json(String),
73}
74
75impl Display for InsertValues {
76    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
77        match self {
78            InsertValues::Values(columns) => {
79                write!(f, "VALUES ({})", columns.iter().join(", "))
80            }
81            InsertValues::Json(text) => {
82                write!(f, "JSON {}", text)
83            }
84        }
85    }
86}