mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-26 11:31:37 +08:00
Merge pull request #144 from jgallagher/gwenn-chrono
From/ToSql for chrono types.
This commit is contained in:
commit
68256a54ad
@ -13,4 +13,5 @@ script:
|
|||||||
- cargo test --features load_extension
|
- cargo test --features load_extension
|
||||||
- cargo test --features trace
|
- cargo test --features trace
|
||||||
- cargo test --features functions
|
- cargo test --features functions
|
||||||
- cargo test --features "backup blob functions load_extension trace"
|
- cargo test --features chrono
|
||||||
|
- cargo test --features "backup blob chrono functions load_extension trace"
|
||||||
|
@ -24,6 +24,7 @@ time = "~0.1.0"
|
|||||||
bitflags = "~0.1"
|
bitflags = "~0.1"
|
||||||
libc = "~0.2"
|
libc = "~0.2"
|
||||||
clippy = {version = "~0.0.58", optional = true}
|
clippy = {version = "~0.0.58", optional = true}
|
||||||
|
chrono = { version = "~0.2", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tempdir = "~0.3.4"
|
tempdir = "~0.3.4"
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
# Version UPCOMING (...)
|
# Version UPCOMING (...)
|
||||||
|
|
||||||
|
* Adds support for serializing types from the `chrono` crate. Requires the `chrono` feature.
|
||||||
* Removes `load_extension` feature from `libsqlite3-sys`. `load_extension` is still available
|
* Removes `load_extension` feature from `libsqlite3-sys`. `load_extension` is still available
|
||||||
on rusqlite itself.
|
on rusqlite itself.
|
||||||
* Fixes crash on nightly Rust when using the `trace` feature.
|
* Fixes crash on nightly Rust when using the `trace` feature.
|
||||||
|
@ -16,7 +16,7 @@ build: false
|
|||||||
|
|
||||||
test_script:
|
test_script:
|
||||||
- cargo test --lib --verbose
|
- cargo test --lib --verbose
|
||||||
- cargo test --lib --features "backup blob functions load_extension trace"
|
- cargo test --lib --features "backup blob chrono functions load_extension trace"
|
||||||
|
|
||||||
cache:
|
cache:
|
||||||
- C:\Users\appveyor\.cargo
|
- C:\Users\appveyor\.cargo
|
||||||
|
239
src/types/chrono.rs
Normal file
239
src/types/chrono.rs
Normal file
@ -0,0 +1,239 @@
|
|||||||
|
//! Convert most of the [Time Strings](http://sqlite.org/lang_datefunc.html) to chrono types.
|
||||||
|
extern crate chrono;
|
||||||
|
|
||||||
|
use self::chrono::{NaiveDate, NaiveTime, NaiveDateTime, DateTime, TimeZone, UTC, Local};
|
||||||
|
use libc::c_int;
|
||||||
|
|
||||||
|
use {Error, Result};
|
||||||
|
use types::{FromSql, ToSql};
|
||||||
|
|
||||||
|
use ffi::sqlite3_stmt;
|
||||||
|
|
||||||
|
/// ISO 8601 calendar date without timezone => "YYYY-MM-DD"
|
||||||
|
impl ToSql for NaiveDate {
|
||||||
|
unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int {
|
||||||
|
let date_str = self.format("%Y-%m-%d").to_string();
|
||||||
|
date_str.bind_parameter(stmt, col)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// "YYYY-MM-DD" => ISO 8601 calendar date without timezone.
|
||||||
|
impl FromSql for NaiveDate {
|
||||||
|
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<NaiveDate> {
|
||||||
|
let s = try!(String::column_result(stmt, col));
|
||||||
|
match NaiveDate::parse_from_str(&s, "%Y-%m-%d") {
|
||||||
|
Ok(dt) => Ok(dt),
|
||||||
|
Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
|
||||||
|
String::column_has_valid_sqlite_type(stmt, col)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// ISO 8601 time without timezone => "HH:MM:SS.SSS"
|
||||||
|
impl ToSql for NaiveTime {
|
||||||
|
unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int {
|
||||||
|
let date_str = self.format("%H:%M:%S%.f").to_string();
|
||||||
|
date_str.bind_parameter(stmt, col)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// "HH:MM"/"HH:MM:SS"/"HH:MM:SS.SSS" => ISO 8601 time without timezone.
|
||||||
|
impl FromSql for NaiveTime {
|
||||||
|
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<NaiveTime> {
|
||||||
|
let s = try!(String::column_result(stmt, col));
|
||||||
|
let fmt = match s.len() {
|
||||||
|
5 => "%H:%M",
|
||||||
|
8 => "%H:%M:%S",
|
||||||
|
_ => "%H:%M:%S%.f",
|
||||||
|
};
|
||||||
|
match NaiveTime::parse_from_str(&s, fmt) {
|
||||||
|
Ok(dt) => Ok(dt),
|
||||||
|
Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
|
||||||
|
String::column_has_valid_sqlite_type(stmt, col)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// ISO 8601 combined date and time without timezone => "YYYY-MM-DD HH:MM:SS.SSS"
|
||||||
|
impl ToSql for NaiveDateTime {
|
||||||
|
unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int {
|
||||||
|
let date_str = self.format("%Y-%m-%dT%H:%M:%S%.f").to_string();
|
||||||
|
date_str.bind_parameter(stmt, col)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// "YYYY-MM-DD HH:MM:SS"/"YYYY-MM-DD HH:MM:SS.SSS" => ISO 8601 combined date and time
|
||||||
|
/// without timezone. ("YYYY-MM-DDTHH:MM:SS"/"YYYY-MM-DDTHH:MM:SS.SSS" also supported)
|
||||||
|
impl FromSql for NaiveDateTime {
|
||||||
|
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<NaiveDateTime> {
|
||||||
|
let s = try!(String::column_result(stmt, col));
|
||||||
|
|
||||||
|
let fmt = if s.len() >= 11 && s.as_bytes()[10] == b'T' {
|
||||||
|
"%Y-%m-%dT%H:%M:%S%.f"
|
||||||
|
} else {
|
||||||
|
"%Y-%m-%d %H:%M:%S%.f"
|
||||||
|
};
|
||||||
|
|
||||||
|
match NaiveDateTime::parse_from_str(&s, fmt) {
|
||||||
|
Ok(dt) => Ok(dt),
|
||||||
|
Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
|
||||||
|
String::column_has_valid_sqlite_type(stmt, col)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Date and time with time zone => UTC RFC3339 timestamp ("YYYY-MM-DDTHH:MM:SS.SSS+00:00").
|
||||||
|
impl<Tz: TimeZone> ToSql for DateTime<Tz> {
|
||||||
|
unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int {
|
||||||
|
let utc_dt = self.with_timezone(&UTC);
|
||||||
|
utc_dt.to_rfc3339().bind_parameter(stmt, col)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// RFC3339 ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM") into DateTime<UTC>.
|
||||||
|
impl FromSql for DateTime<UTC> {
|
||||||
|
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<DateTime<UTC>> {
|
||||||
|
let s = {
|
||||||
|
let mut s = try!(String::column_result(stmt, col));
|
||||||
|
if s.len() >= 11 {
|
||||||
|
let sbytes = s.as_mut_vec();
|
||||||
|
if sbytes[10] == b' ' {
|
||||||
|
sbytes[10] = b'T';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s
|
||||||
|
};
|
||||||
|
match DateTime::parse_from_rfc3339(&s) {
|
||||||
|
Ok(dt) => Ok(dt.with_timezone(&UTC)),
|
||||||
|
Err(_) => NaiveDateTime::column_result(stmt, col).map(|dt| UTC.from_utc_datetime(&dt)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
|
||||||
|
String::column_has_valid_sqlite_type(stmt, col)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// RFC3339 ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM") into DateTime<Local>.
|
||||||
|
impl FromSql for DateTime<Local> {
|
||||||
|
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<DateTime<Local>> {
|
||||||
|
let utc_dt = try!(DateTime::<UTC>::column_result(stmt, col));
|
||||||
|
Ok(utc_dt.with_timezone(&Local))
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
|
||||||
|
DateTime::<UTC>::column_has_valid_sqlite_type(stmt, col)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use Connection;
|
||||||
|
use super::chrono::{DateTime, Local, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, UTC,
|
||||||
|
Duration};
|
||||||
|
|
||||||
|
fn checked_memory_handle() -> Connection {
|
||||||
|
let db = Connection::open_in_memory().unwrap();
|
||||||
|
db.execute_batch("CREATE TABLE foo (t TEXT, i INTEGER, f FLOAT, b BLOB)").unwrap();
|
||||||
|
db
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_naive_date() {
|
||||||
|
let db = checked_memory_handle();
|
||||||
|
let date = NaiveDate::from_ymd(2016, 2, 23);
|
||||||
|
db.execute("INSERT INTO foo (t) VALUES (?)", &[&date]).unwrap();
|
||||||
|
|
||||||
|
let s: String = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap();
|
||||||
|
assert_eq!("2016-02-23", s);
|
||||||
|
let t: NaiveDate = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap();
|
||||||
|
assert_eq!(date, t);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_naive_time() {
|
||||||
|
let db = checked_memory_handle();
|
||||||
|
let time = NaiveTime::from_hms(23, 56, 4);
|
||||||
|
db.execute("INSERT INTO foo (t) VALUES (?)", &[&time]).unwrap();
|
||||||
|
|
||||||
|
let s: String = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap();
|
||||||
|
assert_eq!("23:56:04", s);
|
||||||
|
let v: NaiveTime = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap();
|
||||||
|
assert_eq!(time, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_naive_date_time() {
|
||||||
|
let db = checked_memory_handle();
|
||||||
|
let date = NaiveDate::from_ymd(2016, 2, 23);
|
||||||
|
let time = NaiveTime::from_hms(23, 56, 4);
|
||||||
|
let dt = NaiveDateTime::new(date, time);
|
||||||
|
|
||||||
|
db.execute("INSERT INTO foo (t) VALUES (?)", &[&dt]).unwrap();
|
||||||
|
|
||||||
|
let s: String = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap();
|
||||||
|
assert_eq!("2016-02-23T23:56:04", s);
|
||||||
|
let v: NaiveDateTime = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap();
|
||||||
|
assert_eq!(dt, v);
|
||||||
|
|
||||||
|
db.execute("UPDATE foo set b = datetime(t)", &[]).unwrap(); // "YYYY-MM-DD HH:MM:SS"
|
||||||
|
let hms: NaiveDateTime = db.query_row("SELECT b FROM foo", &[], |r| r.get(0)).unwrap();
|
||||||
|
assert_eq!(dt, hms);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_date_time_utc() {
|
||||||
|
let db = checked_memory_handle();
|
||||||
|
let date = NaiveDate::from_ymd(2016, 2, 23);
|
||||||
|
let time = NaiveTime::from_hms_milli(23, 56, 4, 789);
|
||||||
|
let dt = NaiveDateTime::new(date, time);
|
||||||
|
let utc = UTC.from_utc_datetime(&dt);
|
||||||
|
|
||||||
|
db.execute("INSERT INTO foo (t) VALUES (?)", &[&utc]).unwrap();
|
||||||
|
|
||||||
|
let s: String = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap();
|
||||||
|
assert_eq!("2016-02-23T23:56:04.789+00:00", s);
|
||||||
|
|
||||||
|
let v1: DateTime<UTC> = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap();
|
||||||
|
assert_eq!(utc, v1);
|
||||||
|
|
||||||
|
let v2: DateTime<UTC> = db.query_row("SELECT '2016-02-23 23:56:04.789'", &[], |r| r.get(0))
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(utc, v2);
|
||||||
|
|
||||||
|
let v3: DateTime<UTC> = db.query_row("SELECT '2016-02-23 23:56:04'", &[], |r| r.get(0))
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(utc - Duration::milliseconds(789), v3);
|
||||||
|
|
||||||
|
let v4: DateTime<UTC> =
|
||||||
|
db.query_row("SELECT '2016-02-23 23:56:04.789+00:00'", &[], |r| r.get(0)).unwrap();
|
||||||
|
assert_eq!(utc, v4);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_date_time_local() {
|
||||||
|
let db = checked_memory_handle();
|
||||||
|
let date = NaiveDate::from_ymd(2016, 2, 23);
|
||||||
|
let time = NaiveTime::from_hms_milli(23, 56, 4, 789);
|
||||||
|
let dt = NaiveDateTime::new(date, time);
|
||||||
|
let local = Local.from_local_datetime(&dt).single().unwrap();
|
||||||
|
|
||||||
|
db.execute("INSERT INTO foo (t) VALUES (?)", &[&local]).unwrap();
|
||||||
|
|
||||||
|
// Stored string should be in UTC
|
||||||
|
let s: String = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap();
|
||||||
|
assert!(s.ends_with("+00:00"));
|
||||||
|
|
||||||
|
let v: DateTime<Local> = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap();
|
||||||
|
assert_eq!(local, v);
|
||||||
|
}
|
||||||
|
}
|
@ -52,8 +52,6 @@
|
|||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
extern crate time;
|
|
||||||
|
|
||||||
use libc::{c_int, c_double, c_char};
|
use libc::{c_int, c_double, c_char};
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
@ -66,7 +64,9 @@ pub use ffi::sqlite3_column_type;
|
|||||||
|
|
||||||
pub use ffi::{SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, SQLITE_NULL};
|
pub use ffi::{SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, SQLITE_NULL};
|
||||||
|
|
||||||
const SQLITE_DATETIME_FMT: &'static str = "%Y-%m-%d %H:%M:%S";
|
mod time;
|
||||||
|
#[cfg(feature = "chrono")]
|
||||||
|
mod chrono;
|
||||||
|
|
||||||
/// A trait for types that can be converted into SQLite values.
|
/// A trait for types that can be converted into SQLite values.
|
||||||
pub trait ToSql {
|
pub trait ToSql {
|
||||||
@ -154,13 +154,6 @@ impl ToSql for Vec<u8> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToSql for time::Timespec {
|
|
||||||
unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int {
|
|
||||||
let time_str = time::at_utc(*self).strftime(SQLITE_DATETIME_FMT).unwrap().to_string();
|
|
||||||
time_str.bind_parameter(stmt, col)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: ToSql> ToSql for Option<T> {
|
impl<T: ToSql> ToSql for Option<T> {
|
||||||
unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int {
|
unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int {
|
||||||
match *self {
|
match *self {
|
||||||
@ -263,22 +256,6 @@ impl FromSql for Vec<u8> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromSql for time::Timespec {
|
|
||||||
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<time::Timespec> {
|
|
||||||
let col_str = FromSql::column_result(stmt, col);
|
|
||||||
col_str.and_then(|txt: String| {
|
|
||||||
match time::strptime(&txt, SQLITE_DATETIME_FMT) {
|
|
||||||
Ok(tm) => Ok(tm.to_timespec()),
|
|
||||||
Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
|
|
||||||
String::column_has_valid_sqlite_type(stmt, col)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: FromSql> FromSql for Option<T> {
|
impl<T: FromSql> FromSql for Option<T> {
|
||||||
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<Option<T>> {
|
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<Option<T>> {
|
||||||
if sqlite3_column_type(stmt, col) == ffi::SQLITE_NULL {
|
if sqlite3_column_type(stmt, col) == ffi::SQLITE_NULL {
|
||||||
@ -321,17 +298,14 @@ impl FromSql for Value {
|
|||||||
_ => Err(Error::InvalidColumnType),
|
_ => Err(Error::InvalidColumnType),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn column_has_valid_sqlite_type(_: *mut sqlite3_stmt, _: c_int) -> bool {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
#[cfg_attr(feature="clippy", allow(similar_names))]
|
#[cfg_attr(feature="clippy", allow(similar_names))]
|
||||||
mod test {
|
mod test {
|
||||||
|
extern crate time;
|
||||||
|
|
||||||
use Connection;
|
use Connection;
|
||||||
use super::time;
|
|
||||||
use Error;
|
use Error;
|
||||||
use libc::{c_int, c_double};
|
use libc::{c_int, c_double};
|
||||||
use std::f64::EPSILON;
|
use std::f64::EPSILON;
|
||||||
@ -364,20 +338,6 @@ mod test {
|
|||||||
assert_eq!(from, s);
|
assert_eq!(from, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_timespec() {
|
|
||||||
let db = checked_memory_handle();
|
|
||||||
|
|
||||||
let ts = time::Timespec {
|
|
||||||
sec: 10_000,
|
|
||||||
nsec: 0,
|
|
||||||
};
|
|
||||||
db.execute("INSERT INTO foo(t) VALUES (?)", &[&ts]).unwrap();
|
|
||||||
|
|
||||||
let from: time::Timespec = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap();
|
|
||||||
assert_eq!(from, ts);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_option() {
|
fn test_option() {
|
||||||
let db = checked_memory_handle();
|
let db = checked_memory_handle();
|
||||||
@ -456,7 +416,6 @@ mod test {
|
|||||||
assert!(is_invalid_column_type(row.get_checked::<i32, c_double>(2).err().unwrap()));
|
assert!(is_invalid_column_type(row.get_checked::<i32, c_double>(2).err().unwrap()));
|
||||||
assert!(is_invalid_column_type(row.get_checked::<i32, String>(2).err().unwrap()));
|
assert!(is_invalid_column_type(row.get_checked::<i32, String>(2).err().unwrap()));
|
||||||
assert!(is_invalid_column_type(row.get_checked::<i32, Vec<u8>>(2).err().unwrap()));
|
assert!(is_invalid_column_type(row.get_checked::<i32, Vec<u8>>(2).err().unwrap()));
|
||||||
assert!(is_invalid_column_type(row.get_checked::<i32, time::Timespec>(2).err().unwrap()));
|
|
||||||
assert!(is_invalid_column_type(row.get_checked::<i32, Option<c_double>>(2).err().unwrap()));
|
assert!(is_invalid_column_type(row.get_checked::<i32, Option<c_double>>(2).err().unwrap()));
|
||||||
|
|
||||||
// 3 is actually a float (c_double)
|
// 3 is actually a float (c_double)
|
||||||
@ -464,7 +423,6 @@ mod test {
|
|||||||
assert!(is_invalid_column_type(row.get_checked::<i32, i64>(3).err().unwrap()));
|
assert!(is_invalid_column_type(row.get_checked::<i32, i64>(3).err().unwrap()));
|
||||||
assert!(is_invalid_column_type(row.get_checked::<i32, String>(3).err().unwrap()));
|
assert!(is_invalid_column_type(row.get_checked::<i32, String>(3).err().unwrap()));
|
||||||
assert!(is_invalid_column_type(row.get_checked::<i32, Vec<u8>>(3).err().unwrap()));
|
assert!(is_invalid_column_type(row.get_checked::<i32, Vec<u8>>(3).err().unwrap()));
|
||||||
assert!(is_invalid_column_type(row.get_checked::<i32, time::Timespec>(3).err().unwrap()));
|
|
||||||
assert!(is_invalid_column_type(row.get_checked::<i32, Option<c_int>>(3).err().unwrap()));
|
assert!(is_invalid_column_type(row.get_checked::<i32, Option<c_int>>(3).err().unwrap()));
|
||||||
|
|
||||||
// 4 is actually NULL
|
// 4 is actually NULL
|
56
src/types/time.rs
Normal file
56
src/types/time.rs
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
extern crate time;
|
||||||
|
|
||||||
|
use libc::c_int;
|
||||||
|
use {Error, Result};
|
||||||
|
use types::{FromSql, ToSql};
|
||||||
|
|
||||||
|
use ffi::sqlite3_stmt;
|
||||||
|
|
||||||
|
const SQLITE_DATETIME_FMT: &'static str = "%Y-%m-%d %H:%M:%S";
|
||||||
|
|
||||||
|
impl ToSql for time::Timespec {
|
||||||
|
unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int {
|
||||||
|
let time_str = time::at_utc(*self).strftime(SQLITE_DATETIME_FMT).unwrap().to_string();
|
||||||
|
time_str.bind_parameter(stmt, col)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromSql for time::Timespec {
|
||||||
|
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<time::Timespec> {
|
||||||
|
let s = try!(String::column_result(stmt, col));
|
||||||
|
match time::strptime(&s, SQLITE_DATETIME_FMT) {
|
||||||
|
Ok(tm) => Ok(tm.to_timespec()),
|
||||||
|
Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
|
||||||
|
String::column_has_valid_sqlite_type(stmt, col)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use Connection;
|
||||||
|
use super::time;
|
||||||
|
|
||||||
|
fn checked_memory_handle() -> Connection {
|
||||||
|
let db = Connection::open_in_memory().unwrap();
|
||||||
|
db.execute_batch("CREATE TABLE foo (t TEXT, i INTEGER, f FLOAT)").unwrap();
|
||||||
|
db
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_timespec() {
|
||||||
|
let db = checked_memory_handle();
|
||||||
|
|
||||||
|
let ts = time::Timespec {
|
||||||
|
sec: 10_000,
|
||||||
|
nsec: 0,
|
||||||
|
};
|
||||||
|
db.execute("INSERT INTO foo(t) VALUES (?)", &[&ts]).unwrap();
|
||||||
|
|
||||||
|
let from: time::Timespec = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap();
|
||||||
|
assert_eq!(from, ts);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user