Skip to main content

sql_fun_sqlast/sem/create_table/
view_name.rs

1use std::fmt::Display;
2
3use crate::{
4    sem::{CatalogName, FullName, LocalName, SchemaName},
5    syn::RangeVar,
6};
7
8/// view name type
9#[derive(Debug, Clone, Eq, Hash, PartialEq, serde::Serialize, serde::Deserialize)]
10pub struct ViewName(FullName);
11
12impl Display for ViewName {
13    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14        write!(f, "{}", self.0)?;
15        Ok(())
16    }
17}
18
19impl ViewName {
20    fn to_option(string: String) -> Option<String> {
21        if string.is_empty() {
22            None
23        } else {
24            Some(string)
25        }
26    }
27
28    pub(crate) fn full_name(&self) -> &FullName {
29        &self.0
30    }
31}
32
33impl From<RangeVar> for ViewName {
34    fn from(value: RangeVar) -> Self {
35        Self::from(&value)
36    }
37}
38
39impl From<&RangeVar> for ViewName {
40    fn from(value: &RangeVar) -> Self {
41        let catalog = Self::to_option(value.get_catalogname());
42        let schema = Self::to_option(value.get_schemaname());
43        let relation = value.get_relname();
44        Self(FullName::new(
45            &catalog.as_deref().map(CatalogName::from),
46            &schema.as_deref().map(SchemaName::from),
47            &LocalName::from(&relation),
48        ))
49    }
50}