use std::any::TypeId;
use std::borrow::Cow;
use std::fmt;
use std::marker::PhantomData;
use std::sync::Arc;
use navree_kernel::BoxFuture;
use crate::collection::Collection;
use crate::column::Filter;
use crate::error::{OrmError, Result};
use crate::exec::{Access, Executor, Target};
use crate::ir::{Boolean, Expr, Operator, QueryIr, Where, WhereClause};
use crate::model::{Model, ModelMeta};
use crate::query::filters::HasWheres;
use crate::query::{Builder, WhereGroup};
use crate::record::Record;
use crate::relations::access::AccessConstraint;
use crate::relations::any::AnyRecord;
use crate::relations::core::{column_value, qualified};
use crate::relations::eager::{Constraint, ConstraintFn, EagerNode, EagerTree};
use crate::relations::existence::{
ExistsRequest, ExistsSelect, Refine, build_subquery, columns_equal,
};
use crate::relations::keys::{RelationKey, distinct_keys, in_keys, in_keys_on, key_of};
use crate::relations::morph_map::{MorphMap, type_name_of};
use crate::relations::path::{ParsedPath, RelationPath};
use crate::relations::registry::{ErasedRelation, RelationKind, RelationMeta, find};
use crate::relations::{Relation, private};
use crate::value::{Value, is_null};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) enum Trashed {
#[default]
Without,
With,
Only,
}
impl Trashed {
fn apply<M: Model>(self, query: Builder<M>) -> Builder<M> {
match self {
Trashed::Without => query,
Trashed::With => query.with_trashed(),
Trashed::Only => match M::meta().deleted_at() {
Some(column) => query.with_trashed().where_(Filter::from(Where::Null {
expr: Expr::Column(qualified(M::meta().table(), column)),
not: true,
})),
None => query,
},
}
}
}
#[derive(Clone, Debug)]
pub struct MorphTypeLoads {
type_id: TypeId,
model: &'static str,
with: EagerTree,
counts: Vec<String>,
constraints: Vec<Constraint>,
errors: Vec<String>,
}
impl MorphTypeLoads {
fn new<M: Model>() -> Self {
MorphTypeLoads {
type_id: TypeId::of::<M>(),
model: M::meta().name(),
with: EagerTree::default(),
counts: Vec::new(),
constraints: Vec::new(),
errors: Vec::new(),
}
}
fn merge(&mut self, other: &MorphTypeLoads) {
self.with.merge(&other.with);
self.counts.extend(other.counts.iter().cloned());
self.constraints.extend(other.constraints.iter().cloned());
self.errors.extend(other.errors.iter().cloned());
}
fn apply<M: Model>(&self, mut query: Builder<M>) -> Result<Builder<M>> {
if let Some(error) = self.errors.first() {
return Err(OrmError::invalid(error.clone()));
}
for constraint in &self.constraints {
match constraint
.closure
.downcast_ref::<ConstraintFn<Builder<M>>>()
{
Some(apply) => query = (apply.0)(query),
None => {
return Err(OrmError::invalid(format!(
"a morph constraint for {} takes `{}`",
self.model, constraint.takes
)));
}
}
}
query.eager.tree.merge(&self.with);
for count in &self.counts {
query = query.with_count(count.as_str());
}
Ok(query)
}
}
#[derive(Clone, Debug, Default)]
pub struct MorphWith {
types: Vec<MorphTypeLoads>,
}
impl MorphWith {
pub fn new() -> Self {
MorphWith::default()
}
fn loads<M: Model>(&mut self) -> &mut MorphTypeLoads {
let index = match self
.types
.iter()
.position(|loads| loads.type_id == TypeId::of::<M>())
{
Some(index) => index,
None => {
self.types.push(MorphTypeLoads::new::<M>());
self.types.len() - 1
}
};
&mut self.types[index]
}
pub fn on<M: Model>(mut self, paths: impl IntoIterator<Item = impl RelationPath<M>>) -> Self {
let loads = self.loads::<M>();
for path in paths {
let text = path.relation_path().into_owned();
match ParsedPath::parse(&text) {
Ok(parsed) => {
let node = loads.with.entry(&parsed.segments);
if parsed.columns.is_some() {
node.columns = parsed.columns;
}
}
Err(error) => loads.errors.push(error.to_string()),
}
}
self
}
pub fn count_on<M: Model>(
mut self,
paths: impl IntoIterator<Item = impl RelationPath<M>>,
) -> Self {
let loads = self.loads::<M>();
for path in paths {
loads.counts.push(path.relation_path().into_owned());
}
self
}
pub fn constrain<M: Model>(
mut self,
constraint: impl Fn(Builder<M>) -> Builder<M> + Send + Sync + 'static,
) -> Self {
self.loads::<M>()
.constraints
.push(Constraint::new(constraint));
self
}
pub(crate) fn merge(&mut self, other: &MorphWith) {
for loads in &other.types {
match self
.types
.iter_mut()
.find(|existing| existing.type_id == loads.type_id)
{
Some(existing) => existing.merge(loads),
None => self.types.push(loads.clone()),
}
}
}
pub(crate) fn for_type(&self, type_id: TypeId) -> Option<&MorphTypeLoads> {
self.types.iter().find(|loads| loads.type_id == type_id)
}
}
pub struct MorphLoad<'a> {
pub(crate) owner_key: Option<&'a str>,
pub(crate) keys: &'a [(RelationKey, Value)],
pub(crate) loads: Option<&'a MorphTypeLoads>,
pub(crate) children: &'a EagerTree,
pub(crate) conditions: &'a [Filter],
pub(crate) access: &'a [AccessConstraint],
pub(crate) trashed: Trashed,
}
pub struct MorphExists<'a> {
pub(crate) parent: &'static ModelMeta,
pub(crate) id_column: &'a str,
pub(crate) owner_key: Option<&'a str>,
pub(crate) depth: u32,
pub(crate) select: ExistsSelect,
pub(crate) condition: Option<Filter>,
pub(crate) trashed: Trashed,
}
pub trait MorphTarget: Send + Sync + 'static {
fn type_id(&self) -> TypeId;
fn morph_name(&self) -> Cow<'static, str>;
fn normalize_key(&self, value: Value, owner_key: Option<&str>) -> Value;
fn load<'a, 'b>(
&'a self,
target: &'a Target<'b>,
request: MorphLoad<'a>,
) -> BoxFuture<'a, Result<Vec<(RelationKey, AnyRecord)>>>;
fn load_into<'a, 'b>(
&'a self,
target: &'a Target<'b>,
records: Vec<AnyRecord>,
loads: &'a MorphTypeLoads,
aggregate: Option<&'a crate::relations::aggregates::AggregateSpec>,
) -> BoxFuture<'a, Result<Vec<AnyRecord>>>;
fn load_missing_into<'a, 'b>(
&'a self,
target: &'a Target<'b>,
records: Vec<AnyRecord>,
rest: &'a EagerTree,
) -> BoxFuture<'a, Result<Vec<AnyRecord>>>;
fn exists_query(&self, request: MorphExists<'_>) -> Result<QueryIr>;
}
fn typed_records<M: Model>(records: Vec<AnyRecord>) -> Result<Vec<Record<M>>> {
records
.into_iter()
.map(|record| {
record.downcast::<M>().map_err(|other| {
OrmError::invalid(format!(
"a {} among the {} records of a morph load",
other.model_name(),
M::meta().name()
))
})
})
.collect()
}
pub(crate) struct MorphTargetOf<M: Model>(PhantomData<fn() -> M>);
impl<M: Model> MorphTargetOf<M> {
pub(crate) fn new() -> Self {
MorphTargetOf(PhantomData)
}
}
impl<M: Model> MorphTarget for MorphTargetOf<M> {
fn type_id(&self) -> TypeId {
TypeId::of::<M>()
}
fn morph_name(&self) -> Cow<'static, str> {
M::morph_name()
}
fn normalize_key(&self, value: Value, owner_key: Option<&str>) -> Value {
match owner_key {
Some(column) if column != M::meta().key_column() => value,
_ => match <M::Key as crate::Field>::from_value(value.clone()) {
Ok(key) => crate::Field::to_value(&key),
Err(_) => value,
},
}
}
fn load<'a, 'b>(
&'a self,
target: &'a Target<'b>,
request: MorphLoad<'a>,
) -> BoxFuture<'a, Result<Vec<(RelationKey, AnyRecord)>>> {
Box::pin(async move {
let meta = M::meta();
let keys = distinct_keys(
request
.keys
.iter()
.map(|(_, value)| self.normalize_key(value.clone(), request.owner_key)),
);
let owner_key = request.owner_key.unwrap_or(meta.key_column());
let mut query = request
.trashed
.apply(Builder::<M>::new().with_relation_defaults());
if let Some(loads) = request.loads {
query = loads.apply(query)?;
}
for condition in request.conditions {
query = query.where_(condition.clone());
}
for access in request.access {
query = access.apply(query)?;
}
query.eager.tree.merge(request.children);
let query = query.where_(Filter::from(in_keys_on(
target,
qualified(meta.table(), owner_key),
&keys,
)));
let records = query.fetch(target).await?;
let mut found = Vec::with_capacity(records.len());
for record in records {
let value = column_value(&record.model, owner_key, "morph_to")?;
if let Some(key) = key_of(&value) {
found.push((key, AnyRecord::new(record)));
}
}
Ok(found)
})
}
fn load_into<'a, 'b>(
&'a self,
target: &'a Target<'b>,
records: Vec<AnyRecord>,
loads: &'a MorphTypeLoads,
aggregate: Option<&'a crate::relations::aggregates::AggregateSpec>,
) -> BoxFuture<'a, Result<Vec<AnyRecord>>> {
Box::pin(async move {
let mut typed = typed_records::<M>(records)?;
match aggregate {
None => {
crate::relations::eager::load_tree(target, &mut typed, &loads.with).await?;
if !loads.counts.is_empty() {
let count = crate::relations::aggregates::AggregateSpec::count();
crate::relations::aggregates::load_aggregate_on(
target,
&mut typed,
&loads.counts,
&count,
)
.await?;
}
}
Some(spec) => {
let mut paths: Vec<String> = loads
.with
.paths()
.into_iter()
.filter(|path| !path.contains('.'))
.collect();
for counted in &loads.counts {
if !paths.contains(counted) {
paths.push(counted.clone());
}
}
crate::relations::aggregates::load_aggregate_on(
target, &mut typed, &paths, spec,
)
.await?;
}
}
Ok(typed.into_iter().map(AnyRecord::new).collect())
})
}
fn load_missing_into<'a, 'b>(
&'a self,
target: &'a Target<'b>,
records: Vec<AnyRecord>,
rest: &'a EagerTree,
) -> BoxFuture<'a, Result<Vec<AnyRecord>>> {
Box::pin(async move {
let mut typed = typed_records::<M>(records)?;
crate::relations::lazy::load_missing_tree(target, &mut typed, rest).await?;
Ok(typed.into_iter().map(AnyRecord::new).collect())
})
}
fn exists_query(&self, request: MorphExists<'_>) -> Result<QueryIr> {
let meta = M::meta();
let owner_key = request.owner_key.unwrap_or(meta.key_column()).to_owned();
let aliased = (meta.table() == request.parent.table()).then_some(meta.table());
let definition = request.trashed.apply(Builder::<M>::new());
let refine = match request.condition {
Some(filter) => Refine::Filter(filter),
None => Refine::None,
};
let outer = qualified(request.parent.table(), request.id_column);
build_subquery::<M>(
request.parent,
&definition,
ExistsRequest {
depth: request.depth,
select: request.select,
refine,
},
"morph_to",
aliased,
|query, _| query,
|alias| {
vec![columns_equal(
outer,
qualified(alias.unwrap_or(meta.table()), &owner_key),
)]
},
)
}
}
pub trait MorphTargets {
#[doc(hidden)]
fn morph_targets() -> Vec<Arc<dyn MorphTarget>>;
}
macro_rules! morph_targets {
($($model:ident),+) => {
impl<$($model: Model),+> MorphTargets for ($($model,)+) {
fn morph_targets() -> Vec<Arc<dyn MorphTarget>> {
vec![$(Arc::new(MorphTargetOf::<$model>::new()) as Arc<dyn MorphTarget>),+]
}
}
};
}
morph_targets!(A);
morph_targets!(A, B);
morph_targets!(A, B, C);
morph_targets!(A, B, C, D);
morph_targets!(A, B, C, D, E);
morph_targets!(A, B, C, D, E, F);
morph_targets!(A, B, C, D, E, F, G);
morph_targets!(A, B, C, D, E, F, G, H);
pub struct MorphTo<P: Model> {
pub(crate) name: Cow<'static, str>,
pub(crate) parent: Option<P>,
pub(crate) type_column: Cow<'static, str>,
pub(crate) id_column: Cow<'static, str>,
pub(crate) owner_key: Option<Cow<'static, str>>,
pub(crate) targets: Vec<Arc<dyn MorphTarget>>,
pub(crate) trashed: Trashed,
pub(crate) with: MorphWith,
}
impl<P: Model> Clone for MorphTo<P> {
fn clone(&self) -> Self {
MorphTo {
name: self.name.clone(),
parent: self.parent.clone(),
type_column: self.type_column.clone(),
id_column: self.id_column.clone(),
owner_key: self.owner_key.clone(),
targets: self.targets.clone(),
trashed: self.trashed,
with: self.with.clone(),
}
}
}
impl<P: Model> fmt::Debug for MorphTo<P> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MorphTo")
.field("parent", &P::meta().name())
.field("name", &self.name)
.field("type_column", &self.type_column)
.field("id_column", &self.id_column)
.finish_non_exhaustive()
}
}
impl<P: Model> MorphTo<P> {
pub(crate) fn new(parent: Option<P>, name: &str) -> Self {
MorphTo {
name: Cow::Owned(name.to_owned()),
parent,
type_column: Cow::Owned(format!("{name}_type")),
id_column: Cow::Owned(format!("{name}_id")),
owner_key: None,
targets: Vec::new(),
trashed: Trashed::Without,
with: MorphWith::new(),
}
}
pub fn named(mut self, name: impl Into<Cow<'static, str>>) -> Self {
self.name = name.into();
self
}
pub fn type_column(mut self, column: impl Into<Cow<'static, str>>) -> Self {
self.type_column = column.into();
self
}
pub fn id_column(mut self, column: impl Into<Cow<'static, str>>) -> Self {
self.id_column = column.into();
self
}
pub fn owner_key(mut self, column: impl Into<Cow<'static, str>>) -> Self {
self.owner_key = Some(column.into());
self
}
pub fn target<M: Model>(mut self) -> Self {
self.targets
.retain(|target| target.type_id() != TypeId::of::<M>());
self.targets.push(Arc::new(MorphTargetOf::<M>::new()));
self
}
pub fn targets<T: MorphTargets>(mut self) -> Self {
for target in T::morph_targets() {
self.targets
.retain(|existing| existing.type_id() != target.type_id());
self.targets.push(target);
}
self
}
pub fn with_trashed(mut self) -> Self {
self.trashed = Trashed::With;
self
}
pub fn without_trashed(mut self) -> Self {
self.trashed = Trashed::Without;
self
}
pub fn only_trashed(mut self) -> Self {
self.trashed = Trashed::Only;
self
}
pub fn morph_with(mut self, with: MorphWith) -> Self {
self.with.merge(&with);
self
}
pub fn constrain<M: Model>(
mut self,
constraint: impl Fn(Builder<M>) -> Builder<M> + Send + Sync + 'static,
) -> Self {
self.with = std::mem::take(&mut self.with).constrain(constraint);
self
}
pub fn relation_name(&self) -> &str {
&self.name
}
pub fn morph_type(&self) -> &str {
&self.type_column
}
pub fn foreign_key_name(&self) -> &str {
&self.id_column
}
pub fn qualified_foreign_key_name(&self) -> String {
format!("{}.{}", P::meta().table(), self.id_column)
}
pub fn owner_key_name(&self) -> Option<&str> {
self.owner_key.as_deref()
}
pub(crate) fn resolve(&self, name: &str) -> Result<Arc<dyn MorphTarget>> {
if let Some(target) = self
.targets
.iter()
.find(|target| target.morph_name() == name)
{
return Ok(Arc::clone(target));
}
match MorphMap::current().entry(name) {
Some(entry) => Ok(Arc::clone(&entry.target)),
None => Err(OrmError::UnknownMorph {
name: name.to_owned(),
}),
}
}
pub(crate) fn all_targets(&self) -> Vec<Arc<dyn MorphTarget>> {
if !self.targets.is_empty() {
return self.targets.clone();
}
MorphMap::current()
.entries()
.iter()
.map(|entry| Arc::clone(&entry.target))
.collect()
}
pub async fn get<E: Executor + ?Sized>(self, exec: &E) -> Result<Option<AnyRecord>> {
let parent = match &self.parent {
Some(parent) => parent.clone(),
None => {
return Err(OrmError::invalid(format!(
"the relation `{}` of {} was not made from a record",
self.name,
P::meta().name()
)));
}
};
let target = exec.target(Access::Read).await?;
let mut records = [Record::stored(parent)];
let node = EagerNode {
name: self.name.to_string(),
..EagerNode::default()
};
self.load_into(&target, &mut records, &node).await?;
let [record] = records;
Ok(record
.relations
.get::<Option<AnyRecord>>(&self.name)
.cloned()
.flatten())
}
pub fn is<M: Model>(&self, record: &Record<M>) -> bool {
let Some(parent) = &self.parent else {
return false;
};
let (Ok(stored_type), Ok(stored_id)) = (
column_value(parent, &self.type_column, &self.name),
column_value(parent, &self.id_column, &self.name),
) else {
return false;
};
let owner = match &self.owner_key {
Some(column) => match record.model.value(column) {
Ok(Some(value)) => value,
_ => return false,
},
None => crate::Field::to_value(&record.key()),
};
let type_matches = match &stored_type {
Value::String(stored) => *stored == M::morph_name(),
_ => false,
};
type_matches
&& key_of(&stored_id).is_some()
&& key_of(&stored_id) == key_of(&owner)
&& M::meta().connection_name() == P::meta().connection_name()
}
pub fn is_not<M: Model>(&self, record: &Record<M>) -> bool {
!self.is(record)
}
async fn load_into(
&self,
target: &Target<'_>,
parents: &mut [Record<P>],
node: &EagerNode,
) -> Result<()> {
let mut relation = self.clone();
let mut conditions: Vec<Filter> = Vec::new();
let mut access: Vec<AccessConstraint> = Vec::new();
for constraint in &node.constraints {
if let Some(filter) = constraint.filter() {
conditions.push(filter.clone());
continue;
}
if let Some(constraint) = constraint.access() {
access.push(constraint.clone());
continue;
}
match constraint
.closure
.downcast_ref::<ConstraintFn<MorphTo<P>>>()
{
Some(apply) => relation = (apply.0)(relation),
None => {
return Err(OrmError::invalid(format!(
"the constraint on morph_to relation `{}` of {} takes `{}`: constrain a MorphTo<{}>, or each model with MorphWith::constrain",
node.name,
P::meta().name(),
constraint.takes,
P::meta().name()
)));
}
}
}
let mut with = relation.with.clone();
if let Some(per_type) = &node.morph {
with.merge(per_type);
}
let mut groups: Vec<(String, Vec<(usize, Value)>)> = Vec::new();
for (index, parent) in parents.iter().enumerate() {
let stored_type = column_value(&parent.model, &relation.type_column, &relation.name)?;
let id = column_value(&parent.model, &relation.id_column, &relation.name)?;
let name = match stored_type {
Value::String(name) if !name.is_empty() => name,
_ => continue,
};
if is_null(&id) {
continue;
}
match groups.iter_mut().find(|(existing, _)| *existing == name) {
Some((_, members)) => members.push((index, id)),
None => groups.push((name, vec![(index, id)])),
}
}
let mut found: Vec<Option<AnyRecord>> = vec![None; parents.len()];
for (name, members) in groups {
let morph_target = relation.resolve(&name)?;
let keys = distinct_keys(members.iter().map(|(_, id)| id.clone()));
let records = morph_target
.load(
target,
MorphLoad {
owner_key: relation.owner_key.as_deref(),
keys: &keys,
loads: with.for_type(morph_target.type_id()),
children: &node.children,
conditions: &conditions,
access: &access,
trashed: relation.trashed,
},
)
.await?;
for (index, id) in members {
let key = key_of(&morph_target.normalize_key(id, relation.owner_key.as_deref()));
if let Some((_, record)) = records
.iter()
.find(|(found_key, _)| Some(found_key) == key.as_ref())
{
found[index] = Some(record.clone());
}
}
}
for (parent, value) in parents.iter_mut().zip(found) {
parent.relations.set_relation(node.name.clone(), value);
}
Ok(())
}
pub(crate) fn has_morph_where(
&self,
depth: u32,
types: Option<&[String]>,
op: Operator,
count: u64,
refine: &mut dyn FnMut(&str) -> Option<Filter>,
) -> Result<Where> {
let targets = match types {
Some(names) => names
.iter()
.map(|name| self.resolve(name))
.collect::<Result<Vec<_>>>()?,
None => self.all_targets(),
};
let count_i = i64::try_from(count).unwrap_or(i64::MAX);
let check_null = types.is_none()
&& match op {
Operator::Lt => count >= 1,
Operator::Lte => true,
Operator::Eq => count == 0,
Operator::Ne => count >= 1,
_ => false,
};
if targets.is_empty() {
return Ok(Where::Compare {
left: Expr::Value(Value::I64(0)),
op,
right: Expr::Value(Value::I64(count_i)),
});
}
let exists = count == 1 && matches!(op, Operator::Gte | Operator::Lt);
let parent = P::meta();
let mut alternatives = Vec::with_capacity(targets.len() + 1);
for target in targets {
let name = target.morph_name();
let query = target.exists_query(MorphExists {
parent,
id_column: &self.id_column,
owner_key: self.owner_key.as_deref(),
depth,
select: if exists {
ExistsSelect::One
} else {
ExistsSelect::Count
},
condition: refine(&name),
trashed: self.trashed,
})?;
let check = if exists {
Where::Exists {
query: Box::new(query),
not: op == Operator::Lt,
}
} else {
Where::Compare {
left: Expr::Subquery(Box::new(query)),
op: op.clone(),
right: Expr::Value(Value::I64(count_i)),
}
};
alternatives.push(WhereClause::or(Where::Group(vec![
WhereClause::and(Where::Compare {
left: Expr::Column(qualified(parent.table(), &self.type_column)),
op: Operator::Eq,
right: Expr::Value(Value::from(name.into_owned())),
}),
WhereClause::and(check),
])));
}
if check_null {
alternatives.push(WhereClause::or(Where::Null {
expr: Expr::Column(qualified(parent.table(), &self.type_column)),
not: false,
}));
}
Ok(Where::Group(alternatives))
}
}
pub trait MorphToOps<P: Model>: Send + Sync {
fn has_morph(
&self,
depth: u32,
types: Option<&[String]>,
op: Operator,
count: u64,
refine: &mut dyn FnMut(&str) -> Option<Filter>,
) -> Result<Where>;
#[doc(hidden)]
fn target_for(&self, name: &str) -> Result<Arc<dyn MorphTarget>>;
}
impl<P: Model> MorphToOps<P> for MorphTo<P> {
fn has_morph(
&self,
depth: u32,
types: Option<&[String]>,
op: Operator,
count: u64,
refine: &mut dyn FnMut(&str) -> Option<Filter>,
) -> Result<Where> {
self.has_morph_where(depth, types, op, count, refine)
}
fn target_for(&self, name: &str) -> Result<Arc<dyn MorphTarget>> {
self.resolve(name)
}
}
impl<P: Model> private::Erase<P> for MorphTo<P> {
fn erase(self) -> Arc<dyn ErasedRelation<P>> {
let mut unbound = self;
unbound.parent = None;
Arc::new(unbound)
}
fn rename(&mut self, name: Cow<'static, str>) {
self.name = name;
}
}
impl<P: Model> Relation<P> for MorphTo<P> {
type Value = Option<AnyRecord>;
fn name(&self) -> &str {
&self.name
}
fn kind(&self) -> RelationKind {
RelationKind::MorphTo
}
fn meta(&self) -> RelationMeta {
let mut meta = RelationMeta::new::<P>(&self.name, RelationKind::MorphTo);
meta.foreign_key = Some(self.id_column.to_string());
meta.owner_key = self.owner_key.as_ref().map(ToString::to_string);
meta.morph_type = Some(self.type_column.to_string());
meta
}
}
impl<P: Model> ErasedRelation<P> for MorphTo<P> {
fn name(&self) -> &str {
&self.name
}
fn meta(&self) -> RelationMeta {
Relation::meta(self)
}
fn related_type(&self) -> Option<TypeId> {
None
}
fn eager_load<'a, 'b>(
&'a self,
target: &'a Target<'b>,
parents: &'a mut [Record<P>],
node: &'a EagerNode,
) -> BoxFuture<'a, Result<()>> {
Box::pin(async move { self.load_into(target, parents, node).await })
}
fn exists_query(&self, _request: ExistsRequest) -> Result<QueryIr> {
Err(OrmError::invalid(format!(
"`{}` of {} is a morph_to relation whose model varies: count or aggregate each type through its own relations",
self.name,
P::meta().name()
)))
}
fn touch<'a, 'b>(
&'a self,
target: &'a Target<'b>,
parent: &'a Record<P>,
) -> BoxFuture<'a, Result<()>> {
Box::pin(async move {
let stored_type = column_value(&parent.model, &self.type_column, &self.name)?;
let id = column_value(&parent.model, &self.id_column, &self.name)?;
let (Value::String(name), false) = (&stored_type, is_null(&id)) else {
return Ok(());
};
let morph_target = self.resolve(name)?;
let keys = distinct_keys([id]);
let records = morph_target
.load(
target,
MorphLoad {
owner_key: self.owner_key.as_deref(),
keys: &keys,
loads: None,
children: &EagerTree::default(),
conditions: &[],
access: &[],
trashed: Trashed::With,
},
)
.await?;
for (_, mut record) in records {
crate::relations::touch::touch_any(target, &mut record).await?;
}
Ok(())
})
}
fn load_nested_missing<'a, 'b>(
&'a self,
target: &'a Target<'b>,
parents: &'a mut [Record<P>],
rest: &'a EagerTree,
) -> BoxFuture<'a, Result<()>> {
Box::pin(async move {
let mut taken: Vec<Option<AnyRecord>> = parents
.iter_mut()
.map(|parent| {
parent
.relations
.take::<Option<AnyRecord>>(&self.name)
.flatten()
})
.collect();
let mut model_types: Vec<TypeId> = Vec::new();
let mut targets: Vec<Arc<dyn MorphTarget>> = Vec::new();
for record in taken.iter().flatten() {
let name = record.morph_name();
let morph_target = self.resolve(&name)?;
if !model_types.contains(&morph_target.type_id()) {
model_types.push(morph_target.type_id());
targets.push(morph_target);
}
}
for morph_target in targets {
let indexes: Vec<usize> = taken
.iter()
.enumerate()
.filter(|(_, record)| {
record
.as_ref()
.is_some_and(|record| record.morph_name() == morph_target.morph_name())
})
.map(|(index, _)| index)
.collect();
let records: Vec<AnyRecord> = indexes
.iter()
.filter_map(|index| taken[*index].take())
.collect();
let loaded = crate::relations::lazy::load_missing_any(
target,
morph_target.as_ref(),
records,
rest,
)
.await?;
for (index, record) in indexes.into_iter().zip(loaded) {
taken[index] = Some(record);
}
}
for (parent, record) in parents.iter_mut().zip(taken) {
parent.relations.set_relation(self.name.clone(), record);
}
Ok(())
})
}
fn morph_to(&self) -> Option<&dyn MorphToOps<P>> {
Some(self)
}
}
#[derive(Clone, Debug, Default)]
pub struct MorphTypes {
names: Option<Vec<String>>,
}
impl MorphTypes {
pub fn all() -> Self {
MorphTypes { names: None }
}
pub fn named(names: impl IntoIterator<Item = impl Into<String>>) -> Self {
MorphTypes {
names: Some(names.into_iter().map(Into::into).collect()),
}
}
pub fn of<M: Model>() -> Self {
MorphTypes::named([M::morph_name().into_owned()])
}
pub fn and<M: Model>(mut self) -> Self {
self.names
.get_or_insert_with(Vec::new)
.push(M::morph_name().into_owned());
self
}
}
impl From<&[&str]> for MorphTypes {
fn from(names: &[&str]) -> Self {
MorphTypes::named(names.iter().copied())
}
}
impl<const N: usize> From<[&str; N]> for MorphTypes {
fn from(names: [&str; N]) -> Self {
MorphTypes::named(names)
}
}
impl From<Vec<String>> for MorphTypes {
fn from(names: Vec<String>) -> Self {
MorphTypes { names: Some(names) }
}
}
#[derive(Clone, Debug)]
pub struct MorphRef {
kind: MorphRefKind,
}
#[derive(Clone, Debug)]
enum MorphRefKind {
Records(Vec<AnyRecord>),
Type(String),
Null,
}
impl MorphRef {
pub fn type_of<M: Model>() -> Self {
MorphRef {
kind: MorphRefKind::Type(M::morph_name().into_owned()),
}
}
pub fn named(name: impl Into<String>) -> Self {
MorphRef {
kind: MorphRefKind::Type(name.into()),
}
}
pub fn null() -> Self {
MorphRef {
kind: MorphRefKind::Null,
}
}
}
impl<M: Model> From<&Record<M>> for MorphRef {
fn from(record: &Record<M>) -> Self {
MorphRef {
kind: MorphRefKind::Records(vec![AnyRecord::new(record.without_relations())]),
}
}
}
impl<M: Model> From<&Collection<M>> for MorphRef {
fn from(records: &Collection<M>) -> Self {
MorphRef {
kind: MorphRefKind::Records(
records
.iter()
.map(|record| AnyRecord::new(record.without_relations()))
.collect(),
),
}
}
}
impl From<&AnyRecord> for MorphRef {
fn from(record: &AnyRecord) -> Self {
MorphRef {
kind: MorphRefKind::Records(vec![record.clone()]),
}
}
}
impl<M: Model> Builder<M> {
#[allow(clippy::too_many_arguments)]
fn push_has_morph(
mut self,
boolean: Boolean,
path: &str,
types: MorphTypes,
op: Operator,
count: u64,
refine: &mut dyn FnMut(&str) -> Option<Filter>,
) -> Self {
let relation = match find::<M>(path) {
Ok(relation) => relation,
Err(error) => return self.with_error(error.to_string()),
};
let Some(morph) = relation.inner.morph_to() else {
return self.with_error(format!(
"`{path}` of {} is a {}, not a morph_to relation",
M::meta().name(),
relation.kind()
));
};
match morph.has_morph(self.eager.depth, types.names.as_deref(), op, count, refine) {
Ok(filter) => {
self.push_where(boolean, false, filter);
self
}
Err(error) => self.with_error(error.to_string()),
}
}
pub fn has_morph(self, path: impl RelationPath<M>, types: impl Into<MorphTypes>) -> Self {
let text = path.relation_path().into_owned();
self.push_has_morph(
Boolean::And,
&text,
types.into(),
Operator::Gte,
1,
&mut |_| None,
)
}
pub fn or_has_morph(self, path: impl RelationPath<M>, types: impl Into<MorphTypes>) -> Self {
let text = path.relation_path().into_owned();
self.push_has_morph(
Boolean::Or,
&text,
types.into(),
Operator::Gte,
1,
&mut |_| None,
)
}
pub fn doesnt_have_morph(
self,
path: impl RelationPath<M>,
types: impl Into<MorphTypes>,
) -> Self {
let text = path.relation_path().into_owned();
self.push_has_morph(
Boolean::And,
&text,
types.into(),
Operator::Lt,
1,
&mut |_| None,
)
}
pub fn or_doesnt_have_morph(
self,
path: impl RelationPath<M>,
types: impl Into<MorphTypes>,
) -> Self {
let text = path.relation_path().into_owned();
self.push_has_morph(
Boolean::Or,
&text,
types.into(),
Operator::Lt,
1,
&mut |_| None,
)
}
pub fn where_has_morph(
self,
path: impl RelationPath<M>,
types: impl Into<MorphTypes>,
mut constraint: impl FnMut(WhereGroup, &str) -> WhereGroup,
) -> Self {
let text = path.relation_path().into_owned();
self.push_has_morph(
Boolean::And,
&text,
types.into(),
Operator::Gte,
1,
&mut |ty| group_filter(constraint(WhereGroup::new(), ty)),
)
}
pub fn or_where_has_morph(
self,
path: impl RelationPath<M>,
types: impl Into<MorphTypes>,
mut constraint: impl FnMut(WhereGroup, &str) -> WhereGroup,
) -> Self {
let text = path.relation_path().into_owned();
self.push_has_morph(
Boolean::Or,
&text,
types.into(),
Operator::Gte,
1,
&mut |ty| group_filter(constraint(WhereGroup::new(), ty)),
)
}
pub fn where_doesnt_have_morph(
self,
path: impl RelationPath<M>,
types: impl Into<MorphTypes>,
mut constraint: impl FnMut(WhereGroup, &str) -> WhereGroup,
) -> Self {
let text = path.relation_path().into_owned();
self.push_has_morph(
Boolean::And,
&text,
types.into(),
Operator::Lt,
1,
&mut |ty| group_filter(constraint(WhereGroup::new(), ty)),
)
}
pub fn or_where_doesnt_have_morph(
self,
path: impl RelationPath<M>,
types: impl Into<MorphTypes>,
mut constraint: impl FnMut(WhereGroup, &str) -> WhereGroup,
) -> Self {
let text = path.relation_path().into_owned();
self.push_has_morph(
Boolean::Or,
&text,
types.into(),
Operator::Lt,
1,
&mut |ty| group_filter(constraint(WhereGroup::new(), ty)),
)
}
pub fn where_morph_relation(
self,
path: impl RelationPath<M>,
types: impl Into<MorphTypes>,
filter: impl Into<Filter>,
) -> Self {
let text = path.relation_path().into_owned();
let filter = filter.into();
self.push_has_morph(
Boolean::And,
&text,
types.into(),
Operator::Gte,
1,
&mut |_| Some(filter.clone()),
)
}
pub fn or_where_morph_relation(
self,
path: impl RelationPath<M>,
types: impl Into<MorphTypes>,
filter: impl Into<Filter>,
) -> Self {
let text = path.relation_path().into_owned();
let filter = filter.into();
self.push_has_morph(
Boolean::Or,
&text,
types.into(),
Operator::Gte,
1,
&mut |_| Some(filter.clone()),
)
}
pub fn where_morph_doesnt_have_relation(
self,
path: impl RelationPath<M>,
types: impl Into<MorphTypes>,
filter: impl Into<Filter>,
) -> Self {
let text = path.relation_path().into_owned();
let filter = filter.into();
self.push_has_morph(
Boolean::And,
&text,
types.into(),
Operator::Lt,
1,
&mut |_| Some(filter.clone()),
)
}
pub fn or_where_morph_doesnt_have_relation(
self,
path: impl RelationPath<M>,
types: impl Into<MorphTypes>,
filter: impl Into<Filter>,
) -> Self {
let text = path.relation_path().into_owned();
let filter = filter.into();
self.push_has_morph(
Boolean::Or,
&text,
types.into(),
Operator::Lt,
1,
&mut |_| Some(filter.clone()),
)
}
pub fn where_morphed_to(self, path: impl RelationPath<M>, target: impl Into<MorphRef>) -> Self {
let text = path.relation_path().into_owned();
self.push_morphed_to(Boolean::And, &text, target.into(), false)
}
pub fn where_not_morphed_to(
self,
path: impl RelationPath<M>,
target: impl Into<MorphRef>,
) -> Self {
let text = path.relation_path().into_owned();
self.push_morphed_to(Boolean::And, &text, target.into(), true)
}
pub fn or_where_morphed_to(
self,
path: impl RelationPath<M>,
target: impl Into<MorphRef>,
) -> Self {
let text = path.relation_path().into_owned();
self.push_morphed_to(Boolean::Or, &text, target.into(), false)
}
pub fn or_where_not_morphed_to(
self,
path: impl RelationPath<M>,
target: impl Into<MorphRef>,
) -> Self {
let text = path.relation_path().into_owned();
self.push_morphed_to(Boolean::Or, &text, target.into(), true)
}
fn push_morphed_to(
mut self,
boolean: Boolean,
path: &str,
target: MorphRef,
not: bool,
) -> Self {
match morphed_to_filter::<M>(path, target, not) {
Ok(filter) => {
self.push_where(boolean, false, filter);
self
}
Err(error) => self.with_error(error.to_string()),
}
}
pub fn with_morph(mut self, path: impl RelationPath<M>, with: MorphWith) -> Self {
let text = path.relation_path().into_owned();
match ParsedPath::parse(&text) {
Ok(parsed) => {
let node = self.eager.tree.entry(&parsed.segments);
match &mut node.morph {
Some(existing) => existing.merge(&with),
None => node.morph = Some(with),
}
self
}
Err(error) => self.with_error(error.to_string()),
}
}
}
fn group_filter(group: WhereGroup) -> Option<Filter> {
let clauses = group.into_clauses();
(!clauses.is_empty()).then(|| Filter::from(Where::Group(clauses)))
}
fn morphed_to_filter<M: Model>(path: &str, target: MorphRef, not: bool) -> Result<Where> {
let relation = find::<M>(path)?;
let meta = relation.meta();
if meta.kind != RelationKind::MorphTo {
return Err(OrmError::invalid(format!(
"`{path}` of {} is a {}, not a morph_to relation",
M::meta().name(),
meta.kind
)));
}
let table = M::meta().table();
let (Some(type_column), Some(id_column)) = (meta.morph_type, meta.foreign_key) else {
return Err(OrmError::invalid(format!(
"the morph_to relation `{path}` of {} has no columns",
M::meta().name()
)));
};
let type_op = if not {
Operator::NullSafeEq
} else {
Operator::Eq
};
let condition = match target.kind {
MorphRefKind::Null => Where::Null {
expr: Expr::Column(qualified(table, &type_column)),
not,
},
MorphRefKind::Type(name) => {
let check = Where::Compare {
left: Expr::Column(qualified(table, &type_column)),
op: type_op,
right: Expr::Value(Value::from(name)),
};
if not {
Where::Group(vec![WhereClause {
boolean: Boolean::And,
negated: true,
filter: check,
}])
} else {
check
}
}
MorphRefKind::Records(records) => {
if records.is_empty() {
return Err(OrmError::invalid(format!(
"where_morphed_to(`{path}`) was given no records"
)));
}
let mut by_type: Vec<(String, Vec<Value>)> = Vec::new();
for record in &records {
let name = record.morph_name().into_owned();
let key = match &meta.owner_key {
Some(column) => record.value(column)?.ok_or_else(|| {
OrmError::invalid(format!(
"{} has no column `{column}`, which `{path}` points at",
record.model_name()
))
})?,
None => record.key(),
};
match by_type.iter_mut().find(|(existing, _)| *existing == name) {
Some((_, keys)) => keys.push(key),
None => by_type.push((name, vec![key])),
}
}
let alternatives = by_type
.into_iter()
.map(|(name, keys)| {
let keys = distinct_keys(keys);
WhereClause::or(Where::Group(vec![
WhereClause::and(Where::Compare {
left: Expr::Column(qualified(table, &type_column)),
op: type_op.clone(),
right: Expr::Value(Value::from(name)),
}),
WhereClause::and(in_keys(qualified(table, &id_column), &keys)),
]))
})
.collect();
let any = Where::Group(alternatives);
if not {
Where::Group(vec![WhereClause {
boolean: Boolean::And,
negated: true,
filter: any,
}])
} else {
any
}
}
};
Ok(condition)
}
pub(crate) fn stored_type_of<M: Model>() -> Result<Value> {
Ok(Value::from(type_name_of::<M>()?.into_owned()))
}