sqlorm_core/
lib.rs

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