rat_quickdb 0.5.2

强大的跨数据库ODM库,支持自动索引创建、统一接口和现代异步架构
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
//! ModelManager 实现模块
//!
//! 提供模型的通用操作实现

use crate::error::{QuickDbError, QuickDbResult};
use crate::model::traits::{Model, ModelOperations};
use crate::odm::{self, OdmOperations};
use crate::types::*;
use async_trait::async_trait;
use rat_logger::debug;
use std::collections::HashMap;
use std::marker::PhantomData;

/// 模型管理器
///
/// 提供模型的通用操作实现
pub struct ModelManager<T: Model> {
    _phantom: PhantomData<T>,
}

impl<T: Model> ModelManager<T> {
    /// 创建新的模型管理器
    pub fn new() -> Self {
        Self {
            _phantom: PhantomData,
        }
    }

    // ========== 简化方法:接受 QueryCondition(自动转换) ==========

    /// 查找模型(简化方法)
    ///
    /// 接受 `Vec<QueryCondition>` 并自动转换为 `Vec<QueryConditionWithConfig>`
    /// 适用于不需要额外配置的常见查询场景
    pub async fn find(
        conditions: Vec<QueryCondition>,
        options: Option<QueryOptions>,
    ) -> QuickDbResult<Vec<T>> {
        let conditions_with_config: Vec<QueryConditionWithConfig> = conditions
            .into_iter()
            .map(|c| c.into())
            .collect();
        Self::find_with_config(conditions_with_config, options).await
    }

    /// 统计模型数量(简化方法)
    ///
    /// 接受 `Vec<QueryCondition>` 并自动转换为 `Vec<QueryConditionWithConfig>`
    pub async fn count(conditions: Vec<QueryCondition>) -> QuickDbResult<u64> {
        let conditions_with_config: Vec<QueryConditionWithConfig> = conditions
            .into_iter()
            .map(|c| c.into())
            .collect();
        Self::count_with_config(conditions_with_config).await
    }

    /// 批量删除模型(简化方法)
    ///
    /// 接受 `Vec<QueryCondition>` 并自动转换为 `Vec<QueryConditionWithConfig>`
    pub async fn delete_many(conditions: Vec<QueryCondition>) -> QuickDbResult<u64> {
        let conditions_with_config: Vec<QueryConditionWithConfig> = conditions
            .into_iter()
            .map(|c| c.into())
            .collect();
        Self::delete_many_with_config(conditions_with_config).await
    }

    /// 查找模型(简化方法,支持缓存控制)
    ///
    /// 接受 `Vec<QueryCondition>` 并自动转换为 `Vec<QueryConditionWithConfig>`
    pub async fn find_with_cache_control(
        conditions: Vec<QueryCondition>,
        options: Option<QueryOptions>,
        bypass_cache: bool,
    ) -> QuickDbResult<Vec<T>> {
        let conditions_with_config: Vec<QueryConditionWithConfig> = conditions
            .into_iter()
            .map(|c| c.into())
            .collect();
        <Self as ModelOperations<T>>::find_with_cache_control(conditions_with_config, options, bypass_cache).await
    }

    // ========== 完整方法:接受 QueryConditionWithConfig ==========

    /// 查找模型(带配置)
    ///
    /// 接受 `Vec<QueryConditionWithConfig>`,支持大小写不敏感等高级配置
    pub async fn find_with_config(
        conditions: Vec<QueryConditionWithConfig>,
        options: Option<QueryOptions>,
    ) -> QuickDbResult<Vec<T>> {
        <Self as ModelOperations<T>>::find_with_cache_control(conditions, options, false).await
    }

    /// 统计模型数量(带配置)
    ///
    /// 接受 `Vec<QueryConditionWithConfig>`,支持大小写不敏感等高级配置
    pub async fn count_with_config(conditions: Vec<QueryConditionWithConfig>) -> QuickDbResult<u64> {
        <Self as ModelOperations<T>>::count_with_config(conditions).await
    }

