Trait postgres::types::ToSql

source ·
pub trait ToSql: Debug {
    // Required methods
    fn to_sql<W>(
        &self,
        ty: &Type,
        out: &mut W,
        ctx: &SessionInfo<'_>,
    ) -> Result<IsNull>
       where Self: Sized,
             W: Write + ?Sized;
    fn accepts(ty: &Type) -> bool
       where Self: Sized;
    fn to_sql_checked(
        &self,
        ty: &Type,
        out: &mut dyn Write,
        ctx: &SessionInfo<'_>,
    ) -> Result<IsNull>;
}
Expand description

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

§Types

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

Rust typePostgres type(s)
boolBOOL
i8“char”
i16SMALLINT, SMALLSERIAL
i32INT, SERIAL
u32OID
i64BIGINT, BIGSERIAL
f32REAL
f64DOUBLE PRECISION
StringVARCHAR, CHAR(n), TEXT, CITEXT
&strVARCHAR, CHAR(n), TEXT, CITEXT
Vec<u8>BYTEA
&u8BYTEA
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 typePostgres type(s)
serialize::json::JsonJSON, JSONB
serde::json::ValueJSON, JSONB
time::TimespecTIMESTAMP, TIMESTAMP WITH TIME ZONE
chrono::NaiveDateTimeTIMESTAMP
chrono::DateTime<UTC>TIMESTAMP WITH TIME ZONE
chrono::NaiveDateDATE
chrono::NaiveTimeTIME
uuid::UuidUUID

§Nullability

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

Required Methods§

source

fn to_sql<W>( &self, ty: &Type, out: &mut W, ctx: &SessionInfo<'_>, ) -> Result<IsNull>
where Self: Sized, W: Write + ?Sized,

Converts the value of self into the binary format of the specified Postgres Type, writing it to out.

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

The return value indicates if this value should be represented as NULL. If this is the case, implementations must not write anything to out.

source

fn accepts(ty: &Type) -> bool
where Self: Sized,

Determines if a value of this type can be converted to the specified Postgres Type.

source

fn to_sql_checked( &self, ty: &Type, out: &mut dyn Write, ctx: &SessionInfo<'_>, ) -> Result<IsNull>

An adaptor method used internally by Rust-Postgres.

All implementations of this method should be generated by the to_sql_checked!() macro.

Implementations on Foreign Types§

source§

impl ToSql for bool

source§

fn to_sql_checked( &self, ty: &Type, out: &mut dyn Write, ctx: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn to_sql<W: Write + ?Sized>( &self, _: &Type, w: &mut W, _: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn accepts(ty: &Type) -> bool

source§

impl ToSql for f32

source§

fn to_sql_checked( &self, ty: &Type, out: &mut dyn Write, ctx: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn to_sql<W: Write + ?Sized>( &self, _: &Type, w: &mut W, _: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn accepts(ty: &Type) -> bool

source§

impl ToSql for f64

source§

fn to_sql_checked( &self, ty: &Type, out: &mut dyn Write, ctx: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn to_sql<W: Write + ?Sized>( &self, _: &Type, w: &mut W, _: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn accepts(ty: &Type) -> bool

source§

impl ToSql for i8

source§

fn to_sql_checked( &self, ty: &Type, out: &mut dyn Write, ctx: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn to_sql<W: Write + ?Sized>( &self, _: &Type, w: &mut W, _: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn accepts(ty: &Type) -> bool

source§

impl ToSql for i16

source§

fn to_sql_checked( &self, ty: &Type, out: &mut dyn Write, ctx: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn to_sql<W: Write + ?Sized>( &self, _: &Type, w: &mut W, _: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn accepts(ty: &Type) -> bool

source§

impl ToSql for i32

source§

fn to_sql_checked( &self, ty: &Type, out: &mut dyn Write, ctx: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn to_sql<W: Write + ?Sized>( &self, _: &Type, w: &mut W, _: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn accepts(ty: &Type) -> bool

source§

impl ToSql for i64

source§

fn to_sql_checked( &self, ty: &Type, out: &mut dyn Write, ctx: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn to_sql<W: Write + ?Sized>( &self, _: &Type, w: &mut W, _: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn accepts(ty: &Type) -> bool

source§

impl ToSql for u32

source§

fn to_sql_checked( &self, ty: &Type, out: &mut dyn Write, ctx: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn to_sql<W: Write + ?Sized>( &self, _: &Type, w: &mut W, _: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn accepts(ty: &Type) -> bool

source§

impl ToSql for String

source§

fn to_sql_checked( &self, ty: &Type, out: &mut dyn Write, ctx: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn to_sql<W: Write + ?Sized>( &self, ty: &Type, w: &mut W, ctx: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn accepts(ty: &Type) -> bool

source§

impl ToSql for Vec<u8>

source§

fn to_sql_checked( &self, ty: &Type, out: &mut dyn Write, ctx: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn to_sql<W: Write + ?Sized>( &self, ty: &Type, w: &mut W, ctx: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn accepts(ty: &Type) -> bool

source§

impl ToSql for HashMap<String, Option<String>>

source§

fn to_sql_checked( &self, ty: &Type, out: &mut dyn Write, ctx: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn to_sql<W: Write + ?Sized>( &self, _: &Type, w: &mut W, _: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn accepts(ty: &Type) -> bool

source§

impl<'a> ToSql for &'a str

source§

fn to_sql_checked( &self, ty: &Type, out: &mut dyn Write, ctx: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn to_sql<W: Write + ?Sized>( &self, _: &Type, w: &mut W, _: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn accepts(ty: &Type) -> bool

source§

impl<'a> ToSql for &'a [u8]

source§

fn to_sql_checked( &self, ty: &Type, out: &mut dyn Write, ctx: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn to_sql<W: Write + ?Sized>( &self, _: &Type, w: &mut W, _: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn accepts(ty: &Type) -> bool

source§

impl<T: ToSql> ToSql for Option<T>

source§

fn to_sql_checked( &self, ty: &Type, out: &mut dyn Write, ctx: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn to_sql<W: Write + ?Sized>( &self, ty: &Type, out: &mut W, ctx: &SessionInfo<'_>, ) -> Result<IsNull>

source§

fn accepts(ty: &Type) -> bool

Implementors§

source§

impl<'a, T: 'a + ToSql> ToSql for Slice<'a, T>