Replace a lot of manual insert/update with helper.

This commit is contained in:
voussoir 2018-02-16 20:19:18 -08:00
parent 3e5031a1c5
commit a8ead9c0c9
2 changed files with 140 additions and 130 deletions

View file

@ -65,13 +65,10 @@ class GroupableMixin:
# Groupables are only allowed to have 1 parent. # Groupables are only allowed to have 1 parent.
# Unlike photos which can exist in multiple albums. # Unlike photos which can exist in multiple albums.
cur = self.photodb.sql.cursor() cur = self.photodb.sql.cursor()
cur.execute( cur.execute('SELECT parentid FROM %s WHERE memberid == ?' % self.group_table, [member.id])
'SELECT * FROM %s WHERE memberid == ?' % self.group_table,
[member.id]
)
fetch = cur.fetchone() fetch = cur.fetchone()
if fetch is not None: if fetch is not None:
parent_id = fetch[self.group_sql_index['parentid']] parent_id = fetch[0]
if parent_id == self.id: if parent_id == self.id:
return return
that_group = self.group_getter(id=parent_id) that_group = self.group_getter(id=parent_id)
@ -81,26 +78,26 @@ class GroupableMixin:
if my_ancestor == member: if my_ancestor == member:
raise exceptions.RecursiveGrouping(member=member, group=self) raise exceptions.RecursiveGrouping(member=member, group=self)
data = {
'parentid': self.id,
'memberid': member.id,
}
self.photodb.sql_insert(table=self.group_table, data=data, commit=False)
self.photodb._cached_frozen_children = None self.photodb._cached_frozen_children = None
cur.execute(
'INSERT INTO %s VALUES(?, ?)' % self.group_table,
[self.id, member.id]
)
if commit: if commit:
self.photodb.log.debug('Committing - add to group') self.photodb.log.debug('Committing - add to group')
self.photodb.commit() self.photodb.commit()
def children(self): def children(self):
cur = self.photodb.sql.cursor() cur = self.photodb.sql.cursor()
cur.execute( cur.execute('SELECT memberid FROM %s WHERE parentid == ?' % self.group_table, [self.id])
'SELECT * FROM %s WHERE parentid == ?' % self.group_table, fetches = cur.fetchall()
[self.id]
)
fetch = cur.fetchall()
results = [] results = []
for f in fetch: for fetch in fetches:
memberid = f[self.group_sql_index['memberid']] memberid = fetch[0]
child = self.group_getter(id=memberid) child = self.group_getter(id=memberid)
results.append(child) results.append(child)
if isinstance(self, Tag): if isinstance(self, Tag):
@ -284,10 +281,7 @@ class Album(ObjectBase, GroupableMixin):
'albumid': self.id, 'albumid': self.id,
'directory': filepath.absolute_path, 'directory': filepath.absolute_path,
} }
(qmarks, bindings) = sqlhelpers.insert_filler(constants.SQL_ALBUM_DIRECTORY_COLUMNS, data) self.photodb.sql_insert(table='album_associated_directories', data=data, commit=False)
query = 'INSERT INTO album_associated_directories VALUES(%s)' % qmarks
cur = self.photodb.sql.cursor()
cur.execute(query, bindings)
if commit: if commit:
self.photodb.log.debug('Committing - add associated directory') self.photodb.log.debug('Committing - add associated directory')
@ -302,8 +296,12 @@ class Album(ObjectBase, GroupableMixin):
return return
self.photodb.log.debug('Adding photo %s to %s', photo, self) self.photodb.log.debug('Adding photo %s to %s', photo, self)
cur = self.photodb.sql.cursor() data = {
cur.execute('INSERT INTO album_photo_rel VALUES(?, ?)', [self.id, photo.id]) 'albumid': self.id,
'photoid': photo.id,
}
self.photodb.sql_insert(table='album_photo_rel', data=data, commit=False)
self._uncache_sums() self._uncache_sums()
if commit: if commit:
self.photodb.log.debug('Committing - add photo to album') self.photodb.log.debug('Committing - add photo to album')
@ -370,17 +368,21 @@ class Album(ObjectBase, GroupableMixin):
''' '''
Change the title or description. Leave None to keep current value. Change the title or description. Leave None to keep current value.
''' '''
if title is None: if title is None and description is None:
title = self.title return
if description is None:
description = self.description if title is not None:
cur = self.photodb.sql.cursor() self.title = title
cur.execute(
'UPDATE albums SET title=?, description=? WHERE id == ?', if description is not None:
[title, description, self.id] self.description = description
)
self.title = title data = {
self.description = description 'id': self.id,
'title': self.title,
'description': self.description,
}
self.photodb.sql_update(table='albums', pairs=data, where_key='id', commit=False)
if commit: if commit:
self.photodb.log.debug('Committing - edit album') self.photodb.log.debug('Committing - edit album')
@ -413,13 +415,10 @@ class Album(ObjectBase, GroupableMixin):
photos = [] photos = []
generator = helpers.select_generator( generator = helpers.select_generator(
self.photodb.sql, self.photodb.sql,
'SELECT * FROM album_photo_rel WHERE albumid == ?', 'SELECT photoid FROM album_photo_rel WHERE albumid == ?',
[self.id] [self.id]
) )
for photo in generator: photos = [self.photodb.get_photo(id=fetch[0]) for fetch in generator]
photoid = photo[constants.SQL_ALBUMPHOTO['photoid']]
photo = self.photodb.get_photo(photoid)
photos.append(photo)
photos.sort(key=lambda x: x.basename.lower()) photos.sort(key=lambda x: x.basename.lower())
return photos return photos
@ -517,11 +516,13 @@ class Bookmark(ObjectBase):
raise ValueError('Need a URL') raise ValueError('Need a URL')
self.url = url self.url = url
cur = self.photodb.sql.cursor() data = {
cur.execute( 'id': self.id,
'UPDATE bookmarks SET title = ?, url = ? WHERE id == ?', 'title': self.title,
[self.title, self.url, self.id] 'url': self.url,
) }
self.photodb.sql_update(table='bookmarks', pairs=data, where_key='id', commit=False)
if commit: if commit:
self.photodb.log.debug('Committing - edit bookmark') self.photodb.log.debug('Committing - edit bookmark')
self.photodb.commit() self.photodb.commit()
@ -613,10 +614,18 @@ class Photo(ObjectBase):
self.remove_tag(parent) self.remove_tag(parent)
self.photodb.log.debug('Applying tag {tag:s} to photo {pho:s}'.format(tag=tag, pho=self)) self.photodb.log.debug('Applying tag {tag:s} to photo {pho:s}'.format(tag=tag, pho=self))
now = helpers.now()
cur = self.photodb.sql.cursor() data = {
cur.execute('INSERT INTO photo_tag_rel VALUES(?, ?)', [self.id, tag.id]) 'photoid': self.id,
cur.execute('UPDATE photos SET tagged_at = ? WHERE id == ?', [now, self.id]) 'tagid': tag.id
}
self.photodb.sql_insert(table='photo_tag_rel', data=data, commit=False)
data = {
'id': self.id,
'tagged_at': helpers.now(),
}
self.photodb.sql_update(table='photos', data=data, where_key='id', commit=False)
if commit: if commit:
self.photodb.log.debug('Committing - add photo tag') self.photodb.log.debug('Committing - add photo tag')
self.photodb.commit() self.photodb.commit()
@ -628,8 +637,8 @@ class Photo(ObjectBase):
''' '''
cur = self.photodb.sql.cursor() cur = self.photodb.sql.cursor()
cur.execute('SELECT albumid FROM album_photo_rel WHERE photoid == ?', [self.id]) cur.execute('SELECT albumid FROM album_photo_rel WHERE photoid == ?', [self.id])
fetch = cur.fetchall() fetches = cur.fetchall()
albums = [self.photodb.get_album(f[0]) for f in fetch] albums = [self.photodb.get_album(id=fetch[0]) for fetch in fetches]
return albums return albums
@property @property
@ -764,11 +773,11 @@ class Photo(ObjectBase):
if return_filepath != self.thumbnail: if return_filepath != self.thumbnail:
cur = self.photodb.sql.cursor() data = {
cur.execute( 'id': self.id,
'UPDATE photos SET thumbnail = ? WHERE id == ?', 'thumbnail': return_filepath,
[return_filepath, self.id] }
) self.photodb.sql_update(table='photos', pairs=data, where_key='id', commit=False)
self.thumbnail = return_filepath self.thumbnail = return_filepath
self._uncache() self._uncache()
@ -810,8 +819,7 @@ class Photo(ObjectBase):
Create the filepath that should be the location of our thumbnail. Create the filepath that should be the location of our thumbnail.
''' '''
chunked_id = helpers.chunk_sequence(self.id, 3) chunked_id = helpers.chunk_sequence(self.id, 3)
basename = chunked_id[-1] (folder, basename) = (chunked_id[:-1], chunked_id[-1])
folder = chunked_id[:-1]
folder = os.sep.join(folder) folder = os.sep.join(folder)
folder = self.photodb.thumbnail_directory.join(folder) folder = self.photodb.thumbnail_directory.join(folder)
if folder: if folder:
@ -867,11 +875,17 @@ class Photo(ObjectBase):
self.area = self.width * self.height self.area = self.width * self.height
self.ratio = round(self.width / self.height, 2) self.ratio = round(self.width / self.height, 2)
cur = self.photodb.sql.cursor() data = {
cur.execute( 'id': self.id,
'UPDATE photos SET width=?, height=?, area=?, ratio=?, duration=?, bytes=? WHERE id==?', 'width': self.width,
[self.width, self.height, self.area, self.ratio, self.duration, self.bytes, self.id], 'height': self.height,
) 'area': self.area,
'ratio': self.ratio,
'duration': self.duration,
'bytes': self.bytes,
}
self.photodb.sql_update(table='photos', pairs=data, where_key='id', commit=False)
self._uncache() self._uncache()
if commit: if commit:
self.photodb.log.debug('Committing - reload metadata') self.photodb.log.debug('Committing - reload metadata')
@ -893,7 +907,7 @@ class Photo(ObjectBase):
new_filepath = pathclass.Path(new_filepath) new_filepath = pathclass.Path(new_filepath)
if not new_filepath.is_file: if not new_filepath.is_file:
raise FileNotFoundError(new_filepath.absolute_path) raise FileNotFoundError(new_filepath.absolute_path)
cur = self.photodb.sql.cursor()
if not allow_duplicates: if not allow_duplicates:
try: try:
existing = self.photodb.get_photo_by_path(new_filepath) existing = self.photodb.get_photo_by_path(new_filepath)
@ -902,10 +916,13 @@ class Photo(ObjectBase):
pass pass
else: else:
raise exceptions.PhotoExists(existing) raise exceptions.PhotoExists(existing)
cur.execute(
'UPDATE photos SET filepath = ? WHERE id == ?', data = {
[new_filepath.absolute_path, self.id] 'id': self.id,
) 'filepath': new_filepath.absolute_path,
}
self.photodb.sql_update(table='photos', pairs=data, where_key='id', commit=False)
self._uncache() self._uncache()
if commit: if commit:
self.photodb.log.debug('Committing - relocate photo') self.photodb.log.debug('Committing - relocate photo')
@ -925,8 +942,13 @@ class Photo(ObjectBase):
'DELETE FROM photo_tag_rel WHERE photoid == ? AND tagid == ?', 'DELETE FROM photo_tag_rel WHERE photoid == ? AND tagid == ?',
[self.id, tag.id] [self.id, tag.id]
) )
now = helpers.now()
cur.execute('UPDATE photos SET tagged_at = ? WHERE id == ?', [now, self.id]) data = {
'id': self.id,
'tagged_at': helpers.now(),
}
self.photodb.sql_update(table='photos', pairs=data, where_key='id', commit=False)
if commit: if commit:
self.photodb.log.debug('Committing - remove photo tag') self.photodb.log.debug('Committing - remove photo tag')
self.photodb.commit() self.photodb.commit()
@ -1006,16 +1028,12 @@ class Photo(ObjectBase):
''' '''
Return the tags assigned to this Photo. Return the tags assigned to this Photo.
''' '''
tags = []
generator = helpers.select_generator( generator = helpers.select_generator(
self.photodb.sql, self.photodb.sql,
'SELECT * FROM photo_tag_rel WHERE photoid == ?', 'SELECT tagid FROM photo_tag_rel WHERE photoid == ?',
[self.id] [self.id]
) )
for tag in generator: tags = [self.photodb.get_tag(id=fetch[0]) for fetch in generator]
tagid = tag[constants.SQL_PHOTOTAG['tagid']]
tag = self.photodb.get_tag(id=tagid)
tags.append(tag)
return tags return tags
@ -1076,8 +1094,12 @@ class Tag(ObjectBase, GroupableMixin):
self.log.debug('New synonym %s of %s', synname, self.name) self.log.debug('New synonym %s of %s', synname, self.name)
self.photodb._cached_frozen_children = None self.photodb._cached_frozen_children = None
cur = self.photodb.sql.cursor()
cur.execute('INSERT INTO tag_synonyms VALUES(?, ?)', [synname, self.name]) data = {
'name': synname,
'mastername': self.name,
}
self.photodb.sql_insert(table='tag_synonyms', data=data, commit=False)
if commit: if commit:
self.photodb.log.debug('Committing - add synonym') self.photodb.log.debug('Committing - add synonym')
@ -1109,22 +1131,21 @@ class Tag(ObjectBase, GroupableMixin):
# Iterate over all photos with the old tag, and swap them to the new tag # Iterate over all photos with the old tag, and swap them to the new tag
# if they don't already have it. # if they don't already have it.
generator = helpers.select_generator( cur.execute('SELECT photoid FROM photo_tag_rel WHERE tagid == ?', [self.id])
self.photodb.sql, fetches = cur.fetchall()
'SELECT * FROM photo_tag_rel WHERE tagid == ?',
[self.id] for fetch in fetches:
) photoid = fetch[0]
for relationship in generator:
photoid = relationship[constants.SQL_PHOTOTAG['photoid']]
cur.execute( cur.execute(
'SELECT * FROM photo_tag_rel WHERE photoid == ? AND tagid == ?', 'SELECT * FROM photo_tag_rel WHERE photoid == ? AND tagid == ?',
[photoid, mastertag.id] [photoid, mastertag.id]
) )
if cur.fetchone() is None: if cur.fetchone() is None:
cur.execute( data = {
'INSERT INTO photo_tag_rel VALUES(?, ?)', 'photoid': photoid,
[photoid, mastertag.id] 'tagid': mastertag.id,
) }
self.photodb.sql_insert(table='photo_tag_rel', data=data, commit=False)
# Then delete the relationships with the old tag # Then delete the relationships with the old tag
self.delete() self.delete()
@ -1157,13 +1178,16 @@ class Tag(ObjectBase, GroupableMixin):
Change the description. Leave None to keep current value. Change the description. Leave None to keep current value.
''' '''
if description is None: if description is None:
description = self.description return
cur = self.photodb.sql.cursor()
cur.execute(
'UPDATE tags SET description = ? WHERE id == ?',
[description, self.id]
)
self.description = description self.description = description
data = {
'id': self.id,
'description': self.description
}
self.photodb.sql_update(table='tags', pairs=data, where_key='id', commit=False)
self._uncache() self._uncache()
if commit: if commit:
self.photodb.log.debug('Committing - edit tag') self.photodb.log.debug('Committing - edit tag')
@ -1259,8 +1283,14 @@ class Tag(ObjectBase, GroupableMixin):
self._cached_qualified_name = None self._cached_qualified_name = None
self.photodb._cached_frozen_children = None self.photodb._cached_frozen_children = None
data = {
'id': self.id,
'name': new_name,
}
self.photodb.sql_update(table='tags', pairs=data, where_key='id', commit=False)
cur = self.photodb.sql.cursor() cur = self.photodb.sql.cursor()
cur.execute('UPDATE tags SET name = ? WHERE id == ?', [new_name, self.id])
if apply_to_synonyms: if apply_to_synonyms:
cur.execute( cur.execute(
'UPDATE tag_synonyms SET mastername = ? WHERE mastername = ?', 'UPDATE tag_synonyms SET mastername = ? WHERE mastername = ?',
@ -1276,10 +1306,9 @@ class Tag(ObjectBase, GroupableMixin):
def synonyms(self): def synonyms(self):
cur = self.photodb.sql.cursor() cur = self.photodb.sql.cursor()
cur.execute('SELECT name FROM tag_synonyms WHERE mastername == ?', [self.name]) cur.execute('SELECT name FROM tag_synonyms WHERE mastername == ?', [self.name])
fetch = cur.fetchall() fetches = [fetch[0] for fetch in cur.fetchall()]
fetch = [f[0] for f in fetch] fetches.sort()
fetch.sort() return fetches
return fetch
class User(ObjectBase): class User(ObjectBase):

View file

@ -122,19 +122,14 @@ class PDBAlbumMixin:
if not isinstance(description, str): if not isinstance(description, str):
raise TypeError('Description must be string, not %s' % type(description)) raise TypeError('Description must be string, not %s' % type(description))
cur = self.sql.cursor()
self.log.debug('New Album: %s %s', album_id, title) self.log.debug('New Album: %s %s', album_id, title)
data = { data = {
'id': album_id, 'id': album_id,
'title': title, 'title': title,
'description': description, 'description': description,
} }
self.sql_insert(table='albums', data=data, commit=False)
(qmarks, bindings) = sqlhelpers.insert_filler(constants.SQL_ALBUM_COLUMNS, data)
query = 'INSERT INTO albums VALUES(%s)' % qmarks
cur.execute(query, bindings)
album = objects.Album(self, data) album = objects.Album(self, data)
if associated_directory is not None: if associated_directory is not None:
@ -180,11 +175,7 @@ class PDBBookmarkMixin:
'title': title, 'title': title,
'url': url, 'url': url,
} }
self.sql_insert(table='bookmarks', data=data, commit=False)
(qmarks, bindings) = sqlhelpers.insert_filler(constants.SQL_BOOKMARK_COLUMNS, data)
query = 'INSERT INTO bookmarks VALUES(%s)' % qmarks
cur = self.sql.cursor()
cur.execute(query, bindings)
bookmark = objects.Bookmark(self, data) bookmark = objects.Bookmark(self, data)
if commit: if commit:
@ -292,11 +283,8 @@ class PDBPhotoMixin:
'duration': None, 'duration': None,
'thumbnail': None, 'thumbnail': None,
} }
self.sql_insert(table='photos', data=data, commit=False)
(qmarks, bindings) = sqlhelpers.insert_filler(constants.SQL_PHOTO_COLUMNS, data)
query = 'INSERT INTO photos VALUES(%s)' % qmarks
cur = self.sql.cursor()
cur.execute(query, bindings)
photo = objects.Photo(self, data) photo = objects.Photo(self, data)
if do_metadata: if do_metadata:
@ -716,12 +704,12 @@ class PDBTagMixin:
return objects.Tag(self, fetch) return objects.Tag(self, fetch)
# ...or resolve the synonym and try again. # ...or resolve the synonym and try again.
cur.execute('SELECT * FROM tag_synonyms WHERE name == ?', [tagname]) cur.execute('SELECT mastername FROM tag_synonyms WHERE name == ?', [tagname])
fetch = cur.fetchone() fetch = cur.fetchone()
if fetch is None: if fetch 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 = fetch[constants.SQL_SYN['mastername']] tagname = fetch[0]
def get_tags(self): def get_tags(self):
''' '''
@ -755,15 +743,13 @@ class PDBTagMixin:
tagid = self.generate_id('tags') tagid = self.generate_id('tags')
self._cached_frozen_children = None self._cached_frozen_children = None
cur = self.sql.cursor()
data = { data = {
'id': tagid, 'id': tagid,
'name': tagname, 'name': tagname,
'description': description, 'description': description,
} }
(qmarks, bindings) = sqlhelpers.insert_filler(constants.SQL_TAG_COLUMNS, data) self.sql_insert(table='tags', data=data, commit=False)
query = 'INSERT INTO tags VALUES(%s)' % qmarks
cur.execute(query, bindings)
if commit: if commit:
self.log.debug('Committing - new_tag') self.log.debug('Committing - new_tag')
self.commit() self.commit()
@ -930,11 +916,7 @@ class PDBUserMixin:
'password': hashed_password, 'password': hashed_password,
'created': created, 'created': created,
} }
self.sql_insert(table='users', data=data, commit=False)
(qmarks, bindings) = sqlhelpers.insert_filler(constants.SQL_USER_COLUMNS, data)
query = 'INSERT INTO users VALUES(%s)' % qmarks
cur = self.sql.cursor()
cur.execute(query, bindings)
if commit: if commit:
self.log.debug('Committing - register user') self.log.debug('Committing - register user')
@ -1265,7 +1247,7 @@ class PhotoDB(PDBAlbumMixin, PDBBookmarkMixin, PDBPhotoMixin, PDBTagMixin, PDBUs
raise ValueError('Invalid table requested: %s.', table) raise ValueError('Invalid table requested: %s.', table)
cur = self.sql.cursor() cur = self.sql.cursor()
cur.execute('SELECT * FROM id_numbers WHERE tab == ?', [table]) cur.execute('SELECT last_id FROM id_numbers WHERE tab == ?', [table])
fetch = cur.fetchone() fetch = cur.fetchone()
if fetch is None: if fetch is None:
# Register new value # Register new value
@ -1273,7 +1255,7 @@ class PhotoDB(PDBAlbumMixin, PDBBookmarkMixin, PDBPhotoMixin, PDBTagMixin, PDBUs
do_insert = True do_insert = True
else: else:
# Use database value # Use database value
new_id_int = int(fetch[constants.SQL_LASTID['last_id']]) + 1 new_id_int = int(fetch[0]) + 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')
@ -1330,10 +1312,9 @@ class PhotoDB(PDBAlbumMixin, PDBBookmarkMixin, PDBPhotoMixin, PDBTagMixin, PDBUs
cur.execute(query, bindings) cur.execute(query, bindings)
if commit: if commit:
self.sql.commit() self.commit()
def sql_update(self, table, pairs, where_key, *, commit=True): def sql_update(self, table, pairs, where_key, *, commit=True):
column_names = constants.SQL_COLUMNS[table]
cur = self.sql.cursor() cur = self.sql.cursor()
(query, bindings) = sqlhelpers.update_filler(pairs, where_key=where_key) (query, bindings) = sqlhelpers.update_filler(pairs, where_key=where_key)
@ -1341,7 +1322,7 @@ class PhotoDB(PDBAlbumMixin, PDBBookmarkMixin, PDBPhotoMixin, PDBTagMixin, PDBUs
cur.execute(query, bindings) cur.execute(query, bindings)
if commit: if commit:
self.sql.commit() self.commit()
_THING_CLASSES = { _THING_CLASSES = {