Use worms select_one_value.
This commit is contained in:
parent
a39e600619
commit
a436dafa9c
3 changed files with 43 additions and 43 deletions
|
@ -185,19 +185,19 @@ class GroupableMixin(metaclass=abc.ABCMeta):
|
||||||
|
|
||||||
def has_any_child(self) -> bool:
|
def has_any_child(self) -> bool:
|
||||||
query = f'SELECT 1 FROM {self.group_table} WHERE parentid == ? LIMIT 1'
|
query = f'SELECT 1 FROM {self.group_table} WHERE parentid == ? LIMIT 1'
|
||||||
row = self.photodb.select_one(query, [self.id])
|
exists = self.photodb.select_one_value(query, [self.id])
|
||||||
return row is not None
|
return exists is not None
|
||||||
|
|
||||||
def has_any_parent(self) -> bool:
|
def has_any_parent(self) -> bool:
|
||||||
query = f'SELECT 1 FROM {self.group_table} WHERE memberid == ? LIMIT 1'
|
query = f'SELECT 1 FROM {self.group_table} WHERE memberid == ? LIMIT 1'
|
||||||
row = self.photodb.select_one(query, [self.id])
|
exists = self.photodb.select_one_value(query, [self.id])
|
||||||
return row is not None
|
return exists is not None
|
||||||
|
|
||||||
def has_child(self, member) -> bool:
|
def has_child(self, member) -> bool:
|
||||||
self.assert_same_type(member)
|
self.assert_same_type(member)
|
||||||
query = f'SELECT 1 FROM {self.group_table} WHERE parentid == ? AND memberid == ?'
|
query = f'SELECT 1 FROM {self.group_table} WHERE parentid == ? AND memberid == ?'
|
||||||
row = self.photodb.select_one(query, [self.id, member.id])
|
exists = self.photodb.select_one_value(query, [self.id, member.id])
|
||||||
return row is not None
|
return exists is not None
|
||||||
|
|
||||||
def has_descendant(self, descendant) -> bool:
|
def has_descendant(self, descendant) -> bool:
|
||||||
return self in descendant.walk_parents()
|
return self in descendant.walk_parents()
|
||||||
|
@ -205,8 +205,8 @@ class GroupableMixin(metaclass=abc.ABCMeta):
|
||||||
def has_parent(self, parent) -> bool:
|
def has_parent(self, parent) -> bool:
|
||||||
self.assert_same_type(parent)
|
self.assert_same_type(parent)
|
||||||
query = f'SELECT 1 FROM {self.group_table} WHERE parentid == ? AND memberid == ?'
|
query = f'SELECT 1 FROM {self.group_table} WHERE parentid == ? AND memberid == ?'
|
||||||
row = self.photodb.select_one(query, [parent.id, self.id])
|
exists = self.photodb.select_one_value(query, [parent.id, self.id])
|
||||||
return row is not None
|
return exists is not None
|
||||||
|
|
||||||
def _remove_child(self, member):
|
def _remove_child(self, member):
|
||||||
if not self.has_child(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.
|
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 == ?',
|
'SELECT 1 FROM album_associated_directories WHERE albumid == ?',
|
||||||
[self.id]
|
[self.id]
|
||||||
)
|
)
|
||||||
return row is not None
|
return exists is not None
|
||||||
|
|
||||||
def has_any_photo(self, recurse=False) -> bool:
|
def has_any_photo(self, recurse=False) -> bool:
|
||||||
'''
|
'''
|
||||||
|
@ -495,11 +495,11 @@ class Album(ObjectBase, GroupableMixin):
|
||||||
If True, photos in child albums satisfy.
|
If True, photos in child albums satisfy.
|
||||||
If False, only consider this album.
|
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',
|
'SELECT 1 FROM album_photo_rel WHERE albumid == ? LIMIT 1',
|
||||||
[self.id]
|
[self.id]
|
||||||
)
|
)
|
||||||
if row is not None:
|
if exists is not None:
|
||||||
return True
|
return True
|
||||||
if recurse:
|
if recurse:
|
||||||
return self.has_any_subalbum_photo()
|
return self.has_any_subalbum_photo()
|
||||||
|
@ -514,18 +514,18 @@ class Album(ObjectBase, GroupableMixin):
|
||||||
|
|
||||||
def has_associated_directory(self, path) -> bool:
|
def has_associated_directory(self, path) -> bool:
|
||||||
path = pathclass.Path(path)
|
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 == ?',
|
'SELECT 1 FROM album_associated_directories WHERE albumid == ? AND directory == ?',
|
||||||
[self.id, path.absolute_path]
|
[self.id, path.absolute_path]
|
||||||
)
|
)
|
||||||
return row is not None
|
return exists is not None
|
||||||
|
|
||||||
def has_photo(self, photo) -> bool:
|
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 == ?',
|
'SELECT 1 FROM album_photo_rel WHERE albumid == ? AND photoid == ?',
|
||||||
[self.id, photo.id]
|
[self.id, photo.id]
|
||||||
)
|
)
|
||||||
return row is not None
|
return exists is not None
|
||||||
|
|
||||||
def jsonify(self, minimal=False) -> dict:
|
def jsonify(self, minimal=False) -> dict:
|
||||||
j = {
|
j = {
|
||||||
|
@ -623,7 +623,7 @@ class Album(ObjectBase, GroupableMixin):
|
||||||
|
|
||||||
albumids = sqlhelpers.listify(albumids)
|
albumids = sqlhelpers.listify(albumids)
|
||||||
query = query.format(albumids=albumids)
|
query = query.format(albumids=albumids)
|
||||||
total = self.photodb.select_one(query)[0]
|
total = self.photodb.select_one_value(query)
|
||||||
return total
|
return total
|
||||||
|
|
||||||
def sum_children(self, recurse=True) -> int:
|
def sum_children(self, recurse=True) -> int:
|
||||||
|
@ -645,7 +645,7 @@ class Album(ObjectBase, GroupableMixin):
|
||||||
WHERE parentid == ?
|
WHERE parentid == ?
|
||||||
''')
|
''')
|
||||||
bindings = [self.id]
|
bindings = [self.id]
|
||||||
total = self.photodb.select_one(query, bindings)[0]
|
total = self.photodb.select_one_value(query, bindings)
|
||||||
return total
|
return total
|
||||||
|
|
||||||
def sum_photos(self, recurse=True) -> int:
|
def sum_photos(self, recurse=True) -> int:
|
||||||
|
@ -667,7 +667,7 @@ class Album(ObjectBase, GroupableMixin):
|
||||||
|
|
||||||
albumids = sqlhelpers.listify(albumids)
|
albumids = sqlhelpers.listify(albumids)
|
||||||
query = query.format(albumids=albumids)
|
query = query.format(albumids=albumids)
|
||||||
total = self.photodb.select_one(query)[0]
|
total = self.photodb.select_one_value(query)
|
||||||
return total
|
return total
|
||||||
|
|
||||||
# Will add -> Photo when forward references are supported by Python.
|
# 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_by_id = {t.id: t for t in tag_options}
|
||||||
tag_option_ids = sqlhelpers.listify(tag_by_id)
|
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}',
|
f'SELECT tagid FROM photo_tag_rel WHERE photoid == ? AND tagid IN {tag_option_ids}',
|
||||||
[self.id]
|
[self.id]
|
||||||
)
|
)
|
||||||
|
|
||||||
if rel_row is None:
|
if tag_id is None:
|
||||||
return False
|
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:
|
def jsonify(self, include_albums=True, include_tags=True) -> dict:
|
||||||
j = {
|
j = {
|
||||||
|
@ -1755,7 +1755,7 @@ class Tag(ObjectBase, GroupableMixin):
|
||||||
if synname == self.name:
|
if synname == self.name:
|
||||||
raise exceptions.NoSuchSynonym(synname)
|
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 == ?',
|
'SELECT 1 FROM tag_synonyms WHERE mastername == ? AND name == ?',
|
||||||
[self.name, synname]
|
[self.name, synname]
|
||||||
)
|
)
|
||||||
|
@ -1949,23 +1949,23 @@ class User(ObjectBase):
|
||||||
|
|
||||||
def has_any_albums(self) -> bool:
|
def has_any_albums(self) -> bool:
|
||||||
query = f'SELECT 1 FROM albums WHERE author_id == ? LIMIT 1'
|
query = f'SELECT 1 FROM albums WHERE author_id == ? LIMIT 1'
|
||||||
row = self.photodb.select_one(query, [self.id])
|
exists = self.photodb.select_one_value(query, [self.id])
|
||||||
return row is not None
|
return exists is not None
|
||||||
|
|
||||||
def has_any_bookmarks(self) -> bool:
|
def has_any_bookmarks(self) -> bool:
|
||||||
query = f'SELECT 1 FROM bookmarks WHERE author_id == ? LIMIT 1'
|
query = f'SELECT 1 FROM bookmarks WHERE author_id == ? LIMIT 1'
|
||||||
row = self.photodb.select_one(query, [self.id])
|
exists = self.photodb.select_one_value(query, [self.id])
|
||||||
return row is not None
|
return exists is not None
|
||||||
|
|
||||||
def has_any_photos(self) -> bool:
|
def has_any_photos(self) -> bool:
|
||||||
query = f'SELECT 1 FROM photos WHERE author_id == ? LIMIT 1'
|
query = f'SELECT 1 FROM photos WHERE author_id == ? LIMIT 1'
|
||||||
row = self.photodb.select_one(query, [self.id])
|
exists = self.photodb.select_one_value(query, [self.id])
|
||||||
return row is not None
|
return exists is not None
|
||||||
|
|
||||||
def has_any_tags(self) -> bool:
|
def has_any_tags(self) -> bool:
|
||||||
query = f'SELECT 1 FROM tags WHERE author_id == ? LIMIT 1'
|
query = f'SELECT 1 FROM tags WHERE author_id == ? LIMIT 1'
|
||||||
row = self.photodb.select_one(query, [self.id])
|
exists = self.photodb.select_one_value(query, [self.id])
|
||||||
return row is not None
|
return exists is not None
|
||||||
|
|
||||||
def jsonify(self) -> dict:
|
def jsonify(self) -> dict:
|
||||||
j = {
|
j = {
|
||||||
|
|
|
@ -40,7 +40,7 @@ class PDBAlbumMixin:
|
||||||
return self.get_object_by_id(objects.Album, id)
|
return self.get_object_by_id(objects.Album, id)
|
||||||
|
|
||||||
def get_album_count(self) -> int:
|
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]:
|
def get_albums(self) -> typing.Iterable[objects.Album]:
|
||||||
return self.get_objects(objects.Album)
|
return self.get_objects(objects.Album)
|
||||||
|
@ -176,7 +176,7 @@ class PDBBookmarkMixin:
|
||||||
return self.get_object_by_id(objects.Bookmark, id)
|
return self.get_object_by_id(objects.Bookmark, id)
|
||||||
|
|
||||||
def get_bookmark_count(self) -> int:
|
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]:
|
def get_bookmarks(self) -> typing.Iterable[objects.Bookmark]:
|
||||||
return self.get_objects(objects.Bookmark)
|
return self.get_objects(objects.Bookmark)
|
||||||
|
@ -266,7 +266,7 @@ class PDBPhotoMixin:
|
||||||
return photo
|
return photo
|
||||||
|
|
||||||
def get_photo_count(self) -> int:
|
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]:
|
def get_photos(self) -> typing.Iterable[objects.Photo]:
|
||||||
return self.get_objects(objects.Photo)
|
return self.get_objects(objects.Photo)
|
||||||
|
@ -931,17 +931,17 @@ class PDBTagMixin:
|
||||||
# ...or resolve the synonym and try again.
|
# ...or resolve the synonym and try again.
|
||||||
query = 'SELECT mastername FROM tag_synonyms WHERE name == ?'
|
query = 'SELECT mastername FROM tag_synonyms WHERE name == ?'
|
||||||
bindings = [tagname]
|
bindings = [tagname]
|
||||||
name_row = self.select_one(query, bindings)
|
mastername = self.select_one_value(query, bindings)
|
||||||
if name_row is None:
|
if mastername is None:
|
||||||
# was not a master tag or synonym
|
# was not a master tag or synonym
|
||||||
raise exceptions.NoSuchTag(tagname)
|
raise exceptions.NoSuchTag(tagname)
|
||||||
tagname = name_row[0]
|
tagname = mastername
|
||||||
|
|
||||||
tag = self.get_cached_instance(objects.Tag, tag_row)
|
tag = self.get_cached_instance(objects.Tag, tag_row)
|
||||||
return tag
|
return tag
|
||||||
|
|
||||||
def get_tag_count(self) -> int:
|
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]:
|
def get_tags(self) -> typing.Iterable[objects.Tag]:
|
||||||
'''
|
'''
|
||||||
|
@ -1046,7 +1046,7 @@ class PDBUserMixin:
|
||||||
for retry in range(20):
|
for retry in range(20):
|
||||||
user_id = ''.join(random.choices(constants.USER_ID_CHARACTERS, k=length))
|
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:
|
if user_exists is None:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
@ -1078,7 +1078,7 @@ class PDBUserMixin:
|
||||||
return self.get_cached_instance(objects.User, user_row)
|
return self.get_cached_instance(objects.User, user_row)
|
||||||
|
|
||||||
def get_user_count(self) -> int:
|
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]:
|
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']:
|
if table not in ['photos', 'tags', 'albums', 'bookmarks']:
|
||||||
raise ValueError(f'Invalid table requested: {table}.')
|
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:
|
if last_id is None:
|
||||||
# Register new value
|
# Register new value
|
||||||
new_id_int = 1
|
new_id_int = 1
|
||||||
do_insert = True
|
do_insert = True
|
||||||
else:
|
else:
|
||||||
# Use database value
|
# Use database value
|
||||||
new_id_int = int(last_id[0]) + 1
|
new_id_int = int(last_id) + 1
|
||||||
do_insert = False
|
do_insert = False
|
||||||
|
|
||||||
new_id = str(new_id_int).rjust(self.config['id_length'], '0')
|
new_id = str(new_id_int).rjust(self.config['id_length'], '0')
|
||||||
|
|
|
@ -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
|
# 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
|
# can continue to increment safely and separately, instead of starting at
|
||||||
# zero and bumping into existing albums.
|
# 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])
|
photodb.execute('INSERT INTO id_numbers VALUES("albums", ?)', [last_id])
|
||||||
|
|
||||||
# 2. Now's a good chance to rename 'index_grouprel' to 'index_taggroup'.
|
# 2. Now's a good chance to rename 'index_grouprel' to 'index_taggroup'.
|
||||||
|
|
Loading…
Reference in a new issue