tank_core/writer/
context.rs1#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
2pub enum Fragment {
3 #[default]
4 None,
5 Casting,
6 SqlCommentOnColumn,
7 SqlCreateSchema,
8 SqlCreateTable,
9 SqlCreateTablePrimaryKey,
10 SqlCreateTableUnique,
11 SqlDeleteFrom,
12 SqlDeleteFromWhere,
13 SqlDropSchema,
14 SqlDropTable,
15 SqlInsertInto,
16 SqlInsertIntoOnConflict,
17 SqlInsertIntoValues,
18 SqlJoin,
19 SqlSelect,
20 SqlSelectFrom,
21 SqlSelectOrderBy,
22 SqlSelectWhere,
23 Json,
24 JsonKey,
25}
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub struct Context {
29 pub counter: u32,
30 pub fragment: Fragment,
31 pub qualify_columns: bool,
32}
33
34impl Context {
35 pub const fn new(fragment: Fragment, qualify_columns: bool) -> Self {
36 Self {
37 counter: 0,
38 fragment,
39 qualify_columns,
40 }
41 }
42 pub const fn new_qualify(qualify_columns: bool) -> Self {
43 Self {
44 counter: 0,
45 fragment: Fragment::None,
46 qualify_columns,
47 }
48 }
49 pub const fn update_from(&mut self, context: &Context) {
50 self.counter = context.counter;
51 }
52 pub const fn switch_fragment<'s>(&'s mut self, fragment: Fragment) -> ContextUpdater<'s> {
53 ContextUpdater {
54 current: Context { fragment, ..*self },
55 previous: self,
56 }
57 }
58 pub fn is_inside_json(&self) -> bool {
59 self.fragment == Fragment::Json || self.fragment == Fragment::JsonKey
60 }
61}
62
63impl Default for Context {
64 fn default() -> Self {
65 Context::new(Fragment::None, true)
66 }
67}
68
69pub struct ContextUpdater<'a> {
70 pub current: Context,
71 pub previous: &'a mut Context,
72}
73
74impl<'a> Drop for ContextUpdater<'a> {
75 fn drop(&mut self) {
76 self.previous.counter = self.current.counter;
77 }
78}