sql_cli/data/
datasource_trait.rs

1use anyhow::Result;
2use serde_json::Value;
3use std::collections::HashMap;
4
5/// Response from a query operation
6#[derive(Debug, Clone)]
7pub struct DataSourceQueryResponse {
8    pub data: Vec<Value>,
9    pub count: usize,
10    pub columns: Vec<String>,
11    pub table_name: String,
12}
13
14/// Trait for abstracting data sources (CSV, JSON, Database, etc.)
15/// This allows the TUI to work with data without knowing the specific source
16pub trait DataSource: Send + Sync {
17    /// Execute a SQL-like query against the data source
18    fn query(&self, sql: &str) -> Result<DataSourceQueryResponse>;
19
20    /// Execute a query with case-insensitive matching
21    fn query_with_options(
22        &self,
23        sql: &str,
24        case_insensitive: bool,
25    ) -> Result<DataSourceQueryResponse>;
26
27    /// Get the schema (table names and their columns)
28    fn get_schema(&self) -> Option<HashMap<String, Vec<String>>>;
29
30    /// Get the primary table name
31    fn get_table_name(&self) -> String;
32
33    /// Get total row count (unfiltered)
34    fn get_row_count(&self) -> usize;
35
36    /// Check if data source is case-insensitive
37    fn is_case_insensitive(&self) -> bool;
38
39    /// Set case sensitivity
40    fn set_case_insensitive(&mut self, case_insensitive: bool);
41
42    /// Clone the data source into a boxed trait object
43    fn clone_box(&self) -> Box<dyn DataSource>;
44}
45
46/// Helper trait for converting to `DataTable`
47pub trait ToDataTable {
48    /// Convert the data source to a `DataTable` structure
49    fn to_datatable(&self) -> Result<crate::datatable::DataTable>;
50}