Skip to content

Commit afb8129

Browse files
authored
Merge pull request diesel-rs#1082 from Eijebong/decimal_mysql
Add support for the Datetime type for MySQL
2 parents b97636f + aa0d0e1 commit afb8129

4 files changed

Lines changed: 42 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
2222

2323
* Added support for the [Range][range-0.16.0] type on postgreSQL.
2424

25+
* Added support for the Datetime type on MySQL.
26+
2527
* `infer_schema!` will now automatically detect which tables can be joined based
2628
on the presence of foreign key constraints.
2729

diesel/src/mysql/types/date_and_time.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::os::raw as libc;
88
use std::{ptr, mem, slice};
99

1010
use mysql::Mysql;
11-
use types::{ToSql, ToSqlOutput, FromSql, IsNull, Timestamp, Time, Date};
11+
use types::{ToSql, ToSqlOutput, FromSql, IsNull, Timestamp, Time, Date, Datetime};
1212

1313
macro_rules! mysql_time_impls {
1414
($ty:ty) => {
@@ -41,10 +41,24 @@ macro_rules! mysql_time_impls {
4141
}
4242
}
4343

44+
mysql_time_impls!(Datetime);
45+
primitive_impls!(Datetime -> NaiveDateTime);
4446
mysql_time_impls!(Timestamp);
4547
mysql_time_impls!(Time);
4648
mysql_time_impls!(Date);
4749

50+
impl ToSql<Datetime, Mysql> for NaiveDateTime {
51+
fn to_sql<W: Write>(&self, out: &mut ToSqlOutput<W, Mysql>) -> Result<IsNull, Box<Error+Send+Sync>> {
52+
<NaiveDateTime as ToSql<Timestamp, Mysql>>::to_sql(self, out)
53+
}
54+
}
55+
56+
impl FromSql<Datetime, Mysql> for NaiveDateTime {
57+
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error+Send+Sync>> {
58+
<NaiveDateTime as FromSql<Timestamp, Mysql>>::from_sql(bytes)
59+
}
60+
}
61+
4862
impl ToSql<Timestamp, Mysql> for NaiveDateTime {
4963
fn to_sql<W: Write>(&self, out: &mut ToSqlOutput<W, Mysql>) -> Result<IsNull, Box<Error+Send+Sync>> {
5064
let mut mysql_time: ffi::MYSQL_TIME = unsafe { mem::zeroed() };
@@ -134,7 +148,7 @@ mod tests {
134148
use expression::dsl::{sql, now};
135149
use prelude::*;
136150
use select;
137-
use types::{Date, Time, Timestamp};
151+
use types::{Date, Time, Timestamp, Datetime};
138152

139153
fn connection() -> MysqlConnection {
140154
dotenv().ok();
@@ -152,6 +166,8 @@ mod tests {
152166
let time = NaiveDate::from_ymd(1970, 1, 1).and_hms(0, 0, 0);
153167
let query = select(sql::<Timestamp>("CAST('1970-01-01' AS DATETIME)").eq(time));
154168
assert!(query.get_result::<bool>(&connection).unwrap());
169+
let query = select(sql::<Datetime>("CAST('1970-01-01' AS DATETIME)").eq(time));
170+
assert!(query.get_result::<bool>(&connection).unwrap());
155171
}
156172

157173
#[test]
@@ -161,6 +177,9 @@ mod tests {
161177
let epoch_from_sql = select(sql::<Timestamp>("CAST('1970-01-01' AS DATETIME)"))
162178
.get_result(&connection);
163179
assert_eq!(Ok(time), epoch_from_sql);
180+
let epoch_from_sql = select(sql::<Datetime>("CAST('1970-01-01' AS DATETIME)"))
181+
.get_result(&connection);
182+
assert_eq!(Ok(time), epoch_from_sql);
164183
}
165184

166185
#[test]

diesel/src/mysql/types/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ impl HasSqlType<::types::Timestamp> for Mysql {
6262
}
6363
}
6464

65+
impl HasSqlType<Datetime> for Mysql {
66+
fn metadata(_: &()) -> MysqlType {
67+
MysqlType::DateTime
68+
}
69+
}
70+
6571
impl HasSqlType<::types::Numeric> for Mysql {
6672
fn metadata(_: &()) -> MysqlType {
6773
MysqlType::String
@@ -76,3 +82,15 @@ impl QueryId for ::types::Numeric {
7682
true
7783
}
7884
}
85+
86+
/// Represents the MySQL datetime type.
87+
/// ### [`ToSql`](/diesel/types/trait.ToSql.html) impls
88+
///
89+
/// - [`chrono::NaiveDateTime`][NaiveDateTime] with `feature = "chrono"`
90+
///
91+
/// ### [`FromSql`](/diesel/types/trait.FromSql.html) impls
92+
///
93+
/// - [`chrono::NaiveDateTime`][NaiveDateTime] with `feature = "chrono"`
94+
///
95+
/// [NaiveDateTime]: https://lifthrasiir.github.io/rust-chrono/chrono/naive/datetime/struct.NaiveDateTime.html
96+
#[derive(Debug, Clone, Copy, Default)] pub struct Datetime;

diesel_tests/tests/types_roundtrip.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ mod mysql_types {
193193

194194
test_round_trip!(i8_roundtrips, Tinyint, i8);
195195
test_round_trip!(naive_datetime_roundtrips, Timestamp, (i64, u32), mk_naive_datetime);
196+
test_round_trip!(naive_datetime_roundtrips_to_datetime, Datetime, (i64, u32), mk_naive_datetime);
196197
test_round_trip!(naive_time_roundtrips, Time, (u32, u32), mk_naive_time);
197198
test_round_trip!(naive_date_roundtrips, Date, u32, mk_naive_date);
198199
test_round_trip!(bigdecimal_roundtrips, Numeric, (i64, u64), mk_bigdecimal);

0 commit comments

Comments
 (0)