Skip to main content

sofie_db/
selection.rs

1use sofie_core::{SqlValue, Uuid};
2
3use crate::Dialect;
4use crate::aggregate::Table;
5
6#[derive(Clone, Debug)]
7pub struct Selected(pub(crate) Selection);
8
9#[derive(Clone, Debug)]
10pub(crate) enum Selection {
11    Column(String),
12    Expression(&'static str),
13    #[cfg(feature = "joins")]
14    Joined(crate::joins::Join),
15    #[cfg(feature = "children")]
16    Children(&'static crate::ChildTable),
17}
18
19impl Selected {
20    pub(crate) fn column(name: impl Into<String>) -> Self {
21        Self(Selection::Column(name.into()))
22    }
23
24    #[cfg(feature = "listing")]
25    pub(crate) fn column_name(&self) -> Option<&str> {
26        let Selection::Column(name) = &self.0 else {
27            return None;
28        };
29        Some(name)
30    }
31
32    #[cfg(feature = "live")]
33    pub(crate) fn joined_table(&self) -> Option<(&'static str, &'static str)> {
34        match &self.0 {
35            #[cfg(feature = "joins")]
36            Selection::Joined(join) => Some(join.table()),
37            _ => None,
38        }
39    }
40
41    pub(crate) fn is_fixed(&self) -> bool {
42        match self.0 {
43            #[cfg(feature = "joins")]
44            Selection::Joined(_) => false,
45            _ => true,
46        }
47    }
48
49    pub(crate) fn render(
50        &self,
51        base: &Table,
52        dialect: &dyn Dialect,
53        owner: Option<Uuid>,
54        params: &mut Vec<SqlValue>,
55    ) -> String {
56        let _ = (base, owner, &params);
57        match &self.0 {
58            Selection::Column(name) => dialect.quote(name),
59            Selection::Expression(sql) => (*sql).to_owned(),
60            #[cfg(feature = "joins")]
61            Selection::Joined(join) => join.render(base, dialect, owner, params),
62            #[cfg(feature = "children")]
63            Selection::Children(child) => child.render(base, dialect),
64        }
65    }
66}