use std::fmt;
use std::marker::PhantomData;
use serde::{Deserialize, Serialize};
use crate::collection::Collection;
use crate::error::{OrmError, Result};
use crate::exec::{Access, Executor};
use crate::model::{Model, PrimaryKey};
use crate::query::Builder;
use crate::record::Record;
use crate::value::{Field, to_json};
#[derive(Serialize, Deserialize)]
#[serde(bound = "")]
pub struct ModelRef<M> {
model: String,
key: serde_json::Value,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
relations: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
connection: Option<String>,
#[serde(skip)]
_model: PhantomData<fn() -> M>,
}
#[derive(Serialize, Deserialize)]
#[serde(bound = "")]
pub struct ModelRefs<M> {
model: String,
keys: Vec<serde_json::Value>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
relations: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
connection: Option<String>,
#[serde(skip)]
_model: PhantomData<fn() -> M>,
}
fn check_model<M: Model>(model: &str) -> Result<()> {
if model == M::meta().name() {
Ok(())
} else {
Err(OrmError::InvalidQuery(format!(
"this reference names model {model}, not {}",
M::meta().name()
)))
}
}
fn key_from<M: Model>(json: &serde_json::Value) -> Result<M::Key> {
M::Key::from_json(json.clone()).map_err(|error| OrmError::Decode {
model: M::meta().name().into(),
column: M::meta().key_column().to_owned(),
expected: M::Key::type_name(),
message: format!("the reference's key {json}: {error}"),
})
}
impl<M: Model> ModelRef<M> {
pub fn of(record: &Record<M>) -> Result<Self> {
if !record.exists() {
return Err(Record::<M>::not_persisted("refer to"));
}
Ok(ModelRef {
model: M::meta().name().to_owned(),
key: to_json(&record.key().to_value()),
relations: record.relations().names().map(str::to_owned).collect(),
connection: M::meta().connection_name().map(str::to_owned),
_model: PhantomData,
})
}
pub fn model_name(&self) -> &str {
&self.model
}
pub fn key(&self) -> Result<M::Key> {
check_model::<M>(&self.model)?;
key_from::<M>(&self.key)
}
pub fn relations(&self) -> &[String] {
&self.relations
}
pub fn connection(&self) -> Option<&str> {
self.connection.as_deref()
}
pub async fn restore<E: Executor + ?Sized>(&self, exec: &E) -> Result<Record<M>> {
let key = self.key()?;
self.restore_or_skip(exec)
.await?
.ok_or_else(|| OrmError::not_found(M::meta().name(), vec![key.to_key_string()]))
}
pub async fn restore_or_skip<E: Executor + ?Sized>(
&self,
exec: &E,
) -> Result<Option<Record<M>>> {
let key = self.key()?;
let target = exec.target(Access::Write).await?;
Ok(Builder::<M>::new()
.without_global_scopes()
.where_key(key)
.limit(1)
.fetch(&target)
.await?
.into_vec()
.into_iter()
.next())
}
}
impl<M: Model> ModelRefs<M> {
pub fn of(records: &Collection<M>) -> Result<Self> {
let mut keys = Vec::with_capacity(records.len());
let mut relations: Vec<String> = Vec::new();
for record in records.iter() {
if !record.exists() {
return Err(Record::<M>::not_persisted("refer to"));
}
keys.push(to_json(&record.key().to_value()));
for name in record.relations().names() {
if !relations.iter().any(|known| known == name) {
relations.push(name.to_owned());
}
}
}
Ok(ModelRefs {
model: M::meta().name().to_owned(),
keys,
relations,
connection: M::meta().connection_name().map(str::to_owned),
_model: PhantomData,
})
}
pub fn model_name(&self) -> &str {
&self.model
}
pub fn keys(&self) -> Result<Vec<M::Key>> {
check_model::<M>(&self.model)?;
self.keys.iter().map(key_from::<M>).collect()
}
pub fn len(&self) -> usize {
self.keys.len()
}
pub fn is_empty(&self) -> bool {
self.keys.is_empty()
}
pub fn relations(&self) -> &[String] {
&self.relations
}
pub fn connection(&self) -> Option<&str> {
self.connection.as_deref()
}
pub async fn restore<E: Executor + ?Sized>(&self, exec: &E) -> Result<Collection<M>> {
let keys = self.keys()?;
if keys.is_empty() {
return Ok(Collection::new());
}
let target = exec.target(Access::Write).await?;
let mut found = Builder::<M>::new()
.without_global_scopes()
.where_key_in(keys.clone())
.fetch(&target)
.await?
.into_vec();
let mut ordered = Vec::with_capacity(found.len());
for key in keys {
if let Some(index) = found.iter().position(|record| record.key() == key) {
ordered.push(found.swap_remove(index));
}
}
Ok(ordered.into())
}
}
impl<M: Model> Record<M> {
pub fn to_ref(&self) -> Result<ModelRef<M>> {
ModelRef::of(self)
}
}
impl<M: Model> Collection<M> {
pub fn to_refs(&self) -> Result<ModelRefs<M>> {
ModelRefs::of(self)
}
}
impl<M> Clone for ModelRef<M> {
fn clone(&self) -> Self {
ModelRef {
model: self.model.clone(),
key: self.key.clone(),
relations: self.relations.clone(),
connection: self.connection.clone(),
_model: PhantomData,
}
}
}
impl<M> Clone for ModelRefs<M> {
fn clone(&self) -> Self {
ModelRefs {
model: self.model.clone(),
keys: self.keys.clone(),
relations: self.relations.clone(),
connection: self.connection.clone(),
_model: PhantomData,
}
}
}
impl<M> PartialEq for ModelRef<M> {
fn eq(&self, other: &Self) -> bool {
self.model == other.model
&& self.key == other.key
&& self.relations == other.relations
&& self.connection == other.connection
}
}
impl<M> PartialEq for ModelRefs<M> {
fn eq(&self, other: &Self) -> bool {
self.model == other.model
&& self.keys == other.keys
&& self.relations == other.relations
&& self.connection == other.connection
}
}
impl<M> fmt::Debug for ModelRef<M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ModelRef")
.field("model", &self.model)
.field("key", &self.key)
.field("relations", &self.relations)
.field("connection", &self.connection)
.finish()
}
}
impl<M> fmt::Debug for ModelRefs<M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ModelRefs")
.field("model", &self.model)
.field("keys", &self.keys)
.field("relations", &self.relations)
.field("connection", &self.connection)
.finish()
}
}