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

@@ -6,31 +6,14 @@ from the [productionContractData].[26年压力表合同数据] table.
"""
from typing import List, Dict, Any
from db.base_dao import BaseDAO
from db.connection import get_connection
from config.schema import DatabaseType
class ProductionContractDataDAO:
class ProductionContractDataDAO(BaseDAO):
"""Data Access Object for production contract data queries"""
def __init__(self):
self.db = None
def __enter__(self):
"""Enter context manager and establish database connection"""
self.db = get_connection()
self.db.connect()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""Exit context manager and close database connection"""
if self.db:
self.db.disconnect()
def close(self):
"""Close database connection"""
if self.db:
self.db.disconnect()
def query_by_总排号(self, 总排号_list: List[str]) -> List[Dict[str, Any]]:
"""
Query production contract data by 总排号 list.
@@ -50,13 +33,27 @@ class ProductionContractDataDAO:
for i in range(0, len(总排号_list), batch_size):
batch = 总排号_list[i:i + batch_size]
placeholders = ','.join(['?' for _ in batch])
sql = f"""
SELECT [总排号], [生产订单号], [序号], [订单号], [客户名称], [产品型号]
FROM [productionContractData].[26年压力表合同数据]
WHERE [总排号] IN ({placeholders})
ORDER BY [序号]
"""
placeholder = self._get_placeholder()
placeholders = ','.join([placeholder for _ in batch])
# 根据数据库类型选择表名
table_name = self._convert_sql('[productionContractData].[26年压力表合同数据]')
# 根据数据库类型选择列名格式
if self._db_type == DatabaseType.MYSQL:
sql = f"""
SELECT 总排号, 生产订单号, 序号, 订单号, 客户名称, 产品型号
FROM {table_name}
WHERE 总排号 IN ({placeholders})
ORDER BY 序号
"""
else:
sql = f"""
SELECT [总排号], [生产订单号], [序号], [订单号], [客户名称], [产品型号]
FROM {table_name}
WHERE [总排号] IN ({placeholders})
ORDER BY [序号]
"""
with get_connection() as db:
results = db.execute_query(sql, tuple(batch))