sqlorm_core/
lib.rs

1#![cfg(any(feature = "postgres", feature = "sqlite"))]
2
3mod consts;
4mod generic_row;
5pub mod qb;
6pub use consts::*;
7pub use generic_row::GenericRow;
8
9pub use crate::qb::TableInfo;
10pub use async_trait::async_trait;
11pub use qb::Column;
12pub use qb::Condition;
13pub use qb::*;
14mod traits;
15
16#[cfg(any(feature = "postgres", feature = "sqlite"))]
17pub use driver::{Connection, Driver, Pool, Row};
18
19use sqlx::FromRow;
20pub use traits::FromAliasedRow;
21pub use traits::GenericExecutor;
22pub use traits::Table;
23
24#[async_trait]
25impl<T> GenericExecutor<T> for QB<T>
26where
27    T: for<'r> FromRow<'r, Row> + GenericRow + Send + Unpin + std::fmt::Debug,
28{
29    async fn fetch_one_as(mut self, pool: &Pool) -> sqlx::Result<T> {
30        self.eager.clear();
31        self.batch.clear();
32        let row = self.build_query().build().fetch_one(pool).await?;
33        T::from_row(&row)
34    }
35
36    async fn fetch_all_as(self, pool: &Pool) -> sqlx::Result<Vec<T>> {
37        let rows = self.build_query().build().fetch_all(pool).await?;
38        rows.iter().map(T::from_row).collect()
39    }
40}
41
42pub mod driver {
43    #[cfg(all(feature = "postgres", feature = "sqlite"))]
44    compile_error!(
45        "only one database driver can be set – please enable either 'postgres' or 'sqlite' feature, not both"
46    );
47
48    #[cfg(feature = "postgres")]
49    /// Sqlorm Database Driver
50    pub type Driver = sqlx::Postgres;
51
52    #[cfg(feature = "postgres")]
53    /// Sqlorm Database Pool
54    pub type Pool = sqlx::PgPool;
55
56    #[cfg(feature = "postgres")]
57    /// Sqlorm Database Connection
58    pub type Connection = sqlx::PgConnection;
59
60    #[cfg(feature = "postgres")]
61    /// Sqlorm Database Row
62    pub type Row = sqlx::postgres::PgRow;
63
64    #[cfg(feature = "sqlite")]
65    /// Sqlorm Database Driver
66    pub type Driver = sqlx::Sqlite;
67
68    #[cfg(feature = "sqlite")]
69    /// Sqlorm Database Pool
70    pub type Pool = sqlx::SqlitePool;
71
72    #[cfg(feature = "sqlite")]
73    /// Sqlorm Database Connection
74    pub type Connection = sqlx::SqliteConnection;
75
76    #[cfg(feature = "sqlite")]
77    /// Sqlorm Database Row
78    pub type Row = sqlx::sqlite::SqliteRow;
79}
80
81#[doc(hidden)]
82pub use sqlx;