Skip to content

Commit 44e7fd0

Browse files
sgrifFiedzia
authored andcommitted
Pass the right hand side to JoinTo
This is a refactoring in preparation for ad-hoc join clauses. I'm imagining the API being roughly `lhs.inner_join(rhs.on(on_clause))`, which will mean we will have a struct containing the rhs and the on clause. The `JoinTo` impl for this struct will need to just destructure it and return those pieces, so this sets up the trait changes for all existing implementations. I had to bump clippy as part of this change, as it hits a rustc bug that was fixed on more recent nightlies. However, I couldn't bump to the *latest* clippy, as recent nightlies are broken for us due to rust-lang/rust#43153
1 parent 135912f commit 44e7fd0

File tree

11 files changed

+36
-25
lines changed

11 files changed

+36
-25
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ matrix:
4040
allow_failures:
4141
- rust: nightly
4242
include:
43-
- rust: nightly-2017-04-25
43+
- rust: nightly-2017-06-06
4444
env: CLIPPY_AND_COMPILE_TESTS=YESPLEASE
4545
script:
4646
- (cd diesel && cargo rustc --no-default-features --features "lint unstable sqlite postgres mysql extras" -- -Zno-trans)

diesel/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ categories = ["database"]
1414
[dependencies]
1515
byteorder = "1.0"
1616
chrono = { version = "0.4", optional = true }
17-
clippy = { optional = true, version = "=0.0.126" }
17+
clippy = { optional = true, version = "=0.0.138" }
1818
libc = { version = "0.2.0", optional = true }
1919
libsqlite3-sys = { version = ">=0.8.0, <0.9.0", optional = true, features = ["min_sqlite_version_3_7_16"] }
2020
mysqlclient-sys = { version = ">=0.1.0, <0.3.0", optional = true }

