Skip to content

Commit 201c8cd

Browse files
committed
Remove Table constraints from query_source::joins
In order to deal with multitable joins, we'll need to support cases where both `Lhs` and `Rhs` are not tables. `Lhs` will not be a table in the case like `users.inner_joins(posts).inner_joins(comments)`, which should return `(User, Post, Comment)` and represent the user, all their posts, and all comments that they've left (not necessarily on their own posts). `Rhs` will not be a table in the case like `users.inner_joins(posts.inner_joins(comments))`, which should return `(User, (Post, Comment))` and represents the user, all their posts, and all comments left on those posts (not necessarily by that user). Having this distinction (or multi-table joins at all, really) will enable the much trickier case of `users.inner_joins(posts.inner_joins(comments)).inner_joins(comments)`, which requires table aliases to work properly... I'm not entirely sure what I want to do for the design there, but I'm not sure I can fail that case at compile time just yet
1 parent 31cede5 commit 201c8cd

2 files changed

Lines changed: 18 additions & 33 deletions

File tree

diesel/src/macros/mod.rs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,31 +43,16 @@ macro_rules! __diesel_column {
4343
{
4444
}
4545

46-
impl<Right> AppearsOnTable<
47-
Join<$($table)::*, Right, Inner>,
48-
> for $column_name where
49-
Right: Table,
50-
$($table)::*: $crate::JoinTo<Right>
51-
{
52-
}
53-
54-
impl<Left> AppearsOnTable<
55-
Join<Left, $($table)::*, Inner>,
56-
> for $column_name where
57-
Left: $crate::JoinTo<$($table)::*>
58-
{
59-
}
60-
61-
impl<Right> AppearsOnTable<
62-
Join<$($table)::*, Right, LeftOuter>,
46+
impl<Right, Kind> AppearsOnTable<
47+
Join<$($table)::*, Right, Kind>,
6348
> for $column_name where
64-
Right: Table,
49+
Right: QuerySource,
6550
$($table)::*: $crate::JoinTo<Right>
6651
{
6752
}
6853

69-
impl<Left> AppearsOnTable<
70-
Join<Left, $($table)::*, LeftOuter>,
54+
impl<Left, Kind> AppearsOnTable<
55+
Join<Left, $($table)::*, Kind>,
7156
> for $column_name where
7257
Left: $crate::JoinTo<$($table)::*>
7358
{
@@ -438,10 +423,10 @@ macro_rules! table_body {
438423
/// Contains all of the columns of this table
439424
pub mod columns {
440425
use super::table;
441-
use $crate::{Table, Expression, SelectableExpression, AppearsOnTable, QuerySource};
426+
use $crate::{Expression, SelectableExpression, AppearsOnTable, QuerySource};
442427
use $crate::backend::Backend;
443428
use $crate::query_builder::{QueryFragment, AstPass};
444-
use $crate::query_source::joins::{Join, JoinOn, Inner, LeftOuter};
429+
use $crate::query_source::joins::{Join, JoinOn, Inner};
445430
use $crate::result::QueryResult;
446431
$(use $($import)::+;)+
447432

diesel/src/query_source/joins.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use expression::SelectableExpression;
33
use expression::nullable::Nullable;
44
use query_builder::*;
55
use result::QueryResult;
6-
use super::{QuerySource, Table};
6+
use super::QuerySource;
77

88
#[derive(Debug, Clone, Copy)]
99
/// A query source representing the join between two tables
@@ -45,38 +45,38 @@ impl_query_id!(Join<Left, Right, Kind>);
4545
impl_query_id!(JoinOn<Join, On>);
4646

4747
impl<Left, Right> QuerySource for Join<Left, Right, Inner> where
48-
Left: Table + JoinTo<Right>,
49-
Right: Table,
50-
(Left::AllColumns, Right::AllColumns): SelectableExpression<Self>,
48+
Left: QuerySource + JoinTo<Right>,
49+
Right: QuerySource,
50+
(Left::DefaultSelection, Right::DefaultSelection): SelectableExpression<Self>,
5151
Self: Clone,
5252
{
5353
type FromClause = Self;
54-
type DefaultSelection = (Left::AllColumns, Right::AllColumns);
54+
type DefaultSelection = (Left::DefaultSelection, Right::DefaultSelection);
5555

5656
fn from_clause(&self) -> Self::FromClause {
5757
self.clone()
5858
}
5959

6060
fn default_selection(&self) -> Self::DefaultSelection {
61-
(Left::all_columns(), Right::all_columns())
61+
(self.left.default_selection(), self.right.default_selection())
6262
}
6363
}
6464

6565
impl<Left, Right> QuerySource for Join<Left, Right, LeftOuter> where
66-
Left: Table + JoinTo<Right>,
67-
Right: Table,
68-
(Left::AllColumns, Nullable<Right::AllColumns>): SelectableExpression<Self>,
66+
Left: QuerySource + JoinTo<Right>,
67+
Right: QuerySource,
68+
(Left::DefaultSelection, Nullable<Right::DefaultSelection>): SelectableExpression<Self>,
6969
Self: Clone,
7070
{
7171
type FromClause = Self;
72-
type DefaultSelection = (Left::AllColumns, Nullable<Right::AllColumns>);
72+
type DefaultSelection = (Left::DefaultSelection, Nullable<Right::DefaultSelection>);
7373

7474
fn from_clause(&self) -> Self::FromClause {
7575
self.clone()
7676
}
7777

7878
fn default_selection(&self) -> Self::DefaultSelection {
79-
(Left::all_columns(), Right::all_columns().nullable())
79+
(self.left.default_selection(), self.right.default_selection().nullable())
8080
}
8181
}
8282

0 commit comments

Comments
 (0)