Optimize database queries
This commit is contained in:
@@ -70,9 +70,9 @@ def getDatabaseVersion(databaseContext):
|
||||
|
||||
Session = databaseContext['session']
|
||||
s = Session()
|
||||
q = s.query(Property).filter(Property.key == DATABASE_VERSION_KEY)
|
||||
versionProperty = s.query(Property).filter(Property.key == DATABASE_VERSION_KEY).first()
|
||||
|
||||
return int(q.first().value) if q.count() else 0
|
||||
return int(versionProperty.value) if versionProperty is not None else 0
|
||||
|
||||
except Exception as ex:
|
||||
raise click.ClickException(f"getDatabaseVersion(): {repr(ex)}")
|
||||
|
||||
@@ -25,10 +25,9 @@ class MediaController():
|
||||
pid = int(patternId)
|
||||
|
||||
s = self.Session()
|
||||
q = s.query(Pattern).filter(Pattern.id == pid)
|
||||
pattern = s.query(Pattern).filter(Pattern.id == pid).first()
|
||||
|
||||
if q.count():
|
||||
pattern = q.first
|
||||
if pattern is not None:
|
||||
|
||||
for mediaTagKey, mediaTagValue in mediaDescriptor.getTags():
|
||||
self.__tac.updateMediaTag(pid, mediaTagKey, mediaTagValue)
|
||||
|
||||
@@ -19,10 +19,12 @@ class PatternController():
|
||||
try:
|
||||
|
||||
s = self.Session()
|
||||
q = s.query(Pattern).filter(Pattern.show_id == int(patternObj['show_id']),
|
||||
Pattern.pattern == str(patternObj['pattern']))
|
||||
pattern = s.query(Pattern).filter(
|
||||
Pattern.show_id == int(patternObj['show_id']),
|
||||
Pattern.pattern == str(patternObj['pattern']),
|
||||
).first()
|
||||
|
||||
if not q.count():
|
||||
if pattern is None:
|
||||
pattern = Pattern(show_id = int(patternObj['show_id']),
|
||||
pattern = str(patternObj['pattern']))
|
||||
s.add(pattern)
|
||||
@@ -41,11 +43,9 @@ class PatternController():
|
||||
|
||||
try:
|
||||
s = self.Session()
|
||||
q = s.query(Pattern).filter(Pattern.id == int(patternId))
|
||||
pattern = s.query(Pattern).filter(Pattern.id == int(patternId)).first()
|
||||
|
||||
if q.count():
|
||||
|
||||
pattern: Pattern = q.first()
|
||||
if pattern is not None:
|
||||
|
||||
pattern.show_id = int(patternObj['show_id'])
|
||||
pattern.pattern = str(patternObj['pattern'])
|
||||
@@ -69,10 +69,12 @@ class PatternController():
|
||||
|
||||
try:
|
||||
s = self.Session()
|
||||
q = s.query(Pattern).filter(Pattern.show_id == int(patternObj['show_id']), Pattern.pattern == str(patternObj['pattern']))
|
||||
pattern = s.query(Pattern).filter(
|
||||
Pattern.show_id == int(patternObj['show_id']),
|
||||
Pattern.pattern == str(patternObj['pattern']),
|
||||
).first()
|
||||
|
||||
if q.count():
|
||||
pattern = q.first()
|
||||
if pattern is not None:
|
||||
return int(pattern.id)
|
||||
else:
|
||||
return None
|
||||
@@ -90,9 +92,7 @@ class PatternController():
|
||||
|
||||
try:
|
||||
s = self.Session()
|
||||
q = s.query(Pattern).filter(Pattern.id == int(patternId))
|
||||
|
||||
return q.first() if q.count() else None
|
||||
return s.query(Pattern).filter(Pattern.id == int(patternId)).first()
|
||||
|
||||
except Exception as ex:
|
||||
raise click.ClickException(f"PatternController.getPattern(): {repr(ex)}")
|
||||
@@ -103,13 +103,12 @@ class PatternController():
|
||||
def deletePattern(self, patternId):
|
||||
try:
|
||||
s = self.Session()
|
||||
q = s.query(Pattern).filter(Pattern.id == int(patternId))
|
||||
pattern = s.query(Pattern).filter(Pattern.id == int(patternId)).first()
|
||||
|
||||
if q.count():
|
||||
if pattern is not None:
|
||||
|
||||
#DAFUQ: https://stackoverflow.com/a/19245058
|
||||
# q.delete()
|
||||
pattern = q.first()
|
||||
s.delete(pattern)
|
||||
|
||||
s.commit()
|
||||
@@ -158,4 +157,4 @@ class PatternController():
|
||||
# except Exception as ex:
|
||||
# raise click.ClickException(f"PatternController.getMediaDescriptor(): {repr(ex)}")
|
||||
# finally:
|
||||
# s.close()
|
||||
# s.close()
|
||||
|
||||
@@ -101,11 +101,9 @@ class ShiftedSeasonController():
|
||||
try:
|
||||
s = self.Session()
|
||||
|
||||
q = s.query(ShiftedSeason).filter(ShiftedSeason.id == int(shiftedSeasonId))
|
||||
shiftedSeason = s.query(ShiftedSeason).filter(ShiftedSeason.id == int(shiftedSeasonId)).first()
|
||||
|
||||
if q.count():
|
||||
|
||||
shiftedSeason = q.first()
|
||||
if shiftedSeason is not None:
|
||||
|
||||
shiftedSeason.original_season = int(shiftedSeasonObj['original_season'])
|
||||
shiftedSeason.first_episode = int(shiftedSeasonObj['first_episode'])
|
||||
@@ -141,12 +139,14 @@ class ShiftedSeasonController():
|
||||
|
||||
try:
|
||||
s = self.Session()
|
||||
q = s.query(ShiftedSeason).filter(ShiftedSeason.show_id == int(showId),
|
||||
ShiftedSeason.original_season == int(originalSeason),
|
||||
ShiftedSeason.first_episode == int(firstEpisode),
|
||||
ShiftedSeason.last_episode == int(lastEpisode))
|
||||
shiftedSeason = s.query(ShiftedSeason).filter(
|
||||
ShiftedSeason.show_id == int(showId),
|
||||
ShiftedSeason.original_season == int(originalSeason),
|
||||
ShiftedSeason.first_episode == int(firstEpisode),
|
||||
ShiftedSeason.last_episode == int(lastEpisode),
|
||||
).first()
|
||||
|
||||
return q.first().getId() if q.count() else None
|
||||
return shiftedSeason.getId() if shiftedSeason is not None else None
|
||||
|
||||
except Exception as ex:
|
||||
raise click.ClickException(f"PatternController.findShiftedSeason(): {repr(ex)}")
|
||||
@@ -177,9 +177,7 @@ class ShiftedSeasonController():
|
||||
|
||||
try:
|
||||
s = self.Session()
|
||||
q = s.query(ShiftedSeason).filter(ShiftedSeason.id == int(shiftedSeasonId))
|
||||
|
||||
return q.first() if q.count() else None
|
||||
return s.query(ShiftedSeason).filter(ShiftedSeason.id == int(shiftedSeasonId)).first()
|
||||
|
||||
except Exception as ex:
|
||||
raise click.ClickException(f"ShiftedSeasonController.getShiftedSeason(): {repr(ex)}")
|
||||
@@ -194,13 +192,12 @@ class ShiftedSeasonController():
|
||||
|
||||
try:
|
||||
s = self.Session()
|
||||
q = s.query(ShiftedSeason).filter(ShiftedSeason.id == int(shiftedSeasonId))
|
||||
shiftedSeason = s.query(ShiftedSeason).filter(ShiftedSeason.id == int(shiftedSeasonId)).first()
|
||||
|
||||
if q.count():
|
||||
if shiftedSeason is not None:
|
||||
|
||||
#DAFUQ: https://stackoverflow.com/a/19245058
|
||||
# q.delete()
|
||||
shiftedSeason = q.first()
|
||||
s.delete(shiftedSeason)
|
||||
|
||||
s.commit()
|
||||
|
||||
@@ -16,10 +16,9 @@ class ShowController():
|
||||
|
||||
try:
|
||||
s = self.Session()
|
||||
q = s.query(Show).filter(Show.id == showId)
|
||||
show = s.query(Show).filter(Show.id == showId).first()
|
||||
|
||||
if q.count():
|
||||
show: Show = q.first()
|
||||
if show is not None:
|
||||
return show.getDescriptor(self.context)
|
||||
|
||||
except Exception as ex:
|
||||
@@ -31,9 +30,7 @@ class ShowController():
|
||||
|
||||
try:
|
||||
s = self.Session()
|
||||
q = s.query(Show).filter(Show.id == showId)
|
||||
|
||||
return q.first() if q.count() else None
|
||||
return s.query(Show).filter(Show.id == showId).first()
|
||||
|
||||
except Exception as ex:
|
||||
raise click.ClickException(f"ShowController.getShow(): {repr(ex)}")
|
||||
@@ -44,12 +41,7 @@ class ShowController():
|
||||
|
||||
try:
|
||||
s = self.Session()
|
||||
q = s.query(Show)
|
||||
|
||||
if q.count():
|
||||
return q.all()
|
||||
else:
|
||||
return []
|
||||
return s.query(Show).all()
|
||||
|
||||
except Exception as ex:
|
||||
raise click.ClickException(f"ShowController.getAllShows(): {repr(ex)}")
|
||||
@@ -61,9 +53,9 @@ class ShowController():
|
||||
|
||||
try:
|
||||
s = self.Session()
|
||||
q = s.query(Show).filter(Show.id == showDescriptor.getId())
|
||||
currentShow = s.query(Show).filter(Show.id == showDescriptor.getId()).first()
|
||||
|
||||
if not q.count():
|
||||
if currentShow is None:
|
||||
show = Show(id = int(showDescriptor.getId()),
|
||||
name = str(showDescriptor.getName()),
|
||||
year = int(showDescriptor.getYear()),
|
||||
@@ -76,9 +68,6 @@ class ShowController():
|
||||
s.commit()
|
||||
return True
|
||||
else:
|
||||
|
||||
currentShow = q.first()
|
||||
|
||||
changed = False
|
||||
if currentShow.name != str(showDescriptor.getName()):
|
||||
currentShow.name = str(showDescriptor.getName())
|
||||
@@ -113,14 +102,12 @@ class ShowController():
|
||||
def deleteShow(self, show_id):
|
||||
try:
|
||||
s = self.Session()
|
||||
q = s.query(Show).filter(Show.id == int(show_id))
|
||||
show = s.query(Show).filter(Show.id == int(show_id)).first()
|
||||
|
||||
|
||||
if q.count():
|
||||
if show is not None:
|
||||
|
||||
#DAFUQ: https://stackoverflow.com/a/19245058
|
||||
# q.delete()
|
||||
show = q.first()
|
||||
s.delete(show)
|
||||
|
||||
s.commit()
|
||||
|
||||
@@ -67,10 +67,11 @@ class TagController():
|
||||
try:
|
||||
s = self.Session()
|
||||
|
||||
q = s.query(MediaTag).filter(MediaTag.pattern_id == int(patternId),
|
||||
MediaTag.key == str(tagKey))
|
||||
if q.count():
|
||||
tag = q.first()
|
||||
tag = s.query(MediaTag).filter(
|
||||
MediaTag.pattern_id == int(patternId),
|
||||
MediaTag.key == str(tagKey),
|
||||
).first()
|
||||
if tag is not None:
|
||||
s.delete(tag)
|
||||
s.commit()
|
||||
return True
|
||||
@@ -107,12 +108,8 @@ class TagController():
|
||||
try:
|
||||
s = self.Session()
|
||||
|
||||
q = s.query(MediaTag).filter(MediaTag.pattern_id == int(patternId))
|
||||
|
||||
if q.count():
|
||||
return {t.key:t.value for t in q.all()}
|
||||
else:
|
||||
return {}
|
||||
tags = s.query(MediaTag).filter(MediaTag.pattern_id == int(patternId)).all()
|
||||
return {t.key:t.value for t in tags}
|
||||
|
||||
except Exception as ex:
|
||||
raise click.ClickException(f"TagController.findAllMediaTags(): {repr(ex)}")
|
||||
@@ -125,12 +122,8 @@ class TagController():
|
||||
try:
|
||||
s = self.Session()
|
||||
|
||||
q = s.query(TrackTag).filter(TrackTag.track_id == int(trackId))
|
||||
|
||||
if q.count():
|
||||
return {t.key:t.value for t in q.all()}
|
||||
else:
|
||||
return {}
|
||||
tags = s.query(TrackTag).filter(TrackTag.track_id == int(trackId)).all()
|
||||
return {t.key:t.value for t in tags}
|
||||
|
||||
except Exception as ex:
|
||||
raise click.ClickException(f"TagController.findAllTracks(): {repr(ex)}")
|
||||
@@ -142,12 +135,7 @@ class TagController():
|
||||
|
||||
try:
|
||||
s = self.Session()
|
||||
q = s.query(Track).filter(MediaTag.track_id == int(trackId), MediaTag.key == str(trackKey))
|
||||
|
||||
if q.count():
|
||||
return q.first()
|
||||
else:
|
||||
return None
|
||||
return s.query(Track).filter(MediaTag.track_id == int(trackId), MediaTag.key == str(trackKey)).first()
|
||||
|
||||
except Exception as ex:
|
||||
raise click.ClickException(f"TagController.findMediaTag(): {repr(ex)}")
|
||||
@@ -158,12 +146,10 @@ class TagController():
|
||||
|
||||
try:
|
||||
s = self.Session()
|
||||
q = s.query(TrackTag).filter(TrackTag.track_id == int(trackId), TrackTag.key == str(tagKey))
|
||||
|
||||
if q.count():
|
||||
return q.first()
|
||||
else:
|
||||
return None
|
||||
return s.query(TrackTag).filter(
|
||||
TrackTag.track_id == int(trackId),
|
||||
TrackTag.key == str(tagKey),
|
||||
).first()
|
||||
|
||||
except Exception as ex:
|
||||
raise click.ClickException(f"TagController.findTrackTag(): {repr(ex)}")
|
||||
@@ -175,11 +161,9 @@ class TagController():
|
||||
def deleteMediaTag(self, tagId) -> bool:
|
||||
try:
|
||||
s = self.Session()
|
||||
q = s.query(MediaTag).filter(MediaTag.id == int(tagId))
|
||||
tag = s.query(MediaTag).filter(MediaTag.id == int(tagId)).first()
|
||||
|
||||
if q.count():
|
||||
|
||||
tag = q.first()
|
||||
if tag is not None:
|
||||
|
||||
s.delete(tag)
|
||||
|
||||
@@ -201,11 +185,9 @@ class TagController():
|
||||
|
||||
try:
|
||||
s = self.Session()
|
||||
q = s.query(TrackTag).filter(TrackTag.id == int(tagId))
|
||||
tag = s.query(TrackTag).filter(TrackTag.id == int(tagId)).first()
|
||||
|
||||
if q.count():
|
||||
|
||||
tag = q.first()
|
||||
if tag is not None:
|
||||
|
||||
s.delete(tag)
|
||||
|
||||
|
||||
@@ -75,11 +75,9 @@ class TrackController():
|
||||
|
||||
try:
|
||||
s = self.Session()
|
||||
q = s.query(Track).filter(Track.id == int(trackId))
|
||||
track = s.query(Track).filter(Track.id == int(trackId)).first()
|
||||
|
||||
if q.count():
|
||||
|
||||
track : Track = q.first()
|
||||
if track is not None:
|
||||
|
||||
track.index = int(trackDescriptor.getIndex())
|
||||
|
||||
@@ -193,12 +191,10 @@ class TrackController():
|
||||
|
||||
try:
|
||||
s = self.Session()
|
||||
q = s.query(Track).filter(Track.pattern_id == int(patternId), Track.index == int(index))
|
||||
|
||||
if q.count():
|
||||
return q.first()
|
||||
else:
|
||||
return None
|
||||
return s.query(Track).filter(
|
||||
Track.pattern_id == int(patternId),
|
||||
Track.index == int(index),
|
||||
).first()
|
||||
|
||||
except Exception as ex:
|
||||
raise click.ClickException(f"TrackController.getTrack(): {repr(ex)}")
|
||||
@@ -218,11 +214,9 @@ class TrackController():
|
||||
|
||||
try:
|
||||
s = self.Session()
|
||||
q = s.query(Track).filter(Track.pattern_id == patternId, Track.index == index)
|
||||
track = s.query(Track).filter(Track.pattern_id == patternId, Track.index == index).first()
|
||||
|
||||
if q.count():
|
||||
|
||||
track : Track = q.first()
|
||||
if track is not None:
|
||||
|
||||
if state:
|
||||
track.setDisposition(disposition)
|
||||
@@ -244,10 +238,10 @@ class TrackController():
|
||||
try:
|
||||
s = self.Session()
|
||||
|
||||
q = s.query(Track).filter(Track.id == int(trackId))
|
||||
track = s.query(Track).filter(Track.id == int(trackId)).first()
|
||||
|
||||
if q.count():
|
||||
patternId = int(q.first().pattern_id)
|
||||
if track is not None:
|
||||
patternId = int(track.pattern_id)
|
||||
|
||||
q_siblings = s.query(Track).filter(Track.pattern_id == patternId).order_by(Track.index)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user