clia_rustorm_dao/
column_name.rs

1use crate::common;
2use serde_derive::{
3    Deserialize,
4    Serialize,
5};
6
7#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
8pub struct ColumnName {
9    pub name: String,
10    pub table: Option<String>,
11    pub alias: Option<String>,
12}
13
14impl ColumnName {
15    /// create table with name
16    pub fn from(arg: &str) -> Self {
17        if arg.contains('.') {
18            let splinters = arg.split('.').collect::<Vec<&str>>();
19            assert!(
20                splinters.len() == 2,
21                "There should only be 2 parts, trying to split `.` {}",
22                arg
23            );
24            let table = splinters[0].to_owned();
25            let name = splinters[1].to_owned();
26            ColumnName {
27                name,
28                table: Some(table),
29                alias: None,
30            }
31        } else {
32            ColumnName {
33                name: arg.to_owned(),
34                table: None,
35                alias: None,
36            }
37        }
38    }
39
40    /// return the long name of the table using schema.table_name
41    pub fn complete_name(&self) -> String {
42        match self.table {
43            Some(ref table) => format!("{}.{}", table, self.name),
44            None => self.name.to_owned(),
45        }
46    }
47
48    pub fn safe_complete_name(&self) -> String {
49        match self.table {
50            Some(ref table) => format!("{}.{}", common::keywords_safe(table), self.name),
51            None => self.name.to_owned(),
52        }
53    }
54}
55
56pub trait ToColumnNames {
57    /// extract the columns from struct
58    fn to_column_names() -> Vec<ColumnName>;
59}