From 662005c481ed5b7af5b7ee739097d0128238255a Mon Sep 17 00:00:00 2001 From: skrphenix Date: Sat, 10 Feb 2024 18:40:35 +0530 Subject: [PATCH] adding docs --- economy with MYSQL/modules/bank_funcs.py | 8 ++++-- economy with MYSQL/modules/ext.py | 27 +++++++++++++++++++ economy with MYSQL/modules/inventory_funcs.py | 8 +----- economy with SQLITE3/modules/ext.py | 27 +++++++++++++++++++ economy with aiosqlite/modules/ext.py | 27 +++++++++++++++++++ 5 files changed, 88 insertions(+), 9 deletions(-) diff --git a/economy with MYSQL/modules/bank_funcs.py b/economy with MYSQL/modules/bank_funcs.py index cf0858f..46b9ede 100644 --- a/economy with MYSQL/modules/bank_funcs.py +++ b/economy with MYSQL/modules/bank_funcs.py @@ -29,14 +29,18 @@ async def create_table(self) -> None: conn.close() async def open_acc(self, user: discord.Member) -> None: + conn = await self.db.connect() data = await self.db.execute( - f"SELECT * FROM `{TABLE_NAME}` WHERE userID = %s", (user.id,), fetch="one" + f"SELECT * FROM `{TABLE_NAME}` WHERE userID = %s", (user.id,), fetch="one", conn=conn ) if data is None: await self.db.run( - f"INSERT INTO `{TABLE_NAME}`(userID, wallet) VALUES(%s, %s)", (user.id, 5000) + f"INSERT INTO `{TABLE_NAME}`(userID, wallet) VALUES(%s, %s)", \ + (user.id, 5000), conn=conn ) + conn.close() + async def get_acc(self, user: discord.Member) -> Optional[Any]: return await self.db.execute( f"SELECT * FROM `{TABLE_NAME}` WHERE userID = %s", (user.id,), fetch="one" diff --git a/economy with MYSQL/modules/ext.py b/economy with MYSQL/modules/ext.py index 25b661a..6da5fd7 100644 --- a/economy with MYSQL/modules/ext.py +++ b/economy with MYSQL/modules/ext.py @@ -27,12 +27,29 @@ async def _fetch(cursor, mode: str) -> Optional[Any]: return None async def connect(self): + """ + creates a new pool connection to the database. + """ + return self._pool.get_connection() async def execute( self, query: str, values: Tuple = (), *, fetch: str = None, commit: bool = False, conn: mysql_pooling.PooledMySQLConnection = None ) -> Optional[Any]: + """ + runs the query without committing any changes to the database if `commit` is not passed at func call. + + Tip: use this method for fetching like:`SELECT` queries. + + :param query: SQL query. + :param values: values to be passed to the query. + :param fetch: Takes ('one', 'many', 'all'). + :param commit: Commits the changes to the database if it's set to `True`. + :param conn: pass the new pool connection to execute two or more methods. If passed, you've to close it manually. + :return: required data. + """ + bypass = conn conn = await self.connect() if conn is None else conn cursor = conn.cursor() @@ -53,4 +70,14 @@ async def execute( return data async def run(self, query: str, values: Tuple = (), *, conn: mysql_pooling.PooledMySQLConnection = None) -> None: + """ + runs the query and commits any changes to the database directly. + + Tip: use this method if you want to commit changes to the database. Like: `CREATE, UPDATE, INSERT, DELETE`, etc. + + :param query: SQL query + :param values: values to be passed to the query. + :param conn: pass the new pool connection to execute two or more methods. If passed, you've to close it manually. + """ + await self.execute(query, values, commit=True, conn=conn) diff --git a/economy with MYSQL/modules/inventory_funcs.py b/economy with MYSQL/modules/inventory_funcs.py index 793e45e..8fd061f 100644 --- a/economy with MYSQL/modules/inventory_funcs.py +++ b/economy with MYSQL/modules/inventory_funcs.py @@ -52,13 +52,7 @@ async def open_acc(self, user: discord.Member) -> None: (user.id,), conn=conn ) - for item in item_names: - await self.db.run( - f"UPDATE `{TABLE_NAME}` SET `{item}` = 0 WHERE userID = %s", - (user.id,), conn=conn - ) - - conn.close() + conn.close() async def get_acc(self, user: discord.Member) -> Optional[Any]: return await self.db.execute( diff --git a/economy with SQLITE3/modules/ext.py b/economy with SQLITE3/modules/ext.py index 1399019..9182141 100644 --- a/economy with SQLITE3/modules/ext.py +++ b/economy with SQLITE3/modules/ext.py @@ -19,12 +19,29 @@ async def _fetch(cursor: sqlite3.Cursor, mode: str) -> Optional[Any]: return None async def connect(self) -> sqlite3.Connection: + """ + creates a new connection to the database. + """ + return sqlite3.connect(self.db_name) async def execute( self, query: str, values: Tuple = (), *, fetch: str = None, commit: bool = False, conn: sqlite3.Connection = None ) -> Optional[Any]: + """ + runs the query without committing any changes to the database if `commit` is not passed at func call. + + Tip: use this method for fetching like:`SELECT` queries. + + :param query: SQL query. + :param values: values to be passed to the query. + :param fetch: Takes ('one', 'many', 'all'). + :param commit: Commits the changes to the database if it's set to `True`. + :param conn: pass the new connection to execute two or more methods. If passed, you've to close it manually. + :return: required data. + """ + bypass = conn conn = await self.connect() if conn is None else conn cursor = conn.cursor() @@ -45,4 +62,14 @@ async def execute( return data async def run(self, query: str, values: Tuple = (), *, conn: sqlite3.Connection = None) -> None: + """ + runs the query and commits any changes to the database directly. + + Tip: use this method if you want to commit changes to the database. Like: `CREATE, UPDATE, INSERT, DELETE`, etc. + + :param query: SQL query + :param values: values to be passed to the query. + :param conn: pass the new connection to execute two or more methods. If passed, you've to close it manually. + """ + await self.execute(query, values, commit=True, conn=conn) diff --git a/economy with aiosqlite/modules/ext.py b/economy with aiosqlite/modules/ext.py index b9b1975..8f8947f 100644 --- a/economy with aiosqlite/modules/ext.py +++ b/economy with aiosqlite/modules/ext.py @@ -19,12 +19,29 @@ async def _fetch(cursor: aiosqlite.Cursor, mode: str) -> Optional[Any]: return None async def connect(self) -> aiosqlite.Connection: + """ + creates a new connection to the database. + """ + return await aiosqlite.connect(self.db_name) async def execute( self, query: str, values: Tuple = (), *, fetch: str = None, commit: bool = False, conn: aiosqlite.Connection = None ) -> Optional[Any]: + """ + runs the query without committing any changes to the database if `commit` is not passed at func call. + + Tip: use this method for fetching like:`SELECT` queries. + + :param query: SQL query. + :param values: values to be passed to the query. + :param fetch: Takes ('one', 'many', 'all'). + :param commit: Commits the changes to the database if it's set to `True`. + :param conn: pass the new connection to execute two or more methods. If passed, you've to close it manually. + :return: required data. + """ + bypass = conn conn = await self.connect() if conn is None else conn cursor = await conn.cursor() @@ -45,4 +62,14 @@ async def execute( return data async def run(self, query: str, values: Tuple = (), *, conn: aiosqlite.Connection = None) -> None: + """ + runs the query and commits any changes to the database directly. + + Tip: use this method if you want to commit changes to the database. Like: `CREATE, UPDATE, INSERT, DELETE`, etc. + + :param query: SQL query + :param values: values to be passed to the query. + :param conn: pass the new connection to execute two or more methods. If passed, you've to close it manually. + """ + await self.execute(query, values, commit=True, conn=conn)