ソースを参照

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年前
コミット
f843dd05e5
コミッターのメールアドレスに関連付けられたアカウントが存在しません
1個のファイルの変更7行の追加4行の削除
  1. 7
    4
      agent/component/exesql.py

+ 7
- 4
agent/component/exesql.py ファイルの表示

setattr(self, "_loop", 0) setattr(self, "_loop", 0)
if self._loop >= self._param.loop: if self._loop >= self._param.loop:
self._loop = 0 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 self._loop += 1


ans = self.get_input() ans = self.get_input()
ans = re.sub(r';.*?SELECT ', '; SELECT ', ans, flags=re.IGNORECASE) ans = re.sub(r';.*?SELECT ', '; SELECT ', ans, flags=re.IGNORECASE)
ans = re.sub(r';[^;]*$', r';', ans) ans = re.sub(r';[^;]*$', r';', ans)
if not 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"]: if self._param.db_type in ["mysql", "mariadb"]:
db = MySQLDatabase(self._param.database, user=self._param.username, host=self._param.host, db = MySQLDatabase(self._param.database, user=self._param.username, host=self._param.host,
try: try:
db.connect() db.connect()
except Exception as e: 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 = [] sql_res = []
for single_sql in re.split(r';', ans.replace(r"\n", " ")): for single_sql in re.split(r';', ans.replace(r"\n", " ")):
if not single_sql: if not single_sql:
continue continue
try: try:
query = db.execute_sql(single_sql) 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 = pd.DataFrame([i for i in query.fetchmany(size=self._param.top_n)])
single_res.columns = [i[0] for i in query.description] single_res.columns = [i[0] for i in query.description]
sql_res.append({"content": "\nTotal: " + str(query.rowcount) + "\n" + single_res.to_markdown()}) sql_res.append({"content": "\nTotal: " + str(query.rowcount) + "\n" + single_res.to_markdown()})
db.close() db.close()


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


return pd.DataFrame(sql_res) return pd.DataFrame(sql_res)

読み込み中…
キャンセル
保存