Skip to content

Commit 4200a2c

Browse files
authored
Merge pull request #4811 from weiznich/feature/view_macro
Introduce a view macro
2 parents 5901613 + 50c7b0e commit 4200a2c

24 files changed

+2446
-556
lines changed

diesel/src/internal/table_macro.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub use crate::query_source::aliasing::{
2121
#[doc(hidden)]
2222
pub use crate::query_source::joins::{Inner, Join, JoinOn, LeftOuter};
2323
#[doc(hidden)]
24-
pub use crate::query_source::private::{Pick, Sealed};
24+
pub use crate::query_source::private::{Pick, PlainQuerySource, Sealed};
2525

2626
#[doc(hidden)]
2727
pub mod ops {

diesel/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,8 @@ pub mod prelude {
771771
};
772772
#[doc(inline)]
773773
pub use diesel_derives::table_proc as table;
774+
#[doc(inline)]
775+
pub use diesel_derives::view_proc as view;
774776

775777
#[cfg(feature = "mysql")]
776778
#[doc(inline)]

diesel/src/macros/mod.rs

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ macro_rules! allow_tables_to_appear_in_same_query {
212212
$(
213213
impl $crate::query_source::TableNotEqual<$left_mod::table> for $right_mod::table {}
214214
impl $crate::query_source::TableNotEqual<$right_mod::table> for $left_mod::table {}
215-
$crate::__diesel_internal_backend_specific_allow_tables_to_appear_in_same_query!($left_mod, $right_mod);
216215
)+
217216
$crate::allow_tables_to_appear_in_same_query!($($right_mod,)+);
218217
};
@@ -221,63 +220,6 @@ macro_rules! allow_tables_to_appear_in_same_query {
221220

222221
() => {};
223222
}
224-
#[doc(hidden)]
225-
#[macro_export]
226-
#[cfg(feature = "postgres_backend")]
227-
macro_rules! __diesel_internal_backend_specific_allow_tables_to_appear_in_same_query {
228-
($left:ident, $right:ident) => {
229-
impl $crate::query_source::TableNotEqual<$left::table>
230-
for $crate::query_builder::Only<$right::table>
231-
{
232-
}
233-
impl $crate::query_source::TableNotEqual<$right::table>
234-
for $crate::query_builder::Only<$left::table>
235-
{
236-
}
237-
impl $crate::query_source::TableNotEqual<$crate::query_builder::Only<$left::table>>
238-
for $right::table
239-
{
240-
}
241-
impl $crate::query_source::TableNotEqual<$crate::query_builder::Only<$right::table>>
242-
for $left::table
243-
{
244-
}
245-
impl<TSM> $crate::query_source::TableNotEqual<$left::table>
246-
for $crate::query_builder::Tablesample<$right::table, TSM>
247-
where
248-
TSM: $crate::internal::table_macro::TablesampleMethod,
249-
{
250-
}
251-
impl<TSM> $crate::query_source::TableNotEqual<$right::table>
252-
for $crate::query_builder::Tablesample<$left::table, TSM>
253-
where
254-
TSM: $crate::internal::table_macro::TablesampleMethod,
255-
{
256-
}
257-
impl<TSM>
258-
$crate::query_source::TableNotEqual<
259-
$crate::query_builder::Tablesample<$left::table, TSM>,
260-
> for $right::table
261-
where
262-
TSM: $crate::internal::table_macro::TablesampleMethod,
263-
{
264-
}
265-
impl<TSM>
266-
$crate::query_source::TableNotEqual<
267-
$crate::query_builder::Tablesample<$right::table, TSM>,
268-
> for $left::table
269-
where
270-
TSM: $crate::internal::table_macro::TablesampleMethod,
271-
{
272-
}
273-
};
274-
}
275-
#[doc(hidden)]
276-
#[macro_export]
277-
#[cfg(not(feature = "postgres_backend"))]
278-
macro_rules! __diesel_internal_backend_specific_allow_tables_to_appear_in_same_query {
279-
($left:ident, $right:ident) => {};
280-
}
281223

282224
#[doc(hidden)]
283225
#[macro_export]

diesel/src/pg/query_builder/only.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::expression::{Expression, ValidGrouping};
22
use crate::pg::Pg;
33
use crate::query_builder::{AsQuery, AstPass, FromClause, QueryFragment, QueryId, SelectStatement};
4-
use crate::query_source::QuerySource;
4+
use crate::query_source::private::PlainQuerySource;
5+
use crate::query_source::{QueryRelation, QuerySource, TableNotEqual};
56
use crate::result::QueryResult;
67
use crate::{JoinTo, SelectableExpression, Table};
78

@@ -11,6 +12,24 @@ pub struct Only<S> {
1112
pub(crate) source: S,
1213
}
1314

15+
#[diagnostic::do_not_recommend]
16+
impl<T1, T2> TableNotEqual<T1> for Only<T2>
17+
where
18+
T1: PlainQuerySource,
19+
T2: TableNotEqual<T1>,
20+
Self: Table,
21+
{
22+
}
23+
24+
#[diagnostic::do_not_recommend]
25+
impl<T1, T2> TableNotEqual<Only<T1>> for T2
26+
where
27+
T1: QueryRelation,
28+
T2: PlainQuerySource + TableNotEqual<T1>,
29+
Only<T1>: Table,
30+
{
31+
}
32+
1433
impl<S> QueryId for Only<S>
1534
where
1635
Self: 'static,

diesel/src/pg/query_builder/tablesample.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::expression::{Expression, ValidGrouping};
22
use crate::pg::Pg;
33
use crate::query_builder::{AsQuery, AstPass, FromClause, QueryFragment, QueryId, SelectStatement};
4-
use crate::query_source::QuerySource;
4+
use crate::query_source::private::PlainQuerySource;
5+
use crate::query_source::{QueryRelation, QuerySource, TableNotEqual};
56
use crate::result::QueryResult;
67
use crate::sql_types::{Double, SmallInt};
78
use crate::{JoinTo, SelectableExpression, Table};
@@ -69,6 +70,26 @@ where
6970
}
7071
}
7172

