Fix exeSQL component output (#2141)

### What problem does this PR solve?

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
H 2024-08-28 16:39:10 +08:00 committed by GitHub
parent 3abc9be1c2
commit f843dd05e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

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)