wae-database 0.0.2

WAE Database - 数据库服务抽象层,支持 Turso/PostgreSQL/MySQL
Documentation
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
//! 查询构建器模块
//!
//! 提供 SELECT、INSERT、UPDATE、DELETE 查询构建器

use super::{
    condition::{Condition, Order},
    entity::{Entity, ToRow},
};
use std::marker::PhantomData;
use wae_types::Value;

#[cfg(feature = "limbo")]
use crate::types::from_wae_value;
#[cfg(feature = "limbo")]
use limbo::Value as LimboValue;

#[cfg(feature = "mysql")]
use crate::types::from_wae_to_mysql;
#[cfg(feature = "mysql")]
use mysql_async::Value as MySqlValue;

/// JOIN 类型
#[derive(Debug, Clone, Copy)]
pub enum JoinType {
    /// 内连接
    Inner,
    /// 左连接
    Left,
    /// 右连接
    Right,
    /// 全连接
    Full,
}

/// JOIN 定义
#[derive(Debug, Clone)]
pub struct Join {
    /// JOIN 类型
    join_type: JoinType,
    /// 表名
    table: String,
    /// ON 条件
    on: Condition,
}

impl Join {
    /// 创建新的 JOIN
    pub fn new(join_type: JoinType, table: String, on: Condition) -> Self {
        Self { join_type, table, on }
    }
}

/// SELECT 查询构建器
pub struct SelectBuilder<E: Entity> {
    columns: Vec<String>,
    joins: Vec<Join>,
    conditions: Vec<Condition>,
    group_by: Vec<String>,
    having: Vec<Condition>,
    order_by: Vec<(String, Order)>,
    limit: Option<u64>,
    offset: Option<u64>,
    _marker: PhantomData<E>,
}

impl<E: Entity> SelectBuilder<E> {
    pub(crate) fn new() -> Self {
        Self {
            columns: Vec::new(),
            joins: Vec::new(),
            conditions: Vec::new(),
            group_by: Vec::new(),
            having: Vec::new(),
            order_by: Vec::new(),
            limit: None,
            offset: None,
            _marker: PhantomData,
        }
    }

    /// 选择指定列
    pub fn columns(mut self, columns: &[&str]) -> Self {
        self.columns = columns.iter().map(|s| s.to_string()).collect();
        self
    }

    /// 添加 JOIN
    pub fn join(mut self, join_type: JoinType, table: &str, on: Condition) -> Self {
        self.joins.push(Join::new(join_type, table.to_string(), on));
        self
    }

    /// 添加 INNER JOIN
    pub fn inner_join(mut self, table: &str, on: Condition) -> Self {
        self.joins.push(Join::new(JoinType::Inner, table.to_string(), on));
        self
    }

    /// 添加 LEFT JOIN
    pub fn left_join(mut self, table: &str, on: Condition) -> Self {
        self.joins.push(Join::new(JoinType::Left, table.to_string(), on));
        self
    }

    /// 添加 RIGHT JOIN
    pub fn right_join(mut self, table: &str, on: Condition) -> Self {
        self.joins.push(Join::new(JoinType::Right, table.to_string(), on));
        self
    }

    /// 添加 FULL JOIN
    pub fn full_join(mut self, table: &str, on: Condition) -> Self {
        self.joins.push(Join::new(JoinType::Full, table.to_string(), on));
        self
    }

    /// 添加 WHERE 条件
    pub fn where_(mut self, condition: Condition) -> Self {
        self.conditions.push(condition);
        self
    }

    /// 添加多个 WHERE 条件 (AND 连接)
    pub fn where_all(mut self, conditions: Vec<Condition>) -> Self {
        self.conditions.extend(conditions);
        self
    }

    /// 添加 GROUP BY
    pub fn group_by(mut self, columns: &[&str]) -> Self {
        self.group_by = columns.iter().map(|s| s.to_string()).collect();
        self
    }

    /// 添加 HAVING 条件
    pub fn having(mut self, condition: Condition) -> Self {
        self.having.push(condition);
        self
    }

    /// 添加多个 HAVING 条件 (AND 连接)
    pub fn having_all(mut self, conditions: Vec<Condition>) -> Self {
        self.having.extend(conditions);
        self
    }

    /// 添加排序
    pub fn order_by<C: Into<String>>(mut self, column: C, order: Order) -> Self {
        self.order_by.push((column.into(), order));
        self
    }

    /// 设置 LIMIT
    pub fn limit(mut self, limit: u64) -> Self {
        self.limit = Some(limit);
        self
    }

