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#[derive(PartialEq, Debug, Clone)]
9pub struct Insert {
10 pub begin_batch: Option<BeginBatch>,
12 pub table_name: FQName,
14 pub columns: Vec<Identifier>,
16 pub values: InsertValues,
18 pub using_ttl: Option<TtlTimestamp>,
20 pub if_not_exists: bool,
22}
23
24impl Insert {
25 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 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#[derive(PartialEq, Debug, Clone)]
68pub enum InsertValues {
69 Values(Vec<Operand>),
71 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}