From a436dafa9c12a4c7fb1b0fa29d8ab0e7291b83ef Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Fri, 3 Jun 2022 19:47:27 -0700 Subject: [PATCH] Use worms select_one_value. --- etiquette/objects.py | 62 +++++++++++++++++----------------- etiquette/photodb.py | 22 ++++++------ utilities/database_upgrader.py | 2 +- 3 files changed, 43 insertions(+), 43 deletions(-) diff --git a/etiquette/objects.py b/etiquette/objects.py index 7de448c..e17b583 100644 --- a/etiquette/objects.py +++ b/etiquette/objects.py @@ -185,19 +185,19 @@ class GroupableMixin(metaclass=abc.ABCMeta): def has_any_child(self) -> bool: query = f'SELECT 1 FROM {self.group_table} WHERE parentid == ? LIMIT 1' - row = self.photodb.select_one(query, [self.id]) - return row is not None + exists = self.photodb.select_one_value(query, [self.id]) + return exists is not None def has_any_parent(self) -> bool: query = f'SELECT 1 FROM {self.group_table} WHERE memberid == ? LIMIT 1' - row = self.photodb.select_one(query, [self.id]) - return row is not None + exists = self.photodb.select_one_value(query, [self.id]) + return exists is not None def has_child(self, member) -> bool: self.assert_same_type(member) query = f'SELECT 1 FROM {self.group_table} WHERE parentid == ? AND memberid == ?' - row = self.photodb.select_one(query, [self.id, member.id]) - return row is not None + exists = self.photodb.select_one_value(query, [self.id, member.id]) + return exists is not None def has_descendant(self, descendant) -> bool: return self in descendant.walk_parents() @@ -205,8 +205,8 @@ class GroupableMixin(metaclass=abc.ABCMeta): def has_parent(self, parent) -> bool: self.assert_same_type(parent) query = f'SELECT 1 FROM {self.group_table} WHERE parentid == ? AND memberid == ?' - row = self.photodb.select_one(query, [parent.id, self.id]) - return row is not None + exists = self.photodb.select_one_value(query, [parent.id, self.id]) + return exists is not None def _remove_child(self, member): if not self.has_child(member): @@ -481,11 +481,11 @@ class Album(ObjectBase, GroupableMixin): ''' Return True if this album has at least 1 associated directory. ''' - row = self.photodb.select_one( + exists = self.photodb.select_one_value( 'SELECT 1 FROM album_associated_directories WHERE albumid == ?', [self.id] ) - return row is not None + return exists is not None def has_any_photo(self, recurse=False) -> bool: ''' @@ -495,11 +495,11 @@ class Album(ObjectBase, GroupableMixin): If True, photos in child albums satisfy. If False, only consider this album. ''' - row = self.photodb.select_one( + exists = self.photodb.select_one_value( 'SELECT 1 FROM album_photo_rel WHERE albumid == ? LIMIT 1', [self.id] ) - if row is not None: + if exists is not None: return True if recurse: return self.has_any_subalbum_photo() @@ -514,18 +514,18 @@ class Album(ObjectBase, GroupableMixin): def has_associated_directory(self, path) -> bool: path = pathclass.Path(path) - row = self.photodb.select_one( + exists = self.photodb.select_one_value( 'SELECT 1 FROM album_associated_directories WHERE albumid == ? AND directory == ?', [self.id, path.absolute_path] ) - return row is not None + return exists is not None def has_photo(self, photo) -> bool: - row = self.photodb.select_one( + exists = self.photodb.select_one_value( 'SELECT 1 FROM album_photo_rel WHERE albumid == ? AND photoid == ?', [self.id, photo.id] ) - return row is not None + return exists is not None def jsonify(self, minimal=False) -> dict: j = { @@ -623,7 +623,7 @@ class Album(ObjectBase, GroupableMixin): albumids = sqlhelpers.listify(albumids) query = query.format(albumids=albumids) - total = self.photodb.select_one(query)[0] + total = self.photodb.select_one_value(query) return total def sum_children(self, recurse=True) -> int: @@ -645,7 +645,7 @@ class Album(ObjectBase, GroupableMixin): WHERE parentid == ? ''') bindings = [self.id] - total = self.photodb.select_one(query, bindings)[0] + total = self.photodb.select_one_value(query, bindings) return total def sum_photos(self, recurse=True) -> int: @@ -667,7 +667,7 @@ class Album(ObjectBase, GroupableMixin): albumids = sqlhelpers.listify(albumids) query = query.format(albumids=albumids) - total = self.photodb.select_one(query)[0] + total = self.photodb.select_one_value(query) return total # Will add -> Photo when forward references are supported by Python. @@ -1091,15 +1091,15 @@ class Photo(ObjectBase): tag_by_id = {t.id: t for t in tag_options} tag_option_ids = sqlhelpers.listify(tag_by_id) - rel_row = self.photodb.select_one( + tag_id = self.photodb.select_one_value( f'SELECT tagid FROM photo_tag_rel WHERE photoid == ? AND tagid IN {tag_option_ids}', [self.id] ) - if rel_row is None: + if tag_id is None: return False - return tag_by_id[rel_row[0]] + return tag_by_id[tag_id] def jsonify(self, include_albums=True, include_tags=True) -> dict: j = { @@ -1755,7 +1755,7 @@ class Tag(ObjectBase, GroupableMixin): if synname == self.name: raise exceptions.NoSuchSynonym(synname) - syn_exists = self.photodb.select_one( + syn_exists = self.photodb.select_one_value( 'SELECT 1 FROM tag_synonyms WHERE mastername == ? AND name == ?', [self.name, synname] ) @@ -1949,23 +1949,23 @@ class User(ObjectBase): def has_any_albums(self) -> bool: query = f'SELECT 1 FROM albums WHERE author_id == ? LIMIT 1' - row = self.photodb.select_one(query, [self.id]) - return row is not None + exists = self.photodb.select_one_value(query, [self.id]) + return exists is not None def has_any_bookmarks(self) -> bool: query = f'SELECT 1 FROM bookmarks WHERE author_id == ? LIMIT 1' - row = self.photodb.select_one(query, [self.id]) - return row is not None + exists = self.photodb.select_one_value(query, [self.id]) + return exists is not None def has_any_photos(self) -> bool: query = f'SELECT 1 FROM photos WHERE author_id == ? LIMIT 1' - row = self.photodb.select_one(query, [self.id]) - return row is not None + exists = self.photodb.select_one_value(query, [self.id]) + return exists is not None def has_any_tags(self) -> bool: query = f'SELECT 1 FROM tags WHERE author_id == ? LIMIT 1' - row = self.photodb.select_one(query, [self.id]) - return row is not None + exists = self.photodb.select_one_value(query, [self.id]) + return exists is not None def jsonify(self) -> dict: j = { diff --git a/etiquette/photodb.py b/etiquette/photodb.py index 5b86f69..9426ba4 100644 --- a/etiquette/photodb.py +++ b/etiquette/photodb.py @@ -40,7 +40,7 @@ class PDBAlbumMixin: return self.get_object_by_id(objects.Album, id) def get_album_count(self) -> int: - return self.select_one('SELECT COUNT(id) FROM albums')[0] + return self.select_one_value('SELECT COUNT(id) FROM albums') def get_albums(self) -> typing.Iterable[objects.Album]: return self.get_objects(objects.Album) @@ -176,7 +176,7 @@ class PDBBookmarkMixin: return self.get_object_by_id(objects.Bookmark, id) def get_bookmark_count(self) -> int: - return self.select_one('SELECT COUNT(id) FROM bookmarks')[0] + return self.select_one_value('SELECT COUNT(id) FROM bookmarks') def get_bookmarks(self) -> typing.Iterable[objects.Bookmark]: return self.get_objects(objects.Bookmark) @@ -266,7 +266,7 @@ class PDBPhotoMixin: return photo def get_photo_count(self) -> int: - return self.select_one('SELECT COUNT(id) FROM photos')[0] + return self.select_one_value('SELECT COUNT(id) FROM photos') def get_photos(self) -> typing.Iterable[objects.Photo]: return self.get_objects(objects.Photo) @@ -931,17 +931,17 @@ class PDBTagMixin: # ...or resolve the synonym and try again. query = 'SELECT mastername FROM tag_synonyms WHERE name == ?' bindings = [tagname] - name_row = self.select_one(query, bindings) - if name_row is None: + mastername = self.select_one_value(query, bindings) + if mastername is None: # was not a master tag or synonym raise exceptions.NoSuchTag(tagname) - tagname = name_row[0] + tagname = mastername tag = self.get_cached_instance(objects.Tag, tag_row) return tag def get_tag_count(self) -> int: - return self.select_one('SELECT COUNT(id) FROM tags')[0] + return self.select_one_value('SELECT COUNT(id) FROM tags') def get_tags(self) -> typing.Iterable[objects.Tag]: ''' @@ -1046,7 +1046,7 @@ class PDBUserMixin: for retry in range(20): user_id = ''.join(random.choices(constants.USER_ID_CHARACTERS, k=length)) - user_exists = self.select_one('SELECT 1 FROM users WHERE id == ?', [user_id]) + user_exists = self.select_one_value('SELECT 1 FROM users WHERE id == ?', [user_id]) if user_exists is None: break else: @@ -1078,7 +1078,7 @@ class PDBUserMixin: return self.get_cached_instance(objects.User, user_row) def get_user_count(self) -> int: - return self.select_one('SELECT COUNT(id) FROM users')[0] + return self.select_one_value('SELECT COUNT(id) FROM users') def get_user_id_or_none(self, user_obj_or_id) -> typing.Optional[str]: ''' @@ -1690,14 +1690,14 @@ class PhotoDB( if table not in ['photos', 'tags', 'albums', 'bookmarks']: raise ValueError(f'Invalid table requested: {table}.') - last_id = self.select_one('SELECT last_id FROM id_numbers WHERE tab == ?', [table]) + last_id = self.select_one_value('SELECT last_id FROM id_numbers WHERE tab == ?', [table]) if last_id is None: # Register new value new_id_int = 1 do_insert = True else: # Use database value - new_id_int = int(last_id[0]) + 1 + new_id_int = int(last_id) + 1 do_insert = False new_id = str(new_id_int).rjust(self.config['id_length'], '0') diff --git a/utilities/database_upgrader.py b/utilities/database_upgrader.py index 4353bc9..6d73f11 100644 --- a/utilities/database_upgrader.py +++ b/utilities/database_upgrader.py @@ -144,7 +144,7 @@ def upgrade_5_to_6(photodb): # 1. Start the id_numbers.albums value at the tags value so that the number # can continue to increment safely and separately, instead of starting at # zero and bumping into existing albums. - last_id = photodb.select_one('SELECT last_id FROM id_numbers WHERE tab == "tags"')[0] + last_id = photodb.select_one_value('SELECT last_id FROM id_numbers WHERE tab == "tags"') photodb.execute('INSERT INTO id_numbers VALUES("albums", ?)', [last_id]) # 2. Now's a good chance to rename 'index_grouprel' to 'index_taggroup'.