Merge pull request #434 from gwenn/time

Fix Timespec FromSql implementation (#431)
This commit is contained in:
gwenn 2018-11-22 17:18:54 +01:00 committed by GitHub
commit a6cd3ac1aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 7 deletions

View File

@ -800,7 +800,7 @@ impl InnerConnection {
#[cfg(feature = "hooks")] #[cfg(feature = "hooks")]
fn new(db: *mut ffi::sqlite3) -> InnerConnection { fn new(db: *mut ffi::sqlite3) -> InnerConnection {
InnerConnection { InnerConnection {
db: db, db,
interrupt_lock: Arc::new(Mutex::new(db)), interrupt_lock: Arc::new(Mutex::new(db)),
free_commit_hook: None, free_commit_hook: None,
free_rollback_hook: None, free_rollback_hook: None,

View File

@ -132,7 +132,7 @@ mod test {
use super::chrono::{ use super::chrono::{
DateTime, Duration, Local, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc, DateTime, Duration, Local, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc,
}; };
use {Connection, NO_PARAMS}; use {Connection, Result, NO_PARAMS};
fn checked_memory_handle() -> Connection { fn checked_memory_handle() -> Connection {
let db = Connection::open_in_memory().unwrap(); let db = Connection::open_in_memory().unwrap();
@ -263,4 +263,21 @@ mod test {
.unwrap(); .unwrap();
assert_eq!(local, v); assert_eq!(local, v);
} }
#[test]
fn test_sqlite_functions() {
let db = checked_memory_handle();
let result: Result<NaiveTime> =
db.query_row("SELECT CURRENT_TIME", NO_PARAMS, |r| r.get(0));
assert!(result.is_ok());
let result: Result<NaiveDate> =
db.query_row("SELECT CURRENT_DATE", NO_PARAMS, |r| r.get(0));
assert!(result.is_ok());
let result: Result<NaiveDateTime> =
db.query_row("SELECT CURRENT_TIMESTAMP", NO_PARAMS, |r| r.get(0));
assert!(result.is_ok());
let result: Result<DateTime<Utc>> =
db.query_row("SELECT CURRENT_TIMESTAMP", NO_PARAMS, |r| r.get(0));
assert!(result.is_ok());
}
} }

View File

@ -3,6 +3,7 @@ extern crate time;
use types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef}; use types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef};
use Result; use Result;
const CURRENT_TIMESTAMP_FMT: &str = "%Y-%m-%d %H:%M:%S";
const SQLITE_DATETIME_FMT: &str = "%Y-%m-%dT%H:%M:%S.%fZ"; const SQLITE_DATETIME_FMT: &str = "%Y-%m-%dT%H:%M:%S.%fZ";
const SQLITE_DATETIME_FMT_LEGACY: &str = "%Y-%m-%d %H:%M:%S:%f %Z"; const SQLITE_DATETIME_FMT_LEGACY: &str = "%Y-%m-%d %H:%M:%S:%f %Z";
@ -21,10 +22,13 @@ impl FromSql for time::Timespec {
value value
.as_str() .as_str()
.and_then(|s| { .and_then(|s| {
time::strptime(s, SQLITE_DATETIME_FMT).or_else(|err| { match s.len() {
time::strptime(s, SQLITE_DATETIME_FMT_LEGACY) 19 => time::strptime(s, CURRENT_TIMESTAMP_FMT),
.or_else(|_| Err(FromSqlError::Other(Box::new(err)))) _ => time::strptime(s, SQLITE_DATETIME_FMT).or_else(|err| {
}) time::strptime(s, SQLITE_DATETIME_FMT_LEGACY).or_else(|_| Err(err))
}),
}
.or_else(|err| Err(FromSqlError::Other(Box::new(err))))
}) })
.map(|tm| tm.to_timespec()) .map(|tm| tm.to_timespec())
} }
@ -33,7 +37,7 @@ impl FromSql for time::Timespec {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::time; use super::time;
use {Connection, NO_PARAMS}; use {Connection, Result, NO_PARAMS};
fn checked_memory_handle() -> Connection { fn checked_memory_handle() -> Connection {
let db = Connection::open_in_memory().unwrap(); let db = Connection::open_in_memory().unwrap();
@ -67,4 +71,12 @@ mod test {
assert_eq!(from, ts); assert_eq!(from, ts);
} }
} }
#[test]
fn test_sqlite_functions() {
let db = checked_memory_handle();
let result: Result<time::Timespec> =
db.query_row("SELECT CURRENT_TIMESTAMP", NO_PARAMS, |r| r.get(0));
assert!(result.is_ok());
}
} }