Skip to content

Commit

Permalink
Merge pull request #4251 from Aethelflaed/combination-positional-order
Browse files Browse the repository at this point in the history
Rewrite positional order similarly to OrderDsl and add order clause to combinations (UNION, ...)
  • Loading branch information
weiznich authored Sep 15, 2024
2 parents 055939f + 6e7920c commit ecf6e92
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 140 deletions.
2 changes: 2 additions & 0 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1621,9 +1621,11 @@ define_sql_function! {
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main() {
/// # #[cfg(feature = "serde_json")]
/// # run_test().unwrap();
/// # }
/// #
/// # #[cfg(feature = "serde_json")]
/// # fn run_test() -> QueryResult<()> {
/// # use diesel::dsl::json_object;
/// # use diesel::sql_types::{Array, Json, Nullable, Text};
Expand Down
4 changes: 4 additions & 0 deletions diesel/src/pg/expression/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::expression::{TypedExpressionType, ValidGrouping};
use crate::pg::Pg;
use crate::query_builder::update_statement::changeset::AssignmentTarget;
use crate::query_builder::{AstPass, QueryFragment, QueryId};
use crate::query_dsl::positional_order_dsl::PositionalOrderExpr;
use crate::sql_types::{
Array, Bigint, Bool, DieselNumericOps, Inet, Integer, Jsonb, SqlType, Text,
};
Expand Down Expand Up @@ -71,6 +72,9 @@ __diesel_infix_operator!(
backend: Pg
);

impl<T: PositionalOrderExpr> PositionalOrderExpr for NullsFirst<T> {}
impl<T: PositionalOrderExpr> PositionalOrderExpr for NullsLast<T> {}

#[derive(Debug, Clone, Copy, QueryId, DieselNumericOps, ValidGrouping)]
#[doc(hidden)]
pub struct ArrayIndex<L, R> {
Expand Down
90 changes: 66 additions & 24 deletions diesel/src/query_builder/combination_clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//!
//! Within this module, types commonly use the following abbreviations:
//!
//! O: Order By Clause
//! L: Limit Clause
//! Of: Offset Clause
//! LOf: Limit Offset Clause
Expand All @@ -16,8 +17,10 @@ use crate::query_builder::insert_statement::InsertFromSelect;
use crate::query_builder::limit_clause::{LimitClause, NoLimitClause};
use crate::query_builder::limit_offset_clause::LimitOffsetClause;
use crate::query_builder::offset_clause::{NoOffsetClause, OffsetClause};
use crate::query_builder::order_clause::{NoOrderClause, OrderClause};
use crate::query_builder::{AsQuery, AstPass, Query, QueryFragment, QueryId, SelectQuery};
use crate::query_dsl::methods::*;
use crate::query_dsl::positional_order_dsl::{IntoPositionalOrderExpr, PositionalOrderDsl};
use crate::sql_types::BigInt;
use crate::{CombineDsl, Insertable, QueryDsl, QueryResult, RunQueryDsl, Table};

Expand All @@ -30,12 +33,15 @@ pub struct CombinationClause<
Rule,
Source,
Rhs,
Order = NoOrderClause,
LimitOffset = LimitOffsetClause<NoLimitClause, NoOffsetClause>,
> {
combinator: Combinator,
duplicate_rule: Rule,
source: ParenthesisWrapper<Source>,
rhs: ParenthesisWrapper<Rhs>,
/// The order clause of the query
order: Order,
/// The combined limit/offset clause of the query
limit_offset: LimitOffset,
}
Expand All @@ -53,6 +59,7 @@ impl<Combinator, Rule, Source, Rhs> CombinationClause<Combinator, Rule, Source,
duplicate_rule,
source: ParenthesisWrapper(source),
rhs: ParenthesisWrapper(rhs),
order: NoOrderClause,
limit_offset: LimitOffsetClause {
limit_clause: NoLimitClause,
offset_clause: NoOffsetClause,
Expand All @@ -61,44 +68,44 @@ impl<Combinator, Rule, Source, Rhs> CombinationClause<Combinator, Rule, Source,
}
}

impl<Combinator, Rule, Source, Rhs, LOf> QueryDsl
for CombinationClause<Combinator, Rule, Source, Rhs, LOf>
impl<Combinator, Rule, Source, Rhs, O, LOf> QueryDsl
for CombinationClause<Combinator, Rule, Source, Rhs, O, LOf>
{
}

impl<Combinator, Rule, Source, Rhs, LOf> Query
for CombinationClause<Combinator, Rule, Source, Rhs, LOf>
impl<Combinator, Rule, Source, Rhs, O, LOf> Query
for CombinationClause<Combinator, Rule, Source, Rhs, O, LOf>
where
Source: Query,
Rhs: Query<SqlType = Source::SqlType>,
{
type SqlType = Source::SqlType;
}

impl<Combinator, Rule, Source, Rhs, LOf> SelectQuery
for CombinationClause<Combinator, Rule, Source, Rhs, LOf>
impl<Combinator, Rule, Source, Rhs, O, LOf> SelectQuery
for CombinationClause<Combinator, Rule, Source, Rhs, O, LOf>
where
Source: SelectQuery,
Rhs: SelectQuery<SqlType = Source::SqlType>,
{
type SqlType = Source::SqlType;
}

impl<Combinator, Rule, Source, Rhs, LOf, QS> ValidSubselect<QS>
for CombinationClause<Combinator, Rule, Source, Rhs, LOf>
impl<Combinator, Rule, Source, Rhs, O, LOf, QS> ValidSubselect<QS>
for CombinationClause<Combinator, Rule, Source, Rhs, O, LOf>
where
Source: ValidSubselect<QS>,
Rhs: ValidSubselect<QS>,
{
}

impl<Combinator, Rule, Source, Rhs, LOf, Conn> RunQueryDsl<Conn>
for CombinationClause<Combinator, Rule, Source, Rhs, LOf>
impl<Combinator, Rule, Source, Rhs, O, LOf, Conn> RunQueryDsl<Conn>
for CombinationClause<Combinator, Rule, Source, Rhs, O, LOf>
{
}

impl<Combinator, Rule, Source, Rhs, LOf, T> Insertable<T>
for CombinationClause<Combinator, Rule, Source, Rhs, LOf>
impl<Combinator, Rule, Source, Rhs, O, LOf, T> Insertable<T>
for CombinationClause<Combinator, Rule, Source, Rhs, O, LOf>
where
T: Table,
T::AllColumns: NonAggregate,
Expand All @@ -111,8 +118,8 @@ where
}
}

