1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use async_trait::async_trait;
use luna_orm_trait::FromClause;
use luna_orm_trait::JoinedConditions;
use luna_orm_trait::{Entity, Location, Mutation, OrderBy, Pagination, Primary, Selection};

#[derive(Default)]
pub struct DefaultSqlGenerator {}
impl DefaultSqlGenerator {
    pub fn new() -> Self {
        Self {}
    }
}
impl SqlGenerator for DefaultSqlGenerator {}

#[derive(Default)]
pub struct MySqlGenerator {}
impl MySqlGenerator {
    pub fn new() -> Self {
        Self {}
    }
}
impl SqlGenerator for MySqlGenerator {
    fn get_upsert_sql(&self, entity: &dyn Entity) -> String {
        let table_name = entity.get_table_name();
        let mut field_names = entity.get_primary_fields_name();
        field_names.extend(entity.get_body_fields_name());
        let fields = wrap_fields(&field_names, self.get_wrap_char());
        let primary_field_names = entity.get_primary_fields_name();
        let primary_fields = wrap_fields(&primary_field_names, self.get_wrap_char());
        let marks = generate_question_mark_list(&field_names);
        let body_field_names = entity.get_body_fields_name();
        let assign_clause = wrap_locate_fields(
            &body_field_names,
            self.get_wrap_char(),
            self.get_place_holder(),
        );

        let upsert_sql = format!(
            "INSERT INTO {}{}{} ({}) VALUES({})
            ON DUPLICATE KEY UPDATE SET {}",
            self.get_wrap_char(),
            table_name,
            self.get_wrap_char(),
            fields,
            marks,
            assign_clause
        )
        .to_string();
        self.post_process(upsert_sql)
    }
}

#[derive(Default)]
pub struct PostgresGenerator {}
impl PostgresGenerator {
    pub fn new() -> Self {
        Self {}
    }
}
impl SqlGenerator for PostgresGenerator {
    fn post_process(&self, origin: String) -> String {
        self.pg_post_process(origin)
    }
}
#[async_trait]
pub trait SqlGenerator {
    // const WRAP_CHAR: char = '`'; can not made trait to trait object
    #[inline(always)]
    fn get_wrap_char(&self) -> char {
        '`'
    }

    // const PLACE_HOLDER: char = '?'; can not made trait to trait object
    #[inline(always)]
    fn get_place_holder(&self) -> char {
        '?'
    }

    #[inline]
    fn pg_post_process(&self, origin_sql: String) -> String {
        origin_sql
            .chars()
            .enumerate()
            .map(|(i, c)| match c {
                '?' => format!("${}", i + 1),
                _ => c.to_string(),
            })
            .collect()
    }

    #[inline(always)]
    fn post_process(&self, origin: String) -> String {
        origin
    }

    fn get_select_sql(&self, selection: &dyn Selection, primay: &dyn Primary) -> String {
        let table_name = primay.get_table_name();
        let selected_fields: Vec<String> = selection.get_selected_fields();
        let select_clause = wrap_fields(&selected_fields, self.get_wrap_char());
        let located_fields = primay.get_primary_field_names();
        let where_clause = wrap_locate_str_fields(
            located_fields,
            self.get_wrap_char(),
            self.get_place_holder(),
        );
        let select_sql = format!(
            "SELECT {} FROM {}{}{} WHERE {}",
            select_clause,
            self.get_wrap_char(),
            table_name,
            self.get_wrap_char(),
            where_clause
        )
        .to_string();
        self.post_process(select_sql)
    }

    fn get_search_count_sql(&self, location: &dyn Location) -> String {
        let table_name = location.get_table_name();
        let where_clause = location.get_where_clause(self.get_wrap_char(), self.get_place_holder());

        let select_sql = format!(
            "SELECT COUNT(1) AS {}count{} FROM {}{}{} WHERE {}",
            self.get_wrap_char(),
            self.get_wrap_char(),
            self.get_wrap_char(),
            table_name,
            self.get_wrap_char(),
            where_clause
        )
        .to_string();
        self.post_process(select_sql)
    }

