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>
97 lines
3.3 KiB
Python
97 lines
3.3 KiB
Python
"""
|
|
Data Access Object for production contract data.
|
|
|
|
This module provides query operations for accessing production contract data
|
|
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(BaseDAO):
|
|
"""Data Access Object for production contract data queries"""
|
|
|
|
def query_by_总排号(self, 总排号_list: List[str]) -> List[Dict[str, Any]]:
|
|
"""
|
|
Query production contract data by 总排号 list.
|
|
|
|
Args:
|
|
总排号_list: List of 总排号 values to query
|
|
|
|
Returns:
|
|
List of dictionaries containing 总排号, 生产订单号, 序号, 订单号, 客户名称, 产品型号
|
|
"""
|
|
if not 总排号_list:
|
|
return []
|
|
|
|
# SQL Server parameter limit requires batching
|
|
batch_size = 2000
|
|
all_results = []
|
|
|
|
for i in range(0, len(总排号_list), batch_size):
|
|
batch = 总排号_list[i:i + batch_size]
|
|
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))
|
|
all_results.extend(results)
|
|
|
|
return all_results
|
|
|
|
def get_source_numbers_by_总排号(self, 总排号_list: List[str]) -> List[str]:
|
|
"""
|
|
Extract unique 生产订单号 values by 总排号 list.
|
|
|
|
Args:
|
|
总排号_list: List of 总排号 values to query
|
|
|
|
Returns:
|
|
List of unique 生产订单号 values (SourceNumber)
|
|
"""
|
|
results = self.query_by_总排号(总排号_list)
|
|
# Extract unique 生产订单号 values, excluding None/null values
|
|
source_numbers = list(set(
|
|
[r['生产订单号'] for r in results if r.get('生产订单号')]
|
|
))
|
|
return source_numbers
|
|
|
|
def get_生产订单号_map(self, 总排号_list: List[str]) -> Dict[str, str]:
|
|
"""
|
|
Get mapping between 总排号 and 生产订单号.
|
|
|
|
Args:
|
|
总排号_list: List of 总排号 values to query
|
|
|
|
Returns:
|
|
Dictionary mapping 总排号 -> 生产订单号
|
|
"""
|
|
results = self.query_by_总排号(总排号_list)
|
|
return {
|
|
r['总排号']: r['生产订单号']
|
|
for r in results
|
|
if r.get('总排号') and r.get('生产订单号')
|
|
}
|