tank_core/
column.rs

1use crate::{Expression, OpPrecedence, TableRef, Value, writer::Context};
2use proc_macro2::TokenStream;
3use quote::{ToTokens, TokenStreamExt, quote};
4
5pub trait ColumnTrait {
6    fn column_def(&self) -> &ColumnDef;
7    fn column_ref(&self) -> &ColumnRef;
8}
9
10#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
11pub struct ColumnRef {
12    pub name: &'static str,
13    pub table: &'static str,
14    pub schema: &'static str,
15}
16
17impl ColumnRef {
18    pub fn table_ref(&self) -> TableRef {
19        TableRef {
20            name: self.table,
21            schema: self.schema,
22            ..Default::default()
23        }
24    }
25}
26
27#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
28pub enum PrimaryKeyType {
29    PrimaryKey,
30    PartOfPrimaryKey,
31    #[default]
32    None,
33}
34
35impl ToTokens for PrimaryKeyType {
36    fn to_tokens(&self, tokens: &mut TokenStream) {
37        use PrimaryKeyType::*;
38        tokens.append_all(match self {
39            PrimaryKey => quote!(::tank::PrimaryKeyType::PrimaryKey),
40            PartOfPrimaryKey => quote!(::tank::PrimaryKeyType::PartOfPrimaryKey),
41            None => quote!(::tank::PrimaryKeyType::None),
42        });
43    }
44}
45
46#[derive(Default, Debug, PartialEq, Eq)]
47pub enum Action {
48    #[default]
49    NoAction,
50    Restrict,
51    Cascade,
52    SetNull,
53    SetDefault,
54}
55
56impl ToTokens for Action {
57    fn to_tokens(&self, tokens: &mut TokenStream) {
58        tokens.append_all(match self {
59            Action::NoAction => quote! { ::tank::Action::NoAction },
60            Action::Restrict => quote! { ::tank::Action::Restrict },
61            Action::Cascade => quote! { ::tank::Action::Cascade },
62            Action::SetNull => quote! { ::tank::Action::SetNull },
63            Action::SetDefault => quote! { ::tank::Action::SetDefault },
64        });
65    }
66}
67
68#[derive(Default, Debug)]
69pub struct ColumnDef {
70    pub column_ref: ColumnRef,
71    pub column_type: &'static str,
72    pub value: Value,
73    pub nullable: bool,
74    pub default: Option<Box<dyn Expression>>,
75    pub primary_key: PrimaryKeyType,
76    pub unique: bool,
77    pub references: Option<ColumnRef>,
78    pub on_delete: Option<Action>,
79    pub on_update: Option<Action>,
80    pub passive: bool,
81    pub comment: &'static str,
82}
83
84impl ColumnDef {
85    pub fn name(&self) -> &'static str {
86        &self.column_ref.name
87    }
88    pub fn table(&self) -> &'static str {
89        &self.column_ref.table
90    }
91    pub fn schema(&self) -> &'static str {
92        &self.column_ref.schema
93    }
94}
95
96impl<'a> From<&'a ColumnDef> for &'a ColumnRef {
97    fn from(value: &'a ColumnDef) -> Self {
98        &value.column_ref
99    }
100}
101
102impl OpPrecedence for ColumnRef {
103    fn precedence(&self, _writer: &dyn crate::SqlWriter) -> i32 {
104        1_000_000
105    }
106}
107
108impl Expression for ColumnRef {
109    fn write_query(&self, writer: &dyn crate::SqlWriter, context: &mut Context, buff: &mut String) {
110        writer.write_column_ref(context, buff, self);
111    }
112}
113
114impl OpPrecedence for ColumnDef {
115    fn precedence(&self, _writer: &dyn crate::SqlWriter) -> i32 {
116        1_000_000
117    }
118}
119
120impl Expression for ColumnDef {
121    fn write_query(&self, writer: &dyn crate::SqlWriter, context: &mut Context, buff: &mut String) {
122        writer.write_column_ref(context, buff, &self.column_ref);
123    }
124}