    /// 使用条件组统计模型数量(简化版)
    ///
    /// 接受 `Vec<QueryConditionGroup>`,支持复杂的AND/OR逻辑组合
    pub async fn count_with_groups(
        condition_groups: Vec<QueryConditionGroup>,
    ) -> QuickDbResult<u64> {
        <Self as ModelOperations<T>>::count_with_groups(condition_groups).await
    }

    /// 使用条件组统计模型数量(完整版)
    ///
    /// 接受 `Vec<QueryConditionGroupWithConfig>`,支持大小写不敏感等高级配置
    pub async fn count_with_groups_with_config(
        condition_groups: Vec<QueryConditionGroupWithConfig>,
    ) -> QuickDbResult<u64> {
        <Self as ModelOperations<T>>::count_with_groups_with_config(condition_groups).await
    }

    /// 批量删除模型(带配置)
    ///
    /// 接受 `Vec<QueryConditionWithConfig>`,支持大小写不敏感等高级配置
    pub async fn delete_many_with_config(conditions: Vec<QueryConditionWithConfig>) -> QuickDbResult<u64> {
        <Self as ModelOperations<T>>::delete_many(conditions).await
    }

    /// 创建表(静态便利方法)
    ///
    /// 使用模型的元数据直接创建表,无需插入数据
    /// 这是 ModelOperations::create_table 的静态便利包装器
    ///
    /// # 不推荐使用
    ///
    /// 这个方法通常不需要手动调用,因为在执行其他数据库操作时,
    /// 表会根据需要自动创建。仅在确实需要预先创建表结构时使用。
    pub async fn create_table() -> QuickDbResult<()> {
        <Self as ModelOperations<T>>::create_table().await
    }
}

#[async_trait]
impl<T: Model> ModelOperations<T> for ModelManager<T> {
    async fn save(&self) -> QuickDbResult<String> {
        // 这个方法需要模型实例,应该在具体的模型实现中调用
        Err(QuickDbError::ValidationError {
            field: "save".to_string(),
            message: "save方法需要在模型实例上调用".to_string(),
        })
    }

    async fn find_by_id(id: &str) -> QuickDbResult<Option<T>> {
        let collection_name = T::collection_name();
        let database_alias = T::database_alias();

        debug!("根据ID查找模型: collection={}, id={}", collection_name, id);

        let result = odm::find_by_id(&collection_name, id, database_alias.as_deref()).await?;

        if let Some(data_value) = result {
            // 处理 DataValue::Object 格式的数据
            match data_value {
                DataValue::Object(data_map) => {
                    debug!("从数据库收到的数据: {:?}", data_map);
                    let model: T = match T::from_data_map(data_map.clone()) {
                        Ok(model) => model,
                        Err(e) => {
                            debug!("❌ from_data_map失败: {}, 数据: {:?}", e, data_map);
                            return Err(e);
                        }
                    };
                    Ok(Some(model))
                }
                _ => {
                    // 兼容其他格式,使用直接反序列化
                    debug!("收到非Object格式数据: {:?}", data_value);
                    let model: T = data_value.deserialize_to()?;
                    Ok(Some(model))
                }
            }
        } else {
            Ok(None)
        }
    }

    async fn find_with_cache_control(
        conditions: Vec<QueryConditionWithConfig>,
        options: Option<QueryOptions>,
        bypass_cache: bool,
    ) -> QuickDbResult<Vec<T>> {
        let collection_name = T::collection_name();
        let database_alias = T::database_alias();

        debug!("查找模型(bypass_cache={}): collection={}", bypass_cache, collection_name);

        let result = odm::find_with_cache_control(
            &collection_name,
            conditions,
            options,
            database_alias.as_deref(),
            bypass_cache,
        )
        .await?;

        // result 已经是 Vec<DataValue>,直接处理
        let mut models = Vec::new();
        for data_value in result {
            // 处理 DataValue::Object 格式的数据
            match data_value {
                DataValue::Object(data_map) => {
                    debug!("查询收到的数据: {:?}", data_map);
                    let model: T = match T::from_data_map(data_map.clone()) {
                        Ok(model) => model,
                        Err(e) => {
                            debug!("❌ 查询from_data_map失败: {}, 数据: {:?}", e, data_map);
                            continue;
                        }
                    };
                    models.push(model);
                }
                _ => {
                    // 兼容其他格式,使用直接反序列化
                    debug!("查询收到非Object格式数据: {:?}", data_value);
                    let model: T = data_value.deserialize_to()?;
                    models.push(model);
                }
            }
        }
        Ok(models)
    }

