Skip to content

Commit ec455eb

Browse files
authored
Merge pull request diesel-rs#3908 from colem213/multiconnection-documentation
Include more detail for implementing custom types for diesel::MultiCo…
2 parents 166b97c + 4c7b6ea commit ec455eb

File tree

2 files changed

+97
-6
lines changed

2 files changed

+97
-6
lines changed

diesel/src/pg/types/date_and_time/chrono.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ mod tests {
280280
let query = select(sql::<Date>("'J0'::date").eq(julian_epoch));
281281
assert!(query.get_result::<bool>(connection).unwrap());
282282

283-
let max_date = NaiveDate::MAX;
284-
let query = select(sql::<Date>("'262143-12-31'::date").eq(max_date));
283+
let max_date = NaiveDate::from_ymd_opt(262142, 12, 31).unwrap();
284+
let query = select(sql::<Date>("'262142-12-31'::date").eq(max_date));
285285
assert!(query.get_result::<bool>(connection).unwrap());
286286

287287
let january_first_2018 = NaiveDate::from_ymd_opt(2018, 1, 1).unwrap();
@@ -311,8 +311,8 @@ mod tests {
311311
let query = select(sql::<Date>("'J0'::date"));
312312
assert_eq!(Ok(julian_epoch), query.get_result::<NaiveDate>(connection));
313313

314-
let max_date = NaiveDate::MAX;
315-
let query = select(sql::<Date>("'262143-12-31'::date"));
314+
let max_date = NaiveDate::from_ymd_opt(262142, 12, 31).unwrap();
315+
let query = select(sql::<Date>("'262142-12-31'::date"));
316316
assert_eq!(Ok(max_date), query.get_result::<NaiveDate>(connection));
317317

318318
let january_first_2018 = NaiveDate::from_ymd_opt(2018, 1, 1).unwrap();

diesel_derives/src/lib.rs

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,8 +1630,99 @@ pub fn table_proc(input: TokenStream) -> TokenStream {
16301630
/// * `diesel::sql_types::Timestamp`
16311631
///
16321632
/// Support for additional types can be added by providing manual implementations of
1633-
/// `HasSqlType`, `FromSql` and `ToSql` for the corresponding type + the generated
1634-
/// database backend.
1633+
/// `HasSqlType`, `FromSql` and `ToSql` for the corresponding type, all databases included
1634+
/// in your enum, and the backend generated by this derive called `MultiBackend`.
1635+
/// For example to support a custom enum `MyEnum` with the custom SQL type `MyInteger`:
1636+
/// ```
1637+
/// extern crate diesel;
1638+
/// use diesel::backend::Backend;
1639+
/// use diesel::deserialize::{self, FromSql, FromSqlRow};
1640+
/// use diesel::serialize::{self, IsNull, ToSql};
1641+
/// use diesel::AsExpression;
1642+
/// use diesel::sql_types::{HasSqlType, SqlType};
1643+
/// use diesel::prelude::*;
1644+
///
1645+
/// #[derive(diesel::MultiConnection)]
1646+
/// pub enum AnyConnection {
1647+
/// # #[cfg(feature = "postgres")]
1648+
/// Postgresql(diesel::PgConnection),
1649+
/// # #[cfg(feature = "mysql")]
1650+
/// Mysql(diesel::MysqlConnection),
1651+
/// # #[cfg(feature = "sqlite")]
1652+
/// Sqlite(diesel::SqliteConnection),
1653+
/// }
1654+
///
1655+
/// // defining an custom SQL type is optional
1656+
/// // you can also use types from `diesel::sql_types`
1657+
/// #[derive(Copy, Clone, Debug, SqlType)]
1658+
/// #[diesel(postgres_type(name = "Int4"))]
1659+
/// #[diesel(mysql_type(name = "Long"))]
1660+
/// #[diesel(sqlite_type(name = "Integer"))]
1661+
/// struct MyInteger;
1662+
///
1663+
///
1664+
/// // our custom enum
1665+
/// #[repr(i32)]
1666+
/// #[derive(Debug, Clone, Copy, AsExpression, FromSqlRow)]
1667+
/// #[diesel(sql_type = MyInteger)]
1668+
/// pub enum MyEnum {
1669+
/// A = 1,
1670+
/// B = 2,
1671+
/// }
1672+
///
1673+
/// // The `MultiBackend` type is generated by `#[derive(diesel::MultiConnection)]`
1674+
/// // This part is only required if you define a custom sql type
1675+
/// impl HasSqlType<MyInteger> for MultiBackend {
1676+
/// fn metadata(lookup: &mut Self::MetadataLookup) -> Self::TypeMetadata {
1677+
/// // The `lookup_sql_type` function is exposed by the `MultiBackend` type
1678+
/// MultiBackend::lookup_sql_type::<MyInteger>(lookup)
1679+
/// }
1680+
/// }
1681+
///
1682+
/// impl FromSql<MyInteger, MultiBackend> for MyEnum {
1683+
/// fn from_sql(bytes: <MultiBackend as Backend>::RawValue<'_>) -> deserialize::Result<Self> {
1684+
/// // The `from_sql` function is exposed by the `RawValue` type of the
1685+
/// // `MultiBackend` type
1686+
/// // This requires a `FromSql` impl for each backend
1687+
/// bytes.from_sql::<MyEnum, MyInteger>()
1688+
/// }
1689+
/// }
1690+
///
1691+
/// impl ToSql<MyInteger, MultiBackend> for MyEnum {
1692+
/// fn to_sql<'b>(&'b self, out: &mut serialize::Output<'b, '_, MultiBackend>) -> serialize::Result {
1693+
/// /// `set_value` expects a tuple consisting of the target SQL type
1694+
/// /// and self for `MultiBackend`
1695+
/// /// This requires a `ToSql` impl for each backend
1696+
/// out.set_value((MyInteger, self));
1697+
/// Ok(IsNull::No)
1698+
/// }
1699+
/// }
1700+
/// # #[cfg(feature = "postgres")]
1701+
/// # impl ToSql<MyInteger, diesel::pg::Pg> for MyEnum {
1702+
/// # fn to_sql<'b>(&'b self, out: &mut serialize::Output<'b, '_, diesel::pg::Pg>) -> serialize::Result { todo!() }
1703+
/// # }
1704+
/// # #[cfg(feature = "mysql")]
1705+
/// # impl ToSql<MyInteger, diesel::mysql::Mysql> for MyEnum {
1706+
/// # fn to_sql<'b>(&'b self, out: &mut serialize::Output<'b, '_, diesel::mysql::Mysql>) -> serialize::Result { todo!() }
1707+
/// # }
1708+
/// # #[cfg(feature = "sqlite")]
1709+
/// # impl ToSql<MyInteger, diesel::sqlite::Sqlite> for MyEnum {
1710+
/// # fn to_sql<'b>(&'b self, out: &mut serialize::Output<'b, '_, diesel::sqlite::Sqlite>) -> serialize::Result { todo!() }
1711+
/// # }
1712+
/// # #[cfg(feature = "postgres")]
1713+
/// # impl FromSql<MyInteger, diesel::pg::Pg> for MyEnum {
1714+
/// # fn from_sql(bytes: <diesel::pg::Pg as Backend>::RawValue<'_>) -> deserialize::Result<Self> { todo!() }
1715+
/// # }
1716+
/// # #[cfg(feature = "mysql")]
1717+
/// # impl FromSql<MyInteger, diesel::mysql::Mysql> for MyEnum {
1718+
/// # fn from_sql(bytes: <diesel::mysql::Mysql as Backend>::RawValue<'_>) -> deserialize::Result<Self> { todo!() }
1719+
/// # }
1720+
/// # #[cfg(feature = "sqlite")]
1721+
/// # impl FromSql<MyInteger, diesel::sqlite::Sqlite> for MyEnum {
1722+
/// # fn from_sql(bytes: <diesel::sqlite::Sqlite as Backend>::RawValue<'_>) -> deserialize::Result<Self> { todo!() }
1723+
/// # }
1724+
/// # fn main() {}
1725+
/// ```
16351726
#[proc_macro_derive(MultiConnection)]
16361727
pub fn derive_multiconnection(input: TokenStream) -> TokenStream {
16371728
multiconnection::derive(syn::parse_macro_input!(input)).into()

0 commit comments

Comments
 (0)