impl<Combinator, Rule, Source, OriginRhs, LOf> CombineDsl
for CombinationClause<Combinator, Rule, Source, OriginRhs, LOf>
impl<Combinator, Rule, Source, OriginRhs, O, LOf> CombineDsl
for CombinationClause<Combinator, Rule, Source, OriginRhs, O, LOf>
where
Self: Query,
{
Expand Down Expand Up @@ -161,13 +168,14 @@ where
}
}

impl<Combinator, Rule, Source, Rhs, LOf, DB: Backend> QueryFragment<DB>
for CombinationClause<Combinator, Rule, Source, Rhs, LOf>
impl<Combinator, Rule, Source, Rhs, O, LOf, DB: Backend> QueryFragment<DB>
for CombinationClause<Combinator, Rule, Source, Rhs, O, LOf>
where
Combinator: QueryFragment<DB>,
Rule: QueryFragment<DB>,
ParenthesisWrapper<Source>: QueryFragment<DB>,
ParenthesisWrapper<Rhs>: QueryFragment<DB>,
O: QueryFragment<DB>,
LOf: QueryFragment<DB>,
DB: Backend + SupportsCombinationClause<Combinator, Rule> + DieselReserveSpecialization,
{
Expand All @@ -176,22 +184,53 @@ where
self.combinator.walk_ast(out.reborrow())?;
self.duplicate_rule.walk_ast(out.reborrow())?;
self.rhs.walk_ast(out.reborrow())?;
self.order.walk_ast(out.reborrow())?;
self.limit_offset.walk_ast(out)
}
}

