Skip to content

Commit

Permalink
Merge pull request #9 from DARSHTRON/main
Browse files Browse the repository at this point in the history
refactoring / rewrite the DB module
  • Loading branch information
its-darsh authored Jun 27, 2023
2 parents 258db1a + 42c4952 commit a1201a7
Showing 1 changed file with 56 additions and 28 deletions.
84 changes: 56 additions & 28 deletions cogs/voice/DataBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@

DATABASE_FILE = 'voice.db'

COMMIT = 'commit'
FETCHONE = 'fetchone'
FETCHALL = 'fetchall'

class DataBase():
"""PREPARE YOURSELF TO SEE THE MOST DUMBEST CLASS EVER!""" # FIXME...
"""Pretty dumb `class` to handle the db calls"""
def __init__(self):
pass

Expand All @@ -35,56 +39,80 @@ async def create_conn(self):
return await aiosqlite.connect(DATABASE_FILE)

@classmethod
async def get_guild(self, guild_id):
async def execute(self, query: str, values: tuple, type: str):
db = await self.create_conn()
async with db.execute("SELECT * FROM guilds WHERE guild_id = ?", (guild_id,)) as cursor:
return await cursor.fetchall()
cr = await db.execute(query, values)
return await db.commit() if type == COMMIT else await cr.fetchone() if type == FETCHONE else await cr.fetchall()

@classmethod
async def get_guild(self, guild_id):
return await self.execute(
query='SELECT * FROM guilds WHERE guild_id = ?',
values=(guild_id,),
type=FETCHALL,
)

@classmethod
async def create_channel(self, user_id, channel_id, guild_id):
db = await self.create_conn()
async with db.execute("INSERT INTO channels VALUES (?,?,?)", (user_id, channel_id, guild_id)):
return await db.commit()
return await self.execute(
query="INSERT INTO channels VALUES (?,?,?)",
values=(user_id, channel_id, guild_id),
type=COMMIT,
)

@classmethod
async def delete_channel(self, user_id, channel_id, guild_id):
db = await self.create_conn()
async with db.execute("DELETE FROM channels WHERE user_id = ? AND channel_id = ? AND guild_id = ?", (user_id, channel_id, guild_id)):
return await db.commit()
return await self.execute(
query="DELETE FROM channels WHERE user_id = ? AND channel_id = ? AND guild_id = ?",
values=(user_id, channel_id, guild_id),
type=COMMIT,
)

@classmethod
async def get_channel(self, user_id, channel_id, guild_id):
query, values = ("SELECT * FROM channels WHERE channel_id = ? AND guild_id = ?", (channel_id, guild_id,)) if not user_id else ("SELECT * FROM channels WHERE user_id = ? AND channel_id = ? AND guild_id = ?", (user_id, channel_id, guild_id))
db = await self.create_conn()
async with db.execute(query, values) as cursor:
return await cursor.fetchone()
return await self.execute(
query=query,
values=values,
type=FETCHONE,
)

@classmethod
async def get_rejected_user(self, user_id, channel_id, guild_id):
db = await self.create_conn()
async with db.execute("SELECT * FROM rejected_users WHERE user_id = ? AND channel_id = ? AND guild_id = ?", (user_id, channel_id, guild_id)) as cursor:
return await cursor.fetchone()
return await self.execute(
query="SELECT * FROM rejected_users WHERE user_id = ? AND channel_id = ? AND guild_id = ?",
values=(user_id, channel_id, guild_id),
type=FETCHONE,
)

@classmethod
async def add_user_to_rejected(self, user_id, channel_id, guild_id):
db = await self.create_conn()
async with db.execute("INSERT INTO rejected_users VALUES (?,?,?)", (user_id, channel_id, guild_id)):
return await db.commit()
return await self.execute(
query="INSERT INTO rejected_users VALUES (?,?,?)",
values=(user_id, channel_id, guild_id),
type=COMMIT,
)

@classmethod
async def remove_user_from_reject(self, user_id, channel_id, guild_id):
db = await self.create_conn()
async with db.execute("DELETE FROM rejected_users WHERE user_id = ? AND channel_id = ? AND guild_id = ?", (user_id, channel_id, guild_id)):
return await db.commit()
return await self.execute(
query="DELETE FROM rejected_users WHERE user_id = ? AND channel_id = ? AND guild_id = ?",
values=(user_id, channel_id, guild_id),
type=COMMIT,
)

@classmethod
async def add_guild(self, guild_id, vc_id, vc_category_id, channel_limit, channel_bitrate):
db = await self.create_conn()
async with db.execute("INSERT INTO guilds VALUES (?,?,?,?,?)", (guild_id, vc_id, vc_category_id, channel_limit, channel_bitrate)):
return await db.commit()
return await self.execute(
query="INSERT INTO guilds VALUES (?,?,?,?,?)",
values=(guild_id, vc_id, vc_category_id, channel_limit, channel_bitrate),
type=COMMIT,
)

@classmethod
async def remove_guild(self, guild_id, vc_id):
db = await self.create_conn()
async with db.execute("DELETE FROM guilds WHERE guild_id = ? AND vc_id = ?", (guild_id, vc_id,)):
return await db.commit()
return await self.execute(
query="DELETE FROM guilds WHERE guild_id = ? AND vc_id = ?",
values=(guild_id, vc_id,),
type=COMMIT,
)

0 comments on commit a1201a7

Please sign in to comment.