Apply Black formatter to the entire codebase for consistent code style. Co-Authored-By: Claude (glm-5) <noreply@anthropic.com>
92 lines
2.7 KiB
Python
92 lines
2.7 KiB
Python
"""
|
|
数据库连接组件
|
|
|
|
提供数据库连接和查询接口,支持 SQL Server 和 MySQL
|
|
"""
|
|
|
|
from typing import List, Dict, Any, Optional
|
|
import sys
|
|
import os
|
|
|
|
# 添加项目根目录到 sys.path
|
|
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
if project_root not in sys.path:
|
|
sys.path.insert(0, project_root)
|
|
|
|
from config.schema import DatabaseType
|
|
from db.connection_factory import ConnectionFactory
|
|
from db.base_connection import BaseDatabaseConnection
|
|
|
|
|
|
def get_connection(config=None) -> BaseDatabaseConnection:
|
|
"""
|
|
获取数据库连接实例
|
|
|
|
Args:
|
|
config: 可选的数据库配置对象,默认从用户配置文件加载
|
|
|
|
Returns:
|
|
BaseDatabaseConnection: 数据库连接对象
|
|
"""
|
|
if config is not None:
|
|
# 使用提供的配置
|
|
database_config = config
|
|
else:
|
|
# 从用户配置文件加载
|
|
from config.loader import ConfigLoader
|
|
|
|
app_config = ConfigLoader.load()
|
|
database_config = app_config.database
|
|
|
|
return ConnectionFactory.create_from_config(database_config)
|
|
|
|
|
|
def query_production_orders(总排号_list: List[str]) -> List[Dict[str, Any]]:
|
|
"""
|
|
根据总排号列表查询生产订单号
|
|
|
|
支持两种数据库格式:
|
|
- SQL Server: [productionContractData].[26年压力表合同数据]
|
|
- MySQL: productionContractData_26年压力表合同数据
|
|
|
|
Args:
|
|
总排号_list: 总排号列表
|
|
|
|
Returns:
|
|
List[Dict[str, Any]]: 查询结果
|
|
"""
|
|
from db.table_name_converter import TableNameConverter
|
|
from config.loader import ConfigLoader
|
|
|
|
# 获取当前数据库类型
|
|
app_config = ConfigLoader.load()
|
|
db_type = app_config.database.db_type
|
|
|
|
with get_connection() as db:
|
|
# 获取正确的占位符
|
|
placeholder = db.get_placeholder()
|
|
|
|
# 构建占位符字符串
|
|
placeholders = ",".join([placeholder for _ in 总排号_list])
|
|
|
|
# 根据数据库类型选择表名格式
|
|
if db_type == DatabaseType.MYSQL:
|
|
table_name = "productionContractData_26年压力表合同数据"
|
|
sql = f"""
|
|
SELECT 总排号, 生产订单号, 序号, 订单号, 客户名称, 产品型号
|
|
FROM {table_name}
|
|
WHERE 总排号 IN ({placeholders})
|
|
ORDER BY 序号
|
|
"""
|
|
else:
|
|
table_name = "[productionContractData].[26年压力表合同数据]"
|
|
sql = f"""
|
|
SELECT [总排号], [生产订单号], [序号], [订单号], [客户名称], [产品型号]
|
|
FROM {table_name}
|
|
WHERE [总排号] IN ({placeholders})
|
|
ORDER BY [序号]
|
|
"""
|
|
|
|
results = db.execute_query(sql, tuple(总排号_list))
|
|
return results
|