Skip to content

Commit b757cec

Browse files
authored
Merge pull request diesel-rs#542 from diesel-rs/sg-debug-datetimes
Add `ToSql<.., Debug>` for various date/time types
2 parents 1601be5 + e7dab24 commit b757cec

5 files changed

Lines changed: 43 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
1414
* `#[derive(AsChangeset)]` no longer assumes that `use diesel::prelude::*` has
1515
been done.
1616

17+
* `debug_sql!` can now properly be used with types from `chrono` or
18+
`std::time`.
19+
1720
## [0.9.0] - 2016-12-08
1821

1922
### Added

diesel/src/pg/types/date_and_time/chrono.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ impl ToSql<Timestamptz, Pg> for NaiveDateTime {
7272
}
7373
}
7474

75+
debug_to_sql!(Timestamptz, NaiveDateTime);
76+
7577
impl FromSql<Timestamptz, Pg> for DateTime<UTC> {
7678
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error+Send+Sync>> {
7779
let naive_date_time = try!(<NaiveDateTime as FromSql<Timestamptz, Pg>>::from_sql(bytes));
@@ -85,6 +87,12 @@ impl<TZ: TimeZone> ToSql<Timestamptz, Pg> for DateTime<TZ> {
8587
}
8688
}
8789

90+
impl<TZ: TimeZone> ToSql<Timestamptz, ::backend::Debug> for DateTime<TZ> {
91+
fn to_sql<W: Write>(&self, _: &mut W) -> Result<IsNull, Box<Error+Send+Sync>> {
92+
Ok(IsNull::No)
93+
}
94+
}
95+
8896
fn midnight() -> NaiveTime {
8997
NaiveTime::from_hms(0, 0, 0)
9098
}

diesel/src/pg/types/date_and_time/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ impl ToSql<types::Timestamp, Pg> for PgTimestamp {
111111
}
112112
}
113113

114+
debug_to_sql!(types::Timestamp, PgTimestamp);
115+
114116
impl FromSql<types::Timestamp, Pg> for PgTimestamp {
115117
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error+Send+Sync>> {
116118
FromSql::<types::BigInt, Pg>::from_sql(bytes)
@@ -124,6 +126,8 @@ impl ToSql<types::Timestamptz, Pg> for PgTimestamp {
124126
}
125127
}
126128

129+
debug_to_sql!(types::Timestamptz, PgTimestamp);
130+
127131
impl FromSql<types::Timestamptz, Pg> for PgTimestamp {
128132
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error+Send+Sync>> {
129133
FromSql::<types::Timestamp, Pg>::from_sql(bytes)
@@ -136,6 +140,8 @@ impl ToSql<types::Date, Pg> for PgDate {
136140
}
137141
}
138142

143+
debug_to_sql!(types::Date, PgDate);
144+
139145
impl FromSql<types::Date, Pg> for PgDate {
140146
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error+Send+Sync>> {
141147
FromSql::<types::Integer, Pg>::from_sql(bytes)
@@ -149,6 +155,8 @@ impl ToSql<types::Time, Pg> for PgTime {
149155
}
150156
}
151157

158+
debug_to_sql!(types::Time, PgTime);
159+
152160
impl FromSql<types::Time, Pg> for PgTime {
153161
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error+Send+Sync>> {
154162
FromSql::<types::BigInt, Pg>::from_sql(bytes)
@@ -165,6 +173,8 @@ impl ToSql<types::Interval, Pg> for PgInterval {
165173
}
166174
}
167175

176+
debug_to_sql!(types::Interval, PgInterval);
177+
168178
impl FromSql<types::Interval, Pg> for PgInterval {
169179
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error+Send+Sync>> {
170180
let bytes = not_none!(bytes);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
debug_to_sql!(::types::Timestamp, ::std::time::SystemTime);
2+
3+
#[cfg(feature="chrono")]
4+
mod chrono {
5+
extern crate chrono;
6+
use self::chrono::{NaiveDateTime, NaiveDate, NaiveTime};
7+
8+
debug_to_sql!(::types::Timestamp, NaiveDateTime);
9+
debug_to_sql!(::types::Time, NaiveTime);
10+
debug_to_sql!(::types::Date, NaiveDate);
11+
}

diesel/src/types/impls/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,17 @@ macro_rules! primitive_impls {
143143
}
144144
}
145145

146+
macro_rules! debug_to_sql {
147+
($sql_type:ty, $ty:ty) => {
148+
impl $crate::types::ToSql<$sql_type, $crate::backend::Debug> for $ty {
149+
fn to_sql<W: ::std::io::Write>(&self, _: &mut W) -> Result<$crate::types::IsNull, Box<::std::error::Error+Send+Sync>> {
150+
Ok($crate::types::IsNull::No)
151+
}
152+
}
153+
};
154+
}
155+
156+
mod date_and_time;
146157
pub mod floats;
147158
mod integers;
148159
pub mod option;

0 commit comments

Comments
 (0)