feat: add MySQL database support alongside SQL Server

This commit implements multi-database support, allowing the system to switch
between SQL Server and MySQL databases seamlessly.

## New Features
- Database type selection (SQL Server or MySQL) via configuration
- Automatic table name conversion between formats ([dbo].[table] → dbo_table)
- Automatic parameter placeholder handling (? for SQL Server, %s for MySQL)
- GUI settings tab now includes database type dropdown and MySQL configuration

## Database Abstraction Layer
- db/base_connection.py: Abstract base class for database connections
- db/sqlserver_connection.py: SQL Server implementation
- db/mysql_connection.py: MySQL implementation using mysql-connector-python
- db/connection_factory.py: Factory pattern for creating connections
- db/table_name_converter.py: Table name format conversion utility

## DAO Base Class
- db/base_dao.py: Base DAO with helper methods for SQL conversion and placeholders

## Updated Components
- config/schema.py: Extended with DatabaseType enum and MySQL/SQLServer config classes
- config/defaults.py: Added MySQL default configuration
- config/loader.py: Updated to handle new database structure
- db/connection.py: Refactored to use factory pattern and load user config
- All DAO files: Updated to inherit from BaseDAO with automatic conversion

## Dependencies
- Added mysql-connector-python>=8.0.0 to requirements.txt

## Configuration
To use MySQL, set db_type to "mysql" in config/user_settings.json:
{
  "database": {
    "db_type": "mysql",
    "mysql": {
      "host": "192.168.31.83",
      "port": 3306,
      "database": "BLD_DB",
      "username": "remote_user",
      "password": "3.1415926Beeke"
    }
  }
}

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Misaka
2026-02-09 21:50:54 +08:00
parent 04b99292ad
commit caac411e17
17 changed files with 1661 additions and 513 deletions

View File

@@ -8,7 +8,17 @@
import json
import os
from typing import Any, Dict
from config.schema import AppConfig
from config.schema import (
AppConfig,
ERPConfig,
DatabaseConfig,
PathConfig,
ExtractionConfig,
ValidationConfig,
DatabaseType,
SQLServerConfig,
MySQLConfig,
)
from config.defaults import DEFAULT_APP_CONFIG, DEFAULT_SETTINGS_DICT
@@ -109,6 +119,28 @@ class ConfigLoader:
extraction_dict = settings.get("extraction", {})
validation_dict = settings.get("validation", {})
# 解析数据库类型
db_type_str = database_dict.get("db_type", "sqlserver")
try:
db_type = DatabaseType(db_type_str)
except ValueError:
db_type = DatabaseType.SQLSERVER
# 解析 SQL Server 配置
sqlserver_dict = database_dict.get("sqlserver", {})
sqlserver_config = SQLServerConfig(
driver=sqlserver_dict.get("driver", "ODBC Driver 18 for SQL Server"),
trust_server_certificate=sqlserver_dict.get("trust_server_certificate", "yes"),
)
# 解析 MySQL 配置
mysql_dict = database_dict.get("mysql", {})
mysql_config = MySQLConfig(
host=mysql_dict.get("host", database_dict.get("server", "")),
port=mysql_dict.get("port", 3306),
charset=mysql_dict.get("charset", "utf8mb4"),
)
return AppConfig(
erp=ERPConfig(
url=erp_dict.get("url", ""),
@@ -119,14 +151,13 @@ class ConfigLoader:
auto_close_browser=erp_dict.get("auto_close_browser", True),
),
database=DatabaseConfig(
db_type=db_type,
server=database_dict.get("server", ""),
database=database_dict.get("database", ""),
username=database_dict.get("username", ""),
password=database_dict.get("password", ""),
driver=database_dict.get("driver", "ODBC Driver 18 for SQL Server"),
trust_server_certificate=database_dict.get(
"trust_server_certificate", "yes"
),
sqlserver=sqlserver_config,
mysql=mysql_config,
),
paths=PathConfig(
data_dir=paths_dict.get("data_dir", ""),
@@ -156,5 +187,3 @@ class ConfigLoader:
)
# 为了兼容旧代码,导入必要的类型
from config.schema import ERPConfig, DatabaseConfig, PathConfig, ExtractionConfig, ValidationConfig