fix: support MySQL in production order query by using dynamic placeholders
- 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>
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
"""
|
||||
|
||||
from db.connection import get_connection
|
||||
from config.schema import DatabaseType
|
||||
from config.loader import ConfigLoader
|
||||
|
||||
|
||||
def read_production_ids(file_path):
|
||||
@@ -35,6 +37,10 @@ def query_production_order_numbers(production_ids):
|
||||
if not production_ids:
|
||||
return []
|
||||
|
||||
# 获取当前数据库类型
|
||||
app_config = ConfigLoader.load()
|
||||
db_type = app_config.database.db_type
|
||||
|
||||
# SQL Server 限制每个查询最多 2100 个参数
|
||||
BATCH_SIZE = 2000
|
||||
all_results = []
|
||||
@@ -42,18 +48,31 @@ def query_production_order_numbers(production_ids):
|
||||
# 分批查询
|
||||
for i in range(0, len(production_ids), BATCH_SIZE):
|
||||
batch = production_ids[i : i + BATCH_SIZE]
|
||||
placeholders = ",".join(["?" for _ in batch])
|
||||
|
||||
query = f"""
|
||||
SELECT [生产订单号]
|
||||
FROM [productionContractData].[26年压力表合同数据]
|
||||
WHERE [总排号] IN ({placeholders})
|
||||
"""
|
||||
|
||||
|
||||
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["生产订单号"]]
|
||||
batch_numbers = [row["生产订单号"] for row in results if row.get("生产订单号")]
|
||||
all_results.extend(batch_numbers)
|
||||
|
||||
return all_results
|
||||
|
||||
Reference in New Issue
Block a user