mirror of
https://github.com/isar/rusqlite.git
synced 2025-10-12 22:42:20 +08:00
Add From/ToSql impl. for chrono types.
This commit is contained in:
267
src/types/chrono.rs
Normal file
267
src/types/chrono.rs
Normal file
@@ -0,0 +1,267 @@
|
||||
//! Convert most of the [Time Strings](http://sqlite.org/lang_datefunc.html) to chrono types.
|
||||
extern crate chrono;
|
||||
|
||||
use std::error;
|
||||
use self::chrono::{NaiveDate, NaiveTime, NaiveDateTime, DateTime, TimeZone, UTC, Local};
|
||||
use libc::c_int;
|
||||
|
||||
use {Error, Result};
|
||||
use types::{FromSql, ToSql};
|
||||
|
||||
use ffi;
|
||||
use ffi::sqlite3_stmt;
|
||||
use ffi::sqlite3_column_type;
|
||||
|
||||
const JULIAN_DAY: f64 = 2440587.5; // 1970-01-01 00:00:00 is JD 2440587.5
|
||||
const DAY_IN_SECONDS: f64 = 86400.0;
|
||||
const JULIAN_DAY_GREGORIAN: f64 = 1721424.5; // Jan 1, 1 proleptic Gregorian calendar
|
||||
|
||||
/// 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" or Julian Day => ISO 8601 calendar date without timezone.
|
||||
impl FromSql for NaiveDate {
|
||||
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<NaiveDate> {
|
||||
match sqlite3_column_type(stmt, col) {
|
||||
ffi::SQLITE_TEXT => {
|
||||
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))),
|
||||
}
|
||||
}
|
||||
ffi::SQLITE_FLOAT => {
|
||||
// if column affinity is REAL and an integer/unix timestamp is inserted => unexpected result
|
||||
let mut jd = ffi::sqlite3_column_double(stmt, col);
|
||||
jd -= JULIAN_DAY_GREGORIAN;
|
||||
if jd < i32::min_value() as f64 || jd > i32::max_value() as f64 {
|
||||
let err: Box<error::Error + Sync + Send> = "out-of-range date".into();
|
||||
return Err(Error::FromSqlConversionFailure(err));
|
||||
}
|
||||
match NaiveDate::from_num_days_from_ce_opt(jd as i32) {
|
||||
Some(dt) => Ok(dt),
|
||||
None => {
|
||||
let err: Box<error::Error + Sync + Send> = "out-of-range date".into();
|
||||
Err(Error::FromSqlConversionFailure(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => Err(Error::InvalidColumnType),
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
|
||||
let sqlite_type = sqlite3_column_type(stmt, col);
|
||||
sqlite_type == ffi::SQLITE_TEXT || sqlite_type == ffi::SQLITE_FLOAT
|
||||
}
|
||||
}
|
||||
|
||||
/// 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.3f").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> {
|
||||
match sqlite3_column_type(stmt, col) {
|
||||
ffi::SQLITE_TEXT => {
|
||||
let s = try!(String::column_result(stmt, col));
|
||||
let fmt = match s.len() {
|
||||
5 => "%H:%M",
|
||||
8 => "%H:%M:%S",
|
||||
_ => "%H:%M:%S%.3f",
|
||||
};
|
||||
match NaiveTime::parse_from_str(&s, fmt) {
|
||||
Ok(dt) => Ok(dt),
|
||||
Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))),
|
||||
}
|
||||
}
|
||||
_ => Err(Error::InvalidColumnType),
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
|
||||
sqlite3_column_type(stmt, col) == ffi::SQLITE_TEXT
|
||||
}
|
||||
}
|
||||
|
||||
/// 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-%d %H:%M:%S%.3f").to_string();
|
||||
date_str.bind_parameter(stmt, col)
|
||||
}
|
||||
}
|
||||
|
||||
/// "YYYY-MM-DD HH:MM"/"YYYY-MM-DD HH:MM:SS"/"YYYY-MM-DD HH:MM:SS.SSS"/ Julian Day / Unix Time => ISO 8601 combined date and time without timezone.
|
||||
/// ("YYYY-MM-DDTHH:MM"/"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> {
|
||||
match sqlite3_column_type(stmt, col) {
|
||||
ffi::SQLITE_TEXT => {
|
||||
let s = try!(String::column_result(stmt, col));
|
||||
let fmt = match s.len() {
|
||||
16 => {
|
||||
match s.as_bytes()[10] {
|
||||
b'T' => "%Y-%m-%dT%H:%M",
|
||||
_ => "%Y-%m-%d %H:%M",
|
||||
}
|
||||
}
|
||||
19 => {
|
||||
match s.as_bytes()[10] {
|
||||
b'T' => "%Y-%m-%dT%H:%M:%S",
|
||||
_ => "%Y-%m-%d %H:%M:%S",
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
match s.as_bytes()[10] {
|
||||
b'T' => "%Y-%m-%dT%H:%M:%S%.3f",
|
||||
_ => "%Y-%m-%d %H:%M:%S%.3f",
|
||||
}
|
||||
}
|
||||
};
|
||||
match NaiveDateTime::parse_from_str(&s, fmt) {
|
||||
Ok(dt) => Ok(dt),
|
||||
Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))),
|
||||
}
|
||||
}
|
||||
ffi::SQLITE_INTEGER => {
|
||||
match NaiveDateTime::from_timestamp_opt(ffi::sqlite3_column_int64(stmt, col), 0) {
|
||||
Some(dt) => Ok(dt),
|
||||
None => {
|
||||
let err: Box<error::Error + Sync + Send> = "out-of-range number of seconds"
|
||||
.into();
|
||||
Err(Error::FromSqlConversionFailure(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
ffi::SQLITE_FLOAT => {
|
||||
// if column affinity is REAL and an integer/unix timestamp is inserted => unexpected result
|
||||
let mut jd = ffi::sqlite3_column_double(stmt, col);
|
||||
jd -= JULIAN_DAY;
|
||||
jd *= DAY_IN_SECONDS;
|
||||
let ns = jd.fract() * 10f64.powi(9);
|
||||
match NaiveDateTime::from_timestamp_opt(jd as i64, ns as u32) {
|
||||
Some(dt) => Ok(dt),
|
||||
None => {
|
||||
let err: Box<error::Error + Sync + Send> = "out-of-range number of \
|
||||
seconds and/or invalid \
|
||||
nanosecond"
|
||||
.into();
|
||||
Err(Error::FromSqlConversionFailure(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => Err(Error::InvalidColumnType),
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
|
||||
let sqlite_type = sqlite3_column_type(stmt, col);
|
||||
sqlite_type == ffi::SQLITE_TEXT || sqlite_type == ffi::SQLITE_INTEGER ||
|
||||
sqlite_type == ffi::SQLITE_FLOAT
|
||||
}
|
||||
}
|
||||
|
||||
/// ISO 8601 date and time with time zone => "YYYY-MM-DD HH:MM:SS.SSS[+-]HH:MM"
|
||||
impl ToSql for DateTime<UTC> {
|
||||
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%.3f%:z").to_string();
|
||||
date_str.bind_parameter(stmt, col)
|
||||
}
|
||||
}
|
||||
|
||||
/// "YYYY-MM-DD HH:MM:SS.SSS[+-]HH:MM"/"YYYY-MM-DD HH:MM"/"YYYY-MM-DD HH:MM:SS"/"YYYY-MM-DD HH:MM:SS.SSS"/ Julian Day / Unix Time => ISO 8601 date and time with time zone.
|
||||
/// ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM"/"YYYY-MM-DDTHH:MM"/"YYYY-MM-DDTHH:MM:SS"/"YYYY-MM-DDTHH:MM:SS.SSS" also supported)
|
||||
/// When the timezone is not specified, UTC is used.
|
||||
impl FromSql for DateTime<UTC> {
|
||||
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<DateTime<UTC>> {
|
||||
match sqlite3_column_type(stmt, col) {
|
||||
ffi::SQLITE_TEXT => {
|
||||
let s = try!(String::column_result(stmt, col));
|
||||
if s.len() > 23 {
|
||||
let fmt = if s.as_bytes()[10] == b'T' {
|
||||
"%Y-%m-%dT%H:%M:%S%.3f%:z"
|
||||
} else {
|
||||
"%Y-%m-%d %H:%M:%S%.3f%:z"
|
||||
};
|
||||
match UTC.datetime_from_str(fmt, &s) {
|
||||
Ok(dt) => Ok(dt),
|
||||
Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))),
|
||||
}
|
||||
} else {
|
||||
NaiveDateTime::column_result(stmt, col).map(|dt| UTC.from_utc_datetime(&dt))
|
||||
}
|
||||
}
|
||||
ffi::SQLITE_INTEGER => {
|
||||
NaiveDateTime::column_result(stmt, col).map(|dt| UTC.from_utc_datetime(&dt))
|
||||
}
|
||||
ffi::SQLITE_FLOAT => {
|
||||
NaiveDateTime::column_result(stmt, col).map(|dt| UTC.from_utc_datetime(&dt))
|
||||
}
|
||||
_ => Err(Error::InvalidColumnType),
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
|
||||
NaiveDateTime::column_has_valid_sqlite_type(stmt, col)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// ISO 8601 date and time with time zone => "YYYY-MM-DD HH:MM:SS.SSS[+-]HH:MM"
|
||||
impl ToSql for DateTime<Local> {
|
||||
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%.3f%:z").to_string();
|
||||
date_str.bind_parameter(stmt, col)
|
||||
}
|
||||
}
|
||||
|
||||
/// "YYYY-MM-DD HH:MM:SS.SSS[+-]HH:MM"/"YYYY-MM-DD HH:MM"/"YYYY-MM-DD HH:MM:SS"/"YYYY-MM-DD HH:MM:SS.SSS"/ Julian Day / Unix Time => ISO 8601 date and time with time zone.
|
||||
/// ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM"/"YYYY-MM-DDTHH:MM"/"YYYY-MM-DDTHH:MM:SS"/"YYYY-MM-DDTHH:MM:SS.SSS" also supported)
|
||||
/// When the timezone is not specified, Local is used.
|
||||
impl FromSql for DateTime<Local> {
|
||||
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<DateTime<Local>> {
|
||||
match sqlite3_column_type(stmt, col) {
|
||||
ffi::SQLITE_TEXT => {
|
||||
let s = try!(String::column_result(stmt, col));
|
||||
if s.len() > 23 {
|
||||
let fmt = if s.as_bytes()[10] == b'T' {
|
||||
"%Y-%m-%dT%H:%M:%S%.3f%:z"
|
||||
} else {
|
||||
"%Y-%m-%d %H:%M:%S%.3f%:z"
|
||||
};
|
||||
match Local.datetime_from_str(fmt, &s) {
|
||||
Ok(dt) => Ok(dt),
|
||||
Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))),
|
||||
}
|
||||
} else {
|
||||
NaiveDateTime::column_result(stmt, col).map(|dt| Local.from_utc_datetime(&dt))
|
||||
}
|
||||
}
|
||||
ffi::SQLITE_INTEGER => {
|
||||
NaiveDateTime::column_result(stmt, col).map(|dt| Local.from_utc_datetime(&dt))
|
||||
}
|
||||
ffi::SQLITE_FLOAT => {
|
||||
NaiveDateTime::column_result(stmt, col).map(|dt| Local.from_utc_datetime(&dt))
|
||||
}
|
||||
_ => Err(Error::InvalidColumnType),
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
|
||||
NaiveDateTime::column_has_valid_sqlite_type(stmt, col)
|
||||
}
|
||||
}
|
||||
|
||||
// struct UnixTime(NaiveDateTime);
|
||||
// struct JulianTime(NaiveDateTime)
|
499
src/types/mod.rs
Normal file
499
src/types/mod.rs
Normal file
@@ -0,0 +1,499 @@
|
||||
//! Traits dealing with SQLite data types.
|
||||
//!
|
||||
//! SQLite uses a [dynamic type system](https://www.sqlite.org/datatype3.html). Implementations of
|
||||
//! the `ToSql` and `FromSql` traits are provided for the basic types that SQLite provides methods
|
||||
//! for:
|
||||
//!
|
||||
//! * C integers and doubles (`c_int` and `c_double`)
|
||||
//! * Strings (`String` and `&str`)
|
||||
//! * Blobs (`Vec<u8>` and `&[u8]`)
|
||||
//!
|
||||
//! Additionally, because it is such a common data type, implementations are provided for
|
||||
//! `time::Timespec` that use a string for storage (using the same format string,
|
||||
//! `"%Y-%m-%d %H:%M:%S"`, as SQLite's builtin
|
||||
//! [datetime](https://www.sqlite.org/lang_datefunc.html) function. Note that this storage
|
||||
//! truncates timespecs to the nearest second. If you want different storage for timespecs, you can
|
||||
//! use a newtype. For example, to store timespecs as doubles:
|
||||
//!
|
||||
//! `ToSql` and `FromSql` are also implemented for `Option<T>` where `T` implements `ToSql` or
|
||||
//! `FromSql` for the cases where you want to know if a value was NULL (which gets translated to
|
||||
//! `None`). If you get a value that was NULL in SQLite but you store it into a non-`Option` value
|
||||
//! in Rust, you will get a "sensible" zero value - 0 for numeric types (including timespecs), an
|
||||
//! empty string, or an empty vector of bytes.
|
||||
//!
|
||||
//! ```rust,ignore
|
||||
//! extern crate rusqlite;
|
||||
//! extern crate libc;
|
||||
//!
|
||||
//! use rusqlite::types::{FromSql, ToSql, sqlite3_stmt};
|
||||
//! use rusqlite::{Result};
|
||||
//! use libc::c_int;
|
||||
//! use time;
|
||||
//!
|
||||
//! pub struct TimespecSql(pub time::Timespec);
|
||||
//!
|
||||
//! impl FromSql for TimespecSql {
|
||||
//! unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int)
|
||||
//! -> Result<TimespecSql> {
|
||||
//! let as_f64_result = FromSql::column_result(stmt, col);
|
||||
//! as_f64_result.map(|as_f64: f64| {
|
||||
//! TimespecSql(time::Timespec{ sec: as_f64.trunc() as i64,
|
||||
//! nsec: (as_f64.fract() * 1.0e9) as i32 })
|
||||
//! })
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! impl ToSql for TimespecSql {
|
||||
//! unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int {
|
||||
//! let TimespecSql(ts) = *self;
|
||||
//! let as_f64 = ts.sec as f64 + (ts.nsec as f64) / 1.0e9;
|
||||
//! as_f64.bind_parameter(stmt, col)
|
||||
//! }
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
extern crate time;
|
||||
|
||||
use libc::{c_int, c_double, c_char};
|
||||
use std::ffi::CStr;
|
||||
use std::mem;
|
||||
use std::str;
|
||||
use super::ffi;
|
||||
use super::{Result, Error, str_to_cstring};
|
||||
|
||||
pub use ffi::sqlite3_stmt;
|
||||
pub use ffi::sqlite3_column_type;
|
||||
|
||||
pub use ffi::{SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, SQLITE_NULL};
|
||||
|
||||
#[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;
|
||||
}
|
||||
|
||||
/// A trait for types that can be created from a SQLite value.
|
||||
pub trait FromSql: Sized {
|
||||
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<Self>;
|
||||
|
||||
/// FromSql types can implement this method and use sqlite3_column_type to check that
|
||||
/// the type reported by SQLite matches a type suitable for Self. This method is used
|
||||
/// by `Row::get_checked` to confirm that the column contains a valid type before
|
||||
/// attempting to retrieve the value.
|
||||
unsafe fn column_has_valid_sqlite_type(_: *mut sqlite3_stmt, _: c_int) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! raw_to_impl(
|
||||
($t:ty, $f:ident) => (
|
||||
impl ToSql for $t {
|
||||
unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int {
|
||||
ffi::$f(stmt, col, *self)
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
raw_to_impl!(c_int, sqlite3_bind_int); // i32
|
||||
raw_to_impl!(i64, sqlite3_bind_int64);
|
||||
raw_to_impl!(c_double, sqlite3_bind_double);
|
||||
|
||||
impl ToSql for bool {
|
||||
unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int {
|
||||
match *self {
|
||||
true => ffi::sqlite3_bind_int(stmt, col, 1),
|
||||
_ => ffi::sqlite3_bind_int(stmt, col, 0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ToSql for &'a str {
|
||||
unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int {
|
||||
let length = self.len();
|
||||
if length > ::std::i32::MAX as usize {
|
||||
return ffi::SQLITE_TOOBIG;
|
||||
}
|
||||
match str_to_cstring(self) {
|
||||
Ok(c_str) => {
|
||||
ffi::sqlite3_bind_text(stmt,
|
||||
col,
|
||||
c_str.as_ptr(),
|
||||
length as c_int,
|
||||
ffi::SQLITE_TRANSIENT())
|
||||
}
|
||||
Err(_) => ffi::SQLITE_MISUSE,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToSql for String {
|
||||
unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int {
|
||||
(&self[..]).bind_parameter(stmt, col)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ToSql for &'a [u8] {
|
||||
unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int {
|
||||
if self.len() > ::std::i32::MAX as usize {
|
||||
return ffi::SQLITE_TOOBIG;
|
||||
}
|
||||
ffi::sqlite3_bind_blob(stmt,
|
||||
col,
|
||||
mem::transmute(self.as_ptr()),
|
||||
self.len() as c_int,
|
||||
ffi::SQLITE_TRANSIENT())
|
||||
}
|
||||
}
|
||||
|
||||
impl ToSql for Vec<u8> {
|
||||
unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int {
|
||||
(&self[..]).bind_parameter(stmt, col)
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
None => ffi::sqlite3_bind_null(stmt, col),
|
||||
Some(ref t) => t.bind_parameter(stmt, col),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Empty struct that can be used to fill in a query parameter as `NULL`.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// # extern crate libc;
|
||||
/// # extern crate rusqlite;
|
||||
/// # use rusqlite::{Connection, Result};
|
||||
/// # use rusqlite::types::{Null};
|
||||
/// # use libc::{c_int};
|
||||
/// fn main() {
|
||||
/// }
|
||||
/// fn insert_null(conn: &Connection) -> Result<c_int> {
|
||||
/// conn.execute("INSERT INTO people (name) VALUES (?)", &[&Null])
|
||||
/// }
|
||||
/// ```
|
||||
#[derive(Copy,Clone)]
|
||||
pub struct Null;
|
||||
|
||||
impl ToSql for Null {
|
||||
unsafe fn bind_parameter(&self, stmt: *mut sqlite3_stmt, col: c_int) -> c_int {
|
||||
ffi::sqlite3_bind_null(stmt, col)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! raw_from_impl(
|
||||
($t:ty, $f:ident, $c:expr) => (
|
||||
impl FromSql for $t {
|
||||
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<$t> {
|
||||
Ok(ffi::$f(stmt, col))
|
||||
}
|
||||
|
||||
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
|
||||
sqlite3_column_type(stmt, col) == $c
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
raw_from_impl!(c_int, sqlite3_column_int, ffi::SQLITE_INTEGER); // i32
|
||||
raw_from_impl!(i64, sqlite3_column_int64, ffi::SQLITE_INTEGER);
|
||||
raw_from_impl!(c_double, sqlite3_column_double, ffi::SQLITE_FLOAT); // f64
|
||||
|
||||
impl FromSql for bool {
|
||||
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<bool> {
|
||||
match ffi::sqlite3_column_int(stmt, col) {
|
||||
0 => Ok(false),
|
||||
_ => Ok(true),
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
|
||||
sqlite3_column_type(stmt, col) == ffi::SQLITE_INTEGER
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSql for String {
|
||||
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<String> {
|
||||
let c_text = ffi::sqlite3_column_text(stmt, col);
|
||||
if c_text.is_null() {
|
||||
Ok("".to_string())
|
||||
} else {
|
||||
let c_slice = CStr::from_ptr(c_text as *const c_char).to_bytes();
|
||||
let utf8_str = try!(str::from_utf8(c_slice));
|
||||
Ok(utf8_str.into())
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
|
||||
sqlite3_column_type(stmt, col) == ffi::SQLITE_TEXT
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSql for Vec<u8> {
|
||||
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<Vec<u8>> {
|
||||
use std::slice::from_raw_parts;
|
||||
let c_blob = ffi::sqlite3_column_blob(stmt, col);
|
||||
let len = ffi::sqlite3_column_bytes(stmt, col);
|
||||
|
||||
// The documentation for sqlite3_column_bytes indicates it is always non-negative,
|
||||
// but we should assert here just to be sure.
|
||||
assert!(len >= 0,
|
||||
"unexpected negative return from sqlite3_column_bytes");
|
||||
let len = len as usize;
|
||||
|
||||
Ok(from_raw_parts(mem::transmute(c_blob), len).to_vec())
|
||||
}
|
||||
|
||||
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
|
||||
sqlite3_column_type(stmt, col) == ffi::SQLITE_BLOB
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
Ok(None)
|
||||
} else {
|
||||
FromSql::column_result(stmt, col).map(|t| Some(t))
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
|
||||
sqlite3_column_type(stmt, col) == ffi::SQLITE_NULL ||
|
||||
T::column_has_valid_sqlite_type(stmt, col)
|
||||
}
|
||||
}
|
||||
|
||||
/// Dynamic type value (http://sqlite.org/datatype3.html)
|
||||
/// Value's type is dictated by SQLite (not by the caller).
|
||||
#[derive(Clone,Debug,PartialEq)]
|
||||
pub enum Value {
|
||||
/// The value is a `NULL` value.
|
||||
Null,
|
||||
/// The value is a signed integer.
|
||||
Integer(i64),
|
||||
/// The value is a floating point number.
|
||||
Real(f64),
|
||||
/// The value is a text string.
|
||||
Text(String),
|
||||
/// The value is a blob of data
|
||||
Blob(Vec<u8>),
|
||||
}
|
||||
|
||||
impl FromSql for Value {
|
||||
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<Value> {
|
||||
match sqlite3_column_type(stmt, col) {
|
||||
ffi::SQLITE_TEXT => FromSql::column_result(stmt, col).map(|t| Value::Text(t)),
|
||||
ffi::SQLITE_INTEGER => Ok(Value::Integer(ffi::sqlite3_column_int64(stmt, col))),
|
||||
ffi::SQLITE_FLOAT => Ok(Value::Real(ffi::sqlite3_column_double(stmt, col))),
|
||||
ffi::SQLITE_NULL => Ok(Value::Null),
|
||||
ffi::SQLITE_BLOB => FromSql::column_result(stmt, col).map(|t| Value::Blob(t)),
|
||||
_ => Err(Error::InvalidColumnType),
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn column_has_valid_sqlite_type(_: *mut sqlite3_stmt, _: c_int) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use Connection;
|
||||
use super::time;
|
||||
use Error;
|
||||
use libc::{c_int, c_double};
|
||||
|
||||
fn checked_memory_handle() -> Connection {
|
||||
let db = Connection::open_in_memory().unwrap();
|
||||
db.execute_batch("CREATE TABLE foo (b BLOB, t TEXT, i INTEGER, f FLOAT, n)").unwrap();
|
||||
db
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_blob() {
|
||||
let db = checked_memory_handle();
|
||||
|
||||
let v1234 = vec![1u8, 2, 3, 4];
|
||||
db.execute("INSERT INTO foo(b) VALUES (?)", &[&v1234]).unwrap();
|
||||
|
||||
let v: Vec<u8> = db.query_row("SELECT b FROM foo", &[], |r| r.get(0)).unwrap();
|
||||
assert_eq!(v, v1234);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_str() {
|
||||
let db = checked_memory_handle();
|
||||
|
||||
let s = "hello, world!";
|
||||
db.execute("INSERT INTO foo(t) VALUES (?)", &[&s.to_string()]).unwrap();
|
||||
|
||||
let from: String = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap();
|
||||
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();
|
||||
|
||||
let s = Some("hello, world!");
|
||||
let b = Some(vec![1u8, 2, 3, 4]);
|
||||
|
||||
db.execute("INSERT INTO foo(t) VALUES (?)", &[&s]).unwrap();
|
||||
db.execute("INSERT INTO foo(b) VALUES (?)", &[&b]).unwrap();
|
||||
|
||||
let mut stmt = db.prepare("SELECT t, b FROM foo ORDER BY ROWID ASC").unwrap();
|
||||
let mut rows = stmt.query(&[]).unwrap();
|
||||
|
||||
let row1 = rows.next().unwrap().unwrap();
|
||||
let s1: Option<String> = row1.get(0);
|
||||
let b1: Option<Vec<u8>> = row1.get(1);
|
||||
assert_eq!(s.unwrap(), s1.unwrap());
|
||||
assert!(b1.is_none());
|
||||
|
||||
let row2 = rows.next().unwrap().unwrap();
|
||||
let s2: Option<String> = row2.get(0);
|
||||
let b2: Option<Vec<u8>> = row2.get(1);
|
||||
assert!(s2.is_none());
|
||||
assert_eq!(b, b2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mismatched_types() {
|
||||
fn is_invalid_column_type(err: Error) -> bool {
|
||||
match err {
|
||||
Error::InvalidColumnType => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
let db = checked_memory_handle();
|
||||
|
||||
db.execute("INSERT INTO foo(b, t, i, f) VALUES (X'0102', 'text', 1, 1.5)",
|
||||
&[])
|
||||
.unwrap();
|
||||
|
||||
let mut stmt = db.prepare("SELECT b, t, i, f, n FROM foo").unwrap();
|
||||
let mut rows = stmt.query(&[]).unwrap();
|
||||
|
||||
let row = rows.next().unwrap().unwrap();
|
||||
|
||||
// check the correct types come back as expected
|
||||
assert_eq!(vec![1, 2], row.get_checked::<i32, Vec<u8>>(0).unwrap());
|
||||
assert_eq!("text", row.get_checked::<i32, String>(1).unwrap());
|
||||
assert_eq!(1, row.get_checked::<i32, c_int>(2).unwrap());
|
||||
assert_eq!(1.5, row.get_checked::<i32, c_double>(3).unwrap());
|
||||
assert!(row.get_checked::<i32, Option<c_int>>(4).unwrap().is_none());
|
||||
assert!(row.get_checked::<i32, Option<c_double>>(4).unwrap().is_none());
|
||||
assert!(row.get_checked::<i32, Option<String>>(4).unwrap().is_none());
|
||||
|
||||
// check some invalid types
|
||||
|
||||
// 0 is actually a blob (Vec<u8>)
|
||||
assert!(is_invalid_column_type(row.get_checked::<i32, c_int>(0).err().unwrap()));
|
||||
assert!(is_invalid_column_type(row.get_checked::<i32, c_int>(0).err().unwrap()));
|
||||
assert!(is_invalid_column_type(row.get_checked::<i32, i64>(0).err().unwrap()));
|
||||
assert!(is_invalid_column_type(row.get_checked::<i32, c_double>(0).err().unwrap()));
|
||||
assert!(is_invalid_column_type(row.get_checked::<i32, String>(0).err().unwrap()));
|
||||
assert!(is_invalid_column_type(row.get_checked::<i32, time::Timespec>(0).err().unwrap()));
|
||||
assert!(is_invalid_column_type(row.get_checked::<i32, Option<c_int>>(0).err().unwrap()));
|
||||
|
||||
// 1 is actually a text (String)
|
||||
assert!(is_invalid_column_type(row.get_checked::<i32, c_int>(1).err().unwrap()));
|
||||
assert!(is_invalid_column_type(row.get_checked::<i32, i64>(1).err().unwrap()));
|
||||
assert!(is_invalid_column_type(row.get_checked::<i32, c_double>(1).err().unwrap()));
|
||||
assert!(is_invalid_column_type(row.get_checked::<i32, Vec<u8>>(1).err().unwrap()));
|
||||
assert!(is_invalid_column_type(row.get_checked::<i32, Option<c_int>>(1).err().unwrap()));
|
||||
|
||||
// 2 is actually an integer
|
||||
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)
|
||||
assert!(is_invalid_column_type(row.get_checked::<i32, c_int>(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, 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
|
||||
assert!(is_invalid_column_type(row.get_checked::<i32, c_int>(4).err().unwrap()));
|
||||
assert!(is_invalid_column_type(row.get_checked::<i32, i64>(4).err().unwrap()));
|
||||
assert!(is_invalid_column_type(row.get_checked::<i32, c_double>(4).err().unwrap()));
|
||||
assert!(is_invalid_column_type(row.get_checked::<i32, String>(4).err().unwrap()));
|
||||
assert!(is_invalid_column_type(row.get_checked::<i32, Vec<u8>>(4).err().unwrap()));
|
||||
assert!(is_invalid_column_type(row.get_checked::<i32, time::Timespec>(4).err().unwrap()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_dynamic_type() {
|
||||
use super::Value;
|
||||
let db = checked_memory_handle();
|
||||
|
||||
db.execute("INSERT INTO foo(b, t, i, f) VALUES (X'0102', 'text', 1, 1.5)",
|
||||
&[])
|
||||
.unwrap();
|
||||
|
||||
let mut stmt = db.prepare("SELECT b, t, i, f, n FROM foo").unwrap();
|
||||
let mut rows = stmt.query(&[]).unwrap();
|
||||
|
||||
let row = rows.next().unwrap().unwrap();
|
||||
assert_eq!(Value::Blob(vec![1, 2]),
|
||||
row.get_checked::<i32, Value>(0).unwrap());
|
||||
assert_eq!(Value::Text(String::from("text")),
|
||||
row.get_checked::<i32, Value>(1).unwrap());
|
||||
assert_eq!(Value::Integer(1), row.get_checked::<i32, Value>(2).unwrap());
|
||||
assert_eq!(Value::Real(1.5), row.get_checked::<i32, Value>(3).unwrap());
|
||||
assert_eq!(Value::Null, row.get_checked::<i32, Value>(4).unwrap());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user