feat: add MySQL database support with SQL translation

- 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>
This commit is contained in:
Misaka Server
2026-02-09 20:12:32 +08:00
parent 04b99292ad
commit f02858d5dc
7 changed files with 459 additions and 20 deletions

40
db/connection_factory.py Normal file
View File

@@ -0,0 +1,40 @@
"""
数据库连接工厂
根据配置返回 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()