aboutsummaryrefslogtreecommitdiff
path: root/src/types/mod.rs
diff options
context:
space:
mode:
authorHaibo Huang <hhb@google.com>2020-09-16 19:15:45 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2020-09-16 19:15:45 +0000
commitb3882bc3670d9d26c6d731e04fa0ef879746c90d (patch)
tree7627b35b901cea37bfd0a90396c150eddd1e526b /src/types/mod.rs
parent6a763eeeeac193c60806ba755cb331bdbf6228da (diff)
parent0053da6db0d781003180e44ff8363fd1def947a1 (diff)
downloadrusqlite-b3882bc3670d9d26c6d731e04fa0ef879746c90d.tar.gz
Upgrade rust/crates/rusqlite to 0.24.0 am: b5372b7e69 am: 29fb7839dc am: 76138a0f92 am: 0053da6db0
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/rusqlite/+/1407248 Change-Id: Id8f48d7d90aaba1b94e5d1daa6114f5e1c2d4e97
Diffstat (limited to 'src/types/mod.rs')
-rw-r--r--src/types/mod.rs73
1 files changed, 41 insertions, 32 deletions
diff --git a/src/types/mod.rs b/src/types/mod.rs
index d79ff82..2d163cf 100644
--- a/src/types/mod.rs
+++ b/src/types/mod.rs
@@ -10,40 +10,39 @@
//! * 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 the RFC 3339 date/time format,
+//! Additionally, if the `time` feature is enabled, implementations are
+//! provided for `time::OffsetDateTime` 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:
-//!
-//! ```rust
-//! use rusqlite::types::{FromSql, FromSqlResult, ToSql, ToSqlOutput, ValueRef};
-//! use rusqlite::Result;
-//!
-//! pub struct TimespecSql(pub time::Timespec);
-//!
-//! impl FromSql for TimespecSql {
-//! fn column_result(value: ValueRef) -> FromSqlResult<Self> {
-//! f64::column_result(value).map(|as_f64| {
-//! TimespecSql(time::Timespec {
-//! sec: as_f64.trunc() as i64,
-//! nsec: (as_f64.fract() * 1.0e9) as i32,
-//! })
-//! })
-//! }
-//! }
-//!
-//! impl ToSql for TimespecSql {
-//! fn to_sql(&self) -> Result<ToSqlOutput> {
-//! let TimespecSql(ts) = *self;
-//! let as_f64 = ts.sec as f64 + (ts.nsec as f64) / 1.0e9;
-//! Ok(as_f64.into())
-//! }
-//! }
-//! ```
+//! want different storage for datetimes, you can use a newtype.
//!
+#![cfg_attr(feature = "time", doc = r##"
+For example, to store datetimes as `i64`s counting the number of seconds since
+the Unix epoch:
+
+```
+use rusqlite::types::{FromSql, FromSqlResult, ToSql, ToSqlOutput, ValueRef};
+use rusqlite::Result;
+
+pub struct DateTimeSql(pub time::OffsetDateTime);
+
+impl FromSql for DateTimeSql {
+ fn column_result(value: ValueRef) -> FromSqlResult<Self> {
+ i64::column_result(value).map(|as_i64| {
+ DateTimeSql(time::OffsetDateTime::from_unix_timestamp(as_i64))
+ })
+ }
+}
+
+impl ToSql for DateTimeSql {
+ fn to_sql(&self) -> Result<ToSqlOutput> {
+ Ok(self.0.timestamp().into())
+ }
+}
+```
+
+"##)]
//! `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`).
@@ -60,6 +59,7 @@ mod chrono;
mod from_sql;
#[cfg(feature = "serde_json")]
mod serde_json;
+#[cfg(feature = "time")]
mod time;
mod to_sql;
#[cfg(feature = "url")]
@@ -82,12 +82,19 @@ mod value_ref;
#[derive(Copy, Clone)]
pub struct Null;
+/// SQLite data types.
+/// See [Fundamental Datatypes](https://sqlite.org/c3ref/c_blob.html).
#[derive(Clone, Debug, PartialEq)]
pub enum Type {
+ /// NULL
Null,
+ /// 64-bit signed integer
Integer,
+ /// 64-bit IEEE floating point number
Real,
+ /// String
Text,
+ /// BLOB
Blob,
}
@@ -266,8 +273,9 @@ mod test {
assert!(is_invalid_column_type(
row.get::<_, String>(0).err().unwrap()
));
+ #[cfg(feature = "time")]
assert!(is_invalid_column_type(
- row.get::<_, time::Timespec>(0).err().unwrap()
+ row.get::<_, time::OffsetDateTime>(0).err().unwrap()
));
assert!(is_invalid_column_type(
row.get::<_, Option<c_int>>(0).err().unwrap()
@@ -328,8 +336,9 @@ mod test {
assert!(is_invalid_column_type(
row.get::<_, Vec<u8>>(4).err().unwrap()
));
+ #[cfg(feature = "time")]
assert!(is_invalid_column_type(
- row.get::<_, time::Timespec>(4).err().unwrap()
+ row.get::<_, time::OffsetDateTime>(4).err().unwrap()
));
}