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

@@ -26,12 +26,16 @@ DEFAULT_APP_CONFIG = AppConfig(
auto_close_browser=True,
),
database=DatabaseConfig(
server="192.168.110.114",
db_type="mysql",
host="localhost",
port=3306,
database="CompanyDB",
username="peng",
password="Cqbld123456.",
driver="ODBC Driver 18 for SQL Server",
trust_server_certificate="yes",
username="remote_user",
password="3.1415926Beeke",
# SQL Server fields (not used in MySQL mode)
server="",
driver="",
trust_server_certificate="",
),
paths=PathConfig(
data_dir="D:/python/playwrite/data/",
@@ -59,3 +63,49 @@ DEFAULT_APP_CONFIG = AppConfig(
# 兼容旧版本的字典格式
DEFAULT_SETTINGS_DICT = DEFAULT_APP_CONFIG.to_dict()
# MySQL 配置示例(使用时取消注释并注释掉上面的 DEFAULT_APP_CONFIG
# MYSQL_APP_CONFIG = AppConfig(
# erp=ERPConfig(
# url="https://68.11.34.30:8082/",
# username="BLDpengqiangqiang",
# password="Cqbld123456.",
# headless=True,
# ignore_https_errors=True,
# auto_close_browser=True,
# ),
# database=DatabaseConfig(
# db_type="mysql",
# host="localhost",
# port=3306,
# database="CompanyDB",
# username="mysql_user",
# password="mysql_password",
# # SQL Server 字段MySQL 模式下不使用)
# server="",
# driver="",
# trust_server_certificate="",
# ),
# paths=PathConfig(
# data_dir="D:/python/playwrite/data/",
# production_id_file="ProductionID.txt",
# default_output="离散备料计划维护_合并.xlsx",
# validation_output="物料状态校验结果.xlsx",
# ),
# extraction=ExtractionConfig(
# batch_size=100,
# verbose=True,
# auto_convert=True,
# merge_batches=True,
# enable_db_persistence=False,
# ),
# validation=ValidationConfig(
# data_source="database_full",
# use_database=True,
# batch_size=2000,
# enable_crud_operations=False,
# default_manager="",
# match_mode="substring",
# ),
# )

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,