1use crate::{
2 DynQuery, DefaultValueType, Expression, OpPrecedence, SqlWriter, TableRef, Value,
3 writer::Context,
4};
5use proc_macro2::TokenStream;
6use quote::{ToTokens, TokenStreamExt, quote};
7use std::collections::BTreeMap;
8
9pub trait ColumnTrait {
11 fn column_def(&self) -> &ColumnDef;
13 fn column_ref(&self) -> &ColumnRef;
15}
16
17#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
19pub struct ColumnRef {
20 pub name: &'static str,
22 pub table: &'static str,
24 pub schema: &'static str,
26}
27
28impl ColumnRef {
29 pub fn table(&self) -> TableRef {
31 TableRef {
32 name: self.table.into(),
33 schema: self.schema.into(),
34 ..Default::default()
35 }
36 }
37}
38
39#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
41pub enum PrimaryKeyType {
42 PrimaryKey,
44 PartOfPrimaryKey,
46 #[default]
48 None,
49}
50
51impl ToTokens for PrimaryKeyType {
52 fn to_tokens(&self, tokens: &mut TokenStream) {
53 use PrimaryKeyType::*;
54 tokens.append_all(match self {
55 PrimaryKey => quote!(::tank::PrimaryKeyType::PrimaryKey),
56 PartOfPrimaryKey => quote!(::tank::PrimaryKeyType::PartOfPrimaryKey),
57 None => quote!(::tank::PrimaryKeyType::None),
58 });
59 }
60}
61
62#[derive(Default, Debug, PartialEq, Eq)]
64pub enum Action {
65 #[default]
67 NoAction,
68 Restrict,
70 Cascade,
72 SetNull,
74 SetDefault,
76}
77
78impl ToTokens for Action {
79 fn to_tokens(&self, tokens: &mut TokenStream) {
80 tokens.append_all(match self {
81 Action::NoAction => quote! { ::tank::Action::NoAction },
82 Action::Restrict => quote! { ::tank::Action::Restrict },
83 Action::Cascade => quote! { ::tank::Action::Cascade },
84 Action::SetNull => quote! { ::tank::Action::SetNull },
85 Action::SetDefault => quote! { ::tank::Action::SetDefault },
86 });
87 }
88}
89
90#[derive(Default, Debug)]
92pub struct ColumnDef {
93 pub column_ref: ColumnRef,
95 pub column_type: BTreeMap<&'static str, &'static str>,
97 pub value: Value,
99 pub nullable: bool,
101 pub default: DefaultValueType,
103 pub primary_key: PrimaryKeyType,
105 pub clustering_key: bool,
107 pub unique: bool,
109 pub references: Option<ColumnRef>,
111 pub on_delete: Option<Action>,
113 pub on_update: Option<Action>,
115 pub passive: bool,
117 pub comment: &'static str,
119}
120
121impl ColumnDef {
122 pub fn name(&self) -> &'static str {
124 &self.column_ref.name
125 }
126 pub fn table(&self) -> &'static str {
128 &self.column_ref.table
129 }
130 pub fn schema(&self) -> &'static str {
132 &self.column_ref.schema
133 }
134}
135
136impl<'a> From<&'a ColumnDef> for &'a ColumnRef {
137 fn from(value: &'a ColumnDef) -> Self {
138 &value.column_ref
139 }
140}
141
142impl OpPrecedence for ColumnRef {
143 fn precedence(&self, _writer: &dyn SqlWriter) -> i32 {
144 1_000_000
145 }
146}
147
148impl Expression for ColumnRef {
149 fn write_query(&self, writer: &dyn SqlWriter, context: &mut Context, out: &mut DynQuery) {
150 writer.write_column_ref(context, out, self);
151 }
152}
153
154impl OpPrecedence for ColumnDef {
155 fn precedence(&self, _writer: &dyn SqlWriter) -> i32 {
156 1_000_000
157 }
158}
159
160impl Expression for ColumnDef {
161 fn write_query(&self, writer: &dyn SqlWriter, context: &mut Context, out: &mut DynQuery) {
162 writer.write_column_ref(context, out, &self.column_ref);
163 }
164}