|
| 1 | +use std::fmt::{Display, Formatter}; |
| 2 | +use crate::query::querybuilder::syntax::tokens::{SqlToken, ToSqlTokens}; |
| 3 | + |
| 4 | +#[derive(Clone, Default, Debug)] |
| 5 | +pub struct TableMetadata { |
| 6 | + pub schema: Option<String>, |
| 7 | + pub name: String, |
| 8 | +} // TODO: we can have those fields as Cow<'_> for max performance |
| 9 | + |
| 10 | +impl<'a> ToSqlTokens<'a> for TableMetadata { |
| 11 | + fn to_tokens(&self) -> SqlToken<'a> { |
| 12 | + match &self.schema { |
| 13 | + Some(s) => { |
| 14 | + out.push(SqlToken::Ident(s)); |
| 15 | + out.push(SqlToken::Symbol('.')); |
| 16 | + out.push(SqlToken::Ident(&self.name)); |
| 17 | + } |
| 18 | + None => { |
| 19 | + out.push(SqlToken::Ident(&self.name)); |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | +impl From<&str> for TableMetadata { |
| 25 | + /// Creates a new [`TableMetadata`] from a string slice. |
| 26 | + /// |
| 27 | + /// If the slice contains a dot, we assume that is a schema.table_name format, otherwise, |
| 28 | + /// we assume that the client is just creating a [`Self`] from the passed in string |
| 29 | + fn from(value: &str) -> Self { |
| 30 | + if let Some((schema, table)) = value.split_once('.') { |
| 31 | + TableMetadata { |
| 32 | + schema: Some(schema.to_string()), |
| 33 | + name: table.to_string(), |
| 34 | + } |
| 35 | + } else { |
| 36 | + TableMetadata { |
| 37 | + schema: None, |
| 38 | + name: value.to_string(), |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +impl<'a> TableMetadata { |
| 46 | + pub fn new(schema: &'a str, name: &'a str) -> Self { |
| 47 | + Self { schema: Some(schema.to_string()), name: name.to_string() } |
| 48 | + } |
| 49 | + pub fn schema(&mut self, schema: String) { self.schema = Some(schema); } |
| 50 | + pub fn table_name(&mut self, table_name: String) { self.name = table_name } |
| 51 | + |
| 52 | + /// Returns an already formatted version of the schema and table of a target database table |
| 53 | + /// ready to be used in a SQL statement. |
| 54 | + /// |
| 55 | + /// This method allocates a new string, so it returns an owned one to the callee. |
| 56 | + /// Just take it in consideration if someday someone uses it outside the macro generation |
| 57 | + /// and there's some heavy callee procedure |
| 58 | + pub fn sql(&self) -> String { |
| 59 | + match &self.schema { |
| 60 | + Some(schema_name) => {format!("{}.{}", schema_name, self.name)} |
| 61 | + None => self.name.to_string() |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl Display for TableMetadata { |
| 67 | + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
| 68 | + match &self.schema { |
| 69 | + Some(schema_name) => {write!(f, "{}.{}", schema_name, self.name)} |
| 70 | + None => {write!(f, "{}", self.name)} |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl AsRef<str> for TableMetadata { |
| 76 | + fn as_ref(&self) -> &str { |
| 77 | + self.schema.as_ref().unwrap() |
| 78 | + } |
| 79 | +} |
0 commit comments