    async fn update(&self, _updates: HashMap<String, DataValue>) -> QuickDbResult<bool> {
        // 这个方法需要模型实例,应该在具体的模型实现中调用
        Err(QuickDbError::ValidationError {
            field: "update".to_string(),
            message: "update方法需要在模型实例上调用".to_string(),
        })
    }

    async fn delete(&self) -> QuickDbResult<bool> {
        // 这个方法需要模型实例,应该在具体的模型实现中调用
        Err(QuickDbError::ValidationError {
            field: "delete".to_string(),
            message: "delete方法需要在模型实例上调用".to_string(),
        })
    }

    async fn count(conditions: Vec<QueryCondition>) -> QuickDbResult<u64> {
        let conditions_with_config: Vec<QueryConditionWithConfig> = conditions
            .into_iter()
            .map(|c| c.into())
            .collect();
        Self::count_with_config(conditions_with_config).await
    }

    async fn count_with_config(conditions: Vec<QueryConditionWithConfig>) -> QuickDbResult<u64> {
        let collection_name = T::collection_name();
        let database_alias = T::database_alias();

        debug!("统计模型数量: collection={}", collection_name);

        odm::count(&collection_name, conditions, database_alias.as_deref()).await
    }

    async fn count_with_groups_with_config(
        condition_groups: Vec<QueryConditionGroupWithConfig>,
    ) -> QuickDbResult<u64> {
        let collection_name = T::collection_name();
        let database_alias = T::database_alias();

        debug!("使用条件组统计模型数量: collection={}", collection_name);

        odm::count_with_groups(&collection_name, condition_groups, database_alias.as_deref()).await
    }

    async fn find_with_groups_with_cache_control(
        condition_groups: Vec<QueryConditionGroup>,
        options: Option<QueryOptions>,
        bypass_cache: bool,
    ) -> QuickDbResult<Vec<T>> {
        let collection_name = T::collection_name();
        let database_alias = T::database_alias();

        debug!("使用条件组查找模型(bypass_cache={}): collection={}", bypass_cache, collection_name);

        // 转换为完整版
        let condition_groups_with_config: Vec<QueryConditionGroupWithConfig> = condition_groups
            .into_iter()
            .map(|g| g.into())
            .collect();

        let result = odm::find_with_groups_with_cache_control(
            &collection_name,
            condition_groups_with_config,
            options,
            database_alias.as_deref(),
            bypass_cache,
        )
        .await?;

        // 处理返回的 DataValue 数据
        let mut models = Vec::new();
        for data_value in result {
            let model: T = T::from_data_map(data_value.expect_object()?)?;
            models.push(model);
        }
        Ok(models)
    }

    async fn find_with_groups(
        condition_groups: Vec<QueryConditionGroup>,
        options: Option<QueryOptions>,
    ) -> QuickDbResult<Vec<T>> {
        Self::find_with_groups_with_cache_control(condition_groups, options, false).await
    }

    async fn find_with_groups_with_config(
        condition_groups: Vec<QueryConditionGroupWithConfig>,
        options: Option<QueryOptions>,
    ) -> QuickDbResult<Vec<T>> {
        Self::find_with_groups_with_cache_control_and_config(condition_groups, options, false).await
    }

    async fn find_with_groups_with_cache_control_and_config(
        condition_groups: Vec<QueryConditionGroupWithConfig>,
        options: Option<QueryOptions>,
        bypass_cache: bool,
    ) -> QuickDbResult<Vec<T>> {
        let collection_name = T::collection_name();
        let database_alias = T::database_alias();

        debug!("使用条件组查找模型(bypass_cache={}): collection={}", bypass_cache, collection_name);

        let result = odm::find_with_groups_with_cache_control(
            &collection_name,
            condition_groups,
            options,
            database_alias.as_deref(),
            bypass_cache,
        )
        .await?;

        // 处理返回的 DataValue 数据
        let mut models = Vec::new();
        for data_value in result {
            let model: T = T::from_data_map(data_value.expect_object()?)?;
            models.push(model);
        }
        Ok(models)
    }

