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    /// Create a new column with the given name
71    fn create_column<Type: ColumnType>(&self, name: &str) -> Self::Column<Type>;
72
73    /// Convert a typed column to type-erased column
74    fn to_any_column<Type: ColumnType>(
75        &self,
76        column: Self::Column<Type>,
77    ) -> Self::Column<Self::AnyType>;
78
79    /// Attempt to convert a type-erased column back to typed column
80    fn convert_any_column<Type: ColumnType>(
81        &self,
82        any_column: Self::Column<Self::AnyType>,
83    ) -> Option<Self::Column<Type>>;
84
85    /// Create an expression from a template and parameters, similar to Expression::new
86    fn expr(
87        &self,
88        template: impl Into<String>,
89        parameters: Vec<ExpressiveEnum<Self::Value>>,
90    ) -> Expression<Self::Value>;
91
92    /// Create a search condition matching `search_value` across the table's
93    /// searchable fields. This is a *server-side* capability — it pushes the
94    /// match down to the backend's query engine.
95    ///
96    /// Different vendors implement search differently:
97    /// - SQL: `field LIKE '%value%'` (returns Expression)
98    /// - SurrealDB: `field CONTAINS 'value'` (returns Expression)
99    /// - MongoDB: `{ field: { $regex: 'value' } }` (returns MongoCondition)
100    ///
101    /// **Contract for backends that cannot search server-side** (e.g. CSV, redb):
102    /// return a condition that yields an `ErrorKind::Unsupported` error when it is
103    /// *resolved* — never a silent match-all (which leaks every row as if filtered)
104    /// and never a panic. Searching loaded/in-memory data is the Lens/Diorama
105    /// layer's responsibility, not the backend's.
106    fn search_table_condition<E>(
107        &self,
108        table: &Table<Self, E>,
109        search_value: &str,
110    ) -> Self::Condition
111    where
112        E: Entity<Self::Value>,
113        Self: Sized;
114
115    /// Get all data from a table as Record values with IDs (for ReadableValueSet implementation)
116    async fn list_table_values<E>(
117        &self,
118        table: &Table<Self, E>,
119    ) -> Result<IndexMap<Self::Id, Record<Self::Value>>>
120    where
121        E: Entity<Self::Value>,
122        Self: Sized;
123
124    /// Get a single record by ID as Record value (for ReadableValueSet implementation).
125    ///
126    /// Returns `Ok(None)` when no record exists with the given ID.
127    async fn get_table_value<E>(
128        &self,
129        table: &Table<Self, E>,
130        id: &Self::Id,
131    ) -> Result<Option<Record<Self::Value>>>
132    where
133        E: Entity<Self::Value>,
134        Self: Sized;
135
136    /// Get some data from a table as Record value with ID (for ReadableValueSet implementation)
137    async fn get_table_some_value<E>(
138        &self,
139        table: &Table<Self, E>,
140    ) -> Result<Option<(Self::Id, Record<Self::Value>)>>
141    where
142        E: Entity<Self::Value>,
143        Self: Sized;
144
145    /// Get count of records in the table
146    async fn get_table_count<E>(&self, table: &Table<Self, E>) -> Result<i64>
147    where
148        E: Entity<Self::Value>,
149        Self: Sized;
150
151    /// Get sum of a column in the table (returns native value type)
152    async fn get_table_sum<E>(
153        &self,
154        table: &Table<Self, E>,
155        column: &Self::Column<Self::AnyType>,
156    ) -> Result<Self::Value>
157    where
158        E: Entity<Self::Value>,
159        Self: Sized;
160
161    /// Get maximum value of a column in the table (returns native value type)
162    async fn get_table_max<E>(
163        &self,
164        table: &Table<Self, E>,
165        column: &Self::Column<Self::AnyType>,
166    ) -> Result<Self::Value>
167    where
168        E: Entity<Self::Value>,
169        Self: Sized;
170
171    /// Get minimum value of a column in the table (returns native value type)
172    async fn get_table_min<E>(
173        &self,
174        table: &Table<Self, E>,
175        column: &Self::Column<Self::AnyType>,
176    ) -> Result<Self::Value>
177    where
178        E: Entity<Self::Value>,
179        Self: Sized;
180
181    /// Insert a record as Record value (for WritableValueSet implementation)
182    async fn insert_table_value<E>(
183        &self,
184        table: &Table<Self, E>,
185        id: &Self::Id,
186        record: &Record<Self::Value>,
187    ) -> Result<Record<Self::Value>>
188    where
189        E: Entity<Self::Value>,
190        Self: Sized;
191
192    /// Replace a record as Record value (for WritableValueSet implementation)
193    async fn replace_table_value<E>(
194        &self,
195        table: &Table<Self, E>,
196        id: &Self::Id,
197        record: &Record<Self::Value>,
198    ) -> Result<Record<Self::Value>>
199    where
200        E: Entity<Self::Value>,
201        Self: Sized;
202
203    /// Patch a record as Record value (for WritableValueSet implementation)
204    async fn patch_table_value<E>(
205        &self,
206        table: &Table<Self, E>,
207        id: &Self::Id,
208        partial: &Record<Self::Value>,
209    ) -> Result<Record<Self::Value>>
210    where
211        E: Entity<Self::Value>,
212        Self: Sized;
213
214    /// Delete a record by ID (for WritableValueSet implementation)
215    async fn delete_table_value<E>(&self, table: &Table<Self, E>, id: &Self::Id) -> Result<()>
216    where
217        E: Entity<Self::Value>,
218        Self: Sized;
219
220    /// Delete all records (for WritableValueSet implementation)
221    async fn delete_table_all_values<E>(&self, table: &Table<Self, E>) -> Result<()>
222    where
223        E: Entity<Self::Value>,
224        Self: Sized;
225
226    /// Insert a record and return generated ID (for InsertableValueSet implementation)
227    async fn insert_table_return_id_value<E>(
228        &self,
229        table: &Table<Self, E>,
230        record: &Record<Self::Value>,
231    ) -> Result<Self::Id>
232    where
233        E: Entity<Self::Value>,
234        Self: Sized;
235
236    /// Stream all records from a table as (Id, Record) pairs.
237    ///
238    /// Default implementation wraps `list_table_values` into a stream.
239    /// Backends with native streaming (e.g. REST APIs with pagination)
240    /// can override this to yield records incrementally.
241    #[allow(clippy::type_complexity)]
242    fn stream_table_values<'a, E>(
243        &'a self,
244        table: &Table<Self, E>,
245    ) -> Pin<Box<dyn Stream<Item = Result<(Self::Id, Record<Self::Value>)>> + Send + 'a>>
246    where
247        E: Entity<Self::Value> + 'a,
248        Self: Sized,
249    {
250        let table = table.clone();
251        Box::pin(async_stream::stream! {
252            let records = self.list_table_values(&table).await;
253            match records {
254                Ok(map) => {
255                    for item in map {
256                        yield Ok(item);
257                    }
258                }
259                Err(e) => yield Err(e),
260            }
261        })
262    }
263
264    /// Build a condition for "target_field IN (values of source_column from source_table)".
265    ///
266    /// Used by the relationship traversal system (`get_ref_as`) to filter
267    /// a target table by foreign key values from a source table.
268    ///
269    /// Each backend implements this natively:
270    /// - SQL backends build a subquery: `"id" IN (SELECT "bakery_id" FROM "client" WHERE ...)`
271    /// - MongoDB builds a deferred `{ field: { "$in": [...] } }` document
272    /// - CSV fetches values in memory and builds an IN condition
273    fn related_in_condition<SourceE: Entity<Self::Value> + 'static>(
274        &self,
275        target_field: &str,
276        source_table: &Table<Self, SourceE>,
277        source_column: &str,
278    ) -> Self::Condition
279    where
280        Self: Sized;
281
282    /// Build a correlated condition: `target_table.target_field = source_table.source_column`.
283    ///
284    /// Used by `get_subquery_as` / `get_subquery_erased` for embedding correlated
285    /// subqueries inside SELECT expressions (e.g.
286    /// `(SELECT COUNT(*) FROM order WHERE order.client_id = client.id)`), and by
287    /// the generic implicit-reference lowering. SQL and SurrealDB backends
288    /// produce table-qualified equality; backends without correlated subqueries
289    /// leave the default (which panics — they must also keep
290    /// [`supports_traversal`](Self::supports_traversal) `false`).
291    fn related_correlated_condition(
292        &self,
293        target_table: &str,
294        target_field: &str,
295        source_table: &str,
296        source_column: &str,
297    ) -> Self::Condition {
298        let _ = (target_table, target_field, source_table, source_column);
299        unimplemented!("correlated subqueries not supported by this backend")
300    }
301
302    /// Whether this backend can lower a dotted active column
303    /// (`"country.name"`) into its own query — a nested correlated subquery for
304    /// SQL, a native idiom path for SurrealDB. Backends that can build neither
305    /// (e.g. MongoDB, CSV, REST) leave the default `false`, and
306    /// [`Table::with_active_columns`](crate::table::Table::with_active_columns)
307    /// rejects dotted names against them at build time rather than at fetch time.
308    fn supports_traversal(&self) -> bool {
309        false
310    }
311
312    /// Build a backend-native path expression for a dotted active column,
313    /// tried before the generic correlated-subquery chain.
314    ///
315    /// `hops` holds each traversed relation's **foreign-key/link field** (not
316    /// its registry name — a relation named `owner` over a link field `client`
317    /// must lower to `client.…`), and `column` the final field. The default
318    /// returns `None`, so backends fall back to the generic chain built on
319    /// [`related_correlated_condition`](Self::related_correlated_condition).
320    /// SurrealDB overrides this to emit an idiom path (`batch.golf_course.name`,
321    /// each segment escaped separately); multi-hop comes for free. The returned
322    /// expression is unaliased — `select()` aliases it to the dotted name.
323    fn traversal_path_expr(&self, hops: &[&str], column: &str) -> Option<Expression<Self::Value>> {
324        let _ = (hops, column);
325        None
326    }
327
328    /// Return an associated expression that, when resolved, yields all values
329    /// of the given typed column from this table (respecting current conditions).
330    ///
331    /// For query-language backends, this can be a subquery expression.
332    /// For simple backends (CSV), this uses a `DeferredFn` that loads data
333    /// and extracts the column values at execution time.
334    ///
335    /// The returned `AssociatedExpression` can be:
336    /// - Executed directly: `.get().await -> Result<Vec<Type>>`
337    /// - Composed into expressions: used via `Expressive` trait in `in_()` conditions
338    ///
339    /// ```rust,ignore
340    /// let fk_col = source.get_column::<String>("bakery_id").unwrap();
341    /// let fk_values = source.data_source().column_table_values_expr(&source, &fk_col);
342    /// // Execute: let ids = fk_values.get().await?;
343    /// // Or compose: target.add_condition(target["id"].in_((fk_values)));
344    /// ```
345    fn column_table_values_expr<'a, E, Type: ColumnType>(
346        &'a self,
347        table: &Table<Self, E>,
348        column: &Self::Column<Type>,
349    ) -> AssociatedExpression<'a, Self, Self::Value, Vec<Type>>
350    where
351        E: Entity<Self::Value> + 'static,
352        Self: ExprDataSource<Self::Value> + Sized;
353}