-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_data.py
107 lines (91 loc) · 5.03 KB
/
load_data.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
import pandas as pd
import sqlite3
import os
from configs.api import SchemaConfigs as Config
import logging
from datetime import datetime
logs_datetime = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
logging.basicConfig(filename=f"logs/app - {logs_datetime}.log", level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
class DataLoader():
def __init__(self):
self.db_name = Config.DATABASE_NAME
self.table_name = Config.TABLE_NAME
self.table_data = Config.TABLE_DATA
self.conn = None
def connect(self):
try:
if os.path.exists(self.db_name):
self.conn = sqlite3.connect(self.db_name)
logging.info(f"{self.__class__.__name__} [INFO] - connected to database: {self.db_name}")
else:
logging.info(f"{self.__class__.__name__} [INFO] - {self.db_name} does not exist, and has been created!")
self.conn = sqlite3.connect(self.db_name)
except Exception as e:
logging.error(f"{self.__class__.__name__} - [ERROR] {e} occurred when trying to connect to {self.db_name}!")
def write_data(self,data_df,table_exists="upsert"):
try:
if table_exists == "replace":
data_df.to_sql(self.table_name, self.conn, if_exists='replace', index=False)
elif table_exists == "append":
data_df.to_sql(self.table_name, self.conn, if_exists='append', index=False)
elif table_exists == "upsert":
update_columns = [c for c in data_df.columns if c not in ['fixture_id', 'event_timestamp']]
upsert_query = f"""
INSERT INTO {self.table_name} ({', '.join(data_df.columns)})
VALUES ({', '.join(['?' for _ in data_df.columns])})
ON CONFLICT(fixture_id, event_timestamp) DO UPDATE SET
{', '.join([f"{col}=EXCLUDED.{col}" for col in update_columns])};
"""
data_tuples = list(data_df.itertuples(index=False, name=None))
cursor = self.conn.cursor()
try:
cursor.executemany(upsert_query, data_tuples)
self.conn.commit()
print((f"Data upserted successfully into {self.table_name}."))
logging.info(f"{self.__class__.__name__} [INFO] - data upserted successfully into table {self.table_name}.")
except Exception as e:
logging.critical(f"{self.__class__.__name__} [CRITICAL ERROR] {e}")
print(f"A critical error has occurred: {e}")
finally:
cursor.close()
self.conn.close()
except Exception as e:
logging.error(f"{self.__class__.__name__} [ERROR] when trying to write data with {table_exists} parameter!")
def create_table(self):
cursor = self.conn.cursor()
try:
# Check if the table exists
cursor.execute(f"SELECT name FROM sqlite_master WHERE type='table' AND name='{self.table_name}'")
exists = cursor.fetchone()
if not exists:
columns = ', '.join([f"{column_name} {column_type}" for column_name, column_type in self.table_data.items()])
create_table_query = f"CREATE TABLE {self.table_name} ({columns}, UNIQUE(fixture_id, event_timestamp))"
cursor.execute(create_table_query)
self.conn.commit()
logging.info((f"{self.__class__.__name__} - table {self.table_name} created successfully."))
print(f"Table {self.table_name} created successfully.")
else:
logging.info(f"{self.__class__.__name__} [INFO] - table {self.table_name} already exists.")
print(f"{self.__class__.__name__} - table {self.table_name} already exists.")
except Exception as e:
logging.error(f"{self.__class__.__name__} [ERROR] - {e} when trying to create table {self.table_name}!")
print(f"The following error has occurred {e} when trying to create table {self.table_name}!")
finally:
cursor.close()
self.conn.close()
def drop_table(self, table_name):
try:
cursor = self.conn.cursor()
drop_query = f"DROP TABLE IF EXISTS {table_name}"
cursor.execute(drop_query)
self.conn.commit()
logging.info((f"{self.__class__.__name__} - table {table_name} has been dropped successfully."))
print(f"Table {table_name} has been dropped successfully.")
except Exception as e:
logging.error(f"{self.__class__.__name__} [ERROR] while dropping the table {table_name}: {e}")
finally:
cursor.close()
self.conn.close()
def query(self,q):
with sqlite3.connect(self.db_name) as conn:
return pd.read_sql(q,con = conn)