use super::active_model::ActiveModelTrait;
use super::column::ColumnTrait;
use super::iden::IdenStatic;
use super::model::ModelTrait;
use super::primary_key::{PrimaryKeyToColumn, PrimaryKeyTrait};
use super::relation::{RelationBuilder, RelationTrait, RelationType};
use crate::query::{DeleteMany, DeleteOne, Insert, InsertMany, Select, UpdateMany, UpdateOne};
use crate::types::IntoValueTuple;
pub trait EntityTrait: IdenStatic + Default {
type Model: ModelTrait<Entity = Self> + super::FromQueryResult;
type Column: ColumnTrait;
type PrimaryKey: PrimaryKeyTrait + PrimaryKeyToColumn<Column = Self::Column>;
type ActiveModel: ActiveModelTrait<Entity = Self>;
type Relation: RelationTrait;
const TABLE_NAME: &'static str;
fn table_name() -> &'static str {
Self::TABLE_NAME
}
fn belongs_to<R: EntityTrait>(_: R) -> RelationBuilder<Self, R> {
RelationBuilder::new(RelationType::HasOne, true)
}
fn has_one<R: EntityTrait>(_: R) -> RelationBuilder<Self, R> {
RelationBuilder::new(RelationType::HasOne, false)
}
fn has_many<R: EntityTrait>(_: R) -> RelationBuilder<Self, R> {
RelationBuilder::new(RelationType::HasMany, false)
}
fn find() -> Select<Self> {
Select::new()
}
fn find_by_id<T>(id: T) -> Select<Self>
where
T: Into<<Self::PrimaryKey as PrimaryKeyTrait>::ValueType>,
{
Select::new().filter_by_pk(id.into().into_value_tuple())
}
fn insert<A: ActiveModelTrait<Entity = Self>>(model: A) -> Insert<A> {
Insert::one(model)
}
fn insert_many<A: ActiveModelTrait<Entity = Self>, I: IntoIterator<Item = A>>(
models: I,
) -> InsertMany<A> {
InsertMany::many(models)
}
fn update<A: ActiveModelTrait<Entity = Self>>(model: A) -> UpdateOne<A> {
UpdateOne::new(model)
}
fn update_many() -> UpdateMany<Self> {
UpdateMany::new()
}
fn delete<A: ActiveModelTrait<Entity = Self>>(model: A) -> DeleteOne<A> {
DeleteOne::new(model)
}
fn delete_many() -> DeleteMany<Self> {
DeleteMany::new()
}
fn delete_by_id<T>(id: T) -> DeleteMany<Self>
where
T: Into<<Self::PrimaryKey as PrimaryKeyTrait>::ValueType>,
{
DeleteMany::new().filter_by_pk(id.into().into_value_tuple())
}
}