2014-11-04 06:11:00 +08:00
|
|
|
//! Traits dealing with SQLite data types.
|
|
|
|
//!
|
|
|
|
//! SQLite uses a [dynamic type system](https://www.sqlite.org/datatype3.html). Implementations of
|
2018-08-17 00:29:46 +08:00
|
|
|
//! the `ToSql` and `FromSql` traits are provided for the basic types that
|
|
|
|
//! SQLite provides methods for:
|
2014-11-04 06:11:00 +08:00
|
|
|
//!
|
2018-08-17 00:29:46 +08:00
|
|
|
//! * Integers (`i32` and `i64`; SQLite uses `i64` internally, so getting an
|
|
|
|
//! `i32` will truncate if the value is too large or too small).
|
2016-05-25 07:48:26 +08:00
|
|
|
//! * Reals (`f64`)
|
2014-11-04 06:11:00 +08:00
|
|
|
//! * Strings (`String` and `&str`)
|
|
|
|
//! * Blobs (`Vec<u8>` and `&[u8]`)
|
|
|
|
//!
|
2017-12-24 17:02:40 +08:00
|
|
|
//! Additionally, because it is such a common data type, implementations are
|
|
|
|
//! provided for `time::Timespec` that use the RFC 3339 date/time format,
|
|
|
|
//! `"%Y-%m-%dT%H:%M:%S.%fZ"`, to store time values as strings. These values
|
|
|
|
//! can be parsed by SQLite's builtin
|
|
|
|
//! [datetime](https://www.sqlite.org/lang_datefunc.html) functions. If you
|
|
|
|
//! want different storage for timespecs, you can use a newtype. For example, to
|
|
|
|
//! store timespecs as `f64`s:
|
2014-11-04 06:11:00 +08:00
|
|
|
//!
|
2017-01-25 12:57:42 +08:00
|
|
|
//! ```rust
|
2018-08-17 00:29:46 +08:00
|
|
|
//! use rusqlite::types::{FromSql, FromSqlResult, ToSql, ToSqlOutput, ValueRef};
|
|
|
|
//! use rusqlite::Result;
|
2014-11-04 06:11:00 +08:00
|
|
|
//!
|
|
|
|
//! pub struct TimespecSql(pub time::Timespec);
|
|
|
|
//!
|
|
|
|
//! impl FromSql for TimespecSql {
|
2017-01-25 12:57:42 +08:00
|
|
|
//! fn column_result(value: ValueRef) -> FromSqlResult<Self> {
|
2016-05-25 08:12:29 +08:00
|
|
|
//! f64::column_result(value).map(|as_f64| {
|
2018-08-17 00:29:46 +08:00
|
|
|
//! TimespecSql(time::Timespec {
|
|
|
|
//! sec: as_f64.trunc() as i64,
|
|
|
|
//! nsec: (as_f64.fract() * 1.0e9) as i32,
|
|
|
|
//! })
|
2014-11-04 06:11:00 +08:00
|
|
|
//! })
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! impl ToSql for TimespecSql {
|
2017-01-25 12:57:42 +08:00
|
|
|
//! fn to_sql(&self) -> Result<ToSqlOutput> {
|
2014-11-04 06:11:00 +08:00
|
|
|
//! let TimespecSql(ts) = *self;
|
|
|
|
//! let as_f64 = ts.sec as f64 + (ts.nsec as f64) / 1.0e9;
|
2017-01-25 12:57:42 +08:00
|
|
|
//! Ok(as_f64.into())
|
2014-11-04 06:11:00 +08:00
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! ```
|
2016-05-25 07:48:26 +08:00
|
|
|
//!
|
2018-08-17 00:29:46 +08:00
|
|
|
//! `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`).
|
2014-11-04 06:11:00 +08:00
|
|
|
|
2016-12-31 13:35:47 +08:00
|
|
|
pub use self::from_sql::{FromSql, FromSqlError, FromSqlResult};
|
2016-05-26 10:57:43 +08:00
|
|
|
pub use self::to_sql::{ToSql, ToSqlOutput};
|
2016-05-26 11:30:34 +08:00
|
|
|
pub use self::value::Value;
|
2016-05-25 09:34:18 +08:00
|
|
|
pub use self::value_ref::ValueRef;
|
2016-05-23 08:01:03 +08:00
|
|
|
|
2016-05-27 03:03:05 +08:00
|
|
|
use std::fmt;
|
|
|
|
|
2016-02-23 03:36:49 +08:00
|
|
|
#[cfg(feature = "chrono")]
|
|
|
|
mod chrono;
|
2018-08-11 18:48:21 +08:00
|
|
|
mod from_sql;
|
2016-02-26 02:06:37 +08:00
|
|
|
#[cfg(feature = "serde_json")]
|
|
|
|
mod serde_json;
|
2018-08-11 18:48:21 +08:00
|
|
|
mod time;
|
|
|
|
mod to_sql;
|
2019-03-10 11:16:37 +08:00
|
|
|
#[cfg(feature = "url")]
|
|
|
|
mod url;
|
2018-08-11 18:48:21 +08:00
|
|
|
mod value;
|
|
|
|
mod value_ref;
|
2016-02-23 03:36:49 +08:00
|
|
|
|
2014-11-04 06:11:00 +08:00
|
|
|
/// Empty struct that can be used to fill in a query parameter as `NULL`.
|
|
|
|
///
|
|
|
|
/// ## Example
|
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
2015-12-13 03:06:03 +08:00
|
|
|
/// # use rusqlite::{Connection, Result};
|
2014-11-04 06:11:00 +08:00
|
|
|
/// # use rusqlite::types::{Null};
|
2019-10-13 19:08:33 +08:00
|
|
|
///
|
2018-05-24 03:23:28 +08:00
|
|
|
/// fn insert_null(conn: &Connection) -> Result<usize> {
|
2018-09-16 15:49:23 +08:00
|
|
|
/// conn.execute("INSERT INTO people (name) VALUES (?)", &[Null])
|
2014-11-04 06:11:00 +08:00
|
|
|
/// }
|
|
|
|
/// ```
|
2018-08-11 18:48:21 +08:00
|
|
|
#[derive(Copy, Clone)]
|
2014-10-20 07:56:41 +08:00
|
|
|
pub struct Null;
|
|
|
|
|
2018-08-11 18:48:21 +08:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2016-05-27 03:03:05 +08:00
|
|
|
pub enum Type {
|
|
|
|
Null,
|
|
|
|
Integer,
|
|
|
|
Real,
|
|
|
|
Text,
|
|
|
|
Blob,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Type {
|
2018-12-08 04:57:04 +08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-05-27 03:03:05 +08:00
|
|
|
match *self {
|
|
|
|
Type::Null => write!(f, "Null"),
|
|
|
|
Type::Integer => write!(f, "Integer"),
|
|
|
|
Type::Real => write!(f, "Real"),
|
|
|
|
Type::Text => write!(f, "Text"),
|
|
|
|
Type::Blob => write!(f, "Blob"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-20 07:56:41 +08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2018-08-11 18:48:21 +08:00
|
|
|
use super::Value;
|
2018-10-31 03:13:41 +08:00
|
|
|
use crate::{Connection, Error, NO_PARAMS};
|
2018-08-11 18:48:21 +08:00
|
|
|
use std::f64::EPSILON;
|
|
|
|
use std::os::raw::{c_double, c_int};
|
2014-10-20 07:56:41 +08:00
|
|
|
|
2015-12-13 02:50:12 +08:00
|
|
|
fn checked_memory_handle() -> Connection {
|
|
|
|
let db = Connection::open_in_memory().unwrap();
|
2017-04-08 01:43:24 +08:00
|
|
|
db.execute_batch("CREATE TABLE foo (b BLOB, t TEXT, i INTEGER, f FLOAT, n)")
|
|
|
|
.unwrap();
|
2014-10-20 07:56:41 +08:00
|
|
|
db
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_blob() {
|
|
|
|
let db = checked_memory_handle();
|
|
|
|
|
2015-12-11 05:48:09 +08:00
|
|
|
let v1234 = vec![1u8, 2, 3, 4];
|
2017-04-08 01:43:24 +08:00
|
|
|
db.execute("INSERT INTO foo(b) VALUES (?)", &[&v1234])
|
|
|
|
.unwrap();
|
2014-10-20 07:56:41 +08:00
|
|
|
|
2018-08-11 18:48:21 +08:00
|
|
|
let v: Vec<u8> = db
|
2018-09-16 17:10:19 +08:00
|
|
|
.query_row("SELECT b FROM foo", NO_PARAMS, |r| r.get(0))
|
2017-04-08 01:43:24 +08:00
|
|
|
.unwrap();
|
2014-10-20 07:56:41 +08:00
|
|
|
assert_eq!(v, v1234);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-06-14 02:22:21 +08:00
|
|
|
fn test_empty_blob() {
|
|
|
|
let db = checked_memory_handle();
|
|
|
|
|
|
|
|
let empty = vec![];
|
2017-04-08 01:43:24 +08:00
|
|
|
db.execute("INSERT INTO foo(b) VALUES (?)", &[&empty])
|
|
|
|
.unwrap();
|
2016-06-14 02:22:21 +08:00
|
|
|
|
2018-08-11 18:48:21 +08:00
|
|
|
let v: Vec<u8> = db
|
2018-09-16 17:10:19 +08:00
|
|
|
.query_row("SELECT b FROM foo", NO_PARAMS, |r| r.get(0))
|
2017-04-08 01:43:24 +08:00
|
|
|
.unwrap();
|
2016-06-14 02:22:21 +08:00
|
|
|
assert_eq!(v, empty);
|
|
|
|
}
|
|
|
|
|
2014-10-20 07:56:41 +08:00
|
|
|
#[test]
|
|
|
|
fn test_str() {
|
|
|
|
let db = checked_memory_handle();
|
|
|
|
|
2016-05-26 10:57:43 +08:00
|
|
|
let s = "hello, world!";
|
2018-08-11 18:48:21 +08:00
|
|
|
db.execute("INSERT INTO foo(t) VALUES (?)", &[&s]).unwrap();
|
2016-05-26 10:57:43 +08:00
|
|
|
|
2018-08-11 18:48:21 +08:00
|
|
|
let from: String = db
|
2018-09-16 17:10:19 +08:00
|
|
|
.query_row("SELECT t FROM foo", NO_PARAMS, |r| r.get(0))
|
2017-04-08 01:43:24 +08:00
|
|
|
.unwrap();
|
2016-05-26 10:57:43 +08:00
|
|
|
assert_eq!(from, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_string() {
|
|
|
|
let db = checked_memory_handle();
|
|
|
|
|
2014-10-20 07:56:41 +08:00
|
|
|
let s = "hello, world!";
|
2018-09-16 15:49:23 +08:00
|
|
|
db.execute("INSERT INTO foo(t) VALUES (?)", &[s.to_owned()])
|
2017-04-08 01:43:24 +08:00
|
|
|
.unwrap();
|
2014-10-20 07:56:41 +08:00
|
|
|
|
2018-08-11 18:48:21 +08:00
|
|
|
let from: String = db
|
2018-09-16 17:10:19 +08:00
|
|
|
.query_row("SELECT t FROM foo", NO_PARAMS, |r| r.get(0))
|
2017-04-08 01:43:24 +08:00
|
|
|
.unwrap();
|
2015-03-17 12:55:28 +08:00
|
|
|
assert_eq!(from, s);
|
2014-10-20 07:56:41 +08:00
|
|
|
}
|
|
|
|
|
2016-05-26 10:57:43 +08:00
|
|
|
#[test]
|
|
|
|
fn test_value() {
|
|
|
|
let db = checked_memory_handle();
|
|
|
|
|
2018-09-16 15:49:23 +08:00
|
|
|
db.execute("INSERT INTO foo(i) VALUES (?)", &[Value::Integer(10)])
|
2017-04-08 01:43:24 +08:00
|
|
|
.unwrap();
|
2016-05-26 10:57:43 +08:00
|
|
|
|
2018-08-11 18:48:21 +08:00
|
|
|
assert_eq!(
|
|
|
|
10i64,
|
2018-09-16 17:10:19 +08:00
|
|
|
db.query_row::<i64, _, _>("SELECT i FROM foo", NO_PARAMS, |r| r.get(0))
|
2018-08-11 18:48:21 +08:00
|
|
|
.unwrap()
|
|
|
|
);
|
2016-05-26 10:57:43 +08:00
|
|
|
}
|
|
|
|
|
2014-10-20 07:56:41 +08:00
|
|
|
#[test]
|
|
|
|
fn test_option() {
|
|
|
|
let db = checked_memory_handle();
|
|
|
|
|
|
|
|
let s = Some("hello, world!");
|
2015-12-11 05:48:09 +08:00
|
|
|
let b = Some(vec![1u8, 2, 3, 4]);
|
2014-10-20 07:56:41 +08:00
|
|
|
|
2018-08-11 18:48:21 +08:00
|
|
|
db.execute("INSERT INTO foo(t) VALUES (?)", &[&s]).unwrap();
|
|
|
|
db.execute("INSERT INTO foo(b) VALUES (?)", &[&b]).unwrap();
|
2014-10-20 07:56:41 +08:00
|
|
|
|
2018-08-11 18:48:21 +08:00
|
|
|
let mut stmt = db
|
|
|
|
.prepare("SELECT t, b FROM foo ORDER BY ROWID ASC")
|
2017-04-08 01:43:24 +08:00
|
|
|
.unwrap();
|
2018-09-16 17:10:19 +08:00
|
|
|
let mut rows = stmt.query(NO_PARAMS).unwrap();
|
2014-10-20 07:56:41 +08:00
|
|
|
|
2016-05-19 00:33:58 +08:00
|
|
|
{
|
|
|
|
let row1 = rows.next().unwrap().unwrap();
|
2019-02-22 03:48:09 +08:00
|
|
|
let s1: Option<String> = row1.get_unwrap(0);
|
|
|
|
let b1: Option<Vec<u8>> = row1.get_unwrap(1);
|
2016-05-19 00:33:58 +08:00
|
|
|
assert_eq!(s.unwrap(), s1.unwrap());
|
|
|
|
assert!(b1.is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let row2 = rows.next().unwrap().unwrap();
|
2019-02-22 03:48:09 +08:00
|
|
|
let s2: Option<String> = row2.get_unwrap(0);
|
|
|
|
let b2: Option<Vec<u8>> = row2.get_unwrap(1);
|
2016-05-19 00:33:58 +08:00
|
|
|
assert!(s2.is_none());
|
|
|
|
assert_eq!(b, b2);
|
|
|
|
}
|
2014-10-20 07:56:41 +08:00
|
|
|
}
|
2015-05-05 09:47:20 +08:00
|
|
|
|
|
|
|
#[test]
|
2019-03-22 02:13:55 +08:00
|
|
|
#[allow(clippy::cognitive_complexity)]
|
2015-05-05 09:47:20 +08:00
|
|
|
fn test_mismatched_types() {
|
2015-12-13 13:54:08 +08:00
|
|
|
fn is_invalid_column_type(err: Error) -> bool {
|
|
|
|
match err {
|
2019-11-03 18:19:07 +08:00
|
|
|
Error::InvalidColumnType(..) => true,
|
2015-12-13 13:54:08 +08:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 09:47:20 +08:00
|
|
|
let db = checked_memory_handle();
|
|
|
|
|
2018-08-11 18:48:21 +08:00
|
|
|
db.execute(
|
|
|
|
"INSERT INTO foo(b, t, i, f) VALUES (X'0102', 'text', 1, 1.5)",
|
2018-09-16 17:10:19 +08:00
|
|
|
NO_PARAMS,
|
2018-10-28 15:51:02 +08:00
|
|
|
)
|
|
|
|
.unwrap();
|
2015-05-05 09:47:20 +08:00
|
|
|
|
|
|
|
let mut stmt = db.prepare("SELECT b, t, i, f, n FROM foo").unwrap();
|
2018-09-16 17:10:19 +08:00
|
|
|
let mut rows = stmt.query(NO_PARAMS).unwrap();
|
2015-05-05 09:47:20 +08:00
|
|
|
|
|
|
|
let row = rows.next().unwrap().unwrap();
|
|
|
|
|
|
|
|
// check the correct types come back as expected
|
2019-02-22 03:48:09 +08:00
|
|
|
assert_eq!(vec![1, 2], row.get::<_, Vec<u8>>(0).unwrap());
|
|
|
|
assert_eq!("text", row.get::<_, String>(1).unwrap());
|
|
|
|
assert_eq!(1, row.get::<_, c_int>(2).unwrap());
|
|
|
|
assert!((1.5 - row.get::<_, c_double>(3).unwrap()).abs() < EPSILON);
|
|
|
|
assert!(row.get::<_, Option<c_int>>(4).unwrap().is_none());
|
|
|
|
assert!(row.get::<_, Option<c_double>>(4).unwrap().is_none());
|
|
|
|
assert!(row.get::<_, Option<String>>(4).unwrap().is_none());
|
2015-05-05 09:47:20 +08:00
|
|
|
|
|
|
|
// check some invalid types
|
|
|
|
|
|
|
|
// 0 is actually a blob (Vec<u8>)
|
2018-08-11 18:48:21 +08:00
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, c_int>(0).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, c_int>(0).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
2019-02-22 03:48:09 +08:00
|
|
|
assert!(is_invalid_column_type(row.get::<_, i64>(0).err().unwrap()));
|
2018-08-11 18:48:21 +08:00
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, c_double>(0).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, String>(0).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, time::Timespec>(0).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, Option<c_int>>(0).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
2015-05-05 09:47:20 +08:00
|
|
|
|
|
|
|
// 1 is actually a text (String)
|
2018-08-11 18:48:21 +08:00
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, c_int>(1).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
2019-02-22 03:48:09 +08:00
|
|
|
assert!(is_invalid_column_type(row.get::<_, i64>(1).err().unwrap()));
|
2018-08-11 18:48:21 +08:00
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, c_double>(1).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, Vec<u8>>(1).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, Option<c_int>>(1).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
2015-05-05 09:47:20 +08:00
|
|
|
|
|
|
|
// 2 is actually an integer
|
2018-08-11 18:48:21 +08:00
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, String>(2).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, Vec<u8>>(2).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, Option<String>>(2).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
2015-05-05 09:47:20 +08:00
|
|
|
|
|
|
|
// 3 is actually a float (c_double)
|
2018-08-11 18:48:21 +08:00
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, c_int>(3).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
2019-02-22 03:48:09 +08:00
|
|
|
assert!(is_invalid_column_type(row.get::<_, i64>(3).err().unwrap()));
|
2018-08-11 18:48:21 +08:00
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, String>(3).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, Vec<u8>>(3).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, Option<c_int>>(3).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
2015-05-05 09:47:20 +08:00
|
|
|
|
|
|
|
// 4 is actually NULL
|
2018-08-11 18:48:21 +08:00
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, c_int>(4).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
2019-02-22 03:48:09 +08:00
|
|
|
assert!(is_invalid_column_type(row.get::<_, i64>(4).err().unwrap()));
|
2018-08-11 18:48:21 +08:00
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, c_double>(4).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, String>(4).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, Vec<u8>>(4).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
|
|
|
assert!(is_invalid_column_type(
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, time::Timespec>(4).err().unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
));
|
2015-05-05 09:47:20 +08:00
|
|
|
}
|
2016-01-02 17:28:00 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_dynamic_type() {
|
|
|
|
use super::Value;
|
|
|
|
let db = checked_memory_handle();
|
|
|
|
|
2018-08-11 18:48:21 +08:00
|
|
|
db.execute(
|
|
|
|
"INSERT INTO foo(b, t, i, f) VALUES (X'0102', 'text', 1, 1.5)",
|
2018-09-16 17:10:19 +08:00
|
|
|
NO_PARAMS,
|
2018-10-28 15:51:02 +08:00
|
|
|
)
|
|
|
|
.unwrap();
|
2016-01-02 17:28:00 +08:00
|
|
|
|
|
|
|
let mut stmt = db.prepare("SELECT b, t, i, f, n FROM foo").unwrap();
|
2018-09-16 17:10:19 +08:00
|
|
|
let mut rows = stmt.query(NO_PARAMS).unwrap();
|
2016-01-02 17:28:00 +08:00
|
|
|
|
|
|
|
let row = rows.next().unwrap().unwrap();
|
2019-02-22 03:48:09 +08:00
|
|
|
assert_eq!(Value::Blob(vec![1, 2]), row.get::<_, Value>(0).unwrap());
|
2018-08-11 18:48:21 +08:00
|
|
|
assert_eq!(
|
|
|
|
Value::Text(String::from("text")),
|
2019-02-22 03:48:09 +08:00
|
|
|
row.get::<_, Value>(1).unwrap()
|
2018-08-11 18:48:21 +08:00
|
|
|
);
|
2019-02-22 03:48:09 +08:00
|
|
|
assert_eq!(Value::Integer(1), row.get::<_, Value>(2).unwrap());
|
|
|
|
match row.get::<_, Value>(3).unwrap() {
|
2016-03-30 02:18:56 +08:00
|
|
|
Value::Real(val) => assert!((1.5 - val).abs() < EPSILON),
|
|
|
|
x => panic!("Invalid Value {:?}", x),
|
|
|
|
}
|
2019-02-22 03:48:09 +08:00
|
|
|
assert_eq!(Value::Null, row.get::<_, Value>(4).unwrap());
|
2016-01-02 17:28:00 +08:00
|
|
|
}
|
2014-10-20 07:56:41 +08:00
|
|
|
}
|