73+
#[diagnostic::do_not_recommend]
74+
impl<T1, T2, TSM> TableNotEqual<T1> for Tablesample<T2, TSM>
75+
where
76+
T1: QueryRelation,
77+
T2: TableNotEqual<T1>,
78+
TSM: TablesampleMethod,
79+
Self: Table,
80+
{
81+
}
82+
83+
#[diagnostic::do_not_recommend]
84+
impl<T1, T2, TSM> TableNotEqual<Tablesample<T1, TSM>> for T2
85+
where
86+
T1: QueryRelation,
87+
T2: PlainQuerySource + TableNotEqual<T1>,
88+
Tablesample<T1, TSM>: Table,
89+
TSM: TablesampleMethod,
90+
{
91+
}
92+
7293
impl<S, TSM> QueryId for Tablesample<S, TSM>
7394
where
7495
S: QueryId,

diesel/src/query_source/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ pub(crate) mod private {
213213
/// A helper trait to prevent down stream crates form implementing certain traits
214214
#[doc(hidden)]
215215
pub trait Sealed {}
216+
217+
/// A helper trait to mark "plain" tables/views as created by `diesel::table!`/`diesel::view!`
218+
#[doc(hidden)]
219+
pub trait PlainQuerySource: super::QueryRelation {}
216220
}
217221

218222
#[doc(hidden)]

