joker_query/
lib.rs

1
2
3pub mod query;
4
5
6
7
8#[cfg(test)]
9mod tests {
10    
11
12    use crate::query::{
13        operator as op,
14        select::Select,
15        func::Func, 
16        insert::Insert, 
17        update::Update, 
18        set::Pair, 
19        delete::Delete, array::Array,
20    };
21
22
23    #[test]
24    fn select() {
25
26        let result = "        
27            SELECT DISTINCT id, age, fullname FROM customer\nINNER JOIN merchant ON customer.id = merchant.customer_id\nLEFT JOIN product ON customer.id = product.customer_id\nWHERE age BETWEEN 10 AND 25\nAND fullname LIKE 'full%'\nOR fullname NOT IN ('danyal', 'danyalmh', 'danyalai')\nGROUP BY merchant_id\nHAVING COUNT(id) = 2025\nORDER BY fullname ASC, age DESC\nLIMIT 10\nOFFSET 5        
28        ";
29
30        let query = 
31        Select::
32            cols(vec!["id", "age", "fullname"])
33            .distinct()
34            .from("customer")
35            .inner_join("merchant").on("customer.id", "customer_id")
36            .left_join("product").on("customer.id", "customer_id")
37            .where_by("age", op::between(10, 25))
38            .and("fullname", op::like("full%"))
39            .or("fullname", op::not_in(vec!["danyal", "danyalmh", "danyalai"]))
40            .group_by(vec!["merchant_id"])
41            .having(&Func::count("id"), op::eq(2025))            
42            .order_by("fullname")
43            .order_by_desc("age")
44            .limit(10)
45            .offset(5)
46            .build();
47        
48
49
50        assert_eq!(query.trim(), result.trim());
51        println!("\n{}\n", query);
52
53    }
54
55    #[test]
56    fn select_with_source() {
57
58        let query = 
59        Select::
60            cols(vec!["id", "age", "fullname"])
61            .distinct()
62            .from_subquery(
63                Select::
64                        cols(vec!["id", "age", "fullname"])
65                        .from("another_table")
66                        .where_by("age", op::gt(22))
67            )
68            .where_by("age", op::between(10, 25))
69            .order_by_desc("age")
70            .limit(10)
71            .offset(5)
72            .build();
73        
74        println!("\n{}\n", query);
75
76    }
77
78
79
80
81
82    #[test]
83    fn insert() {
84        let query = 
85            Insert::into("customer").cols(vec!["id", "age", "fullname"])
86                .value(Array::new().add("766dc50e").add(25).add("Danyal"))
87                .build();
88
89        println!("\n{}\n", query);
90    }
91
92
93    #[test]
94    fn insert_values() {
95        let query = 
96            Insert::into("customer").cols(vec!["id", "age", "fullname"])
97                .values(vec![
98                    Array::new().add("766dc50e").add(25).add("Danyal"),
99                    Array::new().add("766dc50e").add(25).add("Danyal"),
100                ])
101                .build();
102
103        println!("\n{}\n", query);   
104    }
105
106
107    #[test]
108    fn insert_value_select() {
109
110
111        let query = 
112            Insert::into("customer").cols(vec!["id", "age", "fullname"])
113                .source(
114                    Select::cols(vec!["id", "age", "fullname"]).from("customer_template")
115                                .where_by("age", op::eq(10)).limit(10)
116                )
117                .build();
118
119        println!("\n{}\n", query);  
120            
121    }
122
123
124    #[test]
125    pub fn update() {
126
127        let query = 
128         Update::table("customer")
129            .set(vec![Pair::from("age", 100), Pair::from("fullname", "DanyalMh")])
130            .where_by("id", op::eq(100))
131                .and("fullname", op::not("DanyalMh"))
132            .build();
133            
134        println!("\n{}\n", query)
135    }
136
137    #[test]
138    pub fn delete() {
139
140        let query = 
141            Delete::from("customer")
142                .where_by("fullname", op::neq("DanyalMh"))
143                    .or("age", op::not_between(23, 25))
144                .build();
145
146        println!("\n{}\n", query)
147    }
148
149
150    #[test]
151    pub fn delete_with_sub_query() {
152
153        let query = 
154            Delete::from("customer")
155                .where_by("age", op::lt(
156                    Select::cols(vec!["MIN(age)"]).from("athele")
157                ))
158                .build();
159
160        println!("\n{}\n", query)
161    }
162
163}