db_derive/
table.rs

1use crate::{
2    internal::{PostgreRow, SQLiteRow},
3    Error, Pool, PoolKind, Row,
4};
5
6pub trait Schema {
7    fn schema(pool: &Pool) -> &'static str {
8        match pool.as_kind() {
9            #[cfg(feature = "postgresql")]
10            PoolKind::PostgreSQL => Self::schema_postgres(),
11            #[cfg(feature = "sqlite")]
12            PoolKind::SQLite => Self::schema_sqlite(),
13        }
14    }
15
16    /// The generated table schema for PostgreSQL.
17    #[cfg(feature = "postgresql")]
18    fn schema_postgres() -> &'static str;
19
20    /// The generated table schema for SQLite.
21    ///
22    /// # Examples
23    ///
24    /// ```rust,no_run
25    /// # use db_derive::{prelude::*, Connection};
26    ///
27    /// #[derive(db_derive::Table)]
28    /// #[table(schema)]
29    /// struct Tag {
30    ///     #[table(rename = "Id")]
31    ///     id: String,
32    ///
33    ///     #[table(rename = "Name")]
34    ///     name: String,
35    /// }
36    ///
37    /// assert_eq!("", Tag::schema_sqlite());
38    /// ```
39    #[cfg(feature = "sqlite")]
40    fn schema_sqlite() -> &'static str;
41}
42
43/// The trait allows for the conversion of a resulting query's rows into the given implemented type.
44///
45/// # Examples
46///
47/// ```rust,no_run
48/// # use db_derive::{prelude::*, Pool};
49/// #[derive(db_derive::Table)]
50/// struct Tag {
51///     #[table(rename = "Id")]
52///     id: String,
53///
54///     #[table(rename = "Name")]
55///     name: String,
56/// }
57///
58/// #[derive(db_derive::Query)]
59/// #[query(sql = "SELECT Id, Name FROM Tag WHERE Id = $id;")]
60/// struct GetTagById<'a> {
61///     id: &'a str,
62/// }
63///
64/// fn main() -> Result<(), db_derive::Error> {
65///     let pool = Pool::sqlite("example.db")?;
66///
67///     let get = GetTagById { id: "example" };
68///
69///     let tag = get.query_row::<_, Tag>(&pool)?;
70///
71///     println!("Tag Name: {}", tag.name);
72///
73///     Ok(())
74/// }
75/// ```
76pub trait Table {
77    fn columns() -> usize;
78
79    /// Internally used to transform the resulting query's rows into their given table type.
80    /// This is just a mapper to the row transform implementations for each database.
81    ///
82    /// # Error
83    ///
84    /// This will return with an [`Error`] if a row column is either the wrong type
85    /// or is unable to parse a column.
86    ///
87    /// [`Error`]: db_derive::Error
88    fn from_row(row: Row<'_>) -> Result<Self, Error>
89    where
90        Self: Sized,
91    {
92        match row {
93            #[cfg(feature = "postgresql")]
94            Row::PostgreSQL(row) => Self::from_row_postgres(row),
95            #[cfg(feature = "sqlite")]
96            Row::SQLite(row) => Self::from_row_sqlite(row),
97        }
98    }
99
100    /// The PostgreSQL row transform implementation.
101    ///
102    /// # Error
103    ///
104    /// This will return with an [`Error`] if a row column is either the wrong type
105    /// or is unable to parse a column.
106    ///
107    /// [`Error`]: db_derive::Error
108    #[cfg(feature = "postgresql")]
109    fn from_row_postgres(row: PostgreRow) -> Result<Self, Error>
110    where
111        Self: Sized;
112
113    /// The SQLite row transform implementation.
114    ///
115    /// # Error
116    ///
117    /// This will return with an [`Error`] if a row column is either the wrong type
118    /// or is unable to parse a column.
119    ///
120    /// [`Error`]: db_derive::Error
121    #[cfg(feature = "sqlite")]
122    fn from_row_sqlite<'__table_row>(
123        row: &'__table_row SQLiteRow<'__table_row>,
124    ) -> Result<Self, Error>
125    where
126        Self: Sized;
127}