    /// 设置 OFFSET
    pub fn offset(mut self, offset: u64) -> Self {
        self.offset = Some(offset);
        self
    }

    #[cfg(feature = "limbo")]
    /// 构建 SQL 和参数 (内部使用)
    pub(crate) fn build_limbo(&self) -> (String, Vec<LimboValue>) {
        let columns = if self.columns.is_empty() { "*".to_string() } else { self.columns.join(", ") };

        let mut sql = format!("SELECT {} FROM {}", columns, E::table_name());
        let mut params = Vec::new();

        for join in &self.joins {
            let (on_sql, on_params) = join.on.build_limbo();
            let join_type_str = match join.join_type {
                JoinType::Inner => "INNER JOIN",
                JoinType::Left => "LEFT JOIN",
                JoinType::Right => "RIGHT JOIN",
                JoinType::Full => "FULL JOIN",
            };
            sql.push_str(&format!(" {} {} ON {}", join_type_str, join.table, on_sql));
            params.extend(on_params);
        }

        if !self.conditions.is_empty() {
            let where_conditions = self.conditions.to_vec();
            if where_conditions.len() == 1 {
                let (cond_sql, cond_params) = where_conditions[0].build_limbo();
                sql.push_str(&format!(" WHERE {}", cond_sql));
                params.extend(cond_params);
            }
            else {
                let (cond_sql, cond_params) = Condition::and(where_conditions).build_limbo();
                sql.push_str(&format!(" WHERE {}", cond_sql));
                params.extend(cond_params);
            }
        }

        if !self.group_by.is_empty() {
            sql.push_str(&format!(" GROUP BY {}", self.group_by.join(", ")));
        }

        if !self.having.is_empty() {
            let having_conditions = self.having.to_vec();
            if having_conditions.len() == 1 {
                let (cond_sql, cond_params) = having_conditions[0].build_limbo();
                sql.push_str(&format!(" HAVING {}", cond_sql));
                params.extend(cond_params);
            }
            else {
                let (cond_sql, cond_params) = Condition::and(having_conditions).build_limbo();
                sql.push_str(&format!(" HAVING {}", cond_sql));
                params.extend(cond_params);
            }
        }

        if !self.order_by.is_empty() {
            let order_parts: Vec<String> = self
                .order_by
                .iter()
                .map(|(col, order)| {
                    format!(
                        "{} {}",
                        col,
                        match order {
                            Order::Asc => "ASC",
                            Order::Desc => "DESC",
                        }
                    )
                })
                .collect();
            sql.push_str(&format!(" ORDER BY {}", order_parts.join(", ")));
        }

        if let Some(limit) = self.limit {
            sql.push_str(&format!(" LIMIT {}", limit));
        }

        if let Some(offset) = self.offset {
            sql.push_str(&format!(" OFFSET {}", offset));
        }

        (sql, params)
    }

    #[cfg(feature = "mysql")]
    #[allow(dead_code)]
    /// 构建 SQL 和参数 (内部使用 MySQL)
    pub(crate) fn build_mysql(&self) -> (String, Vec<MySqlValue>) {
        let columns = if self.columns.is_empty() { "*".to_string() } else { self.columns.join(", ") };

        let mut sql = format!("SELECT {} FROM {}", columns, E::table_name());
        let mut params = Vec::new();

        for join in &self.joins {
            let (on_sql, on_params) = join.on.build_mysql();
            let join_type_str = match join.join_type {
                JoinType::Inner => "INNER JOIN",
                JoinType::Left => "LEFT JOIN",
                JoinType::Right => "RIGHT JOIN",
                JoinType::Full => "FULL JOIN",
            };
            sql.push_str(&format!(" {} {} ON {}", join_type_str, join.table, on_sql));
            params.extend(on_params);
        }

        if !self.conditions.is_empty() {
            let where_conditions = self.conditions.to_vec();
            if where_conditions.len() == 1 {
                let (cond_sql, cond_params) = where_conditions[0].build_mysql();
                sql.push_str(&format!(" WHERE {}", cond_sql));
                params.extend(cond_params);
            }
            else {
                let (cond_sql, cond_params) = Condition::and(where_conditions).build_mysql();
                sql.push_str(&format!(" WHERE {}", cond_sql));
                params.extend(cond_params);
            }
        }

        if !self.group_by.is_empty() {
            sql.push_str(&format!(" GROUP BY {}", self.group_by.join(", ")));
        }

        if !self.having.is_empty() {
            let having_conditions = self.having.to_vec();
            if having_conditions.len() == 1 {
                let (cond_sql, cond_params) = having_conditions[0].build_mysql();
                sql.push_str(&format!(" HAVING {}", cond_sql));
                params.extend(cond_params);
            }
            else {
                let (cond_sql, cond_params) = Condition::and(having_conditions).build_mysql();
                sql.push_str(&format!(" HAVING {}", cond_sql));
                params.extend(cond_params);
            }
        }

        if !self.order_by.is_empty() {
            let order_parts: Vec<String> = self
                .order_by
                .iter()
                .map(|(col, order)| {
                    format!(
                        "{} {}",
                        col,
                        match order {
                            Order::Asc => "ASC",
                            Order::Desc => "DESC",
                        }
                    )
                })
                .collect();
            sql.push_str(&format!(" ORDER BY {}", order_parts.join(", ")));
        }

        if let Some(limit) = self.limit {
            sql.push_str(&format!(" LIMIT {}", limit));
        }

        if let Some(offset) = self.offset {
            sql.push_str(&format!(" OFFSET {}", offset));
        }

        (sql, params)
    }

