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
https://github.com/rust-lang/rust/issues/43153
This commit is contained in:
Sean Griffin 2017-07-13 15:56:41 -06:00
parent 2296d23abd
commit 34891a6563
11 changed files with 36 additions and 25 deletions

View File

@ -40,7 +40,7 @@ matrix:
allow_failures:
- rust: nightly
include:
- rust: nightly-2017-04-25
- rust: nightly-2017-06-06
env: CLIPPY_AND_COMPILE_TESTS=YESPLEASE
script:
- (cd diesel && cargo rustc --no-default-features --features "lint unstable sqlite postgres mysql extras" -- -Zno-trans)

View File

@ -14,7 +14,7 @@ categories = ["database"]
[dependencies]
byteorder = "1.0"
chrono = { version = "0.4", optional = true }
clippy = { optional = true, version = "=0.0.126" }
clippy = { optional = true, version = "=0.0.138" }
libc = { version = "0.2.0", optional = true }
libsqlite3-sys = { version = ">=0.8.0, <0.9.0", optional = true, features = ["min_sqlite_version_3_7_16"] }
mysqlclient-sys = { version = ">=0.1.0, <0.3.0", optional = true }

View File

@ -114,6 +114,7 @@ impl<DB, Statement> StatementCache<DB, Statement> where
DB::QueryBuilder: Default,
StatementCacheKey<DB>: Hash + Eq,
{
#[cfg_attr(feature="clippy", allow(new_without_default_derive))]
pub fn new() -> Self {
StatementCache {
cache: RefCell::new(HashMap::new())

View File

@ -648,10 +648,12 @@ macro_rules! table_body {
impl<T> JoinTo<T> for table where
T: JoinTo<table> + JoinTo<PleaseGenerateInverseJoinImpls<table>>,
{
type JoinOnClause = <T as JoinTo<table>>::JoinOnClause;
type FromClause = T;
type OnClause = <T as JoinTo<table>>::OnClause;
fn join_on_clause() -> Self::JoinOnClause {
<T as JoinTo<table>>::join_on_clause()
fn join_target(rhs: T) -> (Self::FromClause, Self::OnClause) {
let (_, on_clause) = T::join_target(table);
(rhs, on_clause)
}
}
@ -803,15 +805,16 @@ macro_rules! joinable_inner {
primary_key_expr = $primary_key_expr:expr,
) => {
impl $crate::JoinTo<$right_table_ty> for $left_table_ty {
type JoinOnClause = $crate::expression::helper_types::Eq<
type FromClause = $right_table_ty;
type OnClause = $crate::expression::helper_types::Eq<
$crate::expression::nullable::Nullable<$foreign_key>,
$crate::expression::nullable::Nullable<$primary_key_ty>,
>;
fn join_on_clause() -> Self::JoinOnClause {
fn join_target(rhs: $right_table_ty) -> (Self::FromClause, Self::OnClause) {
use $crate::{ExpressionMethods, NullableExpressionMethods};
$foreign_key.nullable().eq($primary_key_expr.nullable())
(rhs, $foreign_key.nullable().eq($primary_key_expr.nullable()))
}
}
}

View File

@ -16,6 +16,7 @@ pub struct RawBytesBindCollector<DB: Backend + TypeMetadata> {
}
impl<DB: Backend + TypeMetadata> RawBytesBindCollector<DB> {
#[cfg_attr(feature="clippy", allow(new_without_default_derive))]
pub fn new() -> Self {
RawBytesBindCollector {
metadata: Vec::new(),

View File

@ -306,9 +306,10 @@ impl<F, S, D, W, O, L, Of, G, Rhs> JoinTo<Rhs>
for SelectStatement<F, S, D, W, O, L, Of, G> where
F: JoinTo<Rhs>,
{
type JoinOnClause = F::JoinOnClause;
type FromClause = F::FromClause;
type OnClause = F::OnClause;
fn join_on_clause() -> Self::JoinOnClause {
F::join_on_clause()
fn join_target(rhs: Rhs) -> (Self::FromClause, Self::OnClause) {
F::join_target(rhs)
}
}

View File

@ -31,12 +31,13 @@ pub trait JoinWithImplicitOnClause<Rhs, Kind> {
impl<Lhs, Rhs, Kind> JoinWithImplicitOnClause<Rhs, Kind> for Lhs where
Lhs: JoinTo<Rhs>,
Lhs: InternalJoinDsl<Rhs, Kind, <Lhs as JoinTo<Rhs>>::JoinOnClause>,
Lhs: InternalJoinDsl<<Lhs as JoinTo<Rhs>>::FromClause, Kind, <Lhs as JoinTo<Rhs>>::OnClause>,
{
type Output = <Lhs as InternalJoinDsl<Rhs, Kind, Lhs::JoinOnClause>>::Output;
type Output = <Lhs as InternalJoinDsl<Lhs::FromClause, Kind, Lhs::OnClause>>::Output;
fn join_with_implicit_on_clause(self, rhs: Rhs, kind: Kind) -> Self::Output {
self.join(rhs, kind, Lhs::join_on_clause())
let (from, on) = Lhs::join_target(rhs);
self.join(from, kind, on)
}
}

View File

@ -154,9 +154,11 @@ impl<From, T> SelectableExpression<SelectStatement<From>>
/// the [association annotations](../associations/index.html) from codegen.
pub trait JoinTo<T> {
#[doc(hidden)]
type JoinOnClause;
type FromClause;
#[doc(hidden)]
fn join_on_clause() -> Self::JoinOnClause;
type OnClause;
#[doc(hidden)]
fn join_target(rhs: T) -> (Self::FromClause, Self::OnClause);
}
#[doc(hidden)]
@ -228,20 +230,22 @@ impl<DB: Backend> QueryFragment<DB> for LeftOuter {
impl<Left, Mid, Right, Kind> JoinTo<Right> for Join<Left, Mid, Kind> where
Left: JoinTo<Right>,
{
type JoinOnClause = Left::JoinOnClause;
type FromClause = Left::FromClause;
type OnClause = Left::OnClause;
fn join_on_clause() -> Self::JoinOnClause {
Left::join_on_clause()
fn join_target(rhs: Right) -> (Self::FromClause, Self::OnClause) {
Left::join_target(rhs)
}
}
impl<Join, On, Right> JoinTo<Right> for JoinOn<Join, On> where
Join: JoinTo<Right>,
{
type JoinOnClause = Join::JoinOnClause;
type FromClause = Join::FromClause;
type OnClause = Join::OnClause;
fn join_on_clause() -> Self::JoinOnClause {
Join::join_on_clause()
fn join_target(rhs: Right) -> (Self::FromClause, Self::OnClause) {
Join::join_target(rhs)
}
}

View File

@ -19,7 +19,7 @@ clap = "2.20.3"
diesel = { version = "0.14.0", default-features = false }
dotenv = ">=0.8, <0.11"
diesel_infer_schema = "0.14.0"
clippy = { optional = true, version = "=0.0.126" }
clippy = { optional = true, version = "=0.0.138" }
[dev-dependencies]
tempdir = "0.3.4"

View File

@ -16,7 +16,7 @@ quote = "0.3.12"
dotenv = { version = ">=0.8, <0.11", optional = true }
diesel = { version = "0.14.0", default-features = false }
diesel_infer_schema = { version = "0.14.0", default-features = false, optional = true }
clippy = { optional = true, version = "=0.0.126" }
clippy = { optional = true, version = "=0.0.138" }
[dev-dependencies]
tempdir = "0.3.4"

View File

@ -13,7 +13,7 @@ keywords = ["orm", "database", "postgres", "postgresql", "sql"]
diesel = { version = "0.14.0", default-features = false }
syn = "0.11.4"
quote = "0.3.1"
clippy = { optional = true, version = "=0.0.126" }
clippy = { optional = true, version = "=0.0.138" }
[dev-dependencies]
dotenv = ">=0.8, <0.11"