vantage_sql/primitives/
identifier.rs1use vantage_expressions::{Expression, Expressive};
2
3#[derive(Debug, Clone)]
29pub struct Identifier {
30 parts: Vec<String>,
31 alias: Option<String>,
32}
33
34impl Identifier {
35 pub fn new(name: impl Into<String>) -> Self {
37 Self {
38 parts: vec![name.into()],
39 alias: None,
40 }
41 }
42
43 pub fn dot_of(mut self, prefix: impl Into<String>) -> Self {
46 self.parts.insert(0, prefix.into());
47 self
48 }
49
50 pub fn with_alias(mut self, alias: impl Into<String>) -> Self {
52 self.alias = Some(alias.into());
53 self
54 }
55
56 pub fn name(&self) -> String {
58 self.parts.join(".")
59 }
60
61 pub fn alias(&self) -> Option<&str> {
63 self.alias.as_deref()
64 }
65
66 fn render_with(&self, q: char) -> String {
68 let base = self
69 .parts
70 .iter()
71 .map(|p| format!("{q}{p}{q}"))
72 .collect::<Vec<_>>()
73 .join(".");
74 match &self.alias {
75 Some(alias) => format!("{base} AS {q}{alias}{q}"),
76 None => base,
77 }
78 }
79}
80
81pub fn ident(name: impl Into<String>) -> Identifier {
83 Identifier::new(name)
84}
85
86#[cfg(feature = "sqlite")]
89impl Expressive<crate::sqlite::types::AnySqliteType> for Identifier {
90 fn expr(&self) -> Expression<crate::sqlite::types::AnySqliteType> {
91 Expression::new(self.render_with('"'), vec![])
92 }
93}
94
95#[cfg(feature = "sqlite")]
96impl From<Identifier> for Expression<crate::sqlite::types::AnySqliteType> {
97 fn from(id: Identifier) -> Self {
98 id.expr()
99 }
100}
101
102#[cfg(feature = "postgres")]
103impl Expressive<crate::postgres::types::AnyPostgresType> for Identifier {
104 fn expr(&self) -> Expression<crate::postgres::types::AnyPostgresType> {
105 Expression::new(self.render_with('"'), vec![])
106 }
107}
108
109#[cfg(feature = "postgres")]
110impl From<Identifier> for Expression<crate::postgres::types::AnyPostgresType> {
111 fn from(id: Identifier) -> Self {
112 id.expr()
113 }
114}
115
116#[cfg(feature = "mysql")]
117impl Expressive<crate::mysql::types::AnyMysqlType> for Identifier {
118 fn expr(&self) -> Expression<crate::mysql::types::AnyMysqlType> {
119 Expression::new(self.render_with('`'), vec![])
120 }
121}
122
123#[cfg(feature = "mysql")]
124impl From<Identifier> for Expression<crate::mysql::types::AnyMysqlType> {
125 fn from(id: Identifier) -> Self {
126 id.expr()
127 }
128}