diesel_compile_tests/tests/fail/derive/aliases.stderr

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,13 @@ note: `posts::table` is defined in module `crate::posts` of the current crate
1212
|
1313
LL | / table! {
1414
LL | | posts {
15-
LL | | id -> Integer,
16-
LL | | author -> Integer,
17-
... |
18-
LL | | }
19-
| |_^
15+
| |_________^
2016
note: `users::table` is defined in module `crate::users` of the current crate
2117
--> tests/fail/derive/aliases.rs:6:1
2218
|
2319
LL | / table! {
2420
LL | | users {
25-
LL | | id -> Integer,
26-
LL | | name -> Text,
27-
LL | | }
28-
LL | | }
29-
| |_^
21+
| |_________^
3022
note: required by a bound in `Alias::<S>::field`
3123
--> DIESEL/diesel/diesel/src/query_source/aliasing/alias.rs
3224
|

diesel_compile_tests/tests/fail/derive/has_query.stderr

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,8 @@ error[E0277]: the trait bound `users::table: TableNotEqual<posts::table>` is not
214214
= note: double check that `posts::table` and `users::table` appear in the same `allow_tables_to_appear_in_same_query!`
215215
call if both are tables
216216
= help: the following other types implement trait `TableNotEqual<T>`:
217-
`Only<pg::metadata_lookup::pg_namespace::table>` implements `TableNotEqual<pg::metadata_lookup::pg_type::table>`
218-
`Only<pg::metadata_lookup::pg_type::table>` implements `TableNotEqual<pg::metadata_lookup::pg_namespace::table>`
219-
`Tablesample<pg::metadata_lookup::pg_namespace::table, TSM>` implements `TableNotEqual<pg::metadata_lookup::pg_type::table>`
220-
`Tablesample<pg::metadata_lookup::pg_type::table, TSM>` implements `TableNotEqual<pg::metadata_lookup::pg_namespace::table>`
221-
`pg::metadata_lookup::pg_namespace::table` implements `TableNotEqual<Only<pg::metadata_lookup::pg_type::table>>`
222-
`pg::metadata_lookup::pg_namespace::table` implements `TableNotEqual<Tablesample<pg::metadata_lookup::pg_type::table, TSM>>`
223217
`pg::metadata_lookup::pg_namespace::table` implements `TableNotEqual<pg::metadata_lookup::pg_type::table>`
224-
`pg::metadata_lookup::pg_type::table` implements `TableNotEqual<Only<pg::metadata_lookup::pg_namespace::table>>`
225-
and N others
218+
`pg::metadata_lookup::pg_type::table` implements `TableNotEqual<pg::metadata_lookup::pg_namespace::table>`
226219
= note: required for `users::table` to implement `AppearsInFromClause<posts::table>`
227220
note: required for `posts::columns::id` to implement `AppearsOnTable<users::table>`
228221
--> tests/fail/derive/has_query.rs:12:9
@@ -287,15 +280,8 @@ error[E0277]: the trait bound `users::table: TableNotEqual<posts::table>` is not
287280
= note: double check that `posts::table` and `users::table` appear in the same `allow_tables_to_appear_in_same_query!`
288281
call if both are tables
289282
= help: the following other types implement trait `TableNotEqual<T>`:
290-
`Only<pg::metadata_lookup::pg_namespace::table>` implements `TableNotEqual<pg::metadata_lookup::pg_type::table>`
291-
`Only<pg::metadata_lookup::pg_type::table>` implements `TableNotEqual<pg::metadata_lookup::pg_namespace::table>`
292-
`Tablesample<pg::metadata_lookup::pg_namespace::table, TSM>` implements `TableNotEqual<pg::metadata_lookup::pg_type::table>`
293-
`Tablesample<pg::metadata_lookup::pg_type::table, TSM>` implements `TableNotEqual<pg::metadata_lookup::pg_namespace::table>`
294-
`pg::metadata_lookup::pg_namespace::table` implements `TableNotEqual<Only<pg::metadata_lookup::pg_type::table>>`
295-
`pg::metadata_lookup::pg_namespace::table` implements `TableNotEqual<Tablesample<pg::metadata_lookup::pg_type::table, TSM>>`
296283
`pg::metadata_lookup::pg_namespace::table` implements `TableNotEqual<pg::metadata_lookup::pg_type::table>`
297-
`pg::metadata_lookup::pg_type::table` implements `TableNotEqual<Only<pg::metadata_lookup::pg_namespace::table>>`
298-
and N others
284+
`pg::metadata_lookup::pg_type::table` implements `TableNotEqual<pg::metadata_lookup::pg_namespace::table>`
299285
= note: required for `users::table` to implement `AppearsInFromClause<posts::table>`
300286
note: required for `posts::columns::id` to implement `AppearsOnTable<users::table>`
301287
--> tests/fail/derive/has_query.rs:12:9

diesel_compile_tests/tests/fail/insert_cannot_reference_columns_from_other_table.stderr

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ error[E0271]: type mismatch resolving `<id as Column>::Table == table`
77
| required by a bound introduced by this call
88
|
99
note: expected this to be `users::table`
10-
--> tests/fail/insert_cannot_reference_columns_from_other_table.rs:14:9
11-
|
12-
LL | id -> Integer,
13-
| ^^
14-
= note: `posts::table` and `users::table` have similar names, but are actually distinct types
15-
note: `posts::table` is defined in module `crate::posts` of the current crate
1610
--> tests/fail/insert_cannot_reference_columns_from_other_table.rs:12:1
1711
|
1812
LL | / table! {
@@ -21,15 +15,19 @@ note: `posts::table` is defined in module `crate::posts` of the current crate
2115
LL | | }
2216
LL | | }
2317
| |_^
18+
= note: `posts::table` and `users::table` have similar names, but are actually distinct types
19+
note: `posts::table` is defined in module `crate::posts` of the current crate
20+
--> tests/fail/insert_cannot_reference_columns_from_other_table.rs:12:1
21+
|
22+
LL | / table! {
23+
LL | | posts {
24+
| |_________^
2425
note: `users::table` is defined in module `crate::users` of the current crate
2526
--> tests/fail/insert_cannot_reference_columns_from_other_table.rs:6:1
2627
|
2728
LL | / table! {
2829
LL | | users {
29-
LL | | id -> Integer,
30-
LL | | }
31-
LL | | }
32-
| |_^
30+
| |_________^
3331
= note: required for `&Eq<id, Bound<Integer, i32>>` to implement `diesel::Insertable<users::table>`
3432
= note: 1 redundant requirement hidden
3533
= note: required for `&Grouped<Eq<id, Bound<Integer, i32>>>` to implement `diesel::Insertable<users::table>`
@@ -56,19 +54,13 @@ note: `posts::table` is defined in module `crate::posts` of the current crate
5654
|
5755
LL | / table! {
5856
LL | | posts {
59-
LL | | id -> Integer,
60-
LL | | }
61-
LL | | }
62-
| |_^
57+
| |_________^
6358
note: `users::table` is defined in module `crate::users` of the current crate
6459
--> tests/fail/insert_cannot_reference_columns_from_other_table.rs:6:1
6560
|
6661
LL | / table! {
6762
LL | | users {
68-
LL | | id -> Integer,
69-
LL | | }
70-
LL | | }
71-
| |_^
63+
| |_________^
7264
= note: required for `(&Grouped<Eq<id, Bound<Integer, i32>>>, &Grouped<Eq<id, ...>>)` to implement `diesel::Insertable<users::table>`
7365

7466
= note: this error originates in the macro `table` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -87,19 +79,13 @@ note: `posts::table` is defined in module `crate::posts` of the current crate
8779
|
8880
LL | / table! {
8981
LL | | posts {
90-
LL | | id -> Integer,
91-
LL | | }
92-
LL | | }
93-
| |_^
82+
| |_________^
9483
note: `users::table` is defined in module `crate::users` of the current crate
9584
--> tests/fail/insert_cannot_reference_columns_from_other_table.rs:6:1
9685
|
9786
LL | / table! {
9887
LL | | users {
99-
LL | | id -> Integer,
100-
LL | | }
101-
LL | | }
102-
| |_^
88+
| |_________^
10389
= note: required for `(&Grouped<Eq<id, Bound<Integer, i32>>>, &Grouped<Eq<id, ...>>)` to implement `diesel::Insertable<users::table>`
10490
= note: 1 redundant requirement hidden
10591
= note: required for `&(Grouped<Eq<id, Bound<Integer, i32>>>, Grouped<Eq<id, ...>>)` to implement `diesel::Insertable<users::table>`
@@ -123,12 +109,6 @@ error[E0271]: type mismatch resolving `<id as Column>::Table == table`
123109
| required by a bound introduced by this call
124110
|
125111
note: expected this to be `users::table`
126-
--> tests/fail/insert_cannot_reference_columns_from_other_table.rs:14:9
127-
|
128-
LL | id -> Integer,
129-
| ^^
130-
= note: `posts::table` and `users::table` have similar names, but are actually distinct types
131-
note: `posts::table` is defined in module `crate::posts` of the current crate
132112
--> tests/fail/insert_cannot_reference_columns_from_other_table.rs:12:1
133113
|
134114
LL | / table! {
@@ -137,15 +117,19 @@ note: `posts::table` is defined in module `crate::posts` of the current crate
137117
LL | | }
138118
LL | | }
139119
| |_^
120+
= note: `posts::table` and `users::table` have similar names, but are actually distinct types
121+
note: `posts::table` is defined in module `crate::posts` of the current crate
122+
--> tests/fail/insert_cannot_reference_columns_from_other_table.rs:12:1
123+
|
124+
LL | / table! {
125+
LL | | posts {
126+
| |_________^
140127
note: `users::table` is defined in module `crate::users` of the current crate
141128
--> tests/fail/insert_cannot_reference_columns_from_other_table.rs:6:1
142129
|
143130
LL | / table! {
144131
LL | | users {
145-
LL | | id -> Integer,
146-
LL | | }
147-
LL | | }
148-
| |_^
132+
| |_________^
149133
= note: required for `&Eq<id, Bound<Integer, i32>>` to implement `diesel::Insertable<users::table>`
150134
= note: 3 redundant requirements hidden
151135
= note: required for `&(Grouped<Eq<id, Bound<Integer, i32>>>, Grouped<Eq<id, ...>>)` to implement `diesel::Insertable<users::table>`

0 commit comments

Comments
 (0)