Skip to content

Commit

Permalink
Fix #3748
Browse files Browse the repository at this point in the history
We now also generate the necessary `FromSql`/`ToSql` impls for
`chrono`/`time` types for `Time`/`Date`/`Timestamp`. This follows the
same reasoning as ##3747.
  • Loading branch information
weiznich committed Aug 18, 2023
1 parent 02a37df commit 1d66cdf
Show file tree
Hide file tree
Showing 6 changed files with 398 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ jobs:
- name: Test diesel-derives (nightly)
if: matrix.rust == 'nightly'
shell: bash
run: cargo +${{ matrix.rust }} test --manifest-path diesel_derives/Cargo.toml --no-default-features --features "diesel/${{ matrix.backend }} diesel/unstable"
run: cargo +${{ matrix.rust }} test --manifest-path diesel_derives/Cargo.toml --no-default-features --features "diesel/${{ matrix.backend }} diesel/unstable diesel/time time diesel/chrono chrono"

- name: Test diesel-derives
shell: bash
Expand Down
2 changes: 2 additions & 0 deletions diesel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ returning_clauses_for_sqlite_3_35 = []
i-implement-a-third-party-backend-and-opt-into-breaking-changes = []
nightly-error-messages = []
r2d2 = ["diesel_derives/r2d2", "dep:r2d2"]
chrono = ["diesel_derives/chrono", "dep:chrono"]
time = ["diesel_derives/time", "dep:time"]

[package.metadata.docs.rs]
features = ["postgres", "mysql", "sqlite", "extras"]
Expand Down
8 changes: 8 additions & 0 deletions diesel/src/internal/derives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,12 @@ pub mod multiconnection {

#[doc(hidden)]
pub use crate::query_builder::select_statement::SelectStatementAccessor;

#[doc(hidden)]
#[cfg(feature = "chrono")]
pub use chrono;

#[doc(hidden)]
#[cfg(feature = "time")]
pub use time;
}
2 changes: 2 additions & 0 deletions diesel_derives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ mysql = []
without-deprecated = []
with-deprecated = []
r2d2 = []
chrono = []
time = []
72 changes: 64 additions & 8 deletions diesel_derives/src/multiconnection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ fn generate_row(connection_types: &[ConnectionVariant]) -> TokenStream {
}

fn generate_bind_collector(connection_types: &[ConnectionVariant]) -> TokenStream {
let to_sql_impls = vec![
let mut to_sql_impls = vec![
(
quote::quote!(diesel::sql_types::SmallInt),
quote::quote!(i16),
Expand All @@ -583,11 +583,40 @@ fn generate_bind_collector(connection_types: &[ConnectionVariant]) -> TokenStrea
quote::quote!([u8]),
),
(quote::quote!(diesel::sql_types::Bool), quote::quote!(bool)),
]
.into_iter()
.map(|t| generate_to_sql_impls(t, connection_types));
];
if cfg!(feature = "chrono") {
to_sql_impls.push((
quote::quote!(diesel::sql_types::Timestamp),
quote::quote!(diesel::internal::derives::multiconnection::chrono::NaiveDateTime),
));
to_sql_impls.push((
quote::quote!(diesel::sql_types::Date),
quote::quote!(diesel::internal::derives::multiconnection::chrono::NaiveDate),
));
to_sql_impls.push((
quote::quote!(diesel::sql_types::Time),
quote::quote!(diesel::internal::derives::multiconnection::chrono::NaiveTime),
));
}
if cfg!(feature = "time") {
to_sql_impls.push((
quote::quote!(diesel::sql_types::Timestamp),
quote::quote!(diesel::internal::derives::multiconnection::time::PrimitiveDateTime),
));
to_sql_impls.push((
quote::quote!(diesel::sql_types::Time),
quote::quote!(diesel::internal::derives::multiconnection::time::Time),
));
to_sql_impls.push((
quote::quote!(diesel::sql_types::Date),
quote::quote!(diesel::internal::derives::multiconnection::time::Date),
));
}
let to_sql_impls = to_sql_impls
.into_iter()
.map(|t| generate_to_sql_impls(t, connection_types));

let from_sql_impls = vec![
let mut from_sql_impls = vec![
(
quote::quote!(diesel::sql_types::SmallInt),
quote::quote!(i16),
Expand All @@ -608,9 +637,36 @@ fn generate_bind_collector(connection_types: &[ConnectionVariant]) -> TokenStrea
quote::quote!(Vec<u8>),
),
(quote::quote!(diesel::sql_types::Bool), quote::quote!(bool)),
]
.into_iter()
.map(generate_from_sql_impls);
];
if cfg!(feature = "chrono") {
from_sql_impls.push((
quote::quote!(diesel::sql_types::Timestamp),
quote::quote!(diesel::internal::derives::multiconnection::chrono::NaiveDateTime),
));
from_sql_impls.push((
quote::quote!(diesel::sql_types::Date),
quote::quote!(diesel::internal::derives::multiconnection::chrono::NaiveDate),
));
from_sql_impls.push((
quote::quote!(diesel::sql_types::Time),
quote::quote!(diesel::internal::derives::multiconnection::chrono::NaiveTime),
));
}
if cfg!(feature = "time") {
from_sql_impls.push((
quote::quote!(diesel::sql_types::Timestamp),
quote::quote!(diesel::internal::derives::multiconnection::time::PrimitiveDateTime),
));
from_sql_impls.push((
quote::quote!(diesel::sql_types::Time),
quote::quote!(diesel::internal::derives::multiconnection::time::Time),
));
from_sql_impls.push((
quote::quote!(diesel::sql_types::Date),
quote::quote!(diesel::internal::derives::multiconnection::time::Date),
));
}
let from_sql_impls = from_sql_impls.into_iter().map(generate_from_sql_impls);

let into_bind_value_bounds = connection_types.iter().map(|c| {
let ty = c.ty;
Expand Down
Loading

0 comments on commit 1d66cdf

Please sign in to comment.