- Add MySQLConnection class with automatic SQL Server to MySQL translation - Add connection factory to support both SQL Server and MySQL - Update config schema to support MySQL configuration (host, port, db_type) - Update default config to use MySQL (localhost:3306) - Translate table names: [schema].[table] -> schema_table - Translate placeholders: ? -> %s - Translate MERGE statements to INSERT ... ON DUPLICATE KEY UPDATE Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
41 lines
1008 B
Python
41 lines
1008 B
Python
"""
|
||
数据库连接工厂
|
||
|
||
根据配置返回 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()
|