@@ -1630,8 +1630,99 @@ pub fn table_proc(input: TokenStream) -> TokenStream {
1630
1630
/// * `diesel::sql_types::Timestamp`
1631
1631
///
1632
1632
/// 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
+ /// ```
1635
1726
#[ proc_macro_derive( MultiConnection ) ]
1636
1727
pub fn derive_multiconnection ( input : TokenStream ) -> TokenStream {
1637
1728
multiconnection:: derive ( syn:: parse_macro_input!( input) ) . into ( )
0 commit comments