-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDB_manager.py
326 lines (306 loc) · 19.6 KB
/
DB_manager.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import mysql.connector
import config
class DBManager:
def __init__(self):
config.logger.info(f'Started connecting to DB "{config.DATABASE_NAME}"')
try:
self.welfare_db = mysql.connector.connect(host=config.HOST,
user=config.USERNAME,
passwd=config.PASSWORD)
except (TypeError, ValueError, ConnectionError, Exception) as e:
config.logger.critical(e, exc_info=True)
config.exit_program()
self.cur = self.welfare_db.cursor()
def setup_DB(self):
config.logger.info(f'Started setting up DB "{config.DATABASE_NAME}"')
self._create_table_countries()
years = list(range(config.FIRST_YEAR, config.LAST_YEAR + 1))
welfare_types = [welfare_type.value.replace('-', '_') for welfare_type in config.WelfareType]
for year in years:
for welfare_type in welfare_types:
eval(f'self._create_table_{welfare_type}({year})')
for health_indicator in config.HealthIndicator:
eval(f'self._create_table_health_{health_indicator.name}()')
config.logger.info(f'Finished setting up DB "{config.DATABASE_NAME}"')
def connect_to_DB(self):
self.cur.execute(f'DROP DATABASE IF EXISTS {config.DATABASE_NAME}')
self.cur.execute(f'CREATE DATABASE IF NOT EXISTS {config.DATABASE_NAME}')
self.cur.execute(f'USE {config.DATABASE_NAME}')
config.logger.info(f'Finished connecting to DB "{config.DATABASE_NAME}"')
def _create_table_health_road_death_rate(self):
table_name = config.HealthIndicator.road_death_rate.name
create_table_query = f'''
CREATE TABLE IF NOT EXISTS {table_name} (
id INT PRIMARY KEY AUTO_INCREMENT,
country_id INT,
year INT,
value FLOAT,
description VARCHAR(255),
FOREIGN KEY (country_id) REFERENCES countries(country_id)
)
'''
insert_into_query = f'''
INSERT INTO {table_name} (
country_id,
year,
value,
description
)
VALUES (%s,%s, %s, %s)
'''
self._create_table_health(table_name, create_table_query, insert_into_query)
def _create_table_health_pollution_death_rate(self):
table_name = config.HealthIndicator.pollution_death_rate.name
create_table_query = f'''
CREATE TABLE IF NOT EXISTS {table_name} (
id INT PRIMARY KEY AUTO_INCREMENT,
country_id INT,
year INT,
value FLOAT,
description VARCHAR(255),
FOREIGN KEY (country_id) REFERENCES countries(country_id)
)
'''
insert_into_query = f'''
INSERT INTO {table_name} (
country_id,
year,
value,
description
)
VALUES (%s,%s, %s, %s)
'''
self._create_table_health(table_name, create_table_query, insert_into_query)
def _create_table_health(self, health_indicator, create_table_query, insert_into_query):
if health_indicator not in list(config.countries_health_data.keys()):
return
self.cur.execute(create_table_query)
self.welfare_db.commit()
desc = config.countries_health_data[health_indicator][0]
for row in config.countries_health_data[health_indicator][1]:
country_code = row['SpatialDim']
country_id = config.get_country_id_by_country_code(country_code)
if country_id is None:
continue
year = row['TimeDim']
value = row['NumericValue']
fields = (country_id, year, value, desc)
if fields is None:
continue
self.cur.execute(insert_into_query, fields)
self.welfare_db.commit()
config.logger.info(f'Created table "{health_indicator}"')
def close_DB(self):
self.welfare_db.close()
def _create_table(self, subject, year, create_table_query, insert_into_query):
data_name = f'{subject}_{year}'
if data_name not in list(config.countries_data.keys()):
return
self.cur.execute(create_table_query)
self.welfare_db.commit()
for row in config.countries_data[data_name][1:]:
country_name = row[0]
if country_name not in config.countries_dict:
continue
fields = (config.countries_dict[country_name], year, *row[1:])
if fields is None:
continue
self.cur.execute(insert_into_query, fields)
self.welfare_db.commit()
config.logger.info(f'Created table "{subject}"')
def _create_table_countries(self):
self.cur.execute('''CREATE TABLE IF NOT EXISTS countries(country_id INT PRIMARY KEY,
country_name VARCHAR(255))''')
self.welfare_db.commit()
query = "INSERT INTO countries (country_id, country_name) VALUES (%s, %s)"
countries_list = [(country_id, country_name) for country_name, country_id in config.countries_dict.items()]
self.cur.executemany(query, countries_list)
self.welfare_db.commit()
config.logger.info('Created table "countries"')
def _create_table_property_investment(self, year):
subject = config.WelfareType.property_investment.value.replace('-', '_')
create_table_query = f'''
CREATE TABLE IF NOT EXISTS {subject} (
id INT PRIMARY KEY AUTO_INCREMENT,
country_id INT,
year INT,
price_to_income FLOAT,
gross_rental_inside_center FLOAT,
gross_rental_outside_center FLOAT,
price_rent_city_center FLOAT,
price_rent_outside_city_center FLOAT,
mortgage_as_prc_income FLOAT,
affordability FLOAT,
FOREIGN KEY (country_id) REFERENCES countries(country_id)
)
'''
insert_into_query = f'''
INSERT INTO {subject} (
country_id,
year,
price_to_income,
gross_rental_inside_center,
gross_rental_outside_center,
price_rent_city_center,
price_rent_outside_city_center,
mortgage_as_prc_income,
affordability)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
'''
self._create_table(subject, year, create_table_query, insert_into_query)
def _create_table_cost_of_living(self, year):
subject = config.WelfareType.cost_of_living.value.replace('-', '_')
create_table_query = f'''
CREATE TABLE IF NOT EXISTS {subject} (
id INT PRIMARY KEY AUTO_INCREMENT,
country_id INT,
year INT,
cost_of_living FLOAT,
rent FLOAT,
cost_of_living_and_rent FLOAT,
groceries_prices FLOAT,
restaurant_prices FLOAT,
local_purchasing_power FLOAT,
FOREIGN KEY (country_id) REFERENCES countries(country_id)
)
'''
insert_into_query = f'''
INSERT INTO {subject} (
country_id,
year,
cost_of_living,
rent,
cost_of_living_and_rent,
groceries_prices,
restaurant_prices,
local_purchasing_power)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
'''
self._create_table(subject, year, create_table_query, insert_into_query)
def _create_table_crime(self, year):
subject = config.WelfareType.crime.value.replace('-', '_')
create_table_query = f'''
CREATE TABLE IF NOT EXISTS {subject} (
id INT PRIMARY KEY AUTO_INCREMENT,
country_id INT,
year INT,
crime_index FLOAT,
safety_index FLOAT,
FOREIGN KEY (country_id) REFERENCES countries(country_id)
)
'''
insert_into_query = f'''
INSERT INTO {subject} (
country_id,
year,
crime_index,
safety_index)
VALUES (%s, %s, %s, %s )
'''
self._create_table(subject, year, create_table_query, insert_into_query)
def _create_table_quality_of_life(self, year):
subject = config.WelfareType.quality_of_life.value.replace('-', '_')
create_table_query = f'''
CREATE TABLE IF NOT EXISTS {subject} (
id INT PRIMARY KEY AUTO_INCREMENT,
country_id INT,
year INT,
quality_life_index FLOAT,
purchase_power_index FLOAT,
safety_index FLOAT,
health_care_index FLOAT,
cost_living_index FLOAT,
property_price_to_income_index FLOAT,
traffic_commute_time_index FLOAT,
pollution_index FLOAT,
climate_index VARCHAR(255),
FOREIGN KEY (country_id) REFERENCES countries(country_id)
)
'''
insert_into_query = f'''
INSERT INTO {subject} ( country_id,
year,
quality_life_index,
purchase_power_index,
safety_index,
health_care_index,
cost_living_index,
property_price_to_income_index,
traffic_commute_time_index,
pollution_index,
climate_index)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
'''
self._create_table(subject, year, create_table_query, insert_into_query)
def _create_table_pollution(self, year):
subject = config.WelfareType.pollution.value.replace('-', '_')
create_table_query = f'''
CREATE TABLE IF NOT EXISTS {subject} (
id INT PRIMARY KEY AUTO_INCREMENT,
country_id INT,
year INT,
pollution_index INT,
exp_pollution_index INT,
FOREIGN KEY (country_id) REFERENCES countries(country_id)
)
'''
insert_into_query = f'''
INSERT INTO {subject} (
country_id,
year,
pollution_index,
exp_pollution_index
)
VALUES (%s ,%s, %s, %s)
'''
self._create_table(subject, year, create_table_query, insert_into_query)
def _create_table_health_care(self, year):
subject = config.WelfareType.health_care.value.replace('-', '_')
create_table_query = f'''
CREATE TABLE IF NOT EXISTS {subject} (
id INT PRIMARY KEY AUTO_INCREMENT,
country_id INT,
year INT,
health_care_index FLOAT,
health_care_exp_index FLOAT,
FOREIGN KEY (country_id) REFERENCES countries(country_id)
)
'''
insert_into_query = f'''
INSERT INTO {subject} (
country_id,
year,
health_care_index,
health_care_exp_index
)
VALUES (%s,%s, %s, %s)
'''
self._create_table(subject, year, create_table_query, insert_into_query)
def _create_table_traffic(self, year):
subject = config.WelfareType.traffic.value.replace('-', '_')
create_table_query = f'''
CREATE TABLE IF NOT EXISTS {subject} (
id INT PRIMARY KEY AUTO_INCREMENT,
country_id INT,
year INT,
traffic_index FLOAT,
time_index FLOAT,
time_exp_index FLOAT,
inefficiency_index FLOAT,
CO2_emission_index FLOAT,
FOREIGN KEY (country_id) REFERENCES countries(country_id)
)
'''
insert_into_query = f'''
INSERT INTO {subject} (
country_id,
year,
traffic_index,
time_index,
time_exp_index,
inefficiency_index,
CO2_emission_index
)
VALUES (%s, %s, %s, %s, %s, %s, %s)
'''
self._create_table(subject, year, create_table_query, insert_into_query)