diesel::serialize

Trait ToSql

source
pub trait ToSql<A, DB: Backend>: Debug {
    // Required method
    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, 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, AsExpression)]
#[diesel(sql_type = Integer)]
pub enum MyEnum {
    A = 1,
    B = 2,
}

impl<DB> ToSql<Integer, DB> for MyEnum
where
    DB: Backend,
    i32: ToSql<Integer, DB>,
{
    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result {
        match self {
            MyEnum::A => 1.to_sql(out),
            MyEnum::B => 2.to_sql(out),
        }
    }
}

Using temporary values as part of the ToSql implementation requires additional work.

Backends using RawBytesBindCollector as BindCollector copy the serialized values as part of Write implementation. This includes the Mysql and the Pg backend provided by diesel. This means existing ToSql implementations can be used even with temporary values. For these it is required to call Output::reborrow to shorten the lifetime of the Output type correspondingly.

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

impl ToSql<Integer, diesel::pg::Pg> for MyEnum
where
    i32: ToSql<Integer, diesel::pg::Pg>,
{
    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, diesel::pg::Pg>) -> serialize::Result {
        let v = *self as i32;
        <i32 as ToSql<Integer, diesel::pg::Pg>>::to_sql(&v, &mut out.reborrow())
    }
}

For any other backend the Output::set_value method provides a way to set the output value directly. Checkout the documentation of the corresponding BindCollector::Buffer type for provided From<T> implementations for a list of accepted types. For the Sqlite backend see SqliteBindValue.

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

impl ToSql<Integer, diesel::sqlite::Sqlite> for MyEnum
where
    i32: ToSql<Integer, diesel::sqlite::Sqlite>,
{
    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, diesel::sqlite::Sqlite>) -> serialize::Result {
        out.set_value(*self as i32);
        Ok(IsNull::No)
    }
}

Required Methods§

source

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

See the trait documentation.

Implementations on Foreign Types§

source§

impl ToSql<BigInt, Pg> for i64

source§

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

source§

impl ToSql<Bool, Pg> for bool

source§

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

source§

impl ToSql<CChar, Pg> for u8

source§

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

source§

impl ToSql<Citext, Pg> for str

source§

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

source§

impl ToSql<Citext, Pg> for String

source§

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

source§

impl ToSql<Date, Pg> for NaiveDate

source§

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

source§

impl ToSql<Double, Pg> for f64

source§

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

source§

impl ToSql<Float, Pg> for f32

source§

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

source§

impl ToSql<Integer, Pg> for i32

source§

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

source§

impl ToSql<Interval, Pg> for Duration

source§

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

source§

impl ToSql<Json, Pg> for Value

source§

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

source§

impl ToSql<Jsonb, Pg> for Value

source§

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

source§

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

source§

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

source§

impl ToSql<Oid, Pg> for u32

source§

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

source§

impl ToSql<SmallInt, Pg> for i16

source§

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

source§

impl ToSql<Time, Pg> for NaiveTime

source§

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

source§

impl ToSql<Timestamp, Pg> for NaiveDateTime

source§

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

source§

impl ToSql<Timestamp, Pg> for SystemTime

source§

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

source§

impl ToSql<Timestamptz, Pg> for NaiveDateTime

source§

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

source§

impl ToSql<Uuid, Pg> for Uuid

source§

fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, 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<'b>(&'b self, out: &mut Output<'b, '_, 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<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> Result

source§

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

source§

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

source§

impl<DB> ToSql<Binary, DB> for [u8]
where for<'a> DB: Backend<BindCollector<'a> = RawBytesBindCollector<DB>>,

source§

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

source§

impl<DB> ToSql<Text, DB> for str
where for<'a> DB: Backend<BindCollector<'a> = RawBytesBindCollector<DB>>,

source§

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

source§

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

source§

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

source§

impl<DB, const N: usize> ToSql<Binary, DB> for [u8; N]
where DB: Backend, [u8]: ToSql<Binary, DB>,

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, 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<'__b>(&'__b self, out: &mut Output<'__b, '_, __DB>) -> Result

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

Implementors§

source§

impl ToSql<Date, Pg> for PgDate

source§

impl ToSql<Interval, Pg> for PgInterval

source§

impl ToSql<Money, Pg> for PgMoney

source§

impl ToSql<Numeric, Pg> for PgNumeric

source§

impl ToSql<Time, Pg> for PgTime

source§

impl ToSql<Timestamp, Pg> for PgTimestamp

source§

impl ToSql<Timestamptz, Pg> for PgTimestamp

source§

impl<'a, DB> ToSql<Text, DB> for MigrationVersion<'a>
where Cow<'a, str>: ToSql<Text, DB>, DB: Backend,

source§

impl<'a, __DB> ToSql<Nullable<Text>, __DB> for MigrationVersion<'a>
where __DB: Backend, Self: ToSql<Text, __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<Money>, __DB> for PgMoney
where __DB: Backend, Self: ToSql<Money, __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>,

source§

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