cql3_parser/
create_keyspace.rs

1use crate::common::Identifier;
2use itertools::Itertools;
3use std::fmt::{Display, Formatter};
4
5/// The data necessary to create a keyspace.
6#[derive(PartialEq, Debug, Clone)]
7pub struct CreateKeyspace {
8    /// the name of the keyspace
9    pub name: Identifier,
10    /// replication strategy options.
11    pub replication: Vec<(String, String)>,
12    /// if specified the DURABLE WRITES option will be output.
13    pub durable_writes: Option<bool>,
14    /// only create if the keyspace does not exist.
15    pub if_not_exists: bool,
16}
17
18impl Display for CreateKeyspace {
19    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
20        if let Some(durable_writes) = self.durable_writes {
21            write!(
22                f,
23                "KEYSPACE {}{} WITH REPLICATION = {{{}}} AND DURABLE_WRITES = {}",
24                if self.if_not_exists {
25                    "IF NOT EXISTS "
26                } else {
27                    ""
28                },
29                self.name,
30                self.replication
31                    .iter()
32                    .map(|(x, y)| format!("{}:{}", x, y))
33                    .join(", "),
34                if durable_writes { "TRUE" } else { "FALSE" }
35            )
36        } else {
37            write!(
38                f,
39                "KEYSPACE {}{} WITH REPLICATION = {{{}}}",
40                if self.if_not_exists {
41                    "IF NOT EXISTS "
42                } else {
43                    ""
44                },
45                self.name,
46                self.replication
47                    .iter()
48                    .map(|(x, y)| format!("{}:{}", x, y))
49                    .join(", ")
50            )
51        }
52    }
53}