diesel/src/connection/statement_cache.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ impl<DB, Statement> StatementCache<DB, Statement> where
114114
DB::QueryBuilder: Default,
115115
StatementCacheKey<DB>: Hash + Eq,
116116
{
117+
#[cfg_attr(feature="clippy", allow(new_without_default_derive))]
117118
pub fn new() -> Self {
118119
StatementCache {
119120
cache: RefCell::new(HashMap::new())

diesel/src/macros/mod.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -648,10 +648,12 @@ macro_rules! table_body {
648648
impl<T> JoinTo<T> for table where
649649
T: JoinTo<table> + JoinTo<PleaseGenerateInverseJoinImpls<table>>,
650650
{
651-
type JoinOnClause = <T as JoinTo<table>>::JoinOnClause;
651+
type FromClause = T;
652+
type OnClause = <T as JoinTo<table>>::OnClause;
652653

653-
fn join_on_clause() -> Self::JoinOnClause {
654-
<T as JoinTo<table>>::join_on_clause()
654+
fn join_target(rhs: T) -> (Self::FromClause, Self::OnClause) {
655+
let (_, on_clause) = T::join_target(table);
656+
(rhs, on_clause)
655657
}
656658
}
657659

@@ -803,15 +805,16 @@ macro_rules! joinable_inner {
803805
primary_key_expr = $primary_key_expr:expr,
804806
) => {
805807
impl $crate::JoinTo<$right_table_ty> for $left_table_ty {
806-
type JoinOnClause = $crate::expression::helper_types::Eq<
808+
type FromClause = $right_table_ty;
809+
type OnClause = $crate::expression::helper_types::Eq<
807810
$crate::expression::nullable::Nullable<$foreign_key>,
808811
$crate::expression::nullable::Nullable<$primary_key_ty>,
809812
>;
810813

811-
fn join_on_clause() -> Self::JoinOnClause {
814+
fn join_target(rhs: $right_table_ty) -> (Self::FromClause, Self::OnClause) {
812815
use $crate::{ExpressionMethods, NullableExpressionMethods};
813816

814-
$foreign_key.nullable().eq($primary_key_expr.nullable())
817+
(rhs, $foreign_key.nullable().eq($primary_key_expr.nullable()))
815818
}
816819
}
817820
}

diesel/src/query_builder/bind_collector.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub struct RawBytesBindCollector<DB: Backend + TypeMetadata> {
2020
}
2121

2222
impl<DB: Backend + TypeMetadata> RawBytesBindCollector<DB> {
23+
#[cfg_attr(feature="clippy", allow(new_without_default_derive))]
2324
pub fn new() -> Self {
2425
RawBytesBindCollector {
2526
metadata: Vec::new(),

diesel/src/query_builder/select_statement/dsl_impls.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,10 @@ impl<F, S, D, W, O, L, Of, G, Rhs> JoinTo<Rhs>
306306
for SelectStatement<F, S, D, W, O, L, Of, G> where
307307
F: JoinTo<Rhs>,
308308
{
309-
type JoinOnClause = F::JoinOnClause;
309+
type FromClause = F::FromClause;
310+
type OnClause = F::OnClause;
310311

311-
fn join_on_clause() -> Self::JoinOnClause {
312-
F::join_on_clause()
312+
fn join_target(rhs: Rhs) -> (Self::FromClause, Self::OnClause) {
313+
F::join_target(rhs)
313314
}
314315
}

diesel/src/query_dsl/join_dsl.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ pub trait JoinWithImplicitOnClause<Rhs, Kind> {
3131

3232
impl<Lhs, Rhs, Kind> JoinWithImplicitOnClause<Rhs, Kind> for Lhs where
3333
Lhs: JoinTo<Rhs>,
34-
Lhs: InternalJoinDsl<Rhs, Kind, <Lhs as JoinTo<Rhs>>::JoinOnClause>,
34+
Lhs: InternalJoinDsl<<Lhs as JoinTo<Rhs>>::FromClause, Kind, <Lhs as JoinTo<Rhs>>::OnClause>,
3535
{
36-
type Output = <Lhs as InternalJoinDsl<Rhs, Kind, Lhs::JoinOnClause>>::Output;
36+
type Output = <Lhs as InternalJoinDsl<Lhs::FromClause, Kind, Lhs::OnClause>>::Output;
3737

3838
fn join_with_implicit_on_clause(self, rhs: Rhs, kind: Kind) -> Self::Output {
39-
self.join(rhs, kind, Lhs::join_on_clause())
39+
let (from, on) = Lhs::join_target(rhs);
40+
self.join(from, kind, on)
4041
}
4142
}
4243

diesel/src/query_source/joins.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,11 @@ impl<From, T> SelectableExpression<SelectStatement<From>>
154154
/// the [association annotations](../associations/index.html) from codegen.
155155
pub trait JoinTo<T> {
156156
#[doc(hidden)]
157-
type JoinOnClause;
157+
type FromClause;
158158
#[doc(hidden)]
159-
fn join_on_clause() -> Self::JoinOnClause;
159+
type OnClause;
160+
#[doc(hidden)]
161+
fn join_target(rhs: T) -> (Self::FromClause, Self::OnClause);
160162
}
161163

162164
#[doc(hidden)]
@@ -228,20 +230,22 @@ impl<DB: Backend> QueryFragment<DB> for LeftOuter {
228230
impl<Left, Mid, Right, Kind> JoinTo<Right> for Join<Left, Mid, Kind> where
229231
Left: JoinTo<Right>,
230232
{
231-
type JoinOnClause = Left::JoinOnClause;
233+
type FromClause = Left::FromClause;
234+
type OnClause = Left::OnClause;
232235

233-
fn join_on_clause() -> Self::JoinOnClause {
234-
Left::join_on_clause()
236+
fn join_target(rhs: Right) -> (Self::FromClause, Self::OnClause) {
237+
Left::join_target(rhs)
235238
}
236239
}
237240

238241
impl<Join, On, Right> JoinTo<Right> for JoinOn<Join, On> where
239242
Join: JoinTo<Right>,
240243
{
241-
type JoinOnClause = Join::JoinOnClause;
244+
type FromClause = Join::FromClause;
245+
type OnClause = Join::OnClause;
242246

243-
fn join_on_clause() -> Self::JoinOnClause {
244-
Join::join_on_clause()
247+
fn join_target(rhs: Right) -> (Self::FromClause, Self::OnClause) {
248+
Join::join_target(rhs)
245249
}
246250
}
247251

diesel_cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ clap = "2.20.3"
1919
diesel = { version = "0.14.0", default-features = false }
2020
dotenv = ">=0.8, <0.11"
2121
diesel_infer_schema = "0.14.0"
22-
clippy = { optional = true, version = "=0.0.126" }
22+
clippy = { optional = true, version = "=0.0.138" }
2323

2424
[dev-dependencies]
2525
tempdir = "0.3.4"

diesel_codegen/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ quote = "0.3.12"
1616
dotenv = { version = ">=0.8, <0.11", optional = true }
1717
diesel = { version = "0.14.0", default-features = false }
1818
diesel_infer_schema = { version = "0.14.0", default-features = false, optional = true }
19-
clippy = { optional = true, version = "=0.0.126" }
19+
clippy = { optional = true, version = "=0.0.138" }
2020

2121
[dev-dependencies]
2222
tempdir = "0.3.4"

0 commit comments

Comments
 (0)