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

View File

@@ -44,17 +44,34 @@ class DatabaseConfig:
driver: str = "ODBC Driver 18 for SQL Server"
trust_server_certificate: str = "yes"
# MySQL 支持字段
db_type: str = "sqlserver" # "sqlserver" 或 "mysql"
host: Optional[str] = None # MySQL 主机地址
port: Optional[int] = None # MySQL 端口
def validate(self) -> list[str]:
"""验证配置,返回错误列表"""
errors = []
if not self.server:
errors.append("数据库服务器地址不能为空")
if self.db_type == "mysql":
# MySQL 配置验证
if not self.host:
errors.append("MySQL 主机地址不能为空")
if not self.port:
errors.append("MySQL 端口不能为空")
else:
# SQL Server 配置验证
if not self.server:
errors.append("数据库服务器地址不能为空")
# 通用配置验证
if not self.database:
errors.append("数据库名称不能为空")
if not self.username:
errors.append("数据库用户名不能为空")
if not self.password:
errors.append("数据库密码不能为空")
return errors
@@ -177,6 +194,9 @@ class AppConfig:
"password": self.database.password,
"driver": self.database.driver,
"trust_server_certificate": self.database.trust_server_certificate,
"db_type": self.database.db_type,
"host": self.database.host,
"port": self.database.port,
},
"paths": {
"data_dir": self.paths.data_dir,