Module rusqlite::types [] [src]

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 and i64; SQLite uses i64 internally, so getting an i32 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 a string for storage (using the same format string, "%Y-%m-%d %H:%M:%S", as SQLite's builtin datetime 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 f64s:

extern crate rusqlite;
extern crate time;

use rusqlite::types::{FromSql, FromSqlResult, ValueRef, ToSql, ToSqlOutput};
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

Null

Empty struct that can be used to fill in a query parameter as NULL.

Enums

FromSqlError

Enum listing possible errors from FromSql trait.

ToSqlOutput

ToSqlOutput represents the possible output types for implementors of the ToSql trait.

Type
Value

Owning dynamic type value. Value's type is typically dictated by SQLite (not by the caller).

ValueRef

A non-owning dynamic type value. Typically the memory backing this value is owned by SQLite.

Traits

FromSql

A trait for types that can be created from a SQLite value.

ToSql

A trait for types that can be converted into SQLite values.

Type Definitions

FromSqlResult

Result type for implementors of the FromSql trait.