mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-22 16:29:20 +08:00
Fix Timespec FromSql implementations (#431)
Make sure SQLite built-in CURRENT_TIMESTAMP output is supported.
This commit is contained in:
parent
3a178d6d17
commit
2b8eee2b83
@ -3,6 +3,7 @@ extern crate time;
|
||||
use types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef};
|
||||
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_LEGACY: &str = "%Y-%m-%d %H:%M:%S:%f %Z";
|
||||
|
||||
@ -21,10 +22,13 @@ impl FromSql for time::Timespec {
|
||||
value
|
||||
.as_str()
|
||||
.and_then(|s| {
|
||||
time::strptime(s, SQLITE_DATETIME_FMT).or_else(|err| {
|
||||
time::strptime(s, SQLITE_DATETIME_FMT_LEGACY)
|
||||
.or_else(|_| Err(FromSqlError::Other(Box::new(err))))
|
||||
})
|
||||
match s.len() {
|
||||
19 => time::strptime(s, CURRENT_TIMESTAMP_FMT),
|
||||
_ => 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())
|
||||
}
|
||||
@ -33,7 +37,7 @@ impl FromSql for time::Timespec {
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::time;
|
||||
use {Connection, NO_PARAMS};
|
||||
use {Connection, Result, NO_PARAMS};
|
||||
|
||||
fn checked_memory_handle() -> Connection {
|
||||
let db = Connection::open_in_memory().unwrap();
|
||||
@ -67,4 +71,12 @@ mod test {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user