From 3a25ce6f2da8e1d7941703f95b002a3070ede661 Mon Sep 17 00:00:00 2001 From: Ske Date: Fri, 13 Jul 2018 22:49:27 +0200 Subject: [PATCH] Clean up DB queries a bit --- bot/pluralkit/db.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/bot/pluralkit/db.py b/bot/pluralkit/db.py index a21d820b..e5fe4064 100644 --- a/bot/pluralkit/db.py +++ b/bot/pluralkit/db.py @@ -156,12 +156,29 @@ async def add_message(conn, message_id: str, channel_id: str, member_id: int, se @db_wrap async def get_members_by_account(conn, account_id: str): # Returns a "chimera" object - return await conn.fetch("select members.id, members.hid, members.prefix, members.suffix, members.color, members.name, members.avatar_url, systems.tag, systems.name as system_name, systems.hid as system_hid from systems, members, accounts where accounts.uid = $1 and systems.id = accounts.system and members.system = systems.id", int(account_id)) + return await conn.fetch("""select + members.id, members.hid, members.prefix, members.suffix, members.color, members.name, members.avatar_url, + systems.tag, systems.name as system_name, systems.hid as system_hid + from + systems, members, accounts + where + accounts.uid = $1 + and systems.id = accounts.system + and members.system = systems.id""", int(account_id)) @db_wrap async def get_message_by_sender_and_id(conn, message_id: str, sender_id: str): - return await conn.fetchrow("select messages.*, members.name, members.hid, members.avatar_url, systems.name as system_name, systems.hid as system_hid from messages, members, systems where messages.member = members.id and members.system = systems.id and mid = $1 and sender = $2", int(message_id), int(sender_id)) + return await conn.fetchrow("""select + messages.*, + members.name, members.hid, members.avatar_url, + systems.name as system_name, systems.hid as system_hid + from + messages, members, systems + where + messages.member = members.id + and members.system = systems.id + and mid = $1 and sender = $2""", int(message_id), int(sender_id)) @db_wrap @@ -171,11 +188,20 @@ async def delete_message(conn, message_id: str): @db_wrap async def current_fronter(conn, system_id: int): - return await conn.fetchrow("select *, members.name from switches left outer join members on (members.id = switches.member) where switches.system = $1 order by timestamp desc", system_id) + return await conn.fetchrow("""select *, members.name + from switches + left outer join members on (members.id = switches.member) -- Left outer join instead of normal join - makes name = null instead of just ignoring the row + where switches.system = $1 + order by timestamp desc""", system_id) @db_wrap async def past_fronters(conn, system_id: int, amount: int): - return await conn.fetch("select *, members.name from switches left outer join members on (members.id = switches.member) where switches.system = $1 order by timestamp desc limit $2", system_id, amount) + return await conn.fetch("""select *, members.name + from switches + left outer join members on (members.id = switches.member) -- (see above) + where switches.system = $1 + order by timestamp + desc limit $2""", system_id, amount) @db_wrap async def add_switch(conn, system_id: int, member_id: int):