pub trait ToSql<A, DB: Backend>: Debug {
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. SeeMysqlType
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
implemenation 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
implemenations can be used even with
temporary values. For these it is required to call
Output::reborrow
to shorten the lifetime of the Output
type correspondenly.
#[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
Implementations on Foreign Types
sourceimpl ToSql<Datetime, Mysql> for NaiveDateTime
Available on crate features chrono
and mysql_backend
only.
impl ToSql<Datetime, Mysql> for NaiveDateTime
chrono
and mysql_backend
only.sourceimpl ToSql<Timestamp, Mysql> for NaiveDateTime
Available on crate features chrono
and mysql_backend
only.
impl ToSql<Timestamp, Mysql> for NaiveDateTime
chrono
and mysql_backend
only.sourceimpl ToSql<Time, Mysql> for NaiveTime
Available on crate features chrono
and mysql_backend
only.
impl ToSql<Time, Mysql> for NaiveTime
chrono
and mysql_backend
only.sourceimpl ToSql<Date, Mysql> for NaiveDate
Available on crate features chrono
and mysql_backend
only.
impl ToSql<Date, Mysql> for NaiveDate
chrono
and mysql_backend
only.sourceimpl ToSql<Json, Mysql> for Value
Available on crate features mysql_backend
and serde_json
only.
impl ToSql<Json, Mysql> for Value
mysql_backend
and serde_json
only.sourceimpl ToSql<Numeric, Mysql> for BigDecimal
Available on crate features mysql_backend
and bigdecimal
only.
impl ToSql<Numeric, Mysql> for BigDecimal
mysql_backend
and bigdecimal
only.sourceimpl<ST, T> ToSql<Array<ST>, Pg> for [T] where
Pg: HasSqlType<ST>,
T: ToSql<ST, Pg>,
Available on crate feature postgres_backend
only.
impl<ST, T> ToSql<Array<ST>, Pg> for [T] where
Pg: HasSqlType<ST>,
T: ToSql<ST, Pg>,
postgres_backend
only.sourceimpl<ST, T> ToSql<Nullable<Array<ST>>, Pg> for [T] where
[T]: ToSql<Array<ST>, Pg>,
ST: 'static,
Available on crate feature postgres_backend
only.
impl<ST, T> ToSql<Nullable<Array<ST>>, Pg> for [T] where
[T]: ToSql<Array<ST>, Pg>,
ST: 'static,
postgres_backend
only.sourceimpl<ST, T> ToSql<Array<ST>, Pg> for Vec<T> where
ST: 'static,
[T]: ToSql<Array<ST>, Pg>,
T: Debug,
Available on crate feature postgres_backend
only.
impl<ST, T> ToSql<Array<ST>, Pg> for Vec<T> where
ST: 'static,
[T]: ToSql<Array<ST>, Pg>,
T: Debug,
postgres_backend
only.sourceimpl<ST, T> ToSql<Nullable<Array<ST>>, Pg> for Vec<T> where
ST: 'static,
Vec<T>: ToSql<Array<ST>, Pg>,
Available on crate feature postgres_backend
only.
impl<ST, T> ToSql<Nullable<Array<ST>>, Pg> for Vec<T> where
ST: 'static,
Vec<T>: ToSql<Array<ST>, Pg>,
postgres_backend
only.sourceimpl ToSql<Timestamp, Pg> for NaiveDateTime
Available on crate features chrono
and postgres_backend
only.
impl ToSql<Timestamp, Pg> for NaiveDateTime
chrono
and postgres_backend
only.sourceimpl ToSql<Timestamptz, Pg> for NaiveDateTime
Available on crate features chrono
and postgres_backend
only.
impl ToSql<Timestamptz, Pg> for NaiveDateTime
chrono
and postgres_backend
only.sourceimpl<TZ: TimeZone> ToSql<Timestamptz, Pg> for DateTime<TZ>
Available on crate features chrono
and postgres_backend
only.
impl<TZ: TimeZone> ToSql<Timestamptz, Pg> for DateTime<TZ>
chrono
and postgres_backend
only.sourceimpl ToSql<Time, Pg> for NaiveTime
Available on crate features chrono
and postgres_backend
only.
impl ToSql<Time, Pg> for NaiveTime
chrono
and postgres_backend
only.sourceimpl ToSql<Date, Pg> for NaiveDate
Available on crate features chrono
and postgres_backend
only.
impl ToSql<Date, Pg> for NaiveDate
chrono
and postgres_backend
only.sourceimpl ToSql<Timestamp, Pg> for SystemTime
Available on crate feature postgres_backend
only.
impl ToSql<Timestamp, Pg> for SystemTime
postgres_backend
only.sourceimpl ToSql<Json, Pg> for Value
Available on crate features postgres_backend
and serde_json
only.
impl ToSql<Json, Pg> for Value
postgres_backend
and serde_json
only.sourceimpl ToSql<Jsonb, Pg> for Value
Available on crate features postgres_backend
and serde_json
only.
impl ToSql<Jsonb, Pg> for Value
postgres_backend
and serde_json
only.sourceimpl<__DB> ToSql<Nullable<MacAddr>, __DB> for [u8; 6] where
__DB: Backend,
Self: ToSql<MacAddr, __DB>,
impl<__DB> ToSql<Nullable<MacAddr>, __DB> for [u8; 6] where
__DB: Backend,
Self: ToSql<MacAddr, __DB>,
sourceimpl<__DB> ToSql<Nullable<Inet>, __DB> for IpNetwork where
__DB: Backend,
Self: ToSql<Inet, __DB>,
impl<__DB> ToSql<Nullable<Inet>, __DB> for IpNetwork where
__DB: Backend,
Self: ToSql<Inet, __DB>,
sourceimpl<__DB> ToSql<Nullable<Cidr>, __DB> for IpNetwork where
__DB: Backend,
Self: ToSql<Cidr, __DB>,
impl<__DB> ToSql<Nullable<Cidr>, __DB> for IpNetwork where
__DB: Backend,
Self: ToSql<Cidr, __DB>,
sourceimpl ToSql<Inet, Pg> for IpNetwork
Available on crate features postgres_backend
and network-address
only.
impl ToSql<Inet, Pg> for IpNetwork
postgres_backend
and network-address
only.sourceimpl ToSql<Cidr, Pg> for IpNetwork
Available on crate features postgres_backend
and network-address
only.
impl ToSql<Cidr, Pg> for IpNetwork
postgres_backend
and network-address
only.sourceimpl ToSql<Numeric, Pg> for BigDecimal
Available on crate features postgres_backend
and bigdecimal
only.
impl ToSql<Numeric, Pg> for BigDecimal
postgres_backend
and bigdecimal
only.sourceimpl<ST, T> ToSql<Range<ST>, Pg> for (Bound<T>, Bound<T>) where
T: ToSql<ST, Pg>,
Available on crate feature postgres_backend
only.
impl<ST, T> ToSql<Range<ST>, Pg> for (Bound<T>, Bound<T>) where
T: ToSql<ST, Pg>,
postgres_backend
only.sourceimpl<ST, T> ToSql<Nullable<Range<ST>>, Pg> for (Bound<T>, Bound<T>) where
ST: 'static,
(Bound<T>, Bound<T>): ToSql<Range<ST>, Pg>,
Available on crate feature postgres_backend
only.
impl<ST, T> ToSql<Nullable<Range<ST>>, Pg> for (Bound<T>, Bound<T>) where
ST: 'static,
(Bound<T>, Bound<T>): ToSql<Range<ST>, Pg>,
postgres_backend
only.sourceimpl ToSql<Timestamp, Sqlite> for NaiveDateTime
Available on crate features sqlite
and chrono
only.
impl ToSql<Timestamp, Sqlite> for NaiveDateTime
sqlite
and chrono
only.sourceimpl ToSql<Timestamptz, Sqlite> for NaiveDateTime
Available on crate features sqlite
and chrono
only.
impl ToSql<Timestamptz, Sqlite> for NaiveDateTime
sqlite
and chrono
only.sourceimpl<TZ: TimeZone> ToSql<Timestamptz, Sqlite> for DateTime<TZ>
Available on crate features sqlite
and chrono
only.
impl<TZ: TimeZone> ToSql<Timestamptz, Sqlite> for DateTime<TZ>
sqlite
and chrono
only.sourceimpl ToSql<Timestamptz, Sqlite> for str
Available on crate feature sqlite
only.
impl ToSql<Timestamptz, Sqlite> for str
sqlite
only.sourceimpl ToSql<Timestamptz, Sqlite> for String
Available on crate feature sqlite
only.
impl ToSql<Timestamptz, Sqlite> for String
sqlite
only.sourceimpl<__DB> ToSql<Nullable<Timestamp>, __DB> for SystemTime where
__DB: Backend,
Self: ToSql<Timestamp, __DB>,
impl<__DB> ToSql<Nullable<Timestamp>, __DB> for SystemTime where
__DB: Backend,
Self: ToSql<Timestamp, __DB>,
sourceimpl<__DB> ToSql<Nullable<Date>, __DB> for NaiveDate where
__DB: Backend,
Self: ToSql<Date, __DB>,
impl<__DB> ToSql<Nullable<Date>, __DB> for NaiveDate where
__DB: Backend,
Self: ToSql<Date, __DB>,
sourceimpl<__DB> ToSql<Nullable<Time>, __DB> for NaiveTime where
__DB: Backend,
Self: ToSql<Time, __DB>,
impl<__DB> ToSql<Nullable<Time>, __DB> for NaiveTime where
__DB: Backend,
Self: ToSql<Time, __DB>,
sourceimpl<__DB> ToSql<Nullable<Timestamp>, __DB> for NaiveDateTime where
__DB: Backend,
Self: ToSql<Timestamp, __DB>,
impl<__DB> ToSql<Nullable<Timestamp>, __DB> for NaiveDateTime where
__DB: Backend,
Self: ToSql<Timestamp, __DB>,
sourceimpl<__DB> ToSql<Nullable<Timestamptz>, __DB> for NaiveDateTime where
__DB: Backend,
Self: ToSql<Timestamptz, __DB>,
impl<__DB> ToSql<Nullable<Timestamptz>, __DB> for NaiveDateTime where
__DB: Backend,
Self: ToSql<Timestamptz, __DB>,
sourceimpl<__DB> ToSql<Nullable<Datetime>, __DB> for NaiveDateTime where
__DB: Backend,
Self: ToSql<Datetime, __DB>,
impl<__DB> ToSql<Nullable<Datetime>, __DB> for NaiveDateTime where
__DB: Backend,
Self: ToSql<Datetime, __DB>,
sourceimpl<Tz: TimeZone, __DB> ToSql<Nullable<Timestamptz>, __DB> for DateTime<Tz> where
__DB: Backend,
Self: ToSql<Timestamptz, __DB>,
impl<Tz: TimeZone, __DB> ToSql<Nullable<Timestamptz>, __DB> for DateTime<Tz> where
__DB: Backend,
Self: ToSql<Timestamptz, __DB>,
sourceimpl<Tz: TimeZone, __DB> ToSql<Nullable<Timestamptz>, __DB> for DateTime<Tz> where
__DB: Backend,
Self: ToSql<TimestamptzSqlite, __DB>,
impl<Tz: TimeZone, __DB> ToSql<Nullable<Timestamptz>, __DB> for DateTime<Tz> where
__DB: Backend,
Self: ToSql<TimestamptzSqlite, __DB>,
sourceimpl<__DB> ToSql<Nullable<Numeric>, __DB> for BigDecimal where
__DB: Backend,
Self: ToSql<Numeric, __DB>,
impl<__DB> ToSql<Nullable<Numeric>, __DB> for BigDecimal where
__DB: Backend,
Self: ToSql<Numeric, __DB>,
sourceimpl<__DB> ToSql<Nullable<Json>, __DB> for Value where
__DB: Backend,
Self: ToSql<Json, __DB>,
impl<__DB> ToSql<Nullable<Json>, __DB> for Value where
__DB: Backend,
Self: ToSql<Json, __DB>,
sourceimpl<__DB> ToSql<Nullable<Jsonb>, __DB> for Value where
__DB: Backend,
Self: ToSql<Jsonb, __DB>,
impl<__DB> ToSql<Nullable<Jsonb>, __DB> for Value where
__DB: Backend,
Self: ToSql<Jsonb, __DB>,
sourceimpl<T, ST, DB> ToSql<Nullable<ST>, DB> for Option<T> where
T: ToSql<ST, DB>,
DB: Backend,
ST: SqlType<IsNull = NotNull>,
impl<T, ST, DB> ToSql<Nullable<ST>, DB> for Option<T> where
T: ToSql<ST, DB>,
DB: Backend,
ST: SqlType<IsNull = NotNull>,
sourceimpl<__DB> ToSql<Nullable<TinyInt>, __DB> for i8 where
__DB: Backend,
Self: ToSql<TinyInt, __DB>,
impl<__DB> ToSql<Nullable<TinyInt>, __DB> for i8 where
__DB: Backend,
Self: ToSql<TinyInt, __DB>,
sourceimpl<__DB> ToSql<Nullable<SmallInt>, __DB> for i16 where
__DB: Backend,
Self: ToSql<SmallInt, __DB>,
impl<__DB> ToSql<Nullable<SmallInt>, __DB> for i16 where
__DB: Backend,
Self: ToSql<SmallInt, __DB>,
sourceimpl<__DB> ToSql<Nullable<Integer>, __DB> for i32 where
__DB: Backend,
Self: ToSql<Integer, __DB>,
impl<__DB> ToSql<Nullable<Integer>, __DB> for i32 where
__DB: Backend,
Self: ToSql<Integer, __DB>,
sourceimpl<__DB> ToSql<Nullable<BigInt>, __DB> for i64 where
__DB: Backend,
Self: ToSql<BigInt, __DB>,
impl<__DB> ToSql<Nullable<BigInt>, __DB> for i64 where
__DB: Backend,
Self: ToSql<BigInt, __DB>,
sourceimpl<__DB> ToSql<Nullable<Unsigned<TinyInt>>, __DB> for u8 where
__DB: Backend,
Self: ToSql<Unsigned<TinyInt>, __DB>,
impl<__DB> ToSql<Nullable<Unsigned<TinyInt>>, __DB> for u8 where
__DB: Backend,
Self: ToSql<Unsigned<TinyInt>, __DB>,
sourceimpl<__DB> ToSql<Nullable<Unsigned<SmallInt>>, __DB> for u16 where
__DB: Backend,
Self: ToSql<Unsigned<SmallInt>, __DB>,
impl<__DB> ToSql<Nullable<Unsigned<SmallInt>>, __DB> for u16 where
__DB: Backend,
Self: ToSql<Unsigned<SmallInt>, __DB>,
sourceimpl<__DB> ToSql<Nullable<Unsigned<Integer>>, __DB> for u32 where
__DB: Backend,
Self: ToSql<Unsigned<Integer>, __DB>,
impl<__DB> ToSql<Nullable<Unsigned<Integer>>, __DB> for u32 where
__DB: Backend,
Self: ToSql<Unsigned<Integer>, __DB>,
sourceimpl<__DB> ToSql<Nullable<Unsigned<BigInt>>, __DB> for u64 where
__DB: Backend,
Self: ToSql<Unsigned<BigInt>, __DB>,
impl<__DB> ToSql<Nullable<Unsigned<BigInt>>, __DB> for u64 where
__DB: Backend,
Self: ToSql<Unsigned<BigInt>, __DB>,
sourceimpl<__DB> ToSql<Nullable<Float>, __DB> for f32 where
__DB: Backend,
Self: ToSql<Float, __DB>,
impl<__DB> ToSql<Nullable<Float>, __DB> for f32 where
__DB: Backend,
Self: ToSql<Float, __DB>,
sourceimpl<__DB> ToSql<Nullable<Double>, __DB> for f64 where
__DB: Backend,
Self: ToSql<Double, __DB>,
impl<__DB> ToSql<Nullable<Double>, __DB> for f64 where
__DB: Backend,
Self: ToSql<Double, __DB>,
sourceimpl<__DB> ToSql<Nullable<Text>, __DB> for String where
__DB: Backend,
Self: ToSql<Text, __DB>,
impl<__DB> ToSql<Nullable<Text>, __DB> for String where
__DB: Backend,
Self: ToSql<Text, __DB>,
sourceimpl<__DB> ToSql<Nullable<Date>, __DB> for String where
__DB: Backend,
Self: ToSql<Date, __DB>,
impl<__DB> ToSql<Nullable<Date>, __DB> for String where
__DB: Backend,
Self: ToSql<Date, __DB>,
sourceimpl<__DB> ToSql<Nullable<Time>, __DB> for String where
__DB: Backend,
Self: ToSql<Time, __DB>,
impl<__DB> ToSql<Nullable<Time>, __DB> for String where
__DB: Backend,
Self: ToSql<Time, __DB>,
sourceimpl<__DB> ToSql<Nullable<Timestamp>, __DB> for String where
__DB: Backend,
Self: ToSql<Timestamp, __DB>,
impl<__DB> ToSql<Nullable<Timestamp>, __DB> for String where
__DB: Backend,
Self: ToSql<Timestamp, __DB>,
sourceimpl<__DB> ToSql<Nullable<Timestamp>, __DB> for str where
__DB: Backend,
Self: ToSql<Timestamp, __DB>,
impl<__DB> ToSql<Nullable<Timestamp>, __DB> for str where
__DB: Backend,
Self: ToSql<Timestamp, __DB>,
sourceimpl<__DB> ToSql<Nullable<Binary>, __DB> for Vec<u8> where
__DB: Backend,
Self: ToSql<Binary, __DB>,
impl<__DB> ToSql<Nullable<Binary>, __DB> for Vec<u8> where
__DB: Backend,
Self: ToSql<Binary, __DB>,
sourceimpl<__DB> ToSql<Nullable<Binary>, __DB> for [u8] where
__DB: Backend,
Self: ToSql<Binary, __DB>,
impl<__DB> ToSql<Nullable<Binary>, __DB> for [u8] where
__DB: Backend,
Self: ToSql<Binary, __DB>,
sourceimpl<DB> ToSql<Text, DB> for str where
DB: Backend<BindCollector = RawBytesBindCollector<DB>>,
impl<DB> ToSql<Text, DB> for str where
DB: Backend<BindCollector = RawBytesBindCollector<DB>>,
sourceimpl<DB> ToSql<Binary, DB> for [u8] where
DB: Backend<BindCollector = RawBytesBindCollector<DB>>,
impl<DB> ToSql<Binary, DB> for [u8] where
DB: Backend<BindCollector = RawBytesBindCollector<DB>>,
sourceimpl<'a, T: ?Sized, ST, DB> ToSql<ST, DB> for Cow<'a, T> where
T: 'a + ToOwned + ToSql<ST, DB>,
DB: Backend,
Self: Debug,
impl<'a, T: ?Sized, ST, DB> ToSql<ST, DB> for Cow<'a, T> where
T: 'a + ToOwned + ToSql<ST, DB>,
DB: Backend,
Self: Debug,
Implementors
impl ToSql<Date, Mysql> for MysqlTime
mysql_backend
only.impl ToSql<Date, Pg> for PgDate
postgres_backend
only.impl ToSql<Datetime, Mysql> for MysqlTime
mysql_backend
only.impl ToSql<Interval, Pg> for PgInterval
postgres_backend
only.impl ToSql<Money, Pg> for PgMoney
postgres_backend
only.impl ToSql<Numeric, Pg> for PgNumeric
postgres_backend
only.impl ToSql<Time, Mysql> for MysqlTime
mysql_backend
only.impl ToSql<Time, Pg> for PgTime
postgres_backend
only.impl ToSql<Timestamp, Mysql> for MysqlTime
mysql_backend
only.impl ToSql<Timestamp, Pg> for PgTimestamp
postgres_backend
only.impl ToSql<Timestamptz, Pg> for PgTimestamp
postgres_backend
only.