    /// 批量更新模型
    ///
    /// 根据条件批量更新多条记录,返回受影响的行数
    async fn update_many(
        conditions: Vec<QueryConditionWithConfig>,
        updates: HashMap<String, DataValue>,
    ) -> QuickDbResult<u64> {
        let collection_name = T::collection_name();
        let database_alias = T::database_alias();

        debug!(
            "批量更新模型: collection={}, 条件数量={}",
            collection_name,
            conditions.len()
        );

        odm::update(
            &collection_name,
            conditions,
            updates,
            database_alias.as_deref(),
        )
        .await
    }

    /// 使用操作数组批量更新模型
    ///
    /// 根据条件使用操作数组批量更新多条记录,支持原子性增减操作,返回受影响的行数
    async fn update_many_with_operations(
        conditions: Vec<QueryConditionWithConfig>,
        operations: Vec<crate::types::UpdateOperation>,
    ) -> QuickDbResult<u64> {
        let collection_name = T::collection_name();
        let database_alias = T::database_alias();

        debug!(
            "使用操作数组批量更新模型: collection={}, 条件数量={}, 操作数量={}",
            collection_name,
            conditions.len(),
            operations.len()
        );

        odm::update_with_operations(
            &collection_name,
            conditions,
            operations,
            database_alias.as_deref(),
        )
        .await
    }

    /// 批量删除模型
    ///
    /// 根据条件批量删除多条记录,返回受影响的行数
    async fn delete_many(conditions: Vec<QueryConditionWithConfig>) -> QuickDbResult<u64> {
        let collection_name = T::collection_name();
        let database_alias = T::database_alias();

        debug!(
            "批量删除模型: collection={}, 条件数量={}",
            collection_name,
            conditions.len()
        );

        odm::delete(&collection_name, conditions, database_alias.as_deref()).await
    }

    /// 创建表
    ///
    /// 使用模型的元数据直接创建表,无需插入数据
    /// 复用现有的ensure_table_and_indexes功能
    async fn create_table() -> QuickDbResult<()> {
        let collection_name = T::collection_name();
        let database_alias = T::database_alias();

        debug!("直接创建表: collection={}", collection_name);

        // 获取默认数据库别名
        let alias = database_alias.as_deref().unwrap_or("default");

        // 使用现有的ensure_table_and_indexes功能
        crate::manager::ensure_table_and_indexes(&collection_name, alias).await
    }

    /// 创建存储过程
    ///
    /// 通过模型管理器创建跨模型的存储过程,以当前模型作为基表
    async fn create_stored_procedure(
        config: crate::stored_procedure::StoredProcedureConfig,
    ) -> QuickDbResult<crate::stored_procedure::StoredProcedureCreateResult> {
        debug!("通过模型管理器创建存储过程: {}", config.procedure_name);
        let odm_manager = odm::get_odm_manager().await;
        // 使用config中包含的数据库别名,不传递额外的alias参数
        odm_manager.create_stored_procedure(config).await
    }

    /// 执行存储过程查询
    ///
    /// 通过模型管理器执行存储过程查询,使用当前模型的数据库别名
    async fn execute_stored_procedure(
        procedure_name: &str,
        params: Option<std::collections::HashMap<String, crate::types::DataValue>>,
    ) -> QuickDbResult<crate::stored_procedure::StoredProcedureQueryResult> {
        debug!("通过模型管理器执行存储过程: {}", procedure_name);
        let odm_manager = odm::get_odm_manager().await;
        // 使用模型的数据库别名,如果没有则使用默认
        let database_alias = T::database_alias().or_else(|| Some("default".to_string()));
        odm_manager
            .execute_stored_procedure(procedure_name, database_alias.as_deref(), params)
            .await
    }
}