- Use conn.get_placeholder() instead of hardcoded '?' - Adapt table and column names for both MySQL and SQL Server - Load database type from config to handle dialect differences - Fix ProgrammingError when running with MySQL database Co-Authored-By: Gemini 2.0 Flash <gemini-cli@google.com>
79 lines
2.3 KiB
Python
79 lines
2.3 KiB
Python
"""
|
|
生产订单号查询组件
|
|
从 ProductionID.txt 读取总排号,查询数据库获取生产订单号
|
|
"""
|
|
|
|
from db.connection import get_connection
|
|
from config.schema import DatabaseType
|
|
from config.loader import ConfigLoader
|
|
|
|
|
|
def read_production_ids(file_path):
|
|
"""
|
|
读取 ProductionID.txt 文件,获取总排号列表
|
|
|
|
Args:
|
|
file_path: ProductionID.txt 文件路径
|
|
|
|
Returns:
|
|
总排号列表
|
|
"""
|
|
with open(file_path, "r", encoding="utf-8") as f:
|
|
# 去除空白行和空格
|
|
production_ids = [line.strip() for line in f if line.strip()]
|
|
return production_ids
|
|
|
|
|
|
def query_production_order_numbers(production_ids):
|
|
"""
|
|
根据总排号列表,从数据库查询生产订单号
|
|
|
|
Args:
|
|
production_ids: 总排号列表
|
|
|
|
Returns:
|
|
生产订单号列表
|
|
"""
|
|
if not production_ids:
|
|
return []
|
|
|
|
# 获取当前数据库类型
|
|
app_config = ConfigLoader.load()
|
|
db_type = app_config.database.db_type
|
|
|
|
# SQL Server 限制每个查询最多 2100 个参数
|
|
BATCH_SIZE = 2000
|
|
all_results = []
|
|
|
|
# 分批查询
|
|
for i in range(0, len(production_ids), BATCH_SIZE):
|
|
batch = production_ids[i : i + BATCH_SIZE]
|
|
|
|
with get_connection() as conn:
|
|
# 获取正确的占位符
|
|
placeholder = conn.get_placeholder()
|
|
placeholders = ",".join([placeholder for _ in batch])
|
|
|
|
# 根据数据库类型选择表名和列名格式
|
|
if db_type == DatabaseType.MYSQL:
|
|
table_name = "productionContractData_26年压力表合同数据"
|
|
query = f"""
|
|
SELECT 生产订单号
|
|
FROM {table_name}
|
|
WHERE 总排号 IN ({placeholders})
|
|
"""
|
|
else:
|
|
table_name = "[productionContractData].[26年压力表合同数据]"
|
|
query = f"""
|
|
SELECT [生产订单号]
|
|
FROM {table_name}
|
|
WHERE [总排号] IN ({placeholders})
|
|
"""
|
|
|
|
results = conn.execute_query(query, tuple(batch))
|
|
# 提取生产订单号并去除空值
|
|
batch_numbers = [row["生产订单号"] for row in results if row.get("生产订单号")]
|
|
all_results.extend(batch_numbers)
|
|
|
|
return all_results
|