rat_quickdb 0.5.2

强大的跨数据库ODM库,支持自动索引创建、统一接口和现代异步架构
Documentation
//! # 全局ODM管理器和便捷函数

use crate::error::QuickDbResult;
use crate::odm::manager_core::AsyncOdmManager;
use crate::odm::traits::OdmOperations;
use crate::types::*;
use std::collections::HashMap;

/// 全局异步ODM管理器实例
static ASYNC_ODM_MANAGER: once_cell::sync::Lazy<tokio::sync::RwLock<AsyncOdmManager>> =
    once_cell::sync::Lazy::new(|| tokio::sync::RwLock::new(AsyncOdmManager::new()));

/// 获取全局ODM管理器
pub async fn get_odm_manager() -> tokio::sync::RwLockReadGuard<'static, AsyncOdmManager> {
    ASYNC_ODM_MANAGER.read().await
}

/// 获取可变的全局ODM管理器
pub async fn get_odm_manager_mut() -> tokio::sync::RwLockWriteGuard<'static, AsyncOdmManager> {
    ASYNC_ODM_MANAGER.write().await
}

/// 便捷函数:创建记录
///
/// 【注意】这是一个内部函数,建议通过ModelManager或模型的save方法进行操作
/// 除非您明确知道自己在做什么,否则不要直接调用此函数
#[doc(hidden)]
pub async fn create(
    collection: &str,
    data: HashMap<String, DataValue>,
    alias: Option<&str>,
) -> QuickDbResult<DataValue> {
    // 锁定全局操作
    crate::lock_global_operations();

    let manager = get_odm_manager().await;
    manager.create(collection, data, alias).await
}

/// 便捷函数:根据ID查询记录
///
/// 【注意】这是一个内部函数,建议通过ModelManager或模型的find_by_id方法进行操作
/// 除非您明确知道自己在做什么,否则不要直接调用此函数
#[doc(hidden)]
pub async fn find_by_id(
    collection: &str,
    id: &str,
    alias: Option<&str>,
) -> QuickDbResult<Option<DataValue>> {
    // 锁定全局操作
    crate::lock_global_operations();

    let manager = get_odm_manager().await;
    manager.find_by_id(collection, id, alias).await
}

/// 便捷函数:查询记录(支持缓存控制)
///
/// 【注意】这是一个内部函数,建议通过ModelManager或模型的find方法进行操作
/// 除非您明确知道自己在做什么,否则不要直接调用此函数
#[doc(hidden)]
pub async fn find_with_cache_control(
    collection: &str,
    conditions: Vec<QueryConditionWithConfig>,
    options: Option<QueryOptions>,
    alias: Option<&str>,
    bypass_cache: bool,
) -> QuickDbResult<Vec<DataValue>> {
    // 锁定全局操作
    crate::lock_global_operations();

    let manager = get_odm_manager().await;
    manager.find_with_cache_control(collection, conditions, options, alias, bypass_cache).await
}

/// 便捷函数:查询记录
///
/// 【注意】这是一个内部函数,建议通过ModelManager或模型的find方法进行操作
/// 除非您明确知道自己在做什么,否则不要直接调用此函数
#[doc(hidden)]
pub async fn find(
    collection: &str,
    conditions: Vec<QueryConditionWithConfig>,
    options: Option<QueryOptions>,
    alias: Option<&str>,
) -> QuickDbResult<Vec<DataValue>> {
    find_with_cache_control(collection, conditions, options, alias, false).await
}

/// 分组查询便捷函数(支持缓存控制)
///
/// 【注意】这是一个内部函数,建议通过ModelManager或模型的find_with_groups方法进行操作
/// 除非您明确知道自己在做什么,否则不要直接调用此函数
#[doc(hidden)]
pub async fn find_with_groups_with_cache_control(
    collection: &str,
    condition_groups: Vec<QueryConditionGroupWithConfig>,
    options: Option<QueryOptions>,
    alias: Option<&str>,
    bypass_cache: bool,
) -> QuickDbResult<Vec<DataValue>> {
    // 锁定全局操作
    crate::lock_global_operations();

    let manager = get_odm_manager().await;
    manager
        .find_with_groups_with_cache_control_and_config(collection, condition_groups, options, alias, bypass_cache)
        .await
}

/// 分组查询便捷函数
///
/// 【注意】这是一个内部函数,建议通过ModelManager或模型的find_with_groups方法进行操作
/// 除非您明确知道自己在做什么,否则不要直接调用此函数
#[doc(hidden)]
pub async fn find_with_groups(
    collection: &str,
    condition_groups: Vec<QueryConditionGroup>,
    options: Option<QueryOptions>,
    alias: Option<&str>,
) -> QuickDbResult<Vec<DataValue>> {
    let condition_groups_with_config: Vec<QueryConditionGroupWithConfig> = condition_groups
        .into_iter()
        .map(|g| g.into())
        .collect();
    find_with_groups_with_cache_control(collection, condition_groups_with_config, options, alias, false).await
}

