-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataConnector.py
61 lines (51 loc) · 1.81 KB
/
dataConnector.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
"""
This module provides the DataConnector class for connecting to different types of databases.
"""
from typing import Optional
import aiosqlite
import aiomysql
from .manager.configManager import ConfigManager
class DataConnector:
"""
The DataConnector class provides methods to connect to, disconnect from,
and interact with different types of databases.
"""
conn: Optional[object] = None
@staticmethod
async def connect(configPath: str) -> None:
"""
Connect to the database using the configuration file path.
Args:
configPath (str): The path to the configuration file.
"""
config = ConfigManager(configPath)
db_type = config.get_database_type()
if db_type == "sqlite":
sqliteDataFile = config.get_sqlite_file_path()
DataConnector.conn = await aiosqlite.connect(sqliteDataFile)
print("Connected to SQLite database")
elif db_type == "mysql":
DataConnector.conn = await aiomysql.connect(
host=config.get_mysql_host(),
user=config.get_mysql_username(),
password=config.get_mysql_password(),
db=config.get_mysql_schema(),
)
print("Connected to MySQL database")
else:
print("Invalid database type")
@staticmethod
async def get_cursor() -> Optional[object]:
"""
Get the database cursor.
Returns:
object: The database cursor.
"""
return await DataConnector.conn.cursor()
@staticmethod
async def disconnect() -> None:
"""
Disconnect from the database.
"""
if DataConnector.conn:
await DataConnector.conn.close()