proof_of_sql/base/database/
table_ref.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use alloc::string::ToString;
use core::{
    fmt,
    fmt::{Display, Formatter},
    str::FromStr,
};
use proof_of_sql_parser::{impl_serde_from_str, Identifier, ResourceId};

/// Expression for an SQL table
#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy)]
pub struct TableRef {
    resource_id: ResourceId,
}

impl TableRef {
    /// Creates a new table reference from a resource id
    pub fn new(resource_id: ResourceId) -> Self {
        Self { resource_id }
    }

    /// Returns the identifier of the schema
    pub fn schema_id(&self) -> Identifier {
        self.resource_id.schema()
    }

    /// Returns the identifier of the table
    pub fn table_id(&self) -> Identifier {
        self.resource_id.object_name()
    }

    /// Returns the underlying resource id of the table
    pub fn resource_id(&self) -> ResourceId {
        self.resource_id
    }
}

impl FromStr for TableRef {
    type Err = proof_of_sql_parser::ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self::new(s.parse()?))
    }
}

impl Display for TableRef {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        self.resource_id.fmt(f)
    }
}

impl_serde_from_str!(TableRef);