Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3151 #3155

Merged
merged 1 commit into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions diesel/src/query_builder/combination_clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ where
pub struct CombinationClause<Combinator, Rule, Source, Rhs> {
combinator: Combinator,
duplicate_rule: Rule,
source: Source,
rhs: RhsParenthesisWrapper<Rhs>,
source: ParenthesisWrapper<Source>,
rhs: ParenthesisWrapper<Rhs>,
}

impl<Combinator, Rule, Source, Rhs> CombinationClause<Combinator, Rule, Source, Rhs> {
Expand All @@ -41,8 +41,8 @@ impl<Combinator, Rule, Source, Rhs> CombinationClause<Combinator, Rule, Source,
CombinationClause {
combinator,
duplicate_rule,
source,
rhs: RhsParenthesisWrapper(rhs),
source: ParenthesisWrapper(source),
rhs: ParenthesisWrapper(rhs),
}
}
}
Expand Down Expand Up @@ -129,8 +129,8 @@ impl<Combinator, Rule, Source, Rhs, DB: Backend> QueryFragment<DB>
where
Combinator: QueryFragment<DB>,
Rule: QueryFragment<DB>,
Source: QueryFragment<DB>,
RhsParenthesisWrapper<Rhs>: QueryFragment<DB>,
ParenthesisWrapper<Source>: QueryFragment<DB>,
ParenthesisWrapper<Rhs>: QueryFragment<DB>,
DB: Backend + SupportsCombinationClause<Combinator, Rule> + DieselReserveSpecialization,
{
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
Expand Down Expand Up @@ -215,7 +215,7 @@ pub trait SupportsCombinationClause<Combinator, Rule> {}

#[derive(Debug, Copy, Clone, QueryId)]
/// Wrapper used to wrap rhs sql in parenthesis when supported by backend
pub struct RhsParenthesisWrapper<T>(T);
pub struct ParenthesisWrapper<T>(T);

#[cfg(feature = "postgres")]
mod postgres {
Expand All @@ -224,7 +224,7 @@ mod postgres {
use crate::query_builder::{AstPass, QueryFragment};
use crate::QueryResult;

impl<T: QueryFragment<Pg>> QueryFragment<Pg> for RhsParenthesisWrapper<T> {
impl<T: QueryFragment<Pg>> QueryFragment<Pg> for ParenthesisWrapper<T> {
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
out.push_sql("(");
self.0.walk_ast(out.reborrow())?;
Expand All @@ -248,7 +248,7 @@ mod mysql {
use crate::query_builder::{AstPass, QueryFragment};
use crate::QueryResult;

impl<T: QueryFragment<Mysql>> QueryFragment<Mysql> for RhsParenthesisWrapper<T> {
impl<T: QueryFragment<Mysql>> QueryFragment<Mysql> for ParenthesisWrapper<T> {
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Mysql>) -> QueryResult<()> {
out.push_sql("(");
self.0.walk_ast(out.reborrow())?;
Expand All @@ -268,9 +268,15 @@ mod sqlite {
use crate::sqlite::Sqlite;
use crate::QueryResult;

impl<T: QueryFragment<Sqlite>> QueryFragment<Sqlite> for RhsParenthesisWrapper<T> {
fn walk_ast<'b>(&'b self, out: AstPass<'_, 'b, Sqlite>) -> QueryResult<()> {
self.0.walk_ast(out) // SQLite does not support parenthesis around Ths
impl<T: QueryFragment<Sqlite>> QueryFragment<Sqlite> for ParenthesisWrapper<T> {
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Sqlite>) -> QueryResult<()> {
// SQLite does not support parenthesis around Ths
// we can emulate this by construct a fake outer
// SELECT * FROM (inner_query) statement
out.push_sql("SELECT * FROM (");
self.0.walk_ast(out.reborrow())?;
out.push_sql(")");
Ok(())
}
}

Expand Down
30 changes: 30 additions & 0 deletions diesel_tests/tests/combination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,33 @@ fn except() {
.unwrap();
assert_eq!(expected_data, data);
}

#[test]
fn union_with_order() {
let conn = &mut connection();
let data = vec![
NewUser::new("Sean", None),
NewUser::new("Tess", None),
NewUser::new("Jim", None),
];
insert_into(users::table)
.values(&data)
.execute(conn)
.unwrap();

let users = users::table
.select(users::name)
.order_by(users::id.asc())
.limit(1)
.union(
users::table
.order_by(users::id.desc())
.select(users::name)
.limit(1),
)
.positional_order_by(1)
.load::<String>(conn)
.unwrap();

assert_eq!(vec![String::from("Jim"), "Sean".into()], users);
}