use std::marker::PhantomData;
use crate::BoxFuture;
use crate::col::Column;
use crate::compile::{delete, insert_many, select};
use crate::error::Error;
use crate::executor::{Executor, decode_rows};
use crate::filter::{Filter, FilterNode};
use crate::model::Model;
use crate::value::Value;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum M2mAction {
Attach,
Set,
Detach,
}
pub struct M2mWrite<'db, M, C, J> {
action: M2mAction,
get_parent_pk: fn(&M) -> Value,
join_table: &'static str,
join_owner_col: &'static str,
join_target_col: &'static str,
target_table: &'static str,
target_pk_col: &'static str,
target_ids: Vec<Value>,
setter: fn(&mut M, Vec<C>),
_marker: PhantomData<fn() -> (&'db (), J)>,
}
impl<'db, M, C, J> M2mWrite<'db, M, C, J> {
#[allow(clippy::too_many_arguments)]
pub const fn new(
action: M2mAction,
get_parent_pk: fn(&M) -> Value,
join_table: &'static str,
join_owner_col: &'static str,
join_target_col: &'static str,
target_table: &'static str,
target_pk_col: &'static str,
target_ids: Vec<Value>,
setter: fn(&mut M, Vec<C>),
) -> Self {
Self {
action,
get_parent_pk,
join_table,
join_owner_col,
join_target_col,
target_table,
target_pk_col,
target_ids,
setter,
_marker: PhantomData,
}
}
}
impl<'db, M, C, J> M2mWrite<'db, M, C, J>
where
M: Model,
C: Model + Unpin,
J: Model,
{
pub(crate) async fn execute_insert<'a>(
&'a self,
exec: &'a dyn Executor,
parent: &'a mut M,
) -> Result<(), Error> {
let pk = (self.get_parent_pk)(parent);
let mut affected = 0u64;
if self.action == M2mAction::Set {
affected += self.clear_joins(exec, &pk).await?;
}
if self.action != M2mAction::Detach {
affected += self.insert_joins(exec, &pk).await?;
} else {
affected += self.delete_specific_joins(exec, &pk).await?;
}
let children = if self.action == M2mAction::Detach {
Vec::new()
} else if affected > 0 && !self.target_ids.is_empty() {
self.load_targets(exec).await?
} else {
Vec::new()
};
(self.setter)(parent, children);
Ok(())
}
pub(crate) async fn execute_update<'a>(
&'a self,
exec: &'a dyn Executor,
parent_pk: Value,
) -> Result<u64, Error> {
let mut affected = 0u64;
if self.action == M2mAction::Set {
affected += self.clear_joins(exec, &parent_pk).await?;
}
if self.action != M2mAction::Detach {
affected += self.insert_joins(exec, &parent_pk).await?;
} else {
affected += self.delete_specific_joins(exec, &parent_pk).await?;
}
Ok(affected)
}
pub(crate) fn parent_pk(&self, parent: &M) -> Value {
(self.get_parent_pk)(parent)
}
fn owner_col(&self) -> Column<J, Value> {
Column::new(self.join_table, self.join_owner_col)
}
fn target_col(&self) -> Column<J, Value> {
Column::new(self.join_table, self.join_target_col)
}
fn target_pk(&self) -> Column<C, Value> {
Column::new(self.target_table, self.target_pk_col)
}
async fn clear_joins(&self, exec: &dyn Executor, pk: &Value) -> Result<u64, Error> {
let filter =
Filter::<J>::new(FilterNode::And(Vec::new())).and(self.owner_col().eq(pk.clone()));
let compiled = delete::<J>(exec.dialect(), self.join_table, &filter.node, &[]);
exec.execute_raw(compiled.sql, compiled.binds).await
}
async fn delete_specific_joins(&self, exec: &dyn Executor, pk: &Value) -> Result<u64, Error> {
let filter = Filter::<J>::new(FilterNode::And(Vec::new()))
.and(self.owner_col().eq(pk.clone()))
.and(self.target_col().in_set(self.target_ids.clone()));
let compiled = delete::<J>(exec.dialect(), self.join_table, &filter.node, &[]);
exec.execute_raw(compiled.sql, compiled.binds).await
}
async fn insert_joins(&self, exec: &dyn Executor, pk: &Value) -> Result<u64, Error> {
if self.target_ids.is_empty() {
return Ok(0);
}
let rows: Vec<Vec<(&'static str, Value)>> = self
.target_ids
.iter()
.map(|tid| {
vec![
(self.join_owner_col, pk.clone()),
(self.join_target_col, tid.clone()),
]
})
.collect();
let compiled = insert_many::<J>(exec.dialect(), self.join_table, &rows, &[]);
exec.execute_raw(compiled.sql, compiled.binds).await
}
async fn load_targets(&self, exec: &dyn Executor) -> Result<Vec<C>, Error> {
let live = crate::model::live_rows_node::<C>(self.target_table)
.unwrap_or_else(|| FilterNode::And(Vec::new()));
let filter = Filter::<C>::new(live).and(self.target_pk().in_set(self.target_ids.clone()));
let compiled = select::<C>(
exec.dialect(),
self.target_table,
C::COLUMNS,
&filter.node,
&[],
None,
None,
false,
);
let batch = exec.fetch_all_raw(compiled.sql, compiled.binds).await?;
decode_rows::<C>(batch)
}
}
pub(crate) trait AnyM2mWrite<M: Model>: Send + Sync + std::fmt::Debug {
fn parent_pk(&self, parent: &M) -> Value;
fn execute_insert<'a>(
&'a self,
exec: &'a dyn Executor,
parent: &'a mut M,
) -> BoxFuture<'a, Result<(), Error>>;
fn execute_update<'a>(
&'a self,
exec: &'a dyn Executor,
parent_pk: Value,
) -> BoxFuture<'a, Result<u64, Error>>;
}
impl<M, C, J> std::fmt::Debug for M2mWrite<'_, M, C, J> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("M2mWrite")
.field("action", &self.action)
.field("join_table", &self.join_table)
.field("join_owner_col", &self.join_owner_col)
.field("join_target_col", &self.join_target_col)
.field("target_table", &self.target_table)
.field("target_pk_col", &self.target_pk_col)
.field("target_ids", &self.target_ids)
.finish_non_exhaustive()
}
}
impl<M, C, J> AnyM2mWrite<M> for M2mWrite<'_, M, C, J>
where
M: Model,
C: Model + Unpin,
J: Model,
{
fn parent_pk(&self, parent: &M) -> Value {
self.parent_pk(parent)
}
fn execute_insert<'a>(
&'a self,
exec: &'a dyn Executor,
parent: &'a mut M,
) -> BoxFuture<'a, Result<(), Error>> {
Box::pin(self.execute_insert(exec, parent))
}
fn execute_update<'a>(
&'a self,
exec: &'a dyn Executor,
parent_pk: Value,
) -> BoxFuture<'a, Result<u64, Error>> {
Box::pin(self.execute_update(exec, parent_pk))
}
}