use crate::error::EFResult;
use crate::metadata::EntityTypeMeta;
use crate::provider::DbValue;
use std::any::TypeId;
use std::collections::HashMap;
pub fn materialize_entities<T: IEntityType + IFromRow>(rows: &[Vec<DbValue>]) -> EFResult<Vec<T>> {
let mut entities = Vec::with_capacity(rows.len());
for row in rows {
let entity = T::from_row(row)?;
entities.push(entity);
}
Ok(entities)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EntityState {
Detached,
Added,
Unchanged,
Modified,
Deleted,
}
pub trait IEntityType: Send + Sync + 'static {
fn entity_meta() -> EntityTypeMeta
where
Self: Sized;
fn entity_meta_instance(&self) -> EntityTypeMeta
where
Self: Sized,
{
Self::entity_meta()
}
}
pub trait IFromRow: IEntityType + Sized {
fn from_row(values: &[DbValue]) -> EFResult<Self>;
}
pub trait IGetKeyValues: IEntityType {
fn key_values(&self) -> HashMap<String, DbValue>;
fn set_auto_increment_key(&mut self, _key: i64) {}
fn set_foreign_key(&mut self, _target_type: TypeId, _key: i64) {}
}
pub trait IEntitySnapshot: IEntityType {
fn snapshot(&self) -> HashMap<String, DbValue>;
}
pub trait ILazyInit: IEntityType {
fn attach_lazy_contexts(
&mut self,
provider: std::sync::Arc<dyn crate::provider::IDatabaseProvider>,
filter_map: Option<
std::sync::Arc<std::collections::HashMap<String, crate::query::CompiledFilter>>,
>,
depth: usize,
);
}
#[async_trait::async_trait]
pub trait INavigationSetter: IEntityType {
fn apply_has_many(&mut self, field: &str, rows: &[Vec<DbValue>]) -> EFResult<()> {
let _ = (field, rows);
Ok(())
}
fn apply_reference(&mut self, field: &str, row: &[DbValue]) -> EFResult<()> {
let _ = (field, row);
Ok(())
}
fn drain_has_many(&mut self, field: &str) -> Option<Vec<Box<dyn std::any::Any + Send + Sync>>> {
let _ = field;
None
}
#[allow(clippy::type_complexity)]
async fn load_nested_includes(
entities: &mut [Self],
parent_navigation: &str,
nested: &[crate::query::IncludePath],
provider: &dyn crate::provider::IDatabaseProvider,
filter_map: Option<&std::collections::HashMap<String, crate::query::CompiledFilter>>,
) -> EFResult<()>
where
Self: Sized,
{
let _ = (entities, parent_navigation, nested, provider, filter_map);
Ok(())
}
}