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}
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26pub struct Context {
27 pub counter: u32,
28 pub fragment: Fragment,
29 pub qualify_columns: bool,
30}
31
32impl Context {
33 pub const fn new(fragment: Fragment, qualify_columns: bool) -> Self {
34 Self {
35 counter: 0,
36 fragment,
37 qualify_columns,
38 }
39 }
40 pub const fn new_qualify(qualify_columns: bool) -> Self {
41 Self {
42 counter: 0,
43 fragment: Fragment::None,
44 qualify_columns,
45 }
46 }
47 pub const fn update_from(&mut self, context: &Context) {
48 self.counter = context.counter;
49 }
50}
51
52impl Context {
53 pub const fn switch_fragment<'s>(&'s mut self, fragment: Fragment) -> ContextUpdater<'s> {
54 ContextUpdater {
55 current: Context { fragment, ..*self },
56 previous: self,
57 }
58 }
59}
60
61impl Default for Context {
62 fn default() -> Self {
63 Context::new(Fragment::None, true)
64 }
65}
66
67pub struct ContextUpdater<'a> {
68 pub current: Context,
69 pub previous: &'a mut Context,
70}
71
72impl<'a> Drop for ContextUpdater<'a> {
73 fn drop(&mut self) {
74 self.previous.counter = self.current.counter;
75 }
76}