先跳过 定额信息的提取
This commit is contained in:
@@ -270,6 +270,51 @@ class DifyTool:
|
||||
except (Exception, psycopg2.Error) as error:
|
||||
raise Exception(f"Error while getting conversation_messages: {error}")
|
||||
return None
|
||||
|
||||
def execute_custom_sql(self, sql, params=None, fetch_type='all'):
|
||||
"""
|
||||
执行自定义的SQL查询或命令。
|
||||
|
||||
Args:
|
||||
sql: 要执行的SQL语句
|
||||
params: SQL参数(可选),用于参数化查询
|
||||
fetch_type: 结果获取类型,可选值:'all'(返回所有行), 'one'(返回单行), 'none'(不返回结果)
|
||||
|
||||
Returns:
|
||||
根据fetch_type返回查询结果:
|
||||
- fetch_type='all': 返回包含所有行的列表,每行是一个字典
|
||||
- fetch_type='one': 返回单行字典,如果没有结果则返回None
|
||||
- fetch_type='none': 返回受影响的行数
|
||||
|
||||
Raises:
|
||||
Exception: 如果执行SQL时发生错误
|
||||
"""
|
||||
with self.pg_sql_lock:
|
||||
try:
|
||||
with self.connection.cursor() as cursor:
|
||||
# 执行SQL语句
|
||||
cursor.execute(sql, params or ())
|
||||
|
||||
# 根据fetch_type处理结果
|
||||
if fetch_type == 'all':
|
||||
result = cursor.fetchall()
|
||||
if result:
|
||||
colnames = [desc[0] for desc in cursor.description]
|
||||
return [dict(zip(colnames, row)) for row in result]
|
||||
return []
|
||||
elif fetch_type == 'one':
|
||||
result = cursor.fetchone()
|
||||
if result:
|
||||
colnames = [desc[0] for desc in cursor.description]
|
||||
return dict(zip(colnames, result))
|
||||
return None
|
||||
elif fetch_type == 'none':
|
||||
# 对于UPDATE, INSERT, DELETE等操作,返回受影响的行数
|
||||
return cursor.rowcount
|
||||
else:
|
||||
raise ValueError(f"不支持的fetch_type: {fetch_type}")
|
||||
except (Exception, psycopg2.Error) as error:
|
||||
raise Exception(f"Error executing custom SQL: {error}")
|
||||
|
||||
"""
|
||||
提供用于获取 Dify 应用调试信息的工具类。
|
||||
|
||||
Reference in New Issue
Block a user