Skip to content

Commit 14ac1ec

Browse files
authored
Merge pull request #4011 from Ten0/auto_type_bare_select_and_into_sql
Add support of `dsl::select()` and `.into_sql()` to auto_type
2 parents 5d23a8a + 292ca02 commit 14ac1ec

File tree

9 files changed

+47
-31
lines changed

9 files changed

+47
-31
lines changed

diesel/src/expression/helper_types.rs

+3
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ pub type Otherwise<W, E> = expression::case_when::CaseWhen<
157157
/// Represents the return type of [`.as_select()`](crate::prelude::SelectableHelper::as_select)
158158
pub type AsSelect<Source, DB> = SelectBy<Source, DB>;
159159

160+
/// Represents the return type of [`.into_sql()`](crate::expression::IntoSql::into_sql)
161+
pub type IntoSql<Item, SqlType> = AsExprOf<Item, SqlType>;
162+
160163
/// The return type of [`alias.field(field)`](crate::query_source::Alias::field)
161164
pub type Field<Alias, Field> = Fields<Alias, Field>;
162165

diesel/src/expression/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub(crate) mod dsl {
6565
#[doc(inline)]
6666
pub use super::functions::date_and_time::*;
6767
#[doc(inline)]
68-
pub use super::helper_types::{case_when, Otherwise, When};
68+
pub use super::helper_types::{case_when, IntoSql, Otherwise, When};
6969
#[doc(inline)]
7070
pub use super::not::not;
7171
#[doc(inline)]

diesel/src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,14 @@ pub mod helper_types {
364364
pub type Select<Source, Selection> = <Source as SelectDsl<Selection>>::Output;
365365

366366
/// Represents the return type of [`diesel::select(selection)`](crate::select)
367+
#[allow(non_camel_case_types)] // required for `#[auto_type]`
368+
pub type select<Selection> = crate::query_builder::SelectStatement<
369+
crate::query_builder::NoFromClause,
370+
SelectClause<Selection>,
371+
>;
372+
373+
#[doc(hidden)]
374+
#[deprecated(note = "Use `select` instead")]
367375
pub type BareSelect<Selection> = crate::query_builder::SelectStatement<
368376
crate::query_builder::NoFromClause,
369377
SelectClause<Selection>,
@@ -680,6 +688,11 @@ pub mod prelude {
680688
pub use crate::expression::{
681689
AppearsOnTable, BoxableExpression, Expression, IntoSql, Selectable, SelectableExpression,
682690
};
691+
// If [`IntoSql`](crate::expression::helper_types::IntoSql) the type gets imported at the
692+
// same time as IntoSql the trait (this one) gets imported via the prelude, then
693+
// methods of the trait won't be resolved because the type may take priority over the trait.
694+
// That issue can be avoided by also importing it anonymously:
695+
pub use crate::expression::IntoSql as _;
683696

684697
#[doc(inline)]
685698
pub use crate::expression::functions::define_sql_function;

diesel/src/query_builder/functions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,10 @@ pub fn insert_or_ignore_into<T: Table>(target: T) -> IncompleteInsertOrIgnoreSta
460460
/// Creates a bare select statement, with no from clause. Primarily used for
461461
/// testing diesel itself, but likely useful for third party crates as well. The
462462
/// given expressions must be selectable from anywhere.
463-
pub fn select<T>(expression: T) -> crate::dsl::BareSelect<T>
463+
pub fn select<T>(expression: T) -> crate::dsl::select<T>
464464
where
465465
T: Expression,
466-
crate::dsl::BareSelect<T>: AsQuery,
466+
crate::dsl::select<T>: AsQuery,
467467
{
468468
SelectStatement::new(
469469
SelectClause(expression),

diesel_compile_tests/tests/fail/array_expressions_must_be_correct_type.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ error[E0277]: Cannot select `f64` from `NoFromClause`
2626
note: required by a bound in `diesel::select`
2727
--> $DIESEL/src/query_builder/functions.rs
2828
|
29-
| pub fn select<T>(expression: T) -> crate::dsl::BareSelect<T>
29+
| pub fn select<T>(expression: T) -> crate::dsl::select<T>
3030
| ------ required by a bound in this function
3131
...
32-
| crate::dsl::BareSelect<T>: AsQuery,
33-
| ^^^^^^^ required by this bound in `select`
32+
| crate::dsl::select<T>: AsQuery,
33+
| ^^^^^^^ required by this bound in `select`
3434
= note: consider using `--verbose` to print the full type name to the console
3535
= note: consider using `--verbose` to print the full type name to the console
3636

@@ -60,11 +60,11 @@ error[E0277]: the trait bound `f64: ValidGrouping<()>` is not satisfied
6060
note: required by a bound in `diesel::select`
6161
--> $DIESEL/src/query_builder/functions.rs
6262
|
63-
| pub fn select<T>(expression: T) -> crate::dsl::BareSelect<T>
63+
| pub fn select<T>(expression: T) -> crate::dsl::select<T>
6464
| ------ required by a bound in this function
6565
...
66-
| crate::dsl::BareSelect<T>: AsQuery,
67-
| ^^^^^^^ required by this bound in `select`
66+
| crate::dsl::select<T>: AsQuery,
67+
| ^^^^^^^ required by this bound in `select`
6868
= note: consider using `--verbose` to print the full type name to the console
6969
= note: consider using `--verbose` to print the full type name to the console
7070

diesel_compile_tests/tests/fail/array_expressions_must_be_same_type.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ error[E0277]: Cannot select `f64` from `NoFromClause`
2626
note: required by a bound in `diesel::select`
2727
--> $DIESEL/src/query_builder/functions.rs
2828
|
29-
| pub fn select<T>(expression: T) -> crate::dsl::BareSelect<T>
29+
| pub fn select<T>(expression: T) -> crate::dsl::select<T>
3030
| ------ required by a bound in this function
3131
...
32-
| crate::dsl::BareSelect<T>: AsQuery,
33-
| ^^^^^^^ required by this bound in `select`
32+
| crate::dsl::select<T>: AsQuery,
33+
| ^^^^^^^ required by this bound in `select`
3434
= note: consider using `--verbose` to print the full type name to the console
3535
= note: consider using `--verbose` to print the full type name to the console
3636
= note: consider using `--verbose` to print the full type name to the console
@@ -62,11 +62,11 @@ error[E0277]: the trait bound `f64: ValidGrouping<()>` is not satisfied
6262
note: required by a bound in `diesel::select`
6363
--> $DIESEL/src/query_builder/functions.rs
6464
|
65-
| pub fn select<T>(expression: T) -> crate::dsl::BareSelect<T>
65+
| pub fn select<T>(expression: T) -> crate::dsl::select<T>
6666
| ------ required by a bound in this function
6767
...
68-
| crate::dsl::BareSelect<T>: AsQuery,
69-
| ^^^^^^^ required by this bound in `select`
68+
| crate::dsl::select<T>: AsQuery,
69+
| ^^^^^^^ required by this bound in `select`
7070
= note: consider using `--verbose` to print the full type name to the console
7171
= note: consider using `--verbose` to print the full type name to the console
7272
= note: consider using `--verbose` to print the full type name to the console
@@ -269,11 +269,11 @@ error[E0277]: Cannot select `{integer}` from `NoFromClause`
269269
note: required by a bound in `diesel::select`
270270
--> $DIESEL/src/query_builder/functions.rs
271271
|
272-
| pub fn select<T>(expression: T) -> crate::dsl::BareSelect<T>
272+
| pub fn select<T>(expression: T) -> crate::dsl::select<T>
273273
| ------ required by a bound in this function
274274
...
275-
| crate::dsl::BareSelect<T>: AsQuery,
276-
| ^^^^^^^ required by this bound in `select`
275+
| crate::dsl::select<T>: AsQuery,
276+
| ^^^^^^^ required by this bound in `select`
277277
= note: consider using `--verbose` to print the full type name to the console
278278
= note: consider using `--verbose` to print the full type name to the console
279279
= note: consider using `--verbose` to print the full type name to the console
@@ -305,11 +305,11 @@ error[E0277]: the trait bound `{integer}: ValidGrouping<()>` is not satisfied
305305
note: required by a bound in `diesel::select`
306306
--> $DIESEL/src/query_builder/functions.rs
307307
|
308-
| pub fn select<T>(expression: T) -> crate::dsl::BareSelect<T>
308+
| pub fn select<T>(expression: T) -> crate::dsl::select<T>
309309
| ------ required by a bound in this function
310310
...
311-
| crate::dsl::BareSelect<T>: AsQuery,
312-
| ^^^^^^^ required by this bound in `select`
311+
| crate::dsl::select<T>: AsQuery,
312+
| ^^^^^^^ required by this bound in `select`
313313
= note: consider using `--verbose` to print the full type name to the console
314314
= note: consider using `--verbose` to print the full type name to the console
315315
= note: consider using `--verbose` to print the full type name to the console

diesel_derives/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1778,12 +1778,11 @@ pub fn derive_multiconnection(input: TokenStream) -> TokenStream {
17781778
/// ```
17791779
/// # Limitations
17801780
///
1781-
/// While this attribute tries to support as much of diesels built-in DSL as possible it's unfortunally not
1782-
/// possible to support everything. Notable unsupported types are:
1781+
/// While this attribute tries to support as much of diesels built-in DSL as possible it's
1782+
/// unfortunately not possible to support everything. Notable unsupported types are:
17831783
///
17841784
/// * Update statements
17851785
/// * Insert from select statements
1786-
/// * Select statements without from clause
17871786
/// * Query constructed by `diesel::sql_query`
17881787
/// * Expressions using `diesel::dsl::sql`
17891788
///

diesel_derives/tests/auto_type.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use diesel::dsl::*;
33
use diesel::helper_types::*;
44
use diesel::prelude::*;
5+
use diesel::sql_types;
56

67
table! {
78
users {
@@ -172,10 +173,11 @@ fn test_insert1() -> _ {
172173
insert_into(users::table).values(users::id.eq(42_i32))
173174
}
174175

175-
/*
176-
#[auto_type]
176+
/*#[auto_type]
177177
fn test_insert2() -> _ {
178-
users::table.insert_into(users::table).into_columns(users::all_columns)
178+
users::table
179+
.insert_into(users::table)
180+
.into_columns(users::all_columns)
179181
}*/
180182

181183
#[auto_type]
@@ -188,11 +190,10 @@ fn test_insert_or_replace() -> _ {
188190
replace_into(users::table).values(users::id.eq(42_i32))
189191
}
190192

191-
/*
192193
#[auto_type]
193194
fn test_bare_select() -> _ {
194-
select(1_i32.into_sql::<Integer>())
195-
}*/
195+
select(1_i32.into_sql::<sql_types::Integer>())
196+
}
196197

197198
#[cfg(feature = "postgres")]
198199
#[auto_type]

diesel_tests/tests/types_roundtrip.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ where
2626
+ Clone
2727
+ ::std::fmt::Debug
2828
+ 'static,
29-
for<'a> dsl::BareSelect<<T as AsExpression<ST>>::Expression>:
29+
for<'a> dsl::select<<T as AsExpression<ST>>::Expression>:
3030
AsQuery + LoadQuery<'a, TestConnection, T>,
3131
{
3232
let connection = &mut connection_without_transaction();

0 commit comments

Comments
 (0)