use async_trait::async_trait;
use ciborium::Value as CborValue;
use indexmap::IndexMap;
use vantage_core::Result;
use vantage_table::table::Table;
use vantage_table::traits::table_source::TableSource;
use vantage_types::{EmptyEntity, Entity, InvariantValue, Record};
use vantage_vista::{
Capability, NativeCondition, Reference, Vista, VistaChangeStream, unsupported,
};
use super::shell::SqlTableShell;
pub(crate) type Shell<T, E> = SqlTableShell<T, E, <T as SqlShellDialect>::Extra>;
#[async_trait]
pub(crate) trait SqlShellDialect:
TableSource<Id = String, Value: From<CborValue> + InvariantValue> + Send + Sync
{
type Extra: Clone + Send + Sync + 'static;
const DRIVER: &'static str;
fn cell(value: CborValue) -> Self::Value;
fn cbor(value: Self::Value) -> CborValue;
fn preview<E: Entity<Self::Value>>(table: &Table<Self, E>) -> String;
fn compare(
column: &Self::Column<Self::AnyType>,
template: &str,
value: Self::Value,
) -> Self::Condition;
fn in_list(
column: &Self::Column<Self::AnyType>,
values: &[Self::Value],
negate: bool,
) -> Self::Condition;
fn like(field: &str, pattern: String) -> Self::Condition;
fn order_term(field: &str) -> Self::Condition;
fn ordered_for_paging<E: Entity<Self::Value>>(table: &Table<Self, E>) -> Table<Self, E>;
fn target_factory<E>(
shell: &Shell<Self, E>,
) -> impl Fn(Table<Self, EmptyEntity>) -> Result<Vista> + Send + Sync + 'static
where
E: Entity<Self::Value> + 'static;
fn join_target<E>(
shell: &Shell<Self, E>,
relation: &str,
reference: &Reference,
row: Option<&Record<CborValue>>,
) -> Result<Vista>
where
E: Entity<Self::Value> + 'static,
{
shell.join_target(relation, reference, row)
}
fn watch<E>(_shell: &Shell<Self, E>) -> Result<VistaChangeStream>
where
E: Entity<Self::Value> + 'static,
{
unsupported(Capability::Subscribe)
}
fn add_native_condition<E>(
_shell: &mut Shell<Self, E>,
_condition: NativeCondition,
) -> Result<()>
where
E: Entity<Self::Value> + 'static,
{
unsupported(Capability::NativeConditions)
}
async fn import<E>(
_shell: &Shell<Self, E>,
_records: &IndexMap<String, Record<CborValue>>,
) -> Result<usize>
where
E: Entity<Self::Value> + 'static,
{
unsupported(Capability::Import)
}
#[cfg(feature = "rhai")]
fn register_rhai_extensions(_engine: &mut vantage_rhai::rhai::Engine) {}
}