""" 数据库连接工厂 根据配置返回 MySQL 或 SQL Server 连接实例。 """ from typing import Optional from db.connection import DatabaseConnection from db.mysql_connection import MySQLConnection from config.defaults import DEFAULT_APP_CONFIG def get_connection(db_type: Optional[str] = None): """ 根据配置返回 MySQL 或 SQL Server 连接 Args: db_type: 数据库类型,"mysql" 或 "sqlserver"。 如果为 None,则从 DEFAULT_APP_CONFIG 读取配置 Returns: DatabaseConnection 或 MySQLConnection 实例 Example: >>> # 使用默认配置 >>> conn = get_connection() >>> # 强制使用 MySQL >>> conn = get_connection("mysql") >>> # 强制使用 SQL Server >>> conn = get_connection("sqlserver") """ if db_type is None: db_type = DEFAULT_APP_CONFIG.database.db_type if db_type == "mysql": return MySQLConnection() return DatabaseConnection()