diesel::serialize

Trait ToSql

Source
pub trait ToSql<A, DB: Backend>: Debug {
    // Required method
    fn to_sql<W: Write>(&self, out: &mut Output<'_, W, DB>) -> Result;
}
Expand description

Serializes a single value to be sent to the database.

The output is sent as a bind parameter, and the data must be written in the expected format for the given backend.

When possible, implementations of this trait should prefer using an existing implementation, rather than writing to out directly. (For example, if you are implementing this for an enum, which is represented as an integer in the database, you should use i32::to_sql(x, out) instead of writing to out yourself.

Any types which implement this trait should also #[derive(AsExpression)].

§Backend specific details

  • For PostgreSQL, the bytes will be sent using the binary protocol, not text.
  • For SQLite, all implementations should be written in terms of an existing ToSql implementation.
  • For MySQL, the expected bytes will depend on the return value of type_metadata for the given SQL type. See MysqlType for details.
  • For third party backends, consult that backend’s documentation.

§Examples

Most implementations of this trait will be defined in terms of an existing implementation.

#[repr(i32)]
#[derive(Debug, Clone, Copy)]
pub enum MyEnum {
    A = 1,
    B = 2,
}

impl<DB> ToSql<Integer, DB> for MyEnum
where
    DB: Backend,
    i32: ToSql<Integer, DB>,
{
    fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> serialize::Result {
        (*self as i32).to_sql(out)
    }
}

Required Methods§

Source

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, DB>) -> Result

See the trait documentation.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl ToSql<Cidr, Pg> for IpNetwork

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> Result

Source§

impl ToSql<Inet, Pg> for IpNetwork

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> Result

Source§

impl ToSql<Json, Pg> for Value

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> Result

Source§

impl ToSql<Jsonb, Pg> for Value

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> Result

Source§

impl ToSql<MacAddr, Pg> for [u8; 6]

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> Result

Source§

impl ToSql<Oid, Pg> for u32

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> Result

Source§

impl ToSql<Timestamptz, Pg> for NaiveDateTime

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> Result

Source§

impl ToSql<Uuid, Pg> for Uuid

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> Result

Source§

impl ToSql<Uuid, Pg> for Uuid

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> Result

Source§

impl ToSql<Bool, Pg> for bool

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> Result

Source§

impl ToSql<Date, Pg> for NaiveDate

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> Result

Source§

impl ToSql<Time, Pg> for NaiveTime

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> Result

Source§

impl ToSql<Timestamp, Pg> for NaiveDateTime

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> Result

Source§

impl ToSql<Timestamp, Pg> for SystemTime

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> Result

Source§

impl<'a, A, T, DB> ToSql<A, DB> for &'a T
where DB: Backend, T: ToSql<A, DB> + ?Sized,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, DB>) -> Result

Source§

impl<'a, T, ST, DB> ToSql<ST, DB> for Cow<'a, T>
where T: 'a + ToOwned + ToSql<ST, DB> + ?Sized, DB: Backend, Self: Debug,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, DB>) -> Result

Source§

impl<DB> ToSql<Binary, DB> for Vec<u8>
where DB: Backend, [u8]: ToSql<Binary, DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, DB>) -> Result

Source§

impl<DB> ToSql<Text, DB> for String
where DB: Backend, str: ToSql<Text, DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, DB>) -> Result

Source§

impl<DB: Backend> ToSql<BigInt, DB> for i64

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, DB>) -> Result

Source§

impl<DB: Backend> ToSql<Binary, DB> for [u8]

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, DB>) -> Result

Source§

impl<DB: Backend> ToSql<Double, DB> for f64

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, DB>) -> Result

Source§

impl<DB: Backend> ToSql<Float, DB> for f32

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, DB>) -> Result

Source§

impl<DB: Backend> ToSql<Integer, DB> for i32

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, DB>) -> Result

Source§

impl<DB: Backend> ToSql<SmallInt, DB> for i16

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, DB>) -> Result

Source§

impl<DB: Backend> ToSql<Text, DB> for str

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, DB>) -> Result

Source§

impl<ST, T> ToSql<Array<ST>, Pg> for [T]
where Pg: HasSqlType<ST>, T: ToSql<ST, Pg>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> Result

Source§

impl<ST, T> ToSql<Array<ST>, Pg> for Vec<T>
where [T]: ToSql<Array<ST>, Pg>, T: Debug,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> Result

Source§

impl<ST, T> ToSql<Range<ST>, Pg> for (Bound<T>, Bound<T>)
where T: ToSql<ST, Pg>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> Result

Source§

impl<ST, T> ToSql<Nullable<Array<ST>>, Pg> for [T]
where [T]: ToSql<Array<ST>, Pg>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> Result

Source§

impl<ST, T> ToSql<Nullable<Array<ST>>, Pg> for Vec<T>
where Vec<T>: ToSql<Array<ST>, Pg>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> Result

