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. 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
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§
Implementations on Foreign Types§
source§impl ToSql<Cidr, Pg> for IpNet
Available on crate features ipnet-address
and postgres_backend
only.
impl ToSql<Cidr, Pg> for IpNet
ipnet-address
and postgres_backend
only.source§impl ToSql<Cidr, Pg> for IpNetwork
Available on crate features network-address
and postgres_backend
only.
impl ToSql<Cidr, Pg> for IpNetwork
network-address
and postgres_backend
only.source§impl 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.source§impl 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.source§impl 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.source§impl ToSql<Datetime, Mysql> for OffsetDateTime
Available on crate features time
and mysql_backend
only.
impl ToSql<Datetime, Mysql> for OffsetDateTime
time
and mysql_backend
only.source§impl ToSql<Datetime, Mysql> for PrimitiveDateTime
Available on crate features time
and mysql_backend
only.
impl ToSql<Datetime, Mysql> for PrimitiveDateTime
time
and mysql_backend
only.source§impl ToSql<Inet, Pg> for IpNet
Available on crate features ipnet-address
and postgres_backend
only.
impl ToSql<Inet, Pg> for IpNet
ipnet-address
and postgres_backend
only.source§impl ToSql<Inet, Pg> for IpNetwork
Available on crate features network-address
and postgres_backend
only.
impl ToSql<Inet, Pg> for IpNetwork
network-address
and postgres_backend
only.source§impl ToSql<Json, Mysql> for Value
Available on crate features serde_json
and mysql_backend
only.
impl ToSql<Json, Mysql> for Value
serde_json
and mysql_backend
only.source§impl ToSql<Json, Pg> for Value
Available on crate features serde_json
and postgres_backend
only.
impl ToSql<Json, Pg> for Value
serde_json
and postgres_backend
only.source§impl ToSql<Jsonb, Pg> for Value
Available on crate features serde_json
and postgres_backend
only.
impl ToSql<Jsonb, Pg> for Value
serde_json
and postgres_backend
only.source§impl ToSql<Numeric, Mysql> for BigDecimal
Available on crate features bigdecimal
and mysql_backend
only.
impl ToSql<Numeric, Mysql> for BigDecimal
bigdecimal
and mysql_backend
only.source§impl ToSql<Numeric, Pg> for BigDecimal
Available on crate features bigdecimal
and postgres_backend
only.
impl ToSql<Numeric, Pg> for BigDecimal
bigdecimal
and postgres_backend
only.source§impl 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.source§impl 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.source§impl 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.source§impl ToSql<Timestamp, Mysql> for OffsetDateTime
Available on crate features time
and mysql_backend
only.
impl ToSql<Timestamp, Mysql> for OffsetDateTime
time
and mysql_backend
only.source§impl ToSql<Timestamp, Mysql> for PrimitiveDateTime
Available on crate features time
and mysql_backend
only.
impl ToSql<Timestamp, Mysql> for PrimitiveDateTime
time
and mysql_backend
only.source§impl 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.source§impl ToSql<Timestamp, Pg> for PrimitiveDateTime
Available on crate features time
and postgres_backend
only.
impl ToSql<Timestamp, Pg> for PrimitiveDateTime
time
and postgres_backend
only.source§impl ToSql<Timestamp, Sqlite> for NaiveDateTime
Available on crate features chrono
and sqlite
only.
impl ToSql<Timestamp, Sqlite> for NaiveDateTime
chrono
and sqlite
only.source§impl ToSql<Timestamp, Sqlite> for PrimitiveDateTime
Available on crate features time
and sqlite
only.
impl ToSql<Timestamp, Sqlite> for PrimitiveDateTime
time
and sqlite
only.source§impl 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.source§impl ToSql<Timestamptz, Pg> for OffsetDateTime
Available on crate features time
and postgres_backend
only.
impl ToSql<Timestamptz, Pg> for OffsetDateTime
time
and postgres_backend
only.source§impl ToSql<Timestamptz, Pg> for PrimitiveDateTime
Available on crate features time
and postgres_backend
only.
impl ToSql<Timestamptz, Pg> for PrimitiveDateTime
time
and postgres_backend
only.source§impl ToSql<Timestamptz, Sqlite> for NaiveDateTime
Available on crate features chrono
and sqlite
only.
impl ToSql<Timestamptz, Sqlite> for NaiveDateTime
chrono
and sqlite
only.source§impl ToSql<Timestamptz, Sqlite> for OffsetDateTime
Available on crate features time
and sqlite
only.
impl ToSql<Timestamptz, Sqlite> for OffsetDateTime
time
and sqlite
only.source§impl ToSql<Timestamptz, Sqlite> for PrimitiveDateTime
Available on crate features time
and sqlite
only.
impl ToSql<Timestamptz, Sqlite> for PrimitiveDateTime
time
and sqlite
only.source§impl<DB> ToSql<Binary, DB> for [u8]where
for<'a> DB: Backend<BindCollector<'a> = RawBytesBindCollector<DB>>,
impl<DB> ToSql<Binary, DB> for [u8]where
for<'a> DB: Backend<BindCollector<'a> = RawBytesBindCollector<DB>>,
source§impl<DB> ToSql<Text, DB> for strwhere
for<'a> DB: Backend<BindCollector<'a> = RawBytesBindCollector<DB>>,
impl<DB> ToSql<Text, DB> for strwhere
for<'a> DB: Backend<BindCollector<'a> = RawBytesBindCollector<DB>>,
source§impl<ST, T> ToSql<Nullable<Array<ST>>, Pg> for [T]
Available on crate feature postgres_backend
only.
impl<ST, T> ToSql<Nullable<Array<ST>>, Pg> for [T]
postgres_backend
only.source§impl<ST, T> ToSql<Nullable<Array<ST>>, Pg> for Vec<T>
Available on crate feature postgres_backend
only.
impl<ST, T> ToSql<Nullable<Array<ST>>, Pg> for Vec<T>
postgres_backend
only.source§impl<ST, T> ToSql<Nullable<Range<ST>>, Pg> for (Bound<T>, Bound<T>)
Available on crate feature postgres_backend
only.
impl<ST, T> ToSql<Nullable<Range<ST>>, Pg> for (Bound<T>, Bound<T>)
postgres_backend
only.source§impl<ST, T> ToSql<Range<ST>, Pg> for (Bound<T>, Bound<T>)
Available on crate feature postgres_backend
only.
impl<ST, T> ToSql<Range<ST>, Pg> for (Bound<T>, Bound<T>)
postgres_backend
only.source§impl<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.source§impl<TZ: TimeZone> ToSql<Timestamptz, Sqlite> for DateTime<TZ>
Available on crate features chrono
and sqlite
only.
impl<TZ: TimeZone> ToSql<Timestamptz, Sqlite> for DateTime<TZ>
chrono
and sqlite
only.source§impl<Tz: TimeZone, __DB> ToSql<Nullable<Timestamptz>, __DB> for DateTime<Tz>
Available on crate feature chrono
only.
impl<Tz: TimeZone, __DB> ToSql<Nullable<Timestamptz>, __DB> for DateTime<Tz>
chrono
only.source§impl<Tz: TimeZone, __DB> ToSql<Nullable<Timestamptz>, __DB> for DateTime<Tz>
Available on crate feature chrono
only.
impl<Tz: TimeZone, __DB> ToSql<Nullable<Timestamptz>, __DB> for DateTime<Tz>
chrono
only.source§impl<__DB> ToSql<Nullable<Cidr>, __DB> for IpNet
Available on crate features ipnet-address
and postgres_backend
only.
impl<__DB> ToSql<Nullable<Cidr>, __DB> for IpNet
ipnet-address
and postgres_backend
only.source§impl<__DB> ToSql<Nullable<Cidr>, __DB> for IpNetwork
Available on crate features network-address
and postgres_backend
only.
impl<__DB> ToSql<Nullable<Cidr>, __DB> for IpNetwork
network-address
and postgres_backend
only.source§impl<__DB> ToSql<Nullable<Datetime>, __DB> for NaiveDateTime
Available on crate feature chrono
only.
impl<__DB> ToSql<Nullable<Datetime>, __DB> for NaiveDateTime
chrono
only.source§impl<__DB> ToSql<Nullable<Datetime>, __DB> for OffsetDateTime
Available on crate feature time
only.
impl<__DB> ToSql<Nullable<Datetime>, __DB> for OffsetDateTime
time
only.source§impl<__DB> ToSql<Nullable<Datetime>, __DB> for PrimitiveDateTime
Available on crate feature time
only.
impl<__DB> ToSql<Nullable<Datetime>, __DB> for PrimitiveDateTime
time
only.source§impl<__DB> ToSql<Nullable<Inet>, __DB> for IpNet
Available on crate features ipnet-address
and postgres_backend
only.
impl<__DB> ToSql<Nullable<Inet>, __DB> for IpNet
ipnet-address
and postgres_backend
only.source§impl<__DB> ToSql<Nullable<Inet>, __DB> for IpNetwork
Available on crate features network-address
and postgres_backend
only.
impl<__DB> ToSql<Nullable<Inet>, __DB> for IpNetwork
network-address
and postgres_backend
only.source§impl<__DB> ToSql<Nullable<Json>, __DB> for Value
Available on crate feature serde_json
and (crate features postgres_backend
or mysql_backend
) only.
impl<__DB> ToSql<Nullable<Json>, __DB> for Value
serde_json
and (crate features postgres_backend
or mysql_backend
) only.source§impl<__DB> ToSql<Nullable<Jsonb>, __DB> for Value
Available on crate feature serde_json
and (crate features postgres_backend
or mysql_backend
) only.
impl<__DB> ToSql<Nullable<Jsonb>, __DB> for Value
serde_json
and (crate features postgres_backend
or mysql_backend
) only.source§impl<__DB> ToSql<Nullable<MacAddr>, __DB> for [u8; 6]
Available on crate feature postgres_backend
only.
impl<__DB> ToSql<Nullable<MacAddr>, __DB> for [u8; 6]
postgres_backend
only.source§impl<__DB> ToSql<Nullable<Numeric>, __DB> for BigDecimal
Available on crate feature bigdecimal
only.
impl<__DB> ToSql<Nullable<Numeric>, __DB> for BigDecimal
bigdecimal
only.source§impl<__DB> ToSql<Nullable<Timestamp>, __DB> for NaiveDateTime
Available on crate feature chrono
only.
impl<__DB> ToSql<Nullable<Timestamp>, __DB> for NaiveDateTime
chrono
only.source§impl<__DB> ToSql<Nullable<Timestamp>, __DB> for PrimitiveDateTime
Available on crate feature time
only.
impl<__DB> ToSql<Nullable<Timestamp>, __DB> for PrimitiveDateTime
time
only.source§impl<__DB> ToSql<Nullable<Timestamptz>, __DB> for NaiveDateTime
Available on crate feature chrono
only.
impl<__DB> ToSql<Nullable<Timestamptz>, __DB> for NaiveDateTime
chrono
only.source§impl<__DB> ToSql<Nullable<Timestamptz>, __DB> for OffsetDateTime
Available on crate feature time
only.
impl<__DB> ToSql<Nullable<Timestamptz>, __DB> for OffsetDateTime
time
only.source§impl<__DB> ToSql<Nullable<Timestamptz>, __DB> for PrimitiveDateTime
Available on crate feature time
only.
impl<__DB> ToSql<Nullable<Timestamptz>, __DB> for PrimitiveDateTime
time
only.source§impl<__DB> ToSql<Nullable<Timestamptz>, __DB> for OffsetDateTime
Available on crate feature time
only.
impl<__DB> ToSql<Nullable<Timestamptz>, __DB> for OffsetDateTime
time
only.source§impl<__DB> ToSql<Nullable<Uuid>, __DB> for Uuid
Available on crate features uuid
and postgres_backend
only.
impl<__DB> ToSql<Nullable<Uuid>, __DB> for Uuid
uuid
and postgres_backend
only.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.impl<'a, DB> ToSql<Text, DB> for MigrationVersion<'a>
impl<'a, __DB> ToSql<Nullable<Text>, __DB> for MigrationVersion<'a>
impl<__DB> ToSql<Nullable<Date>, __DB> for MysqlTime
mysql_backend
only.impl<__DB> ToSql<Nullable<Date>, __DB> for PgDate
postgres_backend
only.impl<__DB> ToSql<Nullable<Datetime>, __DB> for MysqlTime
mysql_backend
only.impl<__DB> ToSql<Nullable<Interval>, __DB> for PgInterval
postgres_backend
only.impl<__DB> ToSql<Nullable<Money>, __DB> for PgMoney
postgres_backend
only.impl<__DB> ToSql<Nullable<Numeric>, __DB> for PgNumeric
postgres_backend
only.impl<__DB> ToSql<Nullable<Time>, __DB> for MysqlTime
mysql_backend
only.impl<__DB> ToSql<Nullable<Time>, __DB> for PgTime
postgres_backend
only.impl<__DB> ToSql<Nullable<Timestamp>, __DB> for MysqlTime
mysql_backend
only.impl<__DB> ToSql<Nullable<Timestamp>, __DB> for PgTimestamp
postgres_backend
only.impl<__DB> ToSql<Nullable<Timestamptz>, __DB> for PgTimestamp
postgres_backend
only.