use std::fmt;
use std::marker::PhantomData;
use futures_core::stream::BoxStream;
use sqlx::postgres::PgRow;
use crate::expr::Cond;
use crate::query::{JoinClause, join_scope};
use crate::relation::{BelongsTo, HasMany, HasOne};
use crate::{
Column, Executor, Expr, Model, Order, Page, Projected, Projection, Result, Select, Sql, Update,
};
#[doc(hidden)]
pub mod scope {
use std::marker::PhantomData;
#[derive(Debug)]
pub struct Here;
#[derive(Debug)]
pub struct Last;
#[derive(Debug)]
pub struct There<I>(PhantomData<I>);
}
use scope::{Here, Last, There};
#[diagnostic::on_unimplemented(
message = "`{Self}` is not part of this query",
label = "join `{Self}` first (`.join(…)` / `.left_join(…)`)"
)]
pub trait InScope<S, I> {}
impl<M> InScope<(M, ()), Here> for M {}
impl<M, Rest, N> InScope<(M, (Rest, N)), Last> for N {}
impl<X, M, Rest, N, I> InScope<(M, (Rest, N)), There<I>> for X where X: InScope<(M, Rest), I> {}
#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct JoinCondition(pub(crate) Cond);
pub trait IntoJoin {
type From: Model;
type To: Model;
#[doc(hidden)]
fn condition(&self) -> JoinCondition;
#[doc(hidden)]
fn multiplies(&self) -> bool;
}
fn eq_columns(
left: (&'static str, &'static str),
right: (&'static str, &'static str),
) -> JoinCondition {
JoinCondition(Cond::Columns {
left,
op: "=",
right,
})
}
impl<C: Model, P: Model> IntoJoin for BelongsTo<C, P> {
type From = C;
type To = P;
fn condition(&self) -> JoinCondition {
eq_columns(
(P::TABLE, P::PRIMARY_KEY),
(C::TABLE, self.foreign_key().name()),
)
}
fn multiplies(&self) -> bool {
false
}
}
impl<P: Model, C: Model> IntoJoin for HasMany<P, C> {
type From = P;
type To = C;
fn condition(&self) -> JoinCondition {
eq_columns(
(C::TABLE, self.foreign_key().name()),
(P::TABLE, P::PRIMARY_KEY),
)
}
fn multiplies(&self) -> bool {
true
}
}
impl<P: Model, C: Model> IntoJoin for HasOne<P, C> {
type From = P;
type To = C;
fn condition(&self) -> JoinCondition {
eq_columns(
(C::TABLE, self.foreign_key().name()),
(P::TABLE, P::PRIMARY_KEY),
)
}
fn multiplies(&self) -> bool {
false
}
}
pub struct JoinOn<From, To> {
to: &'static str,
from: &'static str,
_models: PhantomData<fn() -> (From, To)>,
}
impl<From: Model, To: Model> IntoJoin for JoinOn<From, To> {
type From = From;
type To = To;
fn condition(&self) -> JoinCondition {
eq_columns((To::TABLE, self.to), (From::TABLE, self.from))
}
fn multiplies(&self) -> bool {
true
}
}
impl<From, To> fmt::Debug for JoinOn<From, To> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("JoinOn")
.field("to", &self.to)
.field("from", &self.from)
.finish()
}
}
impl<To: Model> Column<To> {
pub fn on<From: Model>(self, other: Column<From>) -> JoinOn<From, To> {
JoinOn {
to: self.name(),
from: other.name(),
_models: PhantomData,
}
}
}
pub trait ScopedItem {
type Model;
#[doc(hidden)]
fn into_projection(self) -> Projection<Self::Model>;
}
impl<X: Model> ScopedItem for Column<X> {
type Model = X;
fn into_projection(self) -> Projection<X> {
self.into()
}
}
impl<X> ScopedItem for Projection<X> {
type Model = X;
fn into_projection(self) -> Projection<X> {
self
}
}
pub trait ScopedProjections<S, I> {
#[doc(hidden)]
fn into_items<R>(self) -> Vec<Projection<R>>;
}
impl<S, I, A> ScopedProjections<S, (I,)> for A
where
A: ScopedItem,
A::Model: InScope<S, I>,
{
fn into_items<R>(self) -> Vec<Projection<R>> {
vec![self.into_projection().retag()]
}
}
macro_rules! scoped_tuples {
($(($($t:ident $i:ident),+)),+ $(,)?) => {$(
#[allow(non_snake_case)]
impl<S, $($i,)+ $($t),+> ScopedProjections<S, ($($i,)+)> for ($($t,)+)
where
$($t: ScopedItem, $t::Model: InScope<S, $i>,)+
{
fn into_items<R>(self) -> Vec<Projection<R>> {
let ($($t,)+) = self;
vec![$($t.into_projection().retag()),+]
}
}
)+};
}
scoped_tuples! {
(A IA, B IB),
(A IA, B IB, C IC),
(A IA, B IB, C IC, D ID),
(A IA, B IB, C IC, D ID, E IE),
(A IA, B IB, C IC, D ID, E IE, F IF),
(A IA, B IB, C IC, D ID, E IE, F IF, G IG),
(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH),
}
#[diagnostic::on_unimplemented(
message = "`{Self}` can't be fetched from this query",
label = "fetch joined models, `Option`s of them or tuples of those"
)]
pub trait JoinedModels<S, I> {
type Output;
#[doc(hidden)]
fn write_columns(sql: &mut Sql);
#[doc(hidden)]
fn decode(row: &PgRow, offset: &mut usize) -> Result<Self::Output>;
}
impl<S, I, N: Model + InScope<S, I>> JoinedModels<S, I> for N {
type Output = N;
fn write_columns(sql: &mut Sql) {
sql.push(", ");
crate::model::push_columns::<N>(sql);
}
fn decode(row: &PgRow, offset: &mut usize) -> Result<N> {
let model = N::from_row_at(row, *offset)?;
*offset += N::COLUMNS.len();
Ok(model)
}
}
impl<S, I, N: Model + InScope<S, I>> JoinedModels<S, I> for Option<N> {
type Output = Option<N>;
fn write_columns(sql: &mut Sql) {
<N as JoinedModels<S, I>>::write_columns(sql);
}
fn decode(row: &PgRow, offset: &mut usize) -> Result<Option<N>> {
use sqlx::{Row, ValueRef};
let mut missing = true;
for key in N::PRIMARY_KEYS {
let at = N::COLUMNS.iter().position(|c| c == key).unwrap_or(0);
missing &= row.try_get_raw(*offset + at)?.is_null();
}
if missing {
*offset += N::COLUMNS.len();
return Ok(None);
}
<N as JoinedModels<S, I>>::decode(row, offset).map(Some)
}
}
macro_rules! joined_model_tuples {
($(($($t:ident $i:ident),+)),+ $(,)?) => {$(
impl<S, $($i,)+ $($t),+> JoinedModels<S, ($($i,)+)> for ($($t,)+)
where
$($t: JoinedModels<S, $i>,)+
{
type Output = ($($t::Output,)+);
fn write_columns(sql: &mut Sql) {
$($t::write_columns(sql);)+
}
fn decode(row: &PgRow, offset: &mut usize) -> Result<Self::Output> {
Ok(($($t::decode(row, offset)?,)+))
}
}
)+};
}
joined_model_tuples! {
(A IA, B IB),
(A IA, B IB, C IC),
(A IA, B IB, C IC, D ID),
(A IA, B IB, C IC, D ID, E IE),
(A IA, B IB, C IC, D ID, E IE, F IF),
(A IA, B IB, C IC, D ID, E IE, F IF, G IG),
(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH),
}
pub struct Joined<M, J> {
select: Select<M>,
_joins: PhantomData<fn() -> J>,
}
impl<M: Model> Select<M> {
pub fn join<R, I>(self, join: R) -> Joined<M, ((), R::To)>
where
R: IntoJoin,
R::From: InScope<(M, ()), I>,
{
Joined::<M, ()>::root(self).push_join(join, false)
}
pub fn left_join<R, I>(self, join: R) -> Joined<M, ((), R::To)>
where
R: IntoJoin,
R::From: InScope<(M, ()), I>,
{
Joined::<M, ()>::root(self).push_join(join, true)
}
}
impl<M: Model, J> Joined<M, J> {
fn from_select<J2>(select: Select<M>) -> Joined<M, J2> {
Joined {
select,
_joins: PhantomData,
}
}
fn root(select: Select<M>) -> Joined<M, ()> {
Joined {
select,
_joins: PhantomData,
}
}
fn push_join<R: IntoJoin, J2>(mut self, join: R, left: bool) -> Joined<M, J2> {
self.select.joins.push(JoinClause {
left,
table: <R::To as Model>::TABLE,
on: join.condition().0,
scope: join_scope::<R::To>,
multiplies: join.multiplies(),
});
Joined::<M, J>::from_select(self.select)
}
pub fn join<R, I>(self, join: R) -> Joined<M, (J, R::To)>
where
R: IntoJoin,
R::From: InScope<(M, J), I>,
{
self.push_join(join, false)
}
pub fn left_join<R, I>(self, join: R) -> Joined<M, (J, R::To)>
where
R: IntoJoin,
R::From: InScope<(M, J), I>,
{
self.push_join(join, true)
}
pub fn filter<X, I>(mut self, expr: Expr<X>) -> Self
where
X: InScope<(M, J), I>,
{
self.select = self.select.filter(expr.retag());
self
}
pub fn order_by<X, I>(mut self, order: impl Into<Order<X>>) -> Self
where
X: InScope<(M, J), I>,
{
self.select = self.select.order_by(order.into().retag::<M>());
self
}
pub fn group_by<X, I>(mut self, column: Column<X>) -> Self
where
X: Model + InScope<(M, J), I>,
{
self.select.group_by.push((X::TABLE, column.name()));
self
}
pub fn having<X, I>(mut self, expr: Expr<X>) -> Self
where
X: InScope<(M, J), I>,
{
self.select.having.push(expr.retag::<M>().cond);
self
}
pub fn limit(mut self, limit: u64) -> Self {
self.select = self.select.limit(limit);
self
}
pub fn offset(mut self, offset: u64) -> Self {
self.select = self.select.offset(offset);
self
}
pub fn with_trashed(mut self) -> Self {
self.select = self.select.with_trashed();
self
}
pub fn on_primary(mut self) -> Self {
self.select = self.select.on_primary();
self
}
pub fn select<P, I>(self, items: P) -> Projected<M>
where
P: ScopedProjections<(M, J), I>,
{
Projected {
select: self.select,
items: items.into_items(),
}
}
pub fn update(self) -> Update<M> {
self.select.key_subselect().update()
}
pub async fn delete<'e, E: Executor<'e>>(self, executor: E) -> Result<u64> {
self.select.key_subselect().delete(executor).await
}
pub async fn force_delete<'e, E: Executor<'e>>(self, executor: E) -> Result<u64> {
self.select.key_subselect().force_delete(executor).await
}
pub async fn restore<'e, E: Executor<'e>>(self, executor: E) -> Result<u64> {
self.select.key_subselect().restore(executor).await
}
pub fn memoize(self, ttl: std::time::Duration) -> crate::Memoized<M> {
self.select.memoize(ttl)
}
#[doc(hidden)]
pub fn into_select(self) -> Select<M> {
self.select
}
pub fn to_sql(&self) -> Sql {
self.select.to_sql()
}
pub async fn all<'e, E: Executor<'e>>(self, executor: E) -> Result<Vec<M>> {
self.select.all(executor).await
}
pub async fn first<'e, E: Executor<'e>>(self, executor: E) -> Result<Option<M>> {
self.select.first(executor).await
}
pub async fn one<'e, E: Executor<'e>>(self, executor: E) -> Result<M> {
self.select.one(executor).await
}
pub async fn count<'e, E: Executor<'e>>(self, executor: E) -> Result<i64> {
self.select.count(executor).await
}
pub async fn exists<'e, E: Executor<'e>>(self, executor: E) -> Result<bool> {
self.select.exists(executor).await
}
pub async fn paginate<'e, E: Executor<'e>>(
self,
executor: E,
page: u64,
per_page: u64,
) -> Result<Page<M>> {
self.select.paginate(executor, page, per_page).await
}
pub async fn all_with<'e, T, I>(
self,
executor: impl Executor<'e>,
) -> Result<Vec<(M, T::Output)>>
where
T: JoinedModels<(M, J), I>,
{
let sql = self.with_sql::<T, I>();
let rows = crate::exec::fetch_rows(executor, &sql, &[], self.select.use_replica()).await?;
rows.iter()
.map(|row| {
let root = M::from_row_at(row, 0)?;
let mut offset = M::COLUMNS.len();
Ok((root, T::decode(row, &mut offset)?))
})
.collect()
}
pub fn with_sql<T, I>(&self) -> Sql
where
T: JoinedModels<(M, J), I>,
{
let mut sql = self.select.new_sql();
sql.push("SELECT ");
crate::model::push_columns::<M>(&mut sql);
T::write_columns(&mut sql);
self.select.write_tail(&mut sql);
sql
}
pub async fn cursor_paginate<'e, E: Executor<'e>>(
self,
executor: E,
after: Option<&crate::Cursor>,
limit: u64,
) -> Result<crate::CursorPage<M>> {
self.select.cursor_paginate(executor, after, limit).await
}
pub fn stream<'e, E: Executor<'e> + 'e>(self, executor: E) -> BoxStream<'e, Result<M>> {
self.select.stream(executor)
}
}
impl<M, J> Clone for Joined<M, J> {
fn clone(&self) -> Self {
Self {
select: self.select.clone(),
_joins: PhantomData,
}
}
}
impl<M: Model, J> fmt::Debug for Joined<M, J> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Joined").field(&self.to_sql()).finish()
}
}