1use crate::{
2 DynQuery, DataSet, quote_cow,
3 writer::{Context, SqlWriter},
4};
5use proc_macro2::TokenStream;
6use quote::{ToTokens, TokenStreamExt, quote};
7use std::borrow::Cow;
8
9#[derive(Default, Clone, PartialEq, Eq, Hash, Debug)]
11pub struct TableRef {
12 pub name: Cow<'static, str>,
14 pub schema: Cow<'static, str>,
16 pub alias: Cow<'static, str>,
18}
19
20impl TableRef {
21 pub fn new(name: impl Into<Cow<'static, str>>) -> Self {
23 Self {
24 name: name.into(),
25 schema: "".into(),
26 alias: "".into(),
27 }
28 }
29 pub fn full_name(&self) -> String {
31 let mut result = String::new();
32 if !self.alias.is_empty() {
33 result.push_str(&self.alias);
34 } else {
35 if !self.schema.is_empty() {
36 result.push_str(&self.schema);
37 result.push('.');
38 }
39 result.push_str(&self.name);
40 }
41 result
42 }
43 pub fn with_alias(&self, alias: Cow<'static, str>) -> Self {
45 let mut result = self.clone();
46 result.alias = alias.into();
47 result
48 }
49}
50
51impl DataSet for TableRef {
52 fn qualified_columns() -> bool
53 where
54 Self: Sized,
55 {
56 false
57 }
58 fn write_query(&self, writer: &dyn SqlWriter, context: &mut Context, out: &mut DynQuery) {
59 writer.write_table_ref(context, out, self)
60 }
61 fn table_ref(&self) -> TableRef {
62 self.clone()
63 }
64}
65
66impl DataSet for &TableRef {
67 fn qualified_columns() -> bool
68 where
69 Self: Sized,
70 {
71 false
72 }
73 fn write_query(&self, writer: &dyn SqlWriter, context: &mut Context, out: &mut DynQuery) {
74 (*writer).write_table_ref(context, out, self)
75 }
76 fn table_ref(&self) -> TableRef {
77 (*self).clone()
78 }
79}
80
81impl ToTokens for TableRef {
82 fn to_tokens(&self, tokens: &mut TokenStream) {
83 let name = &self.name;
84 let schema = &self.schema;
85 let alias = quote_cow(&self.alias);
86 tokens.append_all(quote! {
87 ::tank::TableRef {
88 name: #name,
89 schema: #schema,
90 alias: #alias,
91 }
92 });
93 }
94}
95
96#[derive(Default, Clone, PartialEq, Eq, Debug)]
98pub struct DeclareTableRef(pub TableRef);
99
100impl DataSet for DeclareTableRef {
101 fn qualified_columns() -> bool
102 where
103 Self: Sized,
104 {
105 false
106 }
107 fn write_query(&self, writer: &dyn SqlWriter, context: &mut Context, out: &mut DynQuery) {
108 writer.write_table_ref(context, out, &self.0)
109 }
110 fn table_ref(&self) -> TableRef {
111 self.0.clone()
112 }
113}
114
115impl ToTokens for DeclareTableRef {
116 fn to_tokens(&self, tokens: &mut TokenStream) {
117 let table_ref = &self.0;
118 tokens.append_all(quote!(::tank::DeclareTableRef(#table_ref)));
119 }
120}