    fn get_search_sql(
        &self,
        selection: &dyn Selection,
        location: &dyn Location,
        order_by: Option<&dyn OrderBy>,
    ) -> String {
        let selected_field_names = selection.get_selected_fields();
        let selected_fields = wrap_fields(&selected_field_names, self.get_wrap_char());
        let table_name = location.get_table_name();
        let where_clause = location.get_where_clause(self.get_wrap_char(), self.get_place_holder());
        if order_by.is_none() {
            let select_sql = format!(
                "SELECT {} FROM {}{}{} WHERE {}",
                selected_fields,
                self.get_wrap_char(),
                table_name,
                self.get_wrap_char(),
                where_clause
            )
            .to_string();
            self.post_process(select_sql)
        } else {
            let order_by_field_names = order_by.unwrap().get_order_by_fields();
            let order_by_fields = wrap_str_fields(&order_by_field_names, self.get_wrap_char());
            let select_sql = format!(
                "SELECT {} FROM {}{}{} WHERE {} ORDER BY {}",
                selected_fields,
                self.get_wrap_char(),
                table_name,
                self.get_wrap_char(),
                where_clause,
                order_by_fields
            )
            .to_string();
            self.post_process(select_sql)
        }
    }

    fn get_limit_sql(&self, page: &Pagination) -> String {
        let offset = page.page_size * page.page_num;
        let count = page.page_size;
        return format!("{}, {}", offset, count);
    }

    fn get_paged_search_sql(
        &self,
        selection: &dyn Selection,
        location: &dyn Location,
        order_by: Option<&dyn OrderBy>,
        page: &Pagination,
    ) -> String {
        let selected_field_names = selection.get_selected_fields();
        let selected_fields = wrap_fields(&selected_field_names, self.get_wrap_char());
        let table_name = location.get_table_name();
        let where_clause = location.get_where_clause(self.get_wrap_char(), self.get_place_holder());
        let offset = page.page_size * page.page_num;
        let count = page.page_size;
        if order_by.is_none() {
            let select_sql = format!(
                "SELECT {} FROM {}{}{} WHERE {} LIMIT {},{}",
                selected_fields,
                self.get_wrap_char(),
                table_name,
                self.get_wrap_char(),
                where_clause,
                offset,
                count
            )
            .to_string();
            self.post_process(select_sql)
        } else {
            let order_by_field_names = order_by.unwrap().get_order_by_fields();
            let order_by_fields = wrap_str_fields(order_by_field_names, self.get_wrap_char());
            let select_sql = format!(
                "SELECT {} FROM {}{}{} WHERE {} ORDER BY {} LIMIT {},{}",
                selected_fields,
                self.get_wrap_char(),
                table_name,
                self.get_wrap_char(),
                where_clause,
                order_by_fields,
                offset,
                count
            )
            .to_string();
            self.post_process(select_sql)
        }
    }

    fn get_page_joined_search_sql(
        &self,
        joined_conds: &JoinedConditions,
        locations: &Vec<&dyn Location>,
        order_by: Option<&dyn OrderBy>,
        selections: &Vec<&dyn Selection>,
        page: &Pagination,
    ) -> String {
        let mut selected_field_names: Vec<String> = Vec::new();
        for selection in selections {
            let fields = selection.get_selected_fields();
            selected_field_names.extend(fields);
        }
        let selected_fields = wrap_fields(&selected_field_names, self.get_wrap_char());

        let mut location_stmts: Vec<String> = Vec::new();
        for location in locations {
            let where_clause =
                location.get_where_clause(self.get_wrap_char(), self.get_place_holder());
            location_stmts.push(where_clause);
        }
        let where_clause = location_stmts.join(",");
        let from_clause = joined_conds.get_from_clause();
        let sql: String = format!(
            "SELECT {} FROM {} WHERE {}",
            selected_fields, from_clause, where_clause
        )
        .to_string();
        self.post_process(sql)
    }

    fn get_insert_sql(&self, entity: &dyn Entity) -> String {
        let table_name = entity.get_table_name();
        let mut field_names = entity.get_primary_fields_name();
        field_names.extend(entity.get_body_fields_name());
        let fields = wrap_fields(&field_names, self.get_wrap_char());
        let marks = generate_question_mark_list(&field_names);
        let insert_sql = format!(
            "INSERT INTO {}{}{} ({}) VALUES({})",
            self.get_wrap_char(),
            table_name,
            self.get_wrap_char(),
            fields,
            marks
        )
        .to_string();
        self.post_process(insert_sql)
    }

    fn get_upsert_sql(&self, entity: &dyn Entity) -> String {
        let table_name = entity.get_table_name();
        let mut field_names = entity.get_primary_fields_name();
        field_names.extend(entity.get_body_fields_name());
        let fields = wrap_fields(&field_names, self.get_wrap_char());
        let primary_field_names = entity.get_primary_fields_name();
        let primary_fields = wrap_fields(&primary_field_names, self.get_wrap_char());
        let marks = generate_question_mark_list(&field_names);
        let body_field_names = entity.get_body_fields_name();
        let assign_clause = wrap_locate_fields(
            &body_field_names,
            self.get_wrap_char(),
            self.get_place_holder(),
        );

        let upsert_sql = format!(
            "INSERT INTO {}{}{} ({}) VALUES({})
            ON CONFLICT({}) DO UPDATE SET {}",
            self.get_wrap_char(),
            table_name,
            self.get_wrap_char(),
            fields,
            marks,
            primary_fields,
            assign_clause
        )
        .to_string();
        self.post_process(upsert_sql)
    }