    #[cfg(feature = "postgres")]
    /// 构建 SQL 和参数 (内部使用 PostgreSQL)
    pub(crate) fn build_postgres(&self) -> (String, Vec<crate::connection::postgres::PostgresParam>) {
        let columns = if self.columns.is_empty() { "*".to_string() } else { self.columns.join(", ") };

        let mut sql = format!("SELECT {} FROM {}", columns, E::table_name());
        let mut params = Vec::new();
        let mut param_offset = 0;

        for join in &self.joins {
            let (on_sql, on_params) = join.on.build_postgres();
            let on_sql = replace_placeholders_for_query(&on_sql, param_offset + 1);
            let join_type_str = match join.join_type {
                JoinType::Inner => "INNER JOIN",
                JoinType::Left => "LEFT JOIN",
                JoinType::Right => "RIGHT JOIN",
                JoinType::Full => "FULL JOIN",
            };
            sql.push_str(&format!(" {} {} ON {}", join_type_str, join.table, on_sql));
            let on_params_len = on_params.len();
            params.extend(on_params);
            param_offset += on_params_len;
        }

        if !self.conditions.is_empty() {
            let where_conditions = self.conditions.to_vec();
            let (cond_sql, cond_params) = if where_conditions.len() == 1 {
                where_conditions[0].build_postgres()
            }
            else {
                Condition::and(where_conditions).build_postgres()
            };
            let cond_sql = replace_placeholders_for_query(&cond_sql, param_offset + 1);
            sql.push_str(&format!(" WHERE {}", cond_sql));
            let cond_params_len = cond_params.len();
            params.extend(cond_params);
            param_offset += cond_params_len;
        }

        if !self.group_by.is_empty() {
            sql.push_str(&format!(" GROUP BY {}", self.group_by.join(", ")));
        }

        if !self.having.is_empty() {
            let having_conditions = self.having.to_vec();
            let (cond_sql, cond_params) = if having_conditions.len() == 1 {
                having_conditions[0].build_postgres()
            }
            else {
                Condition::and(having_conditions).build_postgres()
            };
            let cond_sql = replace_placeholders_for_query(&cond_sql, param_offset + 1);
            sql.push_str(&format!(" HAVING {}", cond_sql));
            params.extend(cond_params);
        }

        if !self.order_by.is_empty() {
            let order_parts: Vec<String> = self
                .order_by
                .iter()
                .map(|(col, order)| {
                    format!(
                        "{} {}",
                        col,
                        match order {
                            Order::Asc => "ASC",
                            Order::Desc => "DESC",
                        }
                    )
                })
                .collect();
            sql.push_str(&format!(" ORDER BY {}", order_parts.join(", ")));
        }

        if let Some(limit) = self.limit {
            sql.push_str(&format!(" LIMIT {}", limit));
        }

        if let Some(offset) = self.offset {
            sql.push_str(&format!(" OFFSET {}", offset));
        }

        (sql, params)
    }
}

#[cfg(feature = "postgres")]
fn replace_placeholders_for_query(sql: &str, start_index: usize) -> String {
    let mut result = String::new();
    let mut chars = sql.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '$' {
            let mut num_str = String::new();
            while let Some(&next_c) = chars.peek() {
                if next_c.is_ascii_digit() {
                    num_str.push(next_c);
                    chars.next();
                }
                else {
                    break;
                }
            }
            if !num_str.is_empty() {
                if let Ok(num) = num_str.parse::<usize>() {
                    result.push_str(&format!("${}", num + start_index - 1));
                }
            }
            else {
                result.push(c);
            }
        }
        else {
            result.push(c);
        }
    }
    result
}

