#![doc(hidden)]
pub mod auto;
pub mod index;
pub mod newtype;
pub mod shared_column;
pub mod storage;
pub mod version;
pub mod via;
pub use crate::schema::inventory;
pub use crate::{
Db, Error, Executor, Result, Statement,
schema::{
Auto, Deferred, DiscoverItem, Document, Embed, EmbedCreate, Field, Load, Model, QueryMany,
QueryOne, QueryOptionOne, RelationManyField, RelationOneField, Scalar, Scope, ViaMany,
ViaManyField, ViaPath, ViaTarget, generate_unique_id,
},
stmt::CreateMany,
stmt::{self, Assign, Expr, IntoExpr, IntoInsert, IntoStatement, List, Path},
update_target::UpdateTarget,
};
#[cfg(feature = "serde")]
pub use serde_json;
pub use std::{clone::Clone, convert::Into, default::Default, option::Option};
pub use self::version::Version;
pub use toasty_core as core;
pub type FieldExprTarget<F> = <F as Field>::ExprTarget;
pub type RelationOnePath<F, Origin> =
<<F as RelationOneField>::Target as Model>::OneField<Origin, <F as RelationOneField>::Expr>;
pub trait ModelCodegen: Model {
fn new_one_field<Origin, Target>(path: Path<Origin, Target>) -> Self::OneField<Origin, Target>;
fn to_relation_expr(&self, fields: &[&str]) -> core::stmt::Expr;
}
pub fn scope_fields<S: Scope>(_scope: &S) -> S::Path<S::Item> {
S::new_path_root()
}
pub fn create_in_scope<S: Scope>(scope: S) -> S::Create {
S::create_in_scope(scope)
}
pub fn into_untyped_expr<T, V: IntoExpr<T>>(value: V) -> core::stmt::Expr {
let expr: stmt::Expr<T> = value.into_expr();
expr.into()
}
pub trait EmbeddedRelationValue<F> {
type Model;
fn model_ref(&self) -> Option<&Self::Model>;
}
macro_rules! impl_embedded_relation_value {
($field:ty) => {
impl<M: Model> EmbeddedRelationValue<$field> for M {
type Model = M;
fn model_ref(&self) -> Option<&M> {
Some(self)
}
}
impl<M: Model> EmbeddedRelationValue<$field> for &M {
type Model = M;
fn model_ref(&self) -> Option<&M> {
Some(self)
}
}
impl<M: Model> EmbeddedRelationValue<$field> for &$field {
type Model = M;
fn model_ref(&self) -> Option<&M> {
(*self).model_ref()
}
}
};
}
impl_embedded_relation_value!(Deferred<M>);
impl_embedded_relation_value!(Deferred<Option<M>>);
impl_embedded_relation_value!(Option<M>);
impl<M: Model> EmbeddedRelationValue<M> for M {
type Model = M;
fn model_ref(&self) -> Option<&M> {
Some(self)
}
}
impl<M: Model> EmbeddedRelationValue<M> for &M {
type Model = M;
fn model_ref(&self) -> Option<&M> {
Some(self)
}
}
impl<M: Model> EmbeddedRelationValue<Option<M>> for Option<M> {
type Model = M;
fn model_ref(&self) -> Option<&M> {
self.as_ref()
}
}
impl<M: Model> EmbeddedRelationValue<Deferred<M>> for Deferred<M> {
type Model = M;
fn model_ref(&self) -> Option<&M> {
if self.is_unloaded() {
None
} else {
Some(self.get())
}
}
}
impl<M: Model> EmbeddedRelationValue<Deferred<Option<M>>> for Deferred<Option<M>> {
type Model = M;
fn model_ref(&self) -> Option<&M> {
if self.is_unloaded() {
None
} else {
self.get().as_ref()
}
}
}
pub fn embedded_relation_value_expr<F, V>(value: &V, fields: &[&str]) -> core::stmt::Expr
where
V: EmbeddedRelationValue<F>,
V::Model: ModelCodegen,
{
value
.model_ref()
.map_or_else(core::stmt::Expr::null, |model| {
model.to_relation_expr(fields)
})
}
pub fn chain_or_build_many<Source, Target>(
mut query: stmt::Query<List<Source>>,
field_offset: usize,
path: stmt::Path<Source, List<Target>>,
) -> stmt::Association<List<Target>>
where
Source: Model,
Target: Model,
{
match query.take_via_assoc() {
Some(untyped) => stmt::Association::<List<Source>>::from_untyped(untyped)
.chain_field::<Target>(field_offset),
None => stmt::Association::many(query, path),
}
}
pub fn chain_or_build_many_via_one<Source, Target>(
mut query: stmt::Query<List<Source>>,
field_offset: usize,
path: stmt::Path<Source, Target>,
) -> stmt::Association<List<Target>>
where
Source: Model,
Target: Model,
{
match query.take_via_assoc() {
Some(untyped) => stmt::Association::<List<Source>>::from_untyped(untyped)
.chain_field::<Target>(field_offset),
None => stmt::Association::many_via_one(query, path),
}
}