Skip to main content

vantage_table/traits/
table_source.rs

1use std::hash::Hash;
2use std::pin::Pin;
3
4use async_trait::async_trait;
5use futures_core::Stream;
6use indexmap::IndexMap;
7use vantage_dataset::traits::Result;
8use vantage_expressions::{
9    Expression,
10    traits::associated_expressions::AssociatedExpression,
11    traits::datasource::{DataSource, ExprDataSource},
12    traits::expressive::ExpressiveEnum,
13};
14use vantage_types::{Entity, Record};
15
16use crate::{column::core::ColumnType, table::Table, traits::column_like::ColumnLike};
17
18/// Trait for table data sources that defines column type separate from execution
19/// TableSource represents a data source that can create and manage tables
20#[async_trait]
21pub trait TableSource: DataSource + Clone + 'static {
22    type Column<Type>: ColumnLike<Type> + Clone
23    where
24        Type: ColumnType;
25    type AnyType: ColumnType;
26    type Value: Clone + Send + Sync + 'static;
27    type Id: Send + Sync + Clone + Hash + Eq + 'static;
28
29    /// How this backend names its source. Most backends use `String`;
30    /// SQL/SurrealDB backends use `SelectSource<Self::Select>` so a table can
31    /// be sourced from an arbitrary sub-`SELECT`. See [`crate::source`].
32    type Source: crate::traits::table_source_spec::TableSourceSpec;
33
34    /// The condition type stored by `Table`. SQL/SurrealDB backends use
35    /// `Expression<Self::Value>`; document-oriented backends like MongoDB
36    /// can use a native filter type (e.g. `bson::Document`).
37    type Condition: Clone + Send + Sync + 'static;
38
39    /// Build a textual `field == value` condition.
40    ///
41    /// Generic UIs (e.g. CLI argument parsers) only have strings on
42    /// hand, so they need a way to construct a `Self::Condition` without
43    /// knowing the backend's native expression type. Backends that
44    /// don't support raw text filtering can leave the default, which
45    /// returns an error.
46    fn eq_condition(field: &str, value: &str) -> Result<Self::Condition> {
47        let _ = (field, value);
48        Err(vantage_core::error!(
49            "eq_condition not implemented for this TableSource"
50        ))
51    }
52
53    /// Build a `field == value` condition with a typed `Self::Value`.
54    ///
55    /// Sibling of [`TableSource::eq_condition`] for the case where the caller already
56    /// has a native-typed value in hand (e.g. `Reference::resolve_from_row`
57    /// pulling a join value out of a `Record<Self::Value>`). Avoids going
58    /// through string serialization and avoids the `Expression → T::Condition`
59    /// coercion that some document backends only support via a panic stub.
60    ///
61    /// Backends that participate in row-based reference traversal override;
62    /// the default returns an error so existing impls compile.
63    fn eq_value_condition(&self, field: &str, value: Self::Value) -> Result<Self::Condition> {
64        let _ = (field, value);
65        Err(vantage_core::error!(
66            "eq_value_condition not implemented for this TableSource"
67        ))
68    }
69
70    /// Coerce a reference join value into the backend's native id form.
71    ///
72    /// Reference traversal (`resolve_from_row`) reads the join value raw out
73    /// of the parent row and uses it for both the narrowing eq-condition and
74    /// the target's insert invariant. On scalar-FK backends any value compares
75    /// as-is, so the default is identity. Backends with a richer id type
76    /// override: SurrealDB re-tags a `"table:key"` string — a record id that
77    /// round-tripped through JSON or a script — back into a record id, so the
78    /// condition renders a record literal (and an inserted child stores a
79    /// link), not a quoted string that matches nothing.
80    fn coerce_reference_value(&self, value: Self::Value) -> Self::Value {
81        value
82    }
83
84    /// Create a new column with the given name
85    fn create_column<Type: ColumnType>(&self, name: &str) -> Self::Column<Type>;
86
87    /// Convert a typed column to type-erased column
88    fn to_any_column<Type: ColumnType>(
89        &self,
90        column: Self::Column<Type>,
91    ) -> Self::Column<Self::AnyType>;
92
93    /// Attempt to convert a type-erased column back to typed column
94    fn convert_any_column<Type: ColumnType>(
95        &self,
96        any_column: Self::Column<Self::AnyType>,
97    ) -> Option<Self::Column<Type>>;
98
99    /// Create an expression from a template and parameters, similar to Expression::new
100    fn expr(
101        &self,
102        template: impl Into<String>,
103        parameters: Vec<ExpressiveEnum<Self::Value>>,
104    ) -> Expression<Self::Value>;
105
106    /// Create a search condition matching `search_value` across the table's
107    /// searchable fields. This is a *server-side* capability — it pushes the
108    /// match down to the backend's query engine.
109    ///
110    /// Different vendors implement search differently:
111    /// - SQL: `field LIKE '%value%'` (returns Expression)
112    /// - SurrealDB: `field CONTAINS 'value'` (returns Expression)
113    /// - MongoDB: `{ field: { $regex: 'value' } }` (returns MongoCondition)
114    ///
115    /// **Contract for backends that cannot search server-side** (e.g. CSV, redb):
116    /// return a condition that yields an `ErrorKind::Unsupported` error when it is
117    /// *resolved* — never a silent match-all (which leaks every row as if filtered)
118    /// and never a panic. Searching loaded/in-memory data is the Lens/Diorama
119    /// layer's responsibility, not the backend's.
120    fn search_table_condition<E>(
121        &self,
122        table: &Table<Self, E>,
123        search_value: &str,
124    ) -> Self::Condition
125    where
126        E: Entity<Self::Value>,
127        Self: Sized;
128
129    /// Get all data from a table as Record values with IDs (for ReadableValueSet implementation)
130    async fn list_table_values<E>(
131        &self,
132        table: &Table<Self, E>,
133    ) -> Result<IndexMap<Self::Id, Record<Self::Value>>>
134    where
135        E: Entity<Self::Value>,
136        Self: Sized;
137
138    /// Get a single record by ID as Record value (for ReadableValueSet implementation).
139    ///
140    /// Returns `Ok(None)` when no record exists with the given ID.
141    async fn get_table_value<E>(
142        &self,
143        table: &Table<Self, E>,
144        id: &Self::Id,
145    ) -> Result<Option<Record<Self::Value>>>
146    where
147        E: Entity<Self::Value>,
148        Self: Sized;
149
150    /// Get some data from a table as Record value with ID (for ReadableValueSet implementation)
151    async fn get_table_some_value<E>(
152        &self,
153        table: &Table<Self, E>,
154    ) -> Result<Option<(Self::Id, Record<Self::Value>)>>
155    where
156        E: Entity<Self::Value>,
157        Self: Sized;
158
159    /// Get count of records in the table
160    async fn get_table_count<E>(&self, table: &Table<Self, E>) -> Result<i64>
161    where
162        E: Entity<Self::Value>,
163        Self: Sized;
164
165    /// Get sum of a column in the table (returns native value type)
166    async fn get_table_sum<E>(
167        &self,
168        table: &Table<Self, E>,
169        column: &Self::Column<Self::AnyType>,
170    ) -> Result<Self::Value>
171    where
172        E: Entity<Self::Value>,
173        Self: Sized;
174
175    /// Get maximum value of a column in the table (returns native value type)
176    async fn get_table_max<E>(
177        &self,
178        table: &Table<Self, E>,
179        column: &Self::Column<Self::AnyType>,
180    ) -> Result<Self::Value>
181    where
182        E: Entity<Self::Value>,
183        Self: Sized;
184
185    /// Get minimum value of a column in the table (returns native value type)
186    async fn get_table_min<E>(
187        &self,
188        table: &Table<Self, E>,
189        column: &Self::Column<Self::AnyType>,
190    ) -> Result<Self::Value>
191    where
192        E: Entity<Self::Value>,
193        Self: Sized;
194
195    /// Insert a record as Record value (for WritableValueSet implementation)
196    async fn insert_table_value<E>(
197        &self,
198        table: &Table<Self, E>,
199        id: &Self::Id,
200        record: &Record<Self::Value>,
201    ) -> Result<Record<Self::Value>>
202    where
203        E: Entity<Self::Value>,
204        Self: Sized;
205
206    /// Replace a record as Record value (for WritableValueSet implementation)
207    async fn replace_table_value<E>(
208        &self,
209        table: &Table<Self, E>,
210        id: &Self::Id,
211        record: &Record<Self::Value>,
212    ) -> Result<Record<Self::Value>>
213    where
214        E: Entity<Self::Value>,
215        Self: Sized;
216
217    /// Patch a record as Record value (for WritableValueSet implementation)
218    async fn patch_table_value<E>(
219        &self,
220        table: &Table<Self, E>,
221        id: &Self::Id,
222        partial: &Record<Self::Value>,
223    ) -> Result<Record<Self::Value>>
224    where
225        E: Entity<Self::Value>,
226        Self: Sized;
227
228    /// Delete a record by ID (for WritableValueSet implementation)
229    async fn delete_table_value<E>(&self, table: &Table<Self, E>, id: &Self::Id) -> Result<()>
230    where
231        E: Entity<Self::Value>,
232        Self: Sized;
233
234    /// Delete every record the table represents (for WritableValueSet).
235    ///
236    /// **Honour `table.conditions()`.** A conditioned table is a SUBSET, and
237    /// every read path already treats it as one, so deleting it must delete
238    /// that subset. An implementation that builds its statement from
239    /// `table_name()` alone silently truncates the table instead — the caller
240    /// narrowed to one row, and lost all of them. An unconditioned table
241    /// still deletes everything, which is the only case this looks like.
242    async fn delete_table_all_values<E>(&self, table: &Table<Self, E>) -> Result<()>
243    where
244        E: Entity<Self::Value>,
245        Self: Sized;
246
247    /// Insert a record and return generated ID (for InsertableValueSet implementation)
248    async fn insert_table_return_id_value<E>(
249        &self,
250        table: &Table<Self, E>,
251        record: &Record<Self::Value>,
252    ) -> Result<Self::Id>
253    where
254        E: Entity<Self::Value>,
255        Self: Sized;
256
257    /// Stream all records from a table as (Id, Record) pairs.
258    ///
259    /// Default implementation wraps `list_table_values` into a stream.
260    /// Backends with native streaming (e.g. REST APIs with pagination)
261    /// can override this to yield records incrementally.
262    #[allow(clippy::type_complexity)]
263    fn stream_table_values<'a, E>(
264        &'a self,
265        table: &Table<Self, E>,
266    ) -> Pin<Box<dyn Stream<Item = Result<(Self::Id, Record<Self::Value>)>> + Send + 'a>>
267    where
268        E: Entity<Self::Value> + 'a,
269        Self: Sized,
270    {
271        let table = table.clone();
272        Box::pin(async_stream::stream! {
273            let records = self.list_table_values(&table).await;
274            match records {
275                Ok(map) => {
276                    for item in map {
277                        yield Ok(item);
278                    }
279                }
280                Err(e) => yield Err(e),
281            }
282        })
283    }
284
285    /// Build a condition for "target_field IN (values of source_column from source_table)".
286    ///
287    /// Used by the relationship traversal system (`get_ref_as`) to filter
288    /// a target table by foreign key values from a source table.
289    ///
290    /// Each backend implements this natively:
291    /// - SQL backends build a subquery: `"id" IN (SELECT "bakery_id" FROM "client" WHERE ...)`
292    /// - MongoDB builds a deferred `{ field: { "$in": [...] } }` document
293    /// - CSV fetches values in memory and builds an IN condition
294    fn related_in_condition<SourceE: Entity<Self::Value> + 'static>(
295        &self,
296        target_field: &str,
297        source_table: &Table<Self, SourceE>,
298        source_column: &str,
299    ) -> Self::Condition
300    where
301        Self: Sized;
302
303    /// Build a correlated condition: `target_table.target_field = source_table.source_column`.
304    ///
305    /// Used by `get_subquery_as` / `get_subquery_erased` for embedding correlated
306    /// subqueries inside SELECT expressions (e.g.
307    /// `(SELECT COUNT(*) FROM order WHERE order.client_id = client.id)`), and by
308    /// the generic implicit-reference lowering. SQL and SurrealDB backends
309    /// produce table-qualified equality; backends without correlated subqueries
310    /// leave the default (which panics — they must also keep
311    /// [`supports_traversal`](Self::supports_traversal) `false`).
312    fn related_correlated_condition(
313        &self,
314        target_table: &str,
315        target_field: &str,
316        source_table: &str,
317        source_column: &str,
318    ) -> Self::Condition {
319        let _ = (target_table, target_field, source_table, source_column);
320        unimplemented!("correlated subqueries not supported by this backend")
321    }
322
323    /// Whether this backend can lower a dotted active column
324    /// (`"country.name"`) into its own query — a nested correlated subquery for
325    /// SQL, a native idiom path for SurrealDB. Backends that can build neither
326    /// (e.g. MongoDB, CSV, REST) leave the default `false`, and
327    /// [`Table::with_active_columns`](crate::table::Table::with_active_columns)
328    /// rejects dotted names against them at build time rather than at fetch time.
329    fn supports_traversal(&self) -> bool {
330        false
331    }
332
333    /// Build a backend-native path expression for a dotted active column,
334    /// tried before the generic correlated-subquery chain.
335    ///
336    /// `hops` holds each traversed relation's **foreign-key/link field** (not
337    /// its registry name — a relation named `owner` over a link field `client`
338    /// must lower to `client.…`), and `column` the final field. The default
339    /// returns `None`, so backends fall back to the generic chain built on
340    /// [`related_correlated_condition`](Self::related_correlated_condition).
341    /// SurrealDB overrides this to emit an idiom path (`batch.golf_course.name`,
342    /// each segment escaped separately); multi-hop comes for free. The returned
343    /// expression is unaliased — `select()` aliases it to the dotted name.
344    fn traversal_path_expr(&self, hops: &[&str], column: &str) -> Option<Expression<Self::Value>> {
345        let _ = (hops, column);
346        None
347    }
348
349    /// Return an associated expression that, when resolved, yields all values
350    /// of the given typed column from this table (respecting current conditions).
351    ///
352    /// For query-language backends, this can be a subquery expression.
353    /// For simple backends (CSV), this uses a `DeferredFn` that loads data
354    /// and extracts the column values at execution time.
355    ///
356    /// The returned `AssociatedExpression` can be:
357    /// - Executed directly: `.get().await -> Result<Vec<Type>>`
358    /// - Composed into expressions: used via `Expressive` trait in `in_()` conditions
359    ///
360    /// ```rust,ignore
361    /// let fk_col = source.get_column::<String>("bakery_id").unwrap();
362    /// let fk_values = source.data_source().column_table_values_expr(&source, &fk_col);
363    /// // Execute: let ids = fk_values.get().await?;
364    /// // Or compose: target.add_condition(target["id"].in_((fk_values)));
365    /// ```
366    fn column_table_values_expr<'a, E, Type: ColumnType>(
367        &'a self,
368        table: &Table<Self, E>,
369        column: &Self::Column<Type>,
370    ) -> AssociatedExpression<'a, Self, Self::Value, Vec<Type>>
371    where
372        E: Entity<Self::Value> + 'static,
373        Self: ExprDataSource<Self::Value> + Sized;
374}