1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/// Creates a parameterized, compile-time checked database query that accepts parameters similar to
/// the [`format!`] macro.
///
/// The compile-time checks require a database connection, expecting a `DATABASE_URL` env to be set
/// accordingly.
///
/// The returned type can either be a struct (that implements [`FromRow`]), a literal (string,
/// integer, ...), or a [`Vec`] or [`Option`] of the former.
///
/// A connection is automatically established, but also be explicitly set via
/// [`Sql::run_with`].
///
/// # Examples
///
/// ```
/// # use sqlm_postgres::sql;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let name: String = sql!("SELECT name FROM users WHERE id = {id}", id = 1i64).await?;
/// # Ok(())
/// # }
/// ```
///
/// [`FromRow`]: super::FromRow
/// [`Sql::run_with`]: super::Sql::run_with
pub use sql;
/// A derive necessary to support compile checks between Postgres and Rust enums.
///
/// In addition, enums also need to implement `tokio_postgres`'s [`FromSql`] and [`ToSql`], so it
/// can be read from and written to Postgres.
///
/// # Example
/// ```
/// use sqlm_postgres::{Enum, FromSql, ToSql};
/// #[derive(Debug, Default, FromSql, ToSql, Enum)]
/// #[postgres(name = "role")]
/// enum Role {
/// #[default]
/// #[postgres(name = "user")]
/// User,
/// #[postgres(name = "admin")]
/// Admin,
/// }
/// ```
///
/// [`FromSql`]: crate::FromSql
/// [`ToSql`]: crate::ToSql
pub use Enum;
/// Derive [`FromRow`] for a struct, required read a query result into a struct.
///
/// Each struct property must have a [`Default::default`] implementation (used for null values; you
/// can of course use [`Option`] as its default is simply [`None`]).
/// Alternatively, the default value can be set using a `#[sqlm(default = ...)]` attribute.
///
/// # Example
///
/// ```
/// # #[cfg(feature = "time")]
/// #[derive(sqlm_postgres::FromRow)]
/// struct User {
/// id: i64,
/// name: String,
/// #[sqlm(default = time::OffsetDateTime::UNIX_EPOCH)]
/// created_at: time::OffsetDateTime,
/// }
/// ```
///
/// [`FromRow`]: trait@crate::FromRow
pub use FromRow;