Skip to main content

flyer_orm/query/
mod.rs

1use anyhow::Result;
2use serde::Serialize;
3
4use crate::query::logic::SqlQuery;
5
6pub mod logic;
7
8pub(crate) trait QueryBuilder<'q> {
9    fn new(statement: &'q SqlQuery) -> Self where Self: Sized;
10    fn query(&self) -> Result<String>;
11    fn insert(&self) -> Result<String>;
12    fn update(&self) -> Result<String>;
13    fn delete(&self) -> Result<String>;
14    fn select(&self) -> Result<String>;
15    fn from(&self) -> Result<String>;
16    fn join(&self) -> Result<String>;
17    fn r#where(&self) -> Result<String>;
18    fn group_by(&self) -> Result<String>;
19    fn having(&self) -> Result<String>;
20    fn order_by(&self) -> Result<String>;
21    fn limit(&self) -> Result<String>;
22}
23
24pub trait QueryResult {
25    fn rows_affected(&self) -> u64;
26    fn last_inserted(&self) -> u64;
27}
28
29#[derive(Clone, Debug)]
30pub enum Order {
31    ASC,
32    DESC
33}
34
35impl ToString for Order {
36    fn to_string(&self) -> String {
37        return match self {
38            Order::ASC => String::from("ASC"),
39            Order::DESC => String::from("DESC"),
40        };
41    }
42}
43
44#[derive(Clone, Debug)]
45pub(crate) struct Statement<'q, DB: sqlx::Database> {
46    pub query: SqlQuery,
47    pub arguments: DB::Arguments<'q>, 
48}
49
50#[derive(Debug, sqlx::FromRow)]
51pub(crate) struct Total {
52    pub total: u64
53}
54
55impl <'q, DB>Statement<'q, DB>
56where
57    DB: sqlx::Database
58{
59    pub(crate) fn new(table: &str) -> Self {
60        return Self {
61            query: SqlQuery::new(table),
62            arguments: Default::default(),
63        }
64    }
65}
66
67#[derive(Debug)]
68pub struct Transaction<'t, T: sqlx::Database> {
69    transaction: sqlx::Transaction<'t, T>
70}
71
72impl <'t, T: sqlx::Database>Transaction<'t, T> {
73    pub(crate) fn new(transaction: sqlx::Transaction<'t, T>) -> Self {
74        return Self { transaction: transaction }
75    }
76
77    pub async fn commit(self) -> Result<()> {
78        return self.transaction.commit().await.map_err(|e| e.into());
79    }
80
81    pub async fn rollback(self) -> Result<()> {
82        return self.transaction.rollback().await.map_err(|e| e.into());
83    }
84}
85
86#[derive(Serialize, Clone, Debug)]
87pub struct Pagination<Entity> {
88    pub total: u64,
89    pub page: u64,
90    pub per_page: u64,
91    pub items: Vec<Entity>
92}