Source§

impl<ST, T> ToSql<Nullable<Range<ST>>, Pg> for (Bound<T>, Bound<T>)
where (Bound<T>, Bound<T>): ToSql<Range<ST>, Pg>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> Result

Source§

impl<T, ST, DB> ToSql<Nullable<ST>, DB> for Option<T>
where T: ToSql<ST, DB>, DB: Backend, ST: NotNull,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, DB>) -> Result

Source§

impl<TZ: TimeZone> ToSql<Timestamptz, Pg> for DateTime<TZ>

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> Result

Source§

impl<Tz: TimeZone, __DB> ToSql<Nullable<Timestamptz>, __DB> for DateTime<Tz>
where __DB: Backend, Self: ToSql<Timestamptz, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<Cidr>, __DB> for IpNetwork
where __DB: Backend, Self: ToSql<Cidr, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<Inet>, __DB> for IpNetwork
where __DB: Backend, Self: ToSql<Inet, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<Json>, __DB> for Value
where __DB: Backend, Self: ToSql<Json, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<Jsonb>, __DB> for Value
where __DB: Backend, Self: ToSql<Jsonb, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<MacAddr>, __DB> for [u8; 6]
where __DB: Backend, Self: ToSql<MacAddr, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<Oid>, __DB> for u32
where __DB: Backend, Self: ToSql<Oid, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<Timestamptz>, __DB> for NaiveDateTime
where __DB: Backend, Self: ToSql<Timestamptz, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<Uuid>, __DB> for Uuid
where __DB: Backend, Self: ToSql<Uuid, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<Uuid>, __DB> for Uuid
where __DB: Backend, Self: ToSql<Uuid, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<BigInt>, __DB> for i64
where __DB: Backend, Self: ToSql<BigInt, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<Binary>, __DB> for Vec<u8>
where __DB: Backend, Self: ToSql<Binary, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<Binary>, __DB> for [u8]
where __DB: Backend, Self: ToSql<Binary, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<Bool>, __DB> for bool
where __DB: Backend, Self: ToSql<Bool, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<Date>, __DB> for NaiveDate
where __DB: Backend, Self: ToSql<Date, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<Double>, __DB> for f64
where __DB: Backend, Self: ToSql<Double, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<Float>, __DB> for f32
where __DB: Backend, Self: ToSql<Float, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<Integer>, __DB> for i32
where __DB: Backend, Self: ToSql<Integer, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<SmallInt>, __DB> for i16
where __DB: Backend, Self: ToSql<SmallInt, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<Text>, __DB> for str
where __DB: Backend, Self: ToSql<Text, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<Text>, __DB> for String
where __DB: Backend, Self: ToSql<Text, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<Time>, __DB> for NaiveTime
where __DB: Backend, Self: ToSql<Time, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<Timestamp>, __DB> for NaiveDateTime
where __DB: Backend, Self: ToSql<Timestamp, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Source§

impl<__DB> ToSql<Nullable<Timestamp>, __DB> for SystemTime
where __DB: Backend, Self: ToSql<Timestamp, __DB>,

Source§

fn to_sql<W: Write>(&self, out: &mut Output<'_, W, __DB>) -> Result

Implementors§

Source§

impl ToSql<Money, Pg> for PgMoney

Source§

impl ToSql<Timestamptz, Pg> for PgTimestamp

Source§

impl ToSql<Date, Pg> for PgDate

Source§

impl ToSql<Interval, Pg> for PgInterval

Source§

impl ToSql<Numeric, Pg> for PgNumeric

Source§

impl ToSql<Time, Pg> for PgTime

Source§

impl ToSql<Timestamp, Pg> for PgTimestamp

Source§

impl<__DB> ToSql<Nullable<Money>, __DB> for PgMoney
where __DB: Backend, Self: ToSql<Money, __DB>,

Source§

impl<__DB> ToSql<Nullable<Timestamptz>, __DB> for PgTimestamp
where __DB: Backend, Self: ToSql<Timestamptz, __DB>,

Source§

impl<__DB> ToSql<Nullable<Date>, __DB> for PgDate
where __DB: Backend, Self: ToSql<Date, __DB>,

Source§

impl<__DB> ToSql<Nullable<Interval>, __DB> for PgInterval
where __DB: Backend, Self: ToSql<Interval, __DB>,

Source§

impl<__DB> ToSql<Nullable<Numeric>, __DB> for PgNumeric
where __DB: Backend, Self: ToSql<Numeric, __DB>,

Source§

impl<__DB> ToSql<Nullable<Time>, __DB> for PgTime
where __DB: Backend, Self: ToSql<Time, __DB>,

Source§

impl<__DB> ToSql<Nullable<Timestamp>, __DB> for PgTimestamp
where __DB: Backend, Self: ToSql<Timestamp, __DB>,