impl<ST, Combinator, Rule, Source, Rhs, O, LOf, RawExpr, Expr> PositionalOrderDsl<RawExpr>
for CombinationClause<Combinator, Rule, Source, Rhs, O, LOf>
where
Self: SelectQuery<SqlType = ST>,
CombinationClause<Combinator, Rule, Source, Rhs, OrderClause<Expr>, LOf>:
SelectQuery<SqlType = ST>,
RawExpr: IntoPositionalOrderExpr<Output = Expr>,
{
type Output = CombinationClause<Combinator, Rule, Source, Rhs, OrderClause<Expr>, LOf>;

fn positional_order_by(self, expr: RawExpr) -> Self::Output {
let order = OrderClause(expr.into_positional_expr());

CombinationClause {
combinator: self.combinator,
duplicate_rule: self.duplicate_rule,
source: self.source,
rhs: self.rhs,
order,
limit_offset: self.limit_offset,
}
}
}

#[doc(hidden)]
type Limit = AsExprOf<i64, BigInt>;

impl<ST, Combinator, Rule, Source, Rhs, L, Of> LimitDsl
for CombinationClause<Combinator, Rule, Source, Rhs, LimitOffsetClause<L, Of>>
impl<ST, Combinator, Rule, Source, Rhs, O, L, Of> LimitDsl
for CombinationClause<Combinator, Rule, Source, Rhs, O, LimitOffsetClause<L, Of>>
where
Self: SelectQuery<SqlType = ST>,
CombinationClause<Combinator, Rule, Source, Rhs, LimitOffsetClause<LimitClause<Limit>, Of>>:
CombinationClause<Combinator, Rule, Source, Rhs, O, LimitOffsetClause<LimitClause<Limit>, Of>>:
SelectQuery<SqlType = ST>,
{
type Output =
CombinationClause<Combinator, Rule, Source, Rhs, LimitOffsetClause<LimitClause<Limit>, Of>>;
type Output = CombinationClause<
Combinator,
Rule,
Source,
Rhs,
O,
LimitOffsetClause<LimitClause<Limit>, Of>,
>;

fn limit(self, limit: i64) -> Self::Output {
let limit_clause = LimitClause(limit.into_sql::<BigInt>());
Expand All @@ -200,6 +239,7 @@ where
duplicate_rule: self.duplicate_rule,
source: self.source,
rhs: self.rhs,
order: self.order,
limit_offset: LimitOffsetClause {
limit_clause,
offset_clause: self.limit_offset.offset_clause,
Expand All @@ -211,18 +251,19 @@ where
#[doc(hidden)]
type Offset = Limit;

impl<ST, Combinator, Rule, Source, Rhs, L, Of> OffsetDsl
for CombinationClause<Combinator, Rule, Source, Rhs, LimitOffsetClause<L, Of>>
impl<ST, Combinator, Rule, Source, Rhs, O, L, Of> OffsetDsl
for CombinationClause<Combinator, Rule, Source, Rhs, O, LimitOffsetClause<L, Of>>
where
Self: SelectQuery<SqlType = ST>,
CombinationClause<Combinator, Rule, Source, Rhs, LimitOffsetClause<L, OffsetClause<Offset>>>:
CombinationClause<Combinator, Rule, Source, Rhs, O, LimitOffsetClause<L, OffsetClause<Offset>>>:
SelectQuery<SqlType = ST>,
{
type Output = CombinationClause<
Combinator,
Rule,
Source,
Rhs,
O,
LimitOffsetClause<L, OffsetClause<Offset>>,
>;

Expand All @@ -233,6 +274,7 @@ where
duplicate_rule: self.duplicate_rule,
source: self.source,
rhs: self.rhs,
order: self.order,
limit_offset: LimitOffsetClause {
limit_clause: self.limit_offset.limit_clause,
offset_clause,
Expand Down
Loading

0 comments on commit ecf6e92

Please sign in to comment.