/// 便捷函数:更新记录
///
/// 【注意】这是一个内部函数,建议通过ModelManager或模型的update方法进行操作
/// 除非您明确知道自己在做什么,否则不要直接调用此函数
#[doc(hidden)]
pub async fn update(
    collection: &str,
    conditions: Vec<QueryConditionWithConfig>,
    updates: HashMap<String, DataValue>,
    alias: Option<&str>,
) -> QuickDbResult<u64> {
    // 锁定全局操作
    crate::lock_global_operations();

    let manager = get_odm_manager().await;
    manager.update(collection, conditions, updates, alias).await
}

/// 便捷函数:根据ID更新记录
///
/// 【注意】这是一个内部函数,建议通过ModelManager或模型的update方法进行操作
/// 除非您明确知道自己在做什么,否则不要直接调用此函数
#[doc(hidden)]
pub async fn update_by_id(
    collection: &str,
    id: &str,
    updates: HashMap<String, DataValue>,
    alias: Option<&str>,
) -> QuickDbResult<bool> {
    // 锁定全局操作
    crate::lock_global_operations();

    let manager = get_odm_manager().await;
    manager.update_by_id(collection, id, updates, alias).await
}

/// 便捷函数:使用操作数组更新记录
///
/// 【注意】这是一个内部函数,建议通过ModelManager或模型的update_many_with_operations方法进行操作
/// 除非您明确知道自己在做什么,否则不要直接调用此函数
#[doc(hidden)]
pub async fn update_with_operations(
    collection: &str,
    conditions: Vec<QueryConditionWithConfig>,
    operations: Vec<crate::types::UpdateOperation>,
    alias: Option<&str>,
) -> QuickDbResult<u64> {
    // 锁定全局操作
    crate::lock_global_operations();

    let manager = get_odm_manager().await;
    manager
        .update_with_operations(collection, conditions, operations, alias)
        .await
}

/// 便捷函数:删除记录
///
/// 【注意】这是一个内部函数,建议通过ModelManager或模型的delete方法进行操作
/// 除非您明确知道自己在做什么,否则不要直接调用此函数
#[doc(hidden)]
pub async fn delete(
    collection: &str,
    conditions: Vec<QueryConditionWithConfig>,
    alias: Option<&str>,
) -> QuickDbResult<u64> {
    // 锁定全局操作
    crate::lock_global_operations();

    let manager = get_odm_manager().await;
    manager.delete(collection, conditions, alias).await
}

/// 便捷函数:根据ID删除记录
///
/// 【注意】这是一个内部函数,建议通过ModelManager或模型的delete方法进行操作
/// 除非您明确知道自己在做什么,否则不要直接调用此函数
#[doc(hidden)]
pub async fn delete_by_id(collection: &str, id: &str, alias: Option<&str>) -> QuickDbResult<bool> {
    // 锁定全局操作
    crate::lock_global_operations();

    let manager = get_odm_manager().await;
    manager.delete_by_id(collection, id, alias).await
}

/// 便捷函数:统计记录数量
///
/// 【注意】这是一个内部函数,建议通过ModelManager或模型的count方法进行操作
/// 除非您明确知道自己在做什么,否则不要直接调用此函数
#[doc(hidden)]
pub async fn count(
    collection: &str,
    conditions: Vec<QueryConditionWithConfig>,
    alias: Option<&str>,
) -> QuickDbResult<u64> {
    // 锁定全局操作
    crate::lock_global_operations();

    let manager = get_odm_manager().await;
    manager.count(collection, conditions, alias).await
}

/// 便捷函数:使用条件组统计记录数量
///
/// 【注意】这是一个内部函数,建议通过ModelManager或模型的count_with_groups方法进行操作
/// 除非您明确知道自己在做什么,否则不要直接调用此函数
#[doc(hidden)]
pub async fn count_with_groups(
    collection: &str,
    condition_groups: Vec<QueryConditionGroupWithConfig>,
    alias: Option<&str>,
) -> QuickDbResult<u64> {
    // 锁定全局操作
    crate::lock_global_operations();

    let manager = get_odm_manager().await;
    manager.count_with_groups_with_config(collection, condition_groups, alias).await
}

/// 获取数据库服务器版本信息
pub async fn get_server_version(alias: Option<&str>) -> QuickDbResult<String> {
    // 锁定全局操作
    crate::lock_global_operations();

    let manager = get_odm_manager().await;
    manager.get_server_version(alias).await
}

/// 创建存储过程
pub async fn create_stored_procedure(
    config: crate::stored_procedure::StoredProcedureConfig,
) -> QuickDbResult<crate::stored_procedure::StoredProcedureCreateResult> {
    let manager = get_odm_manager().await;
    manager.create_stored_procedure(config).await
}

/// 执行存储过程查询
pub async fn execute_stored_procedure(
    procedure_name: &str,
    database_alias: Option<&str>,
    params: Option<std::collections::HashMap<String, crate::types::DataValue>>,
) -> QuickDbResult<crate::stored_procedure::StoredProcedureQueryResult> {
    let manager = get_odm_manager().await;
    manager
        .execute_stored_procedure(procedure_name, database_alias, params)
        .await
}