Expand description
Traits dealing with SQLite data types.
SQLite uses a dynamic type system. Implementations of
the ToSql
and FromSql
traits are provided for the basic types that
SQLite provides methods for:
- Integers (
i32
andi64
; SQLite usesi64
internally, so getting ani32
will truncate if the value is too large or too small). - Reals (
f64
) - 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,
"%Y-%m-%dT%H:%M:%S.%fZ"
, to store time values as strings. These values
can be parsed by SQLite’s builtin
datetime functions. If you
want different storage for timespecs, you can use a newtype. For example, to
store timespecs as f64
s:
extern crate rusqlite;
extern crate time;
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())
}
}
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
).
Structs
Empty struct that can be used to fill in a query parameter as
NULL
.Enums
Enum listing possible errors from
FromSql
trait.ToSqlOutput
represents the possible output types for implementors of the
ToSql
trait.Owning dynamic type value. Value’s type is typically
dictated by SQLite (not by the caller).
A non-owning dynamic type value. Typically the
memory backing this value is owned by SQLite.
Traits
A trait for types that can be created from a SQLite value.
A trait for types that can be converted into SQLite values.
Type Definitions
Result type for implementors of the
FromSql
trait.