-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_handler.py
executable file
·355 lines (266 loc) · 9.58 KB
/
db_handler.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env python2.7
from __future__ import print_function
import csv
from db_connection import connect
directory = "csv"
extension = ".csv"
csv_paths = {
'heroes': "{}/heroes{}".format(directory, extension),
'villains': "{}/villains{}".format(directory, extension),
'masterminds': "{}/masterminds{}".format(directory, extension),
'schemes': "{}/schemes{}".format(directory, extension),
'specialRules': "{}/specialRules{}".format(directory, extension)
}
TABLES = {}
TABLES['Heroes'] = """
CREATE TABLE `Heroes` (
`Name` varchar(45) NOT NULL,
`Team` varchar(45) NOT NULL,
`CardSet` varchar(45) NOT NULL,
PRIMARY KEY (`Name`, `CardSet`)
) ENGINE=InnoDB
"""
TABLES['Villains'] = """
CREATE TABLE `Villains` (
`Name` varchar(45) NOT NULL,
`Mastermind` varchar(45),
`IsHenchmanGroup` tinyint(2) NOT NULL,
`CardSet` varchar(45) NOT NULL,
PRIMARY KEY (`Name`, `CardSet`)
) ENGINE=InnoDB
"""
TABLES['Masterminds'] = """
CREATE TABLE `Masterminds` (
`Name` varchar(45) NOT NULL,
`GroupLed` varchar(45) NOT NULL,
`LeadsHenchmenGroup` varchar(45) NOT NULL,
`CardSet` varchar (45) NOT NULL,
PRIMARY KEY (`Name`, `CardSet`)
) ENGINE=InnoDB
"""
TABLES['Schemes'] = """
CREATE TABLE `Schemes` (
`Name` varchar(45) NOT NULL,
`RequiredVillainGroup` varchar(45),
`ExtraVillainGroup` boolean NOT NULL,
`ExtraHenchmanGroup` boolean NOT NULL,
`CardSet` varchar(45) NOT NULL,
PRIMARY KEY (`Name`, `CardSet`)
) ENGINE=InnoDB
"""
TABLES['SpecialRules'] = """
CREATE TABLE `SpecialRules` (
`Scheme` varchar(45) NOT NULL,
`NumberOfPlayers` tinyint(255),
`HeroInVillainDeck` varchar(45),
`RequiredHero` varchar(45),
`NumberOfHeroes` tinyint(255),
`SinisterAmbitions` tinyint(2),
`TyrantVillains` tinyint(2),
PRIMARY KEY (`Scheme`)
) ENGINE=InnoDB
"""
def create(table):
if table == 'all':
for _, path in csv_paths.iteritems():
create_tables_file_opener(path)
else:
create_tables_file_opener(csv_paths[table])
def update(table):
if table == 'all':
for _, path in csv_paths.iteritems():
update_tables_file_opener(path)
else:
update_tables_file_opener(csv_paths[table])
def drop(table):
db_connection = connect()
cursor = db_connection.cursor()
sql = """
DROP TABLE IF EXISTS {}
"""
if table != 'all':
table_length = len(table)
table_name = table[0].upper() + table[1:table_length]
print("Dropping table: {}".format(table_name))
cursor.execute(sql.format(table_name))
else:
print("Dropping all tables")
for key, value in TABLES.iteritems():
cursor.execute(sql.format(key))
db_connection.commit()
db_connection.close()
cursor.close()
def create_heroes(reader):
db_connection = connect()
cursor = db_connection.cursor()
sql = """
INSERT INTO Heroes
(Name, Team, CardSet)
VALUES (%s, %s, %s)
"""
cursor.execute(TABLES['Heroes'])
for hero in reader:
parameters = (hero['Name'], hero['Team'], hero['CardSet'])
print("Inserting {} into Heroes".format(hero['Name']))
cursor.execute(sql, parameters)
db_connection.commit()
db_connection.close()
cursor.close()
def update_heroes(reader):
db_connection = connect()
cursor = db_connection.cursor()
sql = """
UPDATE Heroes
SET Name=%s, Team=%s, CardSet=%s
WHERE Name=%s AND CardSet=%s
"""
for hero in reader:
parameters = (hero['Name'], hero['Team'], hero['CardSet'], hero['Name'], hero['CardSet'])
print("Updating {} in Heroes".format(hero['Name']))
cursor.execute(sql, parameters)
db_connection.commit()
db_connection.close()
cursor.close()
def create_villains(reader):
db_connection = connect()
cursor = db_connection.cursor()
sql = """
INSERT INTO Villains
(Name, Mastermind, IsHenchmanGroup, CardSet)
VALUES (%s, %s, %s, %s)
"""
cursor.execute(TABLES['Villains'])
for villain_group in reader:
parameters = (villain_group['Name'], villain_group['Mastermind'], villain_group['IsHenchmanGroup'],
villain_group['CardSet'])
print("Inserting {} into Villains".format(villain_group['Name']))
cursor.execute(sql, parameters)
db_connection.commit()
db_connection.close()
cursor.close()
def update_villains(reader):
db_connection = connect()
cursor = db_connection.cursor()
sql = """
UPDATE Villains
SET Name=%s, Mastermind=%s, IsHenchmanGroup=%s, CardSet=%s
WHERE Name=%s AND CardSet=%s
"""
for villain in reader:
parameters = (villain['Name'], villain['Mastermind'], villain['IsHenchmanGroup'], villain['CardSet'],
villain['Name'], villain['CardSet'])
print("Updating {} in Villains".format(villain['Name']))
cursor.execute(sql, parameters)
db_connection.commit()
db_connection.close()
cursor.close()
def create_masterminds(reader):
db_connection = connect()
cursor = db_connection.cursor()
sql = """
INSERT INTO Masterminds
(Name, GroupLed, LeadsHenchmenGroup, CardSet)
VALUES (%s, %s, %s, %s)
"""
cursor.execute(TABLES['Masterminds'])
for mastermind in reader:
parameters = (mastermind['Name'], mastermind['GroupLed'], mastermind['LeadsHenchmenGroup'],
mastermind['CardSet'])
print("Inserting {} into Masterminds".format(mastermind['Name']))
cursor.execute(sql, parameters)
db_connection.commit()
db_connection.close()
cursor.close()
def update_masterminds(reader):
db_connection = connect()
cursor = db_connection.cursor()
sql = """
UPDATE Masterminds
SET Name=%s, GroupLed=%s, LeadsHenchmenGroup=%s, CardSet=%s
WHERE Name=%s AND CardSet=%s
"""
for mastermind in reader:
parameters = (mastermind['Name'], mastermind['GroupLed'], mastermind['LeadsHenchmenGroup'],
mastermind['CardSet'], mastermind['Name'], mastermind['CardSet'])
print("Updating {} in Masterminds".format(mastermind['Name']))
cursor.execute(sql, parameters)
db_connection.commit()
db_connection.close()
cursor.close()
def create_schemes(reader):
db_connection = connect()
cursor = db_connection.cursor()
sql = """
INSERT INTO Schemes
(Name, RequiredVillainGroup, ExtraVillainGroup, ExtraHenchmanGroup, CardSet)
VALUES (%s, %s, %s, %s, %s)
"""
cursor.execute(TABLES['Schemes'])
for scheme in reader:
parameters = (scheme['Name'], scheme['RequiredVillainGroup'], scheme['ExtraVillainGroup'],
scheme['ExtraHenchmanGroup'], scheme['CardSet'])
print("Inserting {} into Schemes".format(scheme['Name']))
cursor.execute(sql, parameters)
db_connection.commit()
db_connection.close()
cursor.close()
def update_schemes(reader):
db_connection = connect()
cursor = db_connection.cursor()
sql = """
UPDATE Schemes
SET Name=%s, RequiredVillainGroup=%s, ExtraVillainGroup=%s, ExtraHenchmanGroup=%s, CardSet=%s
WHERE Name=%s AND CardSet=%s
"""
for scheme in reader:
parameters = (scheme['Name'], scheme['RequiredVillainGroup'], scheme['ExtraVillainGroup'],
scheme['ExtraHenchmanGroup'], scheme['CardSet'], scheme['Name'], scheme['CardSet'])
print("Updating {} in Schemes".format(scheme['Name']))
cursor.execute(sql, parameters)
db_connection.commit()
db_connection.close()
cursor.close()
def create_specialRules(reader):
db_connection = connect()
cursor = db_connection.cursor()
sql = """
INSERT INTO SpecialRules
(Scheme, NumberOfPlayers, HeroInVillainDeck, RequiredHero, NumberOfHeroes, SinisterAmbitions, TyrantVillains)
VALUES (%s, %s, %s, %s, %s, %s, %s)
"""
cursor.execute(TABLES['SpecialRules'])
for specialRule in reader:
parameters = (specialRule['Scheme'], specialRule['NumberOfPlayers'], specialRule['HeroInVillainDeck'],
specialRule['RequiredHero'], specialRule['NumberOfHeroes'], specialRule['SinisterAmbitions'],
specialRule['TyrantVillains'])
print("Inserting {} into SpecialRules".format(specialRule['Scheme']))
cursor.execute(sql, parameters)
db_connection.commit()
db_connection.close()
cursor.close()
def create_tables_file_opener(path):
with open(path) as csv_file:
reader = csv.DictReader(csv_file)
if path == csv_paths['heroes']:
create_heroes(reader)
elif path == csv_paths['villains']:
create_villains(reader)
elif path == csv_paths['masterminds']:
create_masterminds(reader)
elif path == csv_paths['schemes']:
create_schemes(reader)
elif path == csv_paths['specialRules']:
create_specialRules(reader)
csv_file.close()
def update_tables_file_opener(path):
with open(path) as csv_file:
reader = csv.DictReader(csv_file)
if path == csv_paths['heroes']:
update_heroes(reader)
elif path == csv_paths['villains']:
update_villains(reader)
elif path == csv_paths['masterminds']:
update_masterminds(reader)
elif path == csv_paths['schemes']:
update_schemes(reader)
csv_file.close()