ormer 0.1.11

An ORM framework with a usage style similar to Linq, supporting Sqlite, PostgresQL, MySQL, MSSQL
Documentation
/// 数据库抽象层模块
/// 根据运行时指定的数据库类型选择对应的数据库后端
#[cfg(any(
    feature = "sqlite",
    feature = "postgresql",
    feature = "mysql",
    feature = "mssql"
))]
use crate::model::DbBackendTypeMapper;

#[cfg(feature = "sqlite")]
pub mod sqlite_backend;

#[cfg(feature = "postgresql")]
pub mod postgresql_backend;

#[cfg(feature = "mysql")]
pub mod mysql_backend;

#[cfg(feature = "mssql")]
pub mod mssql_backend;

/// 公共模块 - 包含共享辅助函数、宏定义、连接池和统一接口
pub mod common;

/// 数据库类型枚举
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DbType {
    /// Sqlite 数据库
    #[cfg(feature = "sqlite")]
    Sqlite,
    /// PostgreSQL 数据库
    #[cfg(feature = "postgresql")]
    PostgreSQL,
    /// MySQL 数据库
    #[cfg(feature = "mysql")]
    MySQL,
    /// MSSQL 数据库
    #[cfg(feature = "mssql")]
    MSSQL,
    /// 空变体,当没有启用任何特性时使用(仅用于编译通过)
    #[cfg(not(any(
        feature = "sqlite",
        feature = "postgresql",
        feature = "mysql",
        feature = "mssql"
    )))]
    None,
}

impl DbType {
    /// 根据 Rust 类型和数据库类型获取 SQL 类型
    pub fn sql_type(
        &self,
        _rust_type: &str,
        _is_primary: bool,
        _is_auto_increment: bool,
        _is_nullable: bool,
        _enum_variants: Option<&[&str]>,
    ) -> String {
        match self {
            #[cfg(feature = "sqlite")]
            DbType::Sqlite => crate::abstract_layer::sqlite_backend::SqliteTypeMapper::sql_type(
                _rust_type,
                _is_primary,
                _is_auto_increment,
                _is_nullable,
                _enum_variants,
            ),
            #[cfg(feature = "postgresql")]
            DbType::PostgreSQL => {
                crate::abstract_layer::postgresql_backend::PostgreSQLTypeMapper::sql_type(
                    _rust_type,
                    _is_primary,
                    _is_auto_increment,
                    _is_nullable,
                    _enum_variants,
                )
            }
            #[cfg(feature = "mysql")]
            DbType::MySQL => crate::abstract_layer::mysql_backend::MySQLTypeMapper::sql_type(
                _rust_type,
                _is_primary,
                _is_auto_increment,
                _is_nullable,
                _enum_variants,
            ),
            #[cfg(feature = "mssql")]
            DbType::MSSQL => crate::abstract_layer::mssql_backend::MSSQLTypeMapper::sql_type(
                _rust_type,
                _is_primary,
                _is_auto_increment,
                _is_nullable,
                _enum_variants,
            ),
            #[cfg(not(any(
                feature = "sqlite",
                feature = "postgresql",
                feature = "mysql",
                feature = "mssql"
            )))]
            DbType::None => {
                // 当没有启用任何特性时,返回空字符串(仅用于编译通过)
                String::new()
            }
        }
    }
}

// 统一使用 common 模块提供接口,当启用任一数据库 feature 时可用
#[cfg(any(
    feature = "sqlite",
    feature = "postgresql",
    feature = "mysql",
    feature = "mssql"
))]
pub use common::{
    AggregateFuture, CollectFuture, CreateTableExecutor, Database, DeleteExecutor,
    DropTableExecutor, GroupedCollectFuture, GroupedSelectExecutor, InsertExecutor,
    InsertOrUpdateExecutor, LeftJoinCollectFuture, LeftJoinedSelectExecutor, MappedCollectFuture,
    MappedSelectExecutor, ModelCollectWithFuture, RelatedCollectFuture, RelatedSelectExecutor,
    SelectExecutor, SelectStream, SelectStreamIterator, Transaction, TransactionInsertExecutor,
    TransactionInsertOrUpdateExecutor, UpdateExecutor,
};

// 连接池类型 - 根据启用的 feature 导出
#[cfg(any(
    feature = "sqlite",
    feature = "postgresql",
    feature = "mysql",
    feature = "mssql"
))]
pub use common::{ConnectionPool, PooledConnection};