clickhouse_client/schema/
mod.rs

1//! DDL
2
3mod query;
4
5pub use query::*;
6
7use crate::value::Type;
8
9/// Table schema
10#[derive(Debug)]
11pub struct TableSchema {
12    /// Name
13    pub name: String,
14    /// Columns
15    pub columns: Vec<ColumnSchema>,
16}
17
18impl TableSchema {
19    /// Creates a new table schema with columns
20    pub fn new(name: &str) -> Self {
21        Self {
22            name: name.to_string(),
23            columns: vec![],
24        }
25    }
26
27    /// Adds a column
28    pub fn with_column(mut self, column: ColumnSchema) -> Self {
29        self.columns.push(column);
30        self
31    }
32
33    /// Adds a column with its fields
34    pub fn column(mut self, id: &str, ty: Type, primary: bool) -> Self {
35        self.columns.push(ColumnSchema::new(id, ty, primary));
36        self
37    }
38
39    /// Adds a column
40    pub fn add_column(&mut self, id: &str, ty: Type, primary: bool) -> &mut Self {
41        self.columns.push(ColumnSchema::new(id, ty, primary));
42        self
43    }
44
45    /// Returns a column by ID
46    pub fn get_column_by_id(&self, id: &str) -> Option<&ColumnSchema> {
47        self.columns.iter().find(|c| c.id.as_str() == id)
48    }
49}
50
51/// Column schema
52#[derive(Debug, Clone)]
53pub struct ColumnSchema {
54    /// ID
55    pub id: String,
56    /// Type (Clickhouse data type)
57    pub ty: Type,
58    /// Is a primary key
59    pub primary: bool,
60}
61
62impl ColumnSchema {
63    /// Creates a new column
64    pub fn new(id: &str, ty: Type, primary: bool) -> Self {
65        Self {
66            id: id.to_string(),
67            ty,
68            primary,
69        }
70    }
71}