Skip to main content

flyer_orm/query/
logic.rs

1use std::marker::PhantomData;
2
3use sqlx::{Encode, Type};
4
5use crate::query;
6
7#[derive(Clone, Debug, Default)]
8pub(crate) enum Condition {
9    #[default]
10    AND,
11    OR
12}
13
14impl ToString for Condition {
15    fn to_string(&self) -> String {
16        return match self {
17            Condition::AND => String::from("AND"),
18            Condition::OR => String::from("OR"),
19        }
20    }
21}
22
23#[derive(Clone, Debug)]
24pub(crate) struct Where {
25    pub condition: Option<Condition>,
26    pub column: Option<String>,
27    pub operator: Option<String>,
28    pub group: Option<Box<Where>>
29}
30
31#[derive(Debug)]
32pub struct WhereGroup<'q, DB: sqlx::Database> {
33    pub(crate) _queries: Vec<Where>,
34    _marker: PhantomData<DB>,
35    _life: PhantomData<&'q ()>
36}
37
38impl <'q, DB>WhereGroup<'q, DB>
39where
40    DB: sqlx::Database
41{
42    pub fn new() -> Self {
43        return Self {
44            _queries: Vec::new(),
45            _marker: PhantomData,
46            _life: PhantomData
47        }
48    }
49
50    pub fn r#where<T: 'q + Encode<'q, DB> + Type<DB>>(&mut self, _column: &str, _operator: &str, _val: T) -> &mut Self {
51        todo!()
52    }
53}
54
55#[derive(Clone, Debug)]
56pub(crate) struct Order {
57    pub column: String,
58    pub order: query::Order,
59}
60
61#[derive(Clone, Debug)]
62pub(crate) enum JoinType {
63    InnerJoin,
64    LeftJoin,
65    RightJoin,
66    FullOuterJoin,
67    CrossJoin
68}
69
70#[derive(Clone, Debug)]
71pub(crate) struct Join {
72    pub table: String,
73    pub column: String,
74    pub operator: String,
75    pub column_table: String, 
76    pub join_type: JoinType
77}
78
79#[derive(Clone, Debug, Default)]
80pub(crate) struct Having {
81    pub column: String,
82    pub operator: String,
83}
84
85#[derive(Clone, Debug, Default)]
86pub(crate) struct SqlQuery {
87    pub table: String,
88    pub select: Vec<String>,
89    pub join: Vec<Join>,
90    pub where_queries: Vec<Where>,
91    pub group_by: Option<String>,
92    pub having: Option<Having>,
93    pub order_by: Option<Vec<Order>>,
94    pub limit: Option<u64>,
95    pub page: Option<u64>, // TODO: must use `offset` or `page` must decide...
96    pub columns: Vec<String>,
97}
98
99impl SqlQuery {
100    pub fn new(table: &str) -> Self {
101        return Self {
102            table: table.to_string(),
103            select: Vec::new(),
104            join: Vec::new(),
105            where_queries: Vec::new(),
106            having: None,
107            group_by: None,
108            order_by: None,
109            limit: None,
110            page: None,
111            columns: Vec::new(),
112        }
113    }
114}