-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmysqldb.py
116 lines (96 loc) · 3.67 KB
/
mysqldb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# import.standard
import asyncio
import os
#from contextlib import asynccontextmanager
from typing import Any, Dict, Iterable, Literal, Optional, Tuple, Union
# import.thirdparty
import aiomysql
from dotenv import load_dotenv
load_dotenv()
loop = asyncio.get_event_loop()
#@asynccontextmanager
async def the_database():
pool = await aiomysql.create_pool(
host=os.getenv("SLOTH_DB_HOST"),
user=os.getenv("SLOTH_DB_USER"),
password=os.getenv("SLOTH_DB_PASSWORD"),
db=os.getenv("SLOTH_DB_NAME"), loop=loop)
db = await pool.acquire()
mycursor = await db.cursor()
return mycursor, db
django_loop = asyncio.get_event_loop()
async def the_django_database():
pool = await aiomysql.create_pool(
host=os.getenv("DJANGO_DB_HOST"),
user=os.getenv("DJANGO_DB_USER"),
password=os.getenv("DJANGO_DB_PASSWORD"),
db=os.getenv("DJANGO_DB_NAME"), loop=django_loop)
db = await pool.acquire()
mycursor = await db.cursor()
return mycursor, db
class DatabaseCore:
COMMITABLE_METHODS = (
"CREATE", "DROP", "INSERT",
"UPDATE", "DELETE",
)
async def get_connection(self, database_name: str) -> Tuple[object, object]:
""" Gets the database connection.
:param database_name: The name of the database to get. """
pool = await aiomysql.create_pool(
host=os.getenv(f"{database_name.upper()}_DB_HOST"),
user=os.getenv(f"{database_name.upper()}_DB_USER"),
password=os.getenv(f"{database_name.upper()}_DB_PASSWORD"),
db=os.getenv(f"{database_name.upper()}_DB_NAME"),
loop=django_loop,
)
db = await pool.acquire()
mycursor = await db.cursor()
return mycursor, db
async def execute_query(self,
query: str,
values: Iterable = [],
connection: Optional[Tuple[object, object]] = None,
execute_many: bool = False,
fetch: Optional[Literal["one", "all"]] = None,
database_name: Literal["sloth", "django"] = "sloth",
description: bool = False
) -> Union[Tuple[Dict[str, Any], Optional[Any], Optional[Any]]]:
""" Executes a database query.
:param query: The query itself to run.
:param values: The values to pass in to the query.
:param fetch: Whether to fetch one, all or no objects from the cursor.
"""
data = [] if fetch == "all" else None
fetch = None if not fetch else fetch.lower()
if not connection:
mycursor, db = await self.get_connection(database_name)
else:
mycursor, db = connection
try:
# Executes the query
if execute_many:
await mycursor.executemany(query, values)
else:
await mycursor.execute(query, values)
if query.upper().strip().startswith(self.COMMITABLE_METHODS):
await db.commit()
if fetch == "one":
data = await mycursor.fetchone()
elif fetch == "all":
data = await mycursor.fetchall()
except Exception as e:
print("Error at query:", str(query))
print("Error:", str(e))
finally:
await mycursor.close()
if description:
return data, mycursor.description
return data
async def table_exists(self, table_name: str) -> bool:
""" Checks whether a given table exists or not.
:param table_name: The table name to check. """
return any(await self.execute_query(
query="SHOW TABLE STATUS LIKE %s;",
values=(table_name,),
fetch="all",
))