Skip to main content

tank_core/writer/
context.rs

1use crate::TableRef;
2use std::borrow::Cow;
3
4#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
5pub enum Fragment {
6    #[default]
7    None,
8    Aliasing,
9    Casting,
10    Json,
11    JsonKey,
12    ParameterBinding,
13    SqlCommentOnColumn,
14    SqlCreateSchema,
15    SqlCreateTable,
16    SqlCreateTablePrimaryKey,
17    SqlCreateTableUnique,
18    SqlDeleteFrom,
19    SqlDeleteFromWhere,
20    SqlDropSchema,
21    SqlDropTable,
22    SqlInsertInto,
23    SqlInsertIntoOnConflict,
24    SqlInsertIntoValues,
25    SqlJoin,
26    SqlSelect,
27    SqlSelectFrom,
28    SqlSelectGroupBy,
29    SqlSelectHaving,
30    SqlSelectOrderBy,
31    Timestamp,
32    SqlSelectWhere,
33}
34
35#[derive(Clone, PartialEq, Eq, Debug)]
36pub struct Context {
37    pub counter: u32,
38    pub fragment: Fragment,
39    pub table_ref: TableRef,
40    pub qualify_columns: bool,
41    pub quote_identifiers: bool,
42}
43
44impl Context {
45    pub const fn new(fragment: Fragment, qualify_columns: bool) -> Self {
46        Self {
47            counter: 0,
48            fragment,
49            table_ref: TableRef::new(Cow::Borrowed("")),
50            qualify_columns,
51            quote_identifiers: true,
52        }
53    }
54    pub const fn empty() -> Self {
55        Self {
56            counter: 0,
57            fragment: Fragment::None,
58            table_ref: TableRef::new(Cow::Borrowed("")),
59            qualify_columns: false,
60            quote_identifiers: false,
61        }
62    }
63    pub const fn fragment(fragment: Fragment) -> Self {
64        Self {
65            counter: 0,
66            fragment,
67            table_ref: TableRef::new(Cow::Borrowed("")),
68            qualify_columns: false,
69            quote_identifiers: true,
70        }
71    }
72    pub const fn qualify(qualify_columns: bool) -> Self {
73        Self {
74            counter: 0,
75            fragment: Fragment::None,
76            table_ref: TableRef::new(Cow::Borrowed("")),
77            qualify_columns,
78            quote_identifiers: true,
79        }
80    }
81    pub const fn qualify_with(table: Cow<'static, str>) -> Self {
82        Self {
83            counter: 0,
84            fragment: Fragment::None,
85            table_ref: TableRef::new(table),
86            qualify_columns: true,
87            quote_identifiers: true,
88        }
89    }
90    pub const fn update_from(&mut self, context: &Context) {
91        self.counter = context.counter;
92    }
93    pub fn switch_fragment<'s>(&'s mut self, fragment: Fragment) -> ContextUpdater<'s> {
94        ContextUpdater {
95            current: Context {
96                fragment,
97                table_ref: self.table_ref.clone(),
98                ..*self
99            },
100            previous: self,
101        }
102    }
103    pub fn switch_table<'s>(&'s mut self, table_ref: TableRef) -> ContextUpdater<'s> {
104        let is_empty = table_ref.is_empty();
105        ContextUpdater {
106            current: Context {
107                table_ref,
108                qualify_columns: !is_empty,
109                ..*self
110            },
111            previous: self,
112        }
113    }
114}
115
116impl Default for Context {
117    fn default() -> Self {
118        Context::new(Fragment::None, true)
119    }
120}
121
122pub struct ContextUpdater<'a> {
123    pub current: Context,
124    pub previous: &'a mut Context,
125}
126
127impl<'a> Drop for ContextUpdater<'a> {
128    fn drop(&mut self) {
129        self.previous.counter = self.current.counter;
130    }
131}