Fix FromSql impl for OffsetDateTime

Fix test doc
This commit is contained in:
gwenn 2021-06-12 18:54:29 +02:00
parent 125f5dfbde
commit bcf28fec35

View File

@ -41,22 +41,24 @@ For example, to store datetimes as `i64`s counting the number of seconds since
the Unix epoch: the Unix epoch:
``` ```
use rusqlite::types::{FromSql, FromSqlResult, ToSql, ToSqlOutput, ValueRef}; use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef};
use rusqlite::Result; use rusqlite::Result;
pub struct DateTimeSql(pub time::OffsetDateTime); pub struct DateTimeSql(pub time::OffsetDateTime);
impl FromSql for DateTimeSql { impl FromSql for DateTimeSql {
fn column_result(value: ValueRef) -> FromSqlResult<Self> { fn column_result(value: ValueRef) -> FromSqlResult<Self> {
i64::column_result(value).map(|as_i64| { i64::column_result(value).and_then(|as_i64| {
DateTimeSql(time::OffsetDateTime::from_unix_timestamp(as_i64)) time::OffsetDateTime::from_unix_timestamp(as_i64)
.map(|odt| DateTimeSql(odt))
.map_err(|err| FromSqlError::Other(Box::new(err)))
}) })
} }
} }
impl ToSql for DateTimeSql { impl ToSql for DateTimeSql {
fn to_sql(&self) -> Result<ToSqlOutput> { fn to_sql(&self) -> Result<ToSqlOutput> {
Ok(self.0.timestamp().into()) Ok(self.0.unix_timestamp().into())
} }
} }
``` ```