1use crate::{Expression, OpPrecedence, TableRef, Value, writer::Context};
2use proc_macro2::TokenStream;
3use quote::{ToTokens, TokenStreamExt, quote};
4
5pub trait ColumnTrait {
7 fn column_def(&self) -> &ColumnDef;
9 fn column_ref(&self) -> &ColumnRef;
11}
12
13#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
15pub struct ColumnRef {
16 pub name: &'static str,
18 pub table: &'static str,
20 pub schema: &'static str,
22}
23
24impl ColumnRef {
25 pub fn table(&self) -> TableRef {
26 TableRef {
27 name: self.table,
28 schema: self.schema,
29 ..Default::default()
30 }
31 }
32}
33
34#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
36pub enum PrimaryKeyType {
37 PrimaryKey,
39 PartOfPrimaryKey,
41 #[default]
43 None,
44}
45
46impl ToTokens for PrimaryKeyType {
47 fn to_tokens(&self, tokens: &mut TokenStream) {
48 use PrimaryKeyType::*;
49 tokens.append_all(match self {
50 PrimaryKey => quote!(::tank::PrimaryKeyType::PrimaryKey),
51 PartOfPrimaryKey => quote!(::tank::PrimaryKeyType::PartOfPrimaryKey),
52 None => quote!(::tank::PrimaryKeyType::None),
53 });
54 }
55}
56
57#[derive(Default, Debug, PartialEq, Eq)]
59pub enum Action {
60 #[default]
62 NoAction,
63 Restrict,
65 Cascade,
67 SetNull,
69 SetDefault,
71}
72
73impl ToTokens for Action {
74 fn to_tokens(&self, tokens: &mut TokenStream) {
75 tokens.append_all(match self {
76 Action::NoAction => quote! { ::tank::Action::NoAction },
77 Action::Restrict => quote! { ::tank::Action::Restrict },
78 Action::Cascade => quote! { ::tank::Action::Cascade },
79 Action::SetNull => quote! { ::tank::Action::SetNull },
80 Action::SetDefault => quote! { ::tank::Action::SetDefault },
81 });
82 }
83}
84
85#[derive(Default, Debug)]
87pub struct ColumnDef {
88 pub column_ref: ColumnRef,
90 pub column_type: &'static str,
92 pub value: Value,
94 pub nullable: bool,
96 pub default: Option<Box<dyn Expression>>,
98 pub primary_key: PrimaryKeyType,
100 pub unique: bool,
102 pub references: Option<ColumnRef>,
104 pub on_delete: Option<Action>,
106 pub on_update: Option<Action>,
108 pub passive: bool,
110 pub comment: &'static str,
112}
113
114impl ColumnDef {
115 pub fn name(&self) -> &'static str {
116 &self.column_ref.name
117 }
118 pub fn table(&self) -> &'static str {
119 &self.column_ref.table
120 }
121 pub fn schema(&self) -> &'static str {
122 &self.column_ref.schema
123 }
124}
125
126impl<'a> From<&'a ColumnDef> for &'a ColumnRef {
127 fn from(value: &'a ColumnDef) -> Self {
128 &value.column_ref
129 }
130}
131
132impl OpPrecedence for ColumnRef {
133 fn precedence(&self, _writer: &dyn crate::SqlWriter) -> i32 {
134 1_000_000
135 }
136}
137
138impl Expression for ColumnRef {
139 fn write_query(&self, writer: &dyn crate::SqlWriter, context: &mut Context, out: &mut String) {
140 writer.write_column_ref(context, out, self);
141 }
142}
143
144impl OpPrecedence for ColumnDef {
145 fn precedence(&self, _writer: &dyn crate::SqlWriter) -> i32 {
146 1_000_000
147 }
148}
149
150impl Expression for ColumnDef {
151 fn write_query(&self, writer: &dyn crate::SqlWriter, context: &mut Context, out: &mut String) {
152 writer.write_column_ref(context, out, &self.column_ref);
153 }
154}