toasty_core/stmt/
table_ref.rs

1use crate::stmt::{ExprArg, TableDerived};
2
3use super::TableId;
4
5#[derive(Debug, Clone, PartialEq)]
6pub enum TableRef {
7    /// An aliased table (in a `FROM` statement or equivalent).
8    Cte {
9        /// What level of nesting the reference is compared to the CTE being
10        /// referenced.
11        nesting: usize,
12
13        /// The index of the CTE in the `WITH` clause
14        index: usize,
15    },
16
17    /// A table derived from a query
18    Derived(TableDerived),
19
20    /// A defined table from the schema
21    Table(TableId),
22
23    /// The table ref will be provided at a later time (and will become a
24    /// derived table)
25    Arg(ExprArg),
26}
27
28impl TableRef {
29    pub fn references(&self, table_id: TableId) -> bool {
30        match self {
31            Self::Cte { .. } => false,
32            Self::Derived { .. } => false,
33            Self::Table(id) => id == &table_id,
34            Self::Arg { .. } => todo!(),
35        }
36    }
37
38    pub fn is_cte(&self) -> bool {
39        matches!(self, Self::Cte { .. })
40    }
41}
42
43impl From<TableId> for TableRef {
44    fn from(value: TableId) -> Self {
45        Self::Table(value)
46    }
47}
48
49impl From<ExprArg> for TableRef {
50    fn from(value: ExprArg) -> Self {
51        TableRef::Arg(value)
52    }
53}
54
55impl PartialEq<TableId> for TableRef {
56    fn eq(&self, other: &TableId) -> bool {
57        self.references(*other)
58    }
59}