-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnector.py
54 lines (44 loc) · 2.13 KB
/
connector.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
import mysql.connector as db
import polars as pl
import numpy as np
import json
def database_connector(database_secret_path: str = "database_secret.json") -> tuple:
"""Connect to the database and return the connector and the cursor.
Args:
database_secret_path (str, optional): The path of the database secret file. Defaults to "database_secret.json".
Returns:
tuple: The connector and the cursor."""
with open(database_secret_path, "r") as f:
database_secret = json.load(f)['database']
connector = db.connect(
user=database_secret["user"],
password=database_secret["password"],
host=database_secret["host"],
port=database_secret["port"],
database=database_secret["database"]
)
cursor = connector.cursor()
return connector, cursor
def database_query(connector: db.MySQLConnection, cursor: db.cursor.MySQLCursor, query: str, verbose: bool = False) -> pl.DataFrame:
"""Query the database and return the result as a polars dataframe.
Before using this function, you should connect to the database using database_connector function.
Connector function will return the connector and the cursor. You should pass the connector and the cursor to this function.
Example:
>>> connector, cursor = database_connector(database_secret_path="secret_key.json")
>>> table_name = "video"
>>> query = f"SELECT * FROM {table_name};"
>>> result = database_query(connector, cursor, query, verbose=False)
>>> if result.shape[0] == 0: return {"error": "No video found."}
Args:
connector (db.MySQLConnection): The connector that is returned from database_connector function.
cursor (db.cursor.MySQLCursor): The cursor that is returned from database_connector function.
query (str): The query that you want to execute.
verbose (bool, optional): If True, print the result. Defaults to False.
Returns:
pl.DataFrame: The result of the query."""
cursor.execute(query)
result = cursor.fetchall()
if verbose:
print(result)
result = np.array(result)
return result