-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataAccess.py
352 lines (284 loc) · 8.94 KB
/
dataAccess.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
from databaseAccess import DatabaseManager as dbm
class DataAccess(object):
def __init__(self):
self.loadAttrs()
def loadAttrs(self):
self.genres = self.getGenres()
self.formats = self.getFormats()
self.directors = self.getDirectors()
self.genres = self.getGenres()
self.formats = self.getFormats()
self.actors = self.getActors()
self.countries = self.getCountries()
self.artists = self.getArtists()
self.films = self.getfilms()
self.musicDVDs = self.getMusicDVDs()
def getMusicDVDs(self):
dm = dbm()
sql = '''
SELECT id, title
FROM music_dvd
'''
results = dm.execute(sql, ())
musicDVDs = {}
for result in results:
id, musicDVD = result
musicDVDs[musicDVD] = id
return musicDVDs
def createMusicDVD(self, title, year, format):
dm = dbm()
sql = 'INSERT INTO music_dvd\
(title, year, format)\
VALUES\
(%s, %s, %s)\
'#%("'" + title + "'", year, format)
results = dm.execute(sql, (title, year, format))
dm.commit()
if results is not False:
sql = 'SELECT id FROM music_dvd WHERE title = %s' #%("'" + title + "'")
results = dm.execute(sql, title)
return results
def getGenres(self):
dm = dbm()
sql = '''
SELECT id, genre
FROM genre
'''
results = dm.execute(sql, ())
genres = {}
for result in results:
id, genre = result
genres[genre] = id
return genres
def getFormats(self):
dm = dbm()
sql = '''
SELECT id, name
FROM format
'''
results = dm.execute(sql, ())
formats = {}
for result in results:
id, format = result
formats[format] = id
return formats
def getfilms(self):
dm = dbm()
sql = '''
SELECT title
FROM films
'''
results = dm.execute(sql, ())
films = []
if results:
for result in results:
films.append(result[0])
return films
def createFilm(self, title, year, genre, length, isCriterion, directorID, format):
dm = dbm()
sql = 'INSERT INTO films\
(title, year, genre, length, criterion_edition, director, format)\
VALUES\
(%s, %s, %s, %s, %s, %s, %s)\
'#%("'" + title + "'", year, genre, length, isCriterion, directorID, format)
results = dm.execute(sql, (title, year, genre, length, isCriterion, directorID, format))
dm.commit()
if results is not False:
sql = 'SELECT id FROM films WHERE title = %s' #%("'" + title + "'")
results = dm.execute(sql, title)
return results
def getDirectors(self):
dm = dbm()
sql = '''
SELECT id, first_name, last_name
FROM director
'''
results = dm.execute(sql, ())
directors = {}
if results:
for result in results:
id, firstName, lastName = result
directors[(firstName, lastName)] = id
return directors
def createDirector(self, nameTupple):
dm = dbm()
sql = 'INSERT INTO director\
(first_name, last_name)\
VALUES\
(%s, %s)'#%("'" + nameTupple[0] + "'", "'" + nameTupple[1] + "'")
results = dm.execute(sql, (nameTupple[0], nameTupple[1]))
dm.commit()
if results is not False:
sql = 'SELECT id FROM director\
WHERE first_name = %s\
AND last_name = %s'#%("'" + nameTupple[0] + "'", "'" + nameTupple[1] + "'")
results = dm.execute(sql, (nameTupple[0], nameTupple[1]))
return results[-1][-1]
return False
def getseries(self):
dm = dbm()
sql = '''
SELECT name
FROM series
'''
results = dm.execute(sql, ())
series = []
if results:
for result in results:
series.append(result[0])
return series
def createseries(self, name, year, genre, format):
dm = dbm()
sql = 'INSERT INTO series\
(name, year, genre, format)\
VALUES\
(%s, %s, %s, %s)\
'#%("'" + name + "'", year, genre, format)
results = dm.execute(sql, (name, year, genre, format))
dm.commit()
if results is not False:
sql = 'SELECT id FROM series WHERE name = %s' #%("'" + name + "'")
results = dm.execute(sql, name)
return results
def getActors(self):
dm = dbm()
sql = '''
SELECT id, first_name, last_name
FROM actor
'''
results = dm.execute(sql, ())
actors = {}
if results:
for result in results:
id, first_name, last_name = result
actors[(first_name, last_name)] = id
return actors
def createActor(self, nameTupple):
dm = dbm()
sql = 'INSERT INTO actor\
(first_name, last_name)\
VALUES\
(%s, %s)'#%("'" + nameTupple[0] + "'", "'" + nameTupple[1] + "'")
results = dm.execute(sql, (nameTupple[0], nameTupple[1]))
dm.commit()
if results is not False:
sql = 'SELECT id FROM actor\
WHERE first_name = %s\
AND last_name = %s'#%("'" + nameTupple[0] + "'", "'" + nameTupple[1] + "'")
results = dm.execute(sql, (nameTupple[0], nameTupple[1]))
return results[0][0]
return False
def getCountries(self):
dm = dbm()
sql = '''
SELECT id, name
FROM country
'''
results = dm.execute(sql, ())
countries = {}
for result in results:
id, name = result
countries[name] = id
return countries
def createCountry(self, country):
dm = dbm()
sql = 'INSERT INTO country\
(name)\
VALUES\
(%s)'#%("'" + country + "'")
results = dm.execute(sql, country)
dm.commit()
if results is not False:
sql = 'SELECT id FROM country\
WHERE name = %s\
'#%("'" + country + "'")
results = dm.execute(sql, country)
return results[0][0]
return False
def getArtists(self):
dm = dbm()
sql = '''
SELECT id, name
FROM artist
'''
results = dm.execute(sql, ())
artists = {}
for result in results:
id, name = result
artists[name] = id
return artists
def createArtist(self, artist):
dm = dbm()
sql = 'INSERT INTO artist\
(name)\
VALUES\
(%s)'#%("'" + artist + "'")
results = dm.execute(sql, artist)
dm.commit()
if results is not False:
sql = 'SELECT id FROM artist\
WHERE name = %s\
'#%("'" + artist + "'")
results = dm.execute(sql, artist)
return results[0][0]
return False
def checkDirector(self, nameTupple):
if nameTupple in self.directors:
return True
else:
return False
def checkActor(self, nameTupple):
if nameTupple in self.actors:
return True
else:
return False
def checkCountry(self, country):
if country not in self.countries:
return False
return True
def checkArtist(self, artist):
if artist not in self.artists:
return False
return True
def relateActor(self, filmId, actorId, isFilm):
dm = dbm()
if isFilm:
sql = 'INSERT INTO film_actor\
(film, actor)\
VALUES\
(%s, %s)'#%(filmId, actorId)
else:
sql = 'INSERT INTO series_actor\
(series, actor)\
VALUES\
(%s, %s)'#%(filmId, actorId)
results = dm.execute(sql, (filmId, actorId))
dm.commit()
def relateCountry(self, filmId, countryId, isFilm):
dm = dbm()
if isFilm:
sql = 'INSERT INTO film_country\
(film, country)\
VALUES\
(%s, %s)'#%(filmId, countryId)
else:
sql = 'INSERT INTO series_country\
(series, country)\
VALUES\
(%s, %s)'#%(filmId, countryId)
results = dm.execute(sql, (filmId, countryId))
dm.commit()
def relateArtistMusic(self, artisdId, diskId, isDVD):
dm = dbm()
if isDVD:
sql = 'INSERT INTO artist_dvd\
(artist, dvdid)\
VALUES\
(%s, %s)'#%(artisdId, diskId)
else:
sql = 'INSERT INTO series_cd\
(artist, cdid)\
VALUES\
(%s, %s)'#%(artisdId, diskId)
results = dm.execute(sql, (artisdId, diskId))
dm.commit()