Resort to the expensive database lookup only if the person exists in the

database.

The database is configured such that each person name within the table
has to be unique. Attempting to add a person which already exists causes
an IntegrityError exception to be thrown. This is a less costly
operation than the cursor.execute and cursor.fetch operations.

Increase performance by reverting to the expensive calls of fetching if
adding a person fails.

This pattern could also be applied to other tables within the database.
This commit is contained in:
Chuddah 2020-02-18 00:45:16 +00:00
commit bdd9a8f727

View file

@ -8,6 +8,7 @@ import logging
from . import artwork
from . import queries as QU
from helper import values
from sqlite3 import IntegrityError
##################################################################################################
@ -156,13 +157,13 @@ class Kodi(object):
return person_id
def get_person(self, *args):
try:
self.cursor.execute(QU.get_person, args)
return self.cursor.fetchone()[0]
except TypeError:
return self.add_person(*args)
except IntegrityError:
# The person already exists in the database
# Now we can do the expensive operation of fetching the id
self.cursor.execute(QU.get_person, args)
return self.cursor.fetchone()[0]
def add_genres(self, genres, *args):