def sql(self, ctx, *, query: str):
"""Run some SQL."""
# the imports are here because I imagine some people would want to use
# this cog as a base for their other cog, and since this one is kinda
# odd and unnecessary for most people, I will make it easy to remove
# for those people.
from .utils.formats import pluralize
import time
query = self.cleanup_code(query)
is_multistatement = query.count(';') > 1
async with ctx.db.get_session() as session:
try:
start = time.perf_counter()
results = [dict(r) async for r in await session.cursor(query)]
dt = (time.perf_counter() - start) * 1000.0
except Exception:
return await ctx.send(f'```py\n{traceback.format_exc()}\n```')
if is_multistatement or not results:
return await ctx.send(f'`{dt:.2f}ms: {results}`')
print(results)
num_rows = len(results)
headers = list(results[0])
rendered = _tabulate((list(r.values()) for r in results), headers)
fmt = f'```\n{rendered}\n```\n*Returned {pluralize(row=num_rows)} in {dt:.2f}ms*'
if len(fmt) > 2000:
fp = io.BytesIO(fmt.encode('utf-8'))
await ctx.send('Too many results...', file=discord.File(fp, 'results.txt'))
else:
await ctx.send(fmt)
评论列表
文章目录