/// INSERT 查询构建器
pub struct InsertBuilder<E: Entity> {
    data: Vec<(&'static str, Value)>,
    _marker: PhantomData<E>,
}

impl<E: Entity> InsertBuilder<E> {
    pub(crate) fn new() -> Self {
        Self { data: Vec::new(), _marker: PhantomData }
    }

    /// 从实体创建
    pub fn from_entity<T: ToRow>(entity: &T) -> Self {
        Self { data: entity.to_row(), _marker: PhantomData }
    }

    /// 添加列值
    pub fn value(mut self, column: &'static str, value: Value) -> Self {
        self.data.push((column, value));
        self
    }

    /// 批量添加列值
    pub fn values(mut self, data: Vec<(&'static str, Value)>) -> Self {
        self.data.extend(data);
        self
    }

    #[cfg(feature = "limbo")]
    /// 构建 SQL 和参数 (内部使用)
    pub(crate) fn build_limbo(&self) -> (String, Vec<LimboValue>) {
        let columns: Vec<&str> = self.data.iter().map(|(col, _)| *col).collect();
        let placeholders: Vec<&str> = self.data.iter().map(|_| "?").collect();
        let params: Vec<LimboValue> = self.data.iter().map(|(_, val)| from_wae_value(val.clone())).collect();

        let sql = format!("INSERT INTO {} ({}) VALUES ({})", E::table_name(), columns.join(", "), placeholders.join(", "));

        (sql, params)
    }

    #[cfg(feature = "mysql")]
    /// 构建 SQL 和参数 (内部使用 MySQL)
    pub(crate) fn build_mysql(&self) -> (String, Vec<MySqlValue>) {
        let columns: Vec<&str> = self.data.iter().map(|(col, _)| *col).collect();
        let placeholders: Vec<&str> = self.data.iter().map(|_| "?").collect();
        let params: Vec<MySqlValue> = self.data.iter().map(|(_, val)| from_wae_to_mysql(val.clone())).collect();

        let sql = format!("INSERT INTO {} ({}) VALUES ({})", E::table_name(), columns.join(", "), placeholders.join(", "));

        (sql, params)
    }
}

/// UPDATE 查询构建器
pub struct UpdateBuilder<E: Entity> {
    data: Vec<(&'static str, Value)>,
    conditions: Vec<Condition>,
    _marker: PhantomData<E>,
}

impl<E: Entity> UpdateBuilder<E> {
    pub(crate) fn new() -> Self {
        Self { data: Vec::new(), conditions: Vec::new(), _marker: PhantomData }
    }

    /// 设置列值
    pub fn set(mut self, column: &'static str, value: Value) -> Self {
        self.data.push((column, value));
        self
    }

