mirror of
https://github.com/isar/rusqlite.git
synced 2026-01-25 22:22:24 +08:00
Make FromSql impl for TimeSpec support double/int.
This commit is contained in:
@@ -52,8 +52,6 @@
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
extern crate time;
|
||||
|
||||
use libc::{c_int, c_double, c_char};
|
||||
use std::ffi::CStr;
|
||||
use std::mem;
|
||||
@@ -66,11 +64,10 @@ pub use ffi::sqlite3_column_type;
|
||||
|
||||
pub use ffi::{SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, SQLITE_NULL};
|
||||
|
||||
mod time;
|
||||
#[cfg(feature = "chrono")]
|
||||
mod chrono;
|
||||
|
||||
const SQLITE_DATETIME_FMT: &'static str = "%Y-%m-%d %H:%M:%S";
|
||||
|
||||
/// A trait for types that can be converted into SQLite values.
|
||||
pub trait ToSql {
|
||||
unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int;
|
||||
@@ -156,13 +153,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> {
|
||||
unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int {
|
||||
match *self {
|
||||
@@ -265,22 +255,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> {
|
||||
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<Option<T>> {
|
||||
if sqlite3_column_type(stmt, col) == ffi::SQLITE_NULL {
|
||||
@@ -331,8 +305,9 @@ impl FromSql for Value {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
extern crate time;
|
||||
|
||||
use Connection;
|
||||
use super::time;
|
||||
use Error;
|
||||
use libc::{c_int, c_double};
|
||||
|
||||
@@ -364,20 +339,6 @@ mod test {
|
||||
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]
|
||||
fn test_option() {
|
||||
let db = checked_memory_handle();
|
||||
@@ -455,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, 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, time::Timespec>(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)
|
||||
@@ -463,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, 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, time::Timespec>(3).err().unwrap()));
|
||||
assert!(is_invalid_column_type(row.get_checked::<i32, Option<c_int>>(3).err().unwrap()));
|
||||
|
||||
// 4 is actually NULL
|
||||
|
||||
Reference in New Issue
Block a user