feat(aurora): allow the Moderation.execute() classmethod to accept a cursor, to prevent opening a new database connection on every call

This commit is contained in:
Seaswimmer 2024-06-03 23:48:21 -04:00
parent bbe8b281d1
commit 460d5a31fc
Signed by untrusted user: cswimr
GPG key ID: 5D671B5D03D65A7F

View file

@ -202,23 +202,30 @@ class Moderation(AuroraGuildModel):
return cls.from_dict(bot=bot, data=case) return cls.from_dict(bot=bot, data=case)
@classmethod @classmethod
def execute(cls, bot: Red, guild_id: int, query: str, parameters: tuple | None = None) -> Tuple["Moderation"]: def execute(cls, bot: Red, guild_id: int, query: str, parameters: tuple | None = None, cursor: Cursor | None = None) -> Tuple["Moderation"]:
from ..utilities.database import connect from ..utilities.database import connect
if not parameters: if not parameters:
parameters = () parameters = ()
with connect() as database: if not cursor:
no_cursor = True
database = connect()
cursor = database.cursor() cursor = database.cursor()
cursor.execute(query, parameters) else:
results = cursor.fetchall() no_cursor = False
cursor.close()
if results: cursor.execute(query, parameters)
cases = [] results = cursor.fetchall()
for result in results: if no_cursor:
case = cls.from_result(bot=bot, result=result, guild_id=guild_id) cursor.close()
if case.moderation_id != 0: database.close()
cases.append(case)
return tuple(cases) if results:
cases = []
for result in results:
case = cls.from_result(bot=bot, result=result, guild_id=guild_id)
if case.moderation_id != 0:
cases.append(case)
return tuple(cases)
return () return ()
@classmethod @classmethod