    /// 批量设置列值
    pub fn set_all(mut self, data: Vec<(&'static str, Value)>) -> Self {
        self.data.extend(data);
        self
    }

    /// 从实体设置值 (排除主键)
    pub fn from_entity<T: ToRow + Entity>(entity: &T) -> Self {
        let id_col = T::id_column();
        let data: Vec<(&'static str, Value)> = entity.to_row().into_iter().filter(|(col, _)| *col != id_col).collect();
        Self { data, conditions: Vec::new(), _marker: PhantomData }
    }

    /// 添加 WHERE 条件
    pub fn where_(mut self, condition: Condition) -> Self {
        self.conditions.push(condition);
        self
    }

    /// 按主键更新
    pub fn where_id(mut self, id: E::Id) -> Self {
        self.conditions.push(Condition::eq(E::id_column(), id));
        self
    }

    #[cfg(feature = "limbo")]
    /// 构建 SQL 和参数 (内部使用)
    pub(crate) fn build_limbo(&self) -> (String, Vec<LimboValue>) {
        let set_parts: Vec<String> = self.data.iter().map(|(col, _)| format!("{} = ?", col)).collect();
        let mut params: Vec<LimboValue> = self.data.iter().map(|(_, val)| from_wae_value(val.clone())).collect();

        let mut sql = format!("UPDATE {} SET {}", E::table_name(), set_parts.join(", "));

        if !self.conditions.is_empty() {
            let where_conditions = self.conditions.to_vec();
            if where_conditions.len() == 1 {
                let (cond_sql, cond_params) = where_conditions[0].build_limbo();
                sql.push_str(&format!(" WHERE {}", cond_sql));
                params.extend(cond_params);
            }
            else {
                let (cond_sql, cond_params) = Condition::and(where_conditions).build_limbo();
                sql.push_str(&format!(" WHERE {}", cond_sql));
                params.extend(cond_params);
            }
        }

        (sql, params)
    }

    #[cfg(feature = "mysql")]
    /// 构建 SQL 和参数 (内部使用 MySQL)
    pub(crate) fn build_mysql(&self) -> (String, Vec<MySqlValue>) {
        let set_parts: Vec<String> = self.data.iter().map(|(col, _)| format!("{} = ?", col)).collect();
        let mut params: Vec<MySqlValue> = self.data.iter().map(|(_, val)| from_wae_to_mysql(val.clone())).collect();

        let mut sql = format!("UPDATE {} SET {}", E::table_name(), set_parts.join(", "));

        if !self.conditions.is_empty() {
            let where_conditions = self.conditions.to_vec();
            if where_conditions.len() == 1 {
                let (cond_sql, cond_params) = where_conditions[0].build_mysql();
                sql.push_str(&format!(" WHERE {}", cond_sql));
                params.extend(cond_params);
            }
            else {
                let (cond_sql, cond_params) = Condition::and(where_conditions).build_mysql();
                sql.push_str(&format!(" WHERE {}", cond_sql));
                params.extend(cond_params);
            }
        }

        (sql, params)
    }
}

/// DELETE 查询构建器
pub struct DeleteBuilder<E: Entity> {
    conditions: Vec<Condition>,
    _marker: PhantomData<E>,
}

impl<E: Entity> DeleteBuilder<E> {
    pub(crate) fn new() -> Self {
        Self { conditions: Vec::new(), _marker: PhantomData }
    }

    /// 添加 WHERE 条件
    pub fn where_(mut self, condition: Condition) -> Self {
        self.conditions.push(condition);
        self
    }

    /// 按主键删除
    pub fn where_id(mut self, id: E::Id) -> Self {
        self.conditions.push(Condition::eq(E::id_column(), id));
        self
    }

    #[cfg(feature = "limbo")]
    /// 构建 SQL 和参数 (内部使用)
    pub(crate) fn build_limbo(&self) -> (String, Vec<LimboValue>) {
        let mut sql = format!("DELETE FROM {}", E::table_name());
        let mut params = Vec::new();

        if !self.conditions.is_empty() {
            let where_conditions = self.conditions.to_vec();
            if where_conditions.len() == 1 {
                let (cond_sql, cond_params) = where_conditions[0].build_limbo();
                sql.push_str(&format!(" WHERE {}", cond_sql));
                params.extend(cond_params);
            }
            else {
                let (cond_sql, cond_params) = Condition::and(where_conditions).build_limbo();
                sql.push_str(&format!(" WHERE {}", cond_sql));
                params.extend(cond_params);
            }
        }

        (sql, params)
    }

    #[cfg(feature = "mysql")]
    /// 构建 SQL 和参数 (内部使用 MySQL)
    pub(crate) fn build_mysql(&self) -> (String, Vec<MySqlValue>) {
        let mut sql = format!("DELETE FROM {}", E::table_name());
        let mut params = Vec::new();

        if !self.conditions.is_empty() {
            let where_conditions = self.conditions.to_vec();
            if where_conditions.len() == 1 {
                let (cond_sql, cond_params) = where_conditions[0].build_mysql();
                sql.push_str(&format!(" WHERE {}", cond_sql));
                params.extend(cond_params);
            }
            else {
                let (cond_sql, cond_params) = Condition::and(where_conditions).build_mysql();
                sql.push_str(&format!(" WHERE {}", cond_sql));
                params.extend(cond_params);
            }
        }

        (sql, params)
    }
}

/// 查询构建器入口
pub struct QueryBuilder;

impl QueryBuilder {
    /// 创建 SELECT 构建器
    pub fn select<E: Entity>() -> SelectBuilder<E> {
        SelectBuilder::new()
    }

    /// 创建 INSERT 构建器
    pub fn insert<E: Entity>() -> InsertBuilder<E> {
        InsertBuilder::new()
    }

    /// 创建 UPDATE 构建器
    pub fn update<E: Entity>() -> UpdateBuilder<E> {
        UpdateBuilder::new()
    }

    /// 创建 DELETE 构建器
    pub fn delete<E: Entity>() -> DeleteBuilder<E> {
        DeleteBuilder::new()
    }
}