Skip to content

Commit

Permalink
adding docs
Browse files Browse the repository at this point in the history
  • Loading branch information
skrphenix committed Feb 10, 2024
1 parent 8580a23 commit 662005c
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 9 deletions.
8 changes: 6 additions & 2 deletions economy with MYSQL/modules/bank_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
27 changes: 27 additions & 0 deletions economy with MYSQL/modules/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
8 changes: 1 addition & 7 deletions economy with MYSQL/modules/inventory_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
27 changes: 27 additions & 0 deletions economy with SQLITE3/modules/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
27 changes: 27 additions & 0 deletions economy with aiosqlite/modules/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)

0 comments on commit 662005c

Please sign in to comment.