toasty_core/stmt/source_table.rs
1use crate::stmt::{ExprArg, Source, SourceTableId, TableFactor};
2
3use super::{TableRef, TableWithJoins};
4
5/// A lowered table-level data source for a `SELECT` statement.
6///
7/// Contains a list of table references (which may be schema tables, CTEs, or
8/// derived tables) and the `FROM` items that reference them by index
9/// ([`SourceTableId`]).
10///
11/// # Examples
12///
13/// ```ignore
14/// use toasty_core::stmt::{SourceTable, TableRef, TableWithJoins, TableFactor, SourceTableId};
15/// use toasty_core::schema::db::TableId;
16///
17/// let source = SourceTable::new(
18/// vec![TableRef::Table(TableId(0))],
19/// TableWithJoins {
20/// relation: TableFactor::Table(SourceTableId(0)),
21/// joins: vec![],
22/// },
23/// );
24/// ```
25#[derive(Debug, Clone, PartialEq)]
26pub struct SourceTable {
27 /// All table references used in this source. Indexed by [`SourceTableId`].
28 pub tables: Vec<TableRef>,
29
30 /// The `FROM` items, each being a table with optional joins.
31 pub from: Vec<TableWithJoins>,
32}
33
34impl SourceTable {
35 /// Creates a new `SourceTable` with a single `FROM` item.
36 pub fn new(tables: Vec<TableRef>, from: TableWithJoins) -> Self {
37 Self {
38 tables,
39 from: vec![from],
40 }
41 }
42}
43
44impl From<ExprArg> for SourceTable {
45 fn from(value: ExprArg) -> Self {
46 SourceTable {
47 tables: vec![TableRef::Arg(value)],
48 from: vec![TableWithJoins {
49 relation: TableFactor::Table(SourceTableId(0)),
50 joins: vec![],
51 }],
52 }
53 }
54}
55
56impl From<SourceTable> for Source {
57 fn from(value: SourceTable) -> Self {
58 Source::Table(value)
59 }
60}