    fn get_update_sql(&self, mutation: &dyn Mutation, primary: &dyn Primary) -> String {
        let table_name = primary.get_table_name();
        let body_field_names = mutation.get_fields_name();
        let body_fields = wrap_locate_fields(
            &body_field_names,
            self.get_wrap_char(),
            self.get_place_holder(),
        );
        let primary_field_names = primary.get_primary_field_names();
        let primary_fields = wrap_locate_str_fields(
            &primary_field_names,
            self.get_wrap_char(),
            self.get_place_holder(),
        );
        let update_sql = format!(
            "UPDATE {}{}{} SET {} WHERE {}",
            self.get_wrap_char(),
            table_name,
            self.get_wrap_char(),
            body_fields,
            primary_fields
        )
        .to_string();
        self.post_process(update_sql)
    }

    fn get_change_sql(&self, mutation: &dyn Mutation, location: &dyn Location) -> String {
        let table_name = location.get_table_name();
        let mutation_fields = mutation.get_fields_name();
        let update_clause = wrap_locate_fields(
            &mutation_fields,
            self.get_wrap_char(),
            self.get_place_holder(),
        );

        let where_clause = location.get_where_clause(self.get_wrap_char(), self.get_place_holder());
        let update_sql = format!(
            "UPDATE {}{}{} SET {} WHERE {}",
            self.get_wrap_char(),
            table_name,
            self.get_wrap_char(),
            update_clause,
            where_clause
        )
        .to_string();
        self.post_process(update_sql)
    }

    fn get_delete_sql(&self, primary: &dyn Primary) -> String {
        let table_name = primary.get_table_name();
        let field_names = primary.get_primary_field_names();
        let where_clause =
            wrap_locate_str_fields(field_names, self.get_wrap_char(), self.get_place_holder());
        let delete_sql = format!(
            "DELETE FROM {}{}{} WHERE {}",
            self.get_wrap_char(),
            table_name,
            self.get_wrap_char(),
            where_clause
        )
        .to_string();
        self.post_process(delete_sql)
    }

    fn get_purify_sql(&self, location: &dyn Location) -> String {
        let table_name = location.get_table_name();
        let where_clause = location.get_where_clause(self.get_wrap_char(), self.get_place_holder());
        let delete_sql = format!(
            "DELETE FROM {}{}{} WHERE {}",
            self.get_wrap_char(),
            table_name,
            self.get_wrap_char(),
            where_clause
        )
        .to_string();
        self.post_process(delete_sql)
    }
}
#[inline]
fn wrap_fields(fields: &[String], wrap_char: char) -> String {
    fields
        .iter()
        .map(|e| format!("{}{}{}", wrap_char, e, wrap_char))
        .collect::<Vec<String>>()
        .join(",")
}

#[inline]
fn wrap_locate_fields(fields: &[String], wrap_char: char, place_holder: char) -> String {
    fields
        .iter()
        .map(|e| format!("{}{}{} = {}", wrap_char, e, wrap_char, place_holder))
        .collect::<Vec<String>>()
        .join(",")
}

#[inline]
fn wrap_str_fields(fields: &[&str], wrap_char: char) -> String {
    fields
        .iter()
        .map(|e| format!("{}{}{}", wrap_char, e, wrap_char))
        .collect::<Vec<String>>()
        .join(",")
}

#[inline]
fn wrap_locate_str_fields(fields: &[&str], wrap_char: char, place_holder: char) -> String {
    fields
        .iter()
        .map(|e| format!("{}{}{} = {}", wrap_char, e, wrap_char, place_holder))
        .collect::<Vec<String>>()
        .join(",")
}

#[inline]
fn wrap_pg_locate_str_fields(fields: &[&str], wrap_char: char) -> String {
    fields
        .iter()
        .enumerate()
        .map(|(i, e)| format!("{}{}{} = ${}", wrap_char, e, wrap_char, i + 1))
        .collect::<Vec<String>>()
        .join(",")
}

#[inline]
fn generate_question_marks(fields: &[&str]) -> String {
    fields
        .iter()
        .map(|_| "?".to_string())
        .collect::<Vec<String>>()
        .join(", ")
}
#[inline]
fn generate_question_mark_list(fields: &[String]) -> String {
    fields
        .iter()
        .map(|_| "?".to_string())
        .collect::<Vec<String>>()
        .join(", ")
}