Trait postgres::types::FromSql [] [src]

pub trait FromSql: Sized {
    fn from_sql<R: Read>(ty: &Type, raw: &mut R, ctx: &SessionInfo) -> Result<Self>;
    fn accepts(ty: &Type) -> bool;

    fn from_sql_nullable<R: Read>(ty: &Type, raw: Option<&mut R>, ctx: &SessionInfo) -> Result<Self> { ... }
}

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

Types

The following implementations are provided by this crate, along with the corresponding Postgres types:

Rust type Postgres type(s)
bool BOOL
i8 "char"
i16 SMALLINT, SMALLSERIAL
i32 INT, SERIAL
u32 OID
i64 BIGINT, BIGSERIAL
f32 REAL
f64 DOUBLE PRECISION
String VARCHAR, CHAR(n), TEXT, CITEXT
Vec<u8> BYTEA
HashMap<String, Option<String>> HSTORE

In addition, some implementations are provided for types in third party crates. These are disabled by default; to opt into one of these implementations, activate the Cargo feature corresponding to the crate's name. For example, the serde feature enables the implementation for the serde::json::Value type.

Rust type Postgres type(s)
serialize::json::Json JSON, JSONB
serde::json::Value JSON, JSONB
time::Timespec TIMESTAMP, TIMESTAMP WITH TIME ZONE
chrono::NaiveDateTime TIMESTAMP
chrono::DateTime<UTC> TIMESTAMP WITH TIME ZONE
chrono::NaiveDate DATE
chrono::NaiveTime TIME
uuid::Uuid UUID

Nullability

In addition to the types listed above, FromSql is implemented for Option<T> where T implements FromSql. An Option<T> represents a nullable Postgres value.

Required Methods

fn from_sql<R: Read>(ty: &Type, raw: &mut R, ctx: &SessionInfo) -> Result<Self>

Creates a new value of this type from a Reader of the binary format of the specified Postgres Type.

The caller of this method is responsible for ensuring that this type is compatible with the Postgres Type.

fn accepts(ty: &Type) -> bool

Determines if a value of this type can be created from the specified Postgres Type.

Provided Methods

fn from_sql_nullable<R: Read>(ty: &Type, raw: Option<&mut R>, ctx: &SessionInfo) -> Result<Self>

Creates a new value of this type from a Read of Postgres data.

If the value was NULL, the Read will be None.

The caller of this method is responsible for ensuring that this type is compatible with the Postgres Type.

The default implementation calls FromSql::from_sql when raw is Some and returns Err(Error::Conversion(Box::new(WasNull)) when raw is None. It does not typically need to be overridden.

Implementors