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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use crate::stmt::{ExprArg, TableDerived};
use super::TableId;
/// A reference to a table within a [`SourceTable`](super::SourceTable).
///
/// Each entry in [`SourceTable::tables`](super::SourceTable) is a `TableRef`
/// that identifies where data comes from: a schema table, a CTE, a derived
/// subquery, or a placeholder argument.
///
/// # Examples
///
/// ```ignore
/// use toasty_core::stmt::TableRef;
/// use toasty_core::schema::db::TableId;
///
/// let table_ref = TableRef::Table(TableId(0));
/// assert!(table_ref.references(TableId(0)));
/// assert!(!table_ref.is_cte());
/// ```
#[derive(Debug, Clone, PartialEq)]
pub enum TableRef {
/// A reference to a CTE defined in a `WITH` clause.
Cte {
/// How many nesting levels up the CTE is defined relative to this
/// reference.
nesting: usize,
/// The index of the CTE within the [`With::ctes`](super::With) vector.
index: usize,
},
/// A derived table (inline subquery).
Derived(TableDerived),
/// A schema-defined table.
Table(TableId),
/// A placeholder that will be replaced with a derived table at a later
/// compilation stage.
Arg(ExprArg),
}
impl TableRef {
/// Returns `true` if this ref points to the given schema table.
pub fn references(&self, table_id: TableId) -> bool {
match self {
Self::Cte { .. } => false,
Self::Derived { .. } => false,
Self::Table(id) => id == &table_id,
Self::Arg { .. } => todo!(),
}
}
/// Returns `true` if this is a `Cte` reference.
pub fn is_cte(&self) -> bool {
matches!(self, Self::Cte { .. })
}
}
impl From<TableId> for TableRef {
fn from(value: TableId) -> Self {
Self::Table(value)
}
}
impl From<ExprArg> for TableRef {
fn from(value: ExprArg) -> Self {
TableRef::Arg(value)
}
}
impl PartialEq<TableId> for TableRef {
fn eq(&self, other: &TableId) -> bool {
self.references(*other)
}
}