Skip to main content

vantage_table/table/impls/
columns.rs

1use indexmap::IndexMap;
2use vantage_expressions::{Expression, Expressive, traits::datasource::ExprDataSource};
3use vantage_types::Entity;
4
5use crate::{
6    column::core::ColumnType, prelude::ColumnLike, table::Table, traits::table_source::TableSource,
7};
8
9impl<T: TableSource, E: Entity<T::Value>> Table<T, E> {
10    /// Add a column to the table (accepts any typed column, converts to `Column<AnyType>`)
11    pub fn add_column<NewColumnType>(&mut self, column: T::Column<NewColumnType>)
12    where
13        NewColumnType: ColumnType,
14    {
15        let name = column.name().to_string();
16
17        if self.columns.contains_key(&name) {
18            panic!("Duplicate column: {}", name);
19        }
20
21        // Convert typed column to Column<AnyType> for storage
22        let any_column = self.data_source.to_any_column(column);
23        self.columns.insert(name, any_column);
24    }
25
26    /// Add a column using builder pattern
27    pub fn with_column<NewColumnType>(mut self, column: T::Column<NewColumnType>) -> Self
28    where
29        NewColumnType: ColumnType,
30    {
31        self.add_column(column);
32        self
33    }
34
35    /// Copy column definitions from another table, skipping any whose name is
36    /// already present. With `names = None`, copies all columns; otherwise only
37    /// the listed ones. Used to inherit columns when deriving a table from
38    /// another (see `Table::derive_from`).
39    pub fn copy_columns_from<E2: Entity<T::Value>>(
40        &mut self,
41        other: &Table<T, E2>,
42        names: Option<&[&str]>,
43    ) {
44        for col in other.columns().values() {
45            let name = col.name();
46            if names.is_some_and(|ns| !ns.contains(&name)) {
47                continue;
48            }
49            if !self.columns.contains_key(name) {
50                self.add_column(col.clone());
51            }
52        }
53    }
54
55    /// Add a typed column to the table (mutable)
56    pub fn add_column_of<NewColumnType>(&mut self, name: impl Into<String>)
57    where
58        NewColumnType: ColumnType,
59    {
60        let column = self
61            .data_source
62            .create_column::<NewColumnType>(&name.into());
63        self.add_column(column);
64    }
65
66    /// Add an ID column — sets both the column and the id_field flag.
67    pub fn with_id_column(mut self, name: impl Into<String>) -> Self
68    where
69        T::Id: ColumnType,
70    {
71        let name = name.into();
72        self.id_field = Some(name.clone());
73        let column = self.data_source.create_column::<T::Id>(&name);
74        self.add_column(column);
75        self
76    }
77
78    /// Add a typed column AND mark it as a display title.
79    ///
80    /// Title columns show alongside the id in generic list views and
81    /// lead the body of single-record displays. Multiple title columns
82    /// are allowed; their order matches the order of these calls.
83    pub fn with_title_column_of<NewColumnType>(mut self, name: impl Into<String>) -> Self
84    where
85        NewColumnType: ColumnType,
86    {
87        let name = name.into();
88        if !self.title_fields.contains(&name) {
89            self.title_fields.push(name.clone());
90        }
91        if self.title_field.is_none() {
92            self.title_field = Some(name.clone());
93        }
94        let column = self.data_source.create_column::<NewColumnType>(&name);
95        self.add_column(column);
96        self
97    }
98
99    /// Add a typed column to the table (builder pattern)
100    pub fn with_column_of<NewColumnType>(self, name: impl Into<String>) -> Self
101    where
102        NewColumnType: ColumnType,
103    {
104        let column = self
105            .data_source
106            .create_column::<NewColumnType>(&name.into());
107        self.with_column(column)
108    }
109
110    /// Get all columns as type-erased columns (`Column<AnyType>`)
111    pub fn columns(&self) -> &IndexMap<String, T::Column<T::AnyType>> {
112        &self.columns
113    }
114
115    /// Get a typed column by converting from stored `Column<AnyType>`
116    pub fn get_column<Type>(&self, name: &str) -> Option<T::Column<Type>>
117    where
118        Type: ColumnType,
119    {
120        let any_column = self.columns.get(name)?;
121        self.data_source
122            .convert_any_column::<Type>(any_column.clone())
123    }
124
125    /// Get an expression for a column or computed expression by name.
126    ///
127    /// If `name` matches a registered expression (from `with_expression`), evaluates
128    /// and returns it. Otherwise returns the column as an expression. Returns `None`
129    /// if the name doesn't match either.
130    pub fn get_column_expr(&self, name: &str) -> Option<vantage_expressions::Expression<T::Value>>
131    where
132        T::Column<T::AnyType>: vantage_expressions::Expressive<T::Value>,
133    {
134        if let Some(expr_fn) = self.expressions.get(name) {
135            Some(expr_fn(self))
136        } else {
137            use vantage_expressions::Expressive;
138            self.columns.get(name).map(|c| c.expr())
139        }
140    }
141}
142
143impl<T, E> Table<T, E>
144where
145    T: TableSource + ExprDataSource<T::Value>,
146    E: Entity<T::Value> + 'static,
147{
148    /// Expression yielding all values of the named column under the
149    /// table's current conditions.
150    ///
151    /// SQL backends materialise this as a `SELECT col FROM tbl WHERE …`
152    /// subquery (embeddable directly into IN clauses); non-query
153    /// backends wrap a `DeferredFn` that runs `list_table_values` and
154    /// projects the column at execute time.
155    ///
156    /// Panics if `column_name` isn't a column on this table — the
157    /// callsite is meant to be a literal column reference, so a typo
158    /// is a programmer error, not a runtime failure mode worth
159    /// surfacing as `Result`.
160    pub fn column_values_expr(&self, column_name: &str) -> Expression<T::Value> {
161        let col = self
162            .get_column::<T::AnyType>(column_name)
163            .unwrap_or_else(|| panic!("column {column_name:?} not found on table"));
164        self.data_source.column_table_values_expr(self, &col).expr()
165    }
166}
167
168#[cfg(test)]
169mod tests {
170    use super::*;
171    use crate::mocks::mock_column::MockColumn;
172    use crate::prelude::MockTableSource;
173    use serde_json::Value;
174    use vantage_types::EmptyEntity;
175
176    #[test]
177    fn test_add_column() {
178        let ds = MockTableSource::new();
179        let mut table = Table::<MockTableSource, EmptyEntity>::new("test", ds);
180
181        table.add_column(MockColumn::<String>::new("name"));
182
183        assert!(table.columns().contains_key("name"));
184        assert_eq!(table.columns().len(), 1);
185    }
186
187    #[test]
188    fn test_with_column() {
189        let ds = MockTableSource::new();
190        let table = Table::<MockTableSource, EmptyEntity>::new("test", ds)
191            .with_column(MockColumn::<Value>::new("name"))
192            .with_column(MockColumn::<i32>::new("email"));
193
194        assert!(table.columns().contains_key("name"));
195        assert!(table.columns().contains_key("email"));
196        assert_eq!(table.columns().len(), 2);
197    }
198
199    #[test]
200    #[should_panic(expected = "Duplicate column")]
201    fn test_duplicate_column_panics() {
202        let ds = MockTableSource::new();
203        let mut table = Table::<MockTableSource, EmptyEntity>::new("test", ds);
204
205        table.add_column(MockColumn::<String>::new("name"));
206        table.add_column(MockColumn::<String>::new("name")); // Should panic
207    }
208
209    #[test]
210    fn test_with_column_of() {
211        let ds = MockTableSource::new();
212        let table = Table::<MockTableSource, EmptyEntity>::new("test", ds)
213            .with_column_of::<String>("name")
214            .with_column_of::<i64>("age")
215            .with_column_of::<bool>("active");
216
217        assert!(table.columns().contains_key("name"));
218        assert!(table.columns().contains_key("age"));
219        assert!(table.columns().contains_key("active"));
220        assert_eq!(table.columns().len(), 3);
221    }
222
223    #[test]
224    fn test_add_column_of() {
225        let ds = MockTableSource::new();
226        let mut table = Table::<MockTableSource, EmptyEntity>::new("test", ds);
227
228        table.add_column_of::<String>("email");
229        table.add_column_of::<i64>("balance");
230
231        assert!(table.columns().contains_key("email"));
232        assert!(table.columns().contains_key("balance"));
233        assert_eq!(table.columns().len(), 2);
234    }
235
236    #[test]
237    fn test_columns_access() {
238        let ds = MockTableSource::new();
239        let table = Table::<MockTableSource, EmptyEntity>::new("test", ds)
240            .with_column_of::<String>("name")
241            .with_column_of::<i64>("age");
242
243        let columns = table.columns();
244        assert!(columns.contains_key("name"));
245        assert!(columns.contains_key("age"));
246        assert_eq!(columns.len(), 2);
247
248        let name_column = table.columns().get("name");
249        assert!(name_column.is_some());
250        assert_eq!(name_column.unwrap().name(), "name");
251
252        let missing_column = table.columns().get("missing");
253        assert!(missing_column.is_none());
254    }
255}