Browse Source

Fix exeSQL component output (#2141)

### What problem does this PR solve?

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
tags/v0.11.0
H 1 year ago
parent
commit
f843dd05e5
No account linked to committer's email address
1 changed files with 7 additions and 4 deletions
  1. 7
    4
      agent/component/exesql.py

+ 7
- 4
agent/component/exesql.py View File

@@ -54,7 +54,7 @@ class ExeSQL(ComponentBase, ABC):
setattr(self, "_loop", 0)
if self._loop >= self._param.loop:
self._loop = 0
raise Exception("Maximum loop time exceeds. Can't query the correct data via sql statement.")
raise Exception("Maximum loop time exceeds. Can't query the correct data via SQL statement.")
self._loop += 1

ans = self.get_input()
@@ -63,7 +63,7 @@ class ExeSQL(ComponentBase, ABC):
ans = re.sub(r';.*?SELECT ', '; SELECT ', ans, flags=re.IGNORECASE)
ans = re.sub(r';[^;]*$', r';', ans)
if not ans:
return ExeSQL.be_output("SQL statement not found!")
raise Exception("SQL statement not found!")

if self._param.db_type in ["mysql", "mariadb"]:
db = MySQLDatabase(self._param.database, user=self._param.username, host=self._param.host,
@@ -75,13 +75,16 @@ class ExeSQL(ComponentBase, ABC):
try:
db.connect()
except Exception as e:
return ExeSQL.be_output("**Error**: \nDatabase Connection Failed! \n" + str(e))
raise Exception("Database Connection Failed! \n" + str(e))
sql_res = []
for single_sql in re.split(r';', ans.replace(r"\n", " ")):
if not single_sql:
continue
try:
query = db.execute_sql(single_sql)
if query.rowcount == 0:
sql_res.append({"content": "\nTotal: " + str(query.rowcount) + "\n No record in the database!"})
continue
single_res = pd.DataFrame([i for i in query.fetchmany(size=self._param.top_n)])
single_res.columns = [i[0] for i in query.description]
sql_res.append({"content": "\nTotal: " + str(query.rowcount) + "\n" + single_res.to_markdown()})
@@ -91,6 +94,6 @@ class ExeSQL(ComponentBase, ABC):
db.close()

if not sql_res:
return ExeSQL.be_output("No record in the database!")
return ExeSQL.be_output("")

return pd.DataFrame(sql_res)

Loading…
Cancel
Save