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:
parent
bbe8b281d1
commit
460d5a31fc
1 changed files with 19 additions and 12 deletions
|
@ -202,23 +202,30 @@ class Moderation(AuroraGuildModel):
|
|||
return cls.from_dict(bot=bot, data=case)
|
||||
|
||||
@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
|
||||
if not parameters:
|
||||
parameters = ()
|
||||
with connect() as database:
|
||||
if not cursor:
|
||||
no_cursor = True
|
||||
database = connect()
|
||||
cursor = database.cursor()
|
||||
cursor.execute(query, parameters)
|
||||
results = cursor.fetchall()
|
||||
cursor.close()
|
||||
else:
|
||||
no_cursor = False
|
||||
|
||||
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)
|
||||
cursor.execute(query, parameters)
|
||||
results = cursor.fetchall()
|
||||
if no_cursor:
|
||||
cursor.close()
|
||||
database.close()
|
||||
|
||||
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 ()
|
||||
|
||||
@classmethod
|
||||
|
|
Loading…
Reference in a new issue