9742: Rewrite remove_* methods

Avoid using table maps in _do_remove.
This commit is contained in:
Nick Hall 2016-10-17 19:13:18 +01:00
parent 6092065fe3
commit a3718e23ab
2 changed files with 22 additions and 41 deletions

View File

@ -1605,77 +1605,75 @@ class DbGeneric(DbWriteBase, DbReadBase, UpdateCallback, Callback):
return self.get_table_func(table_name,"gramps_id_func")(gramps_id)
return None
def remove_person(self, handle, transaction):
"""
Remove the Person specified by the database handle from the
database, preserving the change in the passed transaction.
"""
self._do_remove(handle, transaction, PERSON_KEY)
def remove_source(self, handle, transaction):
"""
Remove the Source specified by the database handle from the
database, preserving the change in the passed transaction.
"""
self._do_remove(handle, transaction, self.source_map,
self.source_id_map, SOURCE_KEY)
self._do_remove(handle, transaction, SOURCE_KEY)
def remove_citation(self, handle, transaction):
"""
Remove the Citation specified by the database handle from the
database, preserving the change in the passed transaction.
"""
self._do_remove(handle, transaction, self.citation_map,
self.citation_id_map, CITATION_KEY)
self._do_remove(handle, transaction, CITATION_KEY)
def remove_event(self, handle, transaction):
"""
Remove the Event specified by the database handle from the
database, preserving the change in the passed transaction.
"""
self._do_remove(handle, transaction, self.event_map,
self.event_id_map, EVENT_KEY)
self._do_remove(handle, transaction, EVENT_KEY)
def remove_media(self, handle, transaction):
"""
Remove the MediaPerson specified by the database handle from the
database, preserving the change in the passed transaction.
"""
self._do_remove(handle, transaction, self.media_map,
self.media_id_map, MEDIA_KEY)
self._do_remove(handle, transaction, MEDIA_KEY)
def remove_place(self, handle, transaction):
"""
Remove the Place specified by the database handle from the
database, preserving the change in the passed transaction.
"""
self._do_remove(handle, transaction, self.place_map,
self.place_id_map, PLACE_KEY)
self._do_remove(handle, transaction, PLACE_KEY)
def remove_family(self, handle, transaction):
"""
Remove the Family specified by the database handle from the
database, preserving the change in the passed transaction.
"""
self._do_remove(handle, transaction, self.family_map,
self.family_id_map, FAMILY_KEY)
self._do_remove(handle, transaction, FAMILY_KEY)
def remove_repository(self, handle, transaction):
"""
Remove the Repository specified by the database handle from the
database, preserving the change in the passed transaction.
"""
self._do_remove(handle, transaction, self.repository_map,
self.repository_id_map, REPOSITORY_KEY)
self._do_remove(handle, transaction, REPOSITORY_KEY)
def remove_note(self, handle, transaction):
"""
Remove the Note specified by the database handle from the
database, preserving the change in the passed transaction.
"""
self._do_remove(handle, transaction, self.note_map,
self.note_id_map, NOTE_KEY)
self._do_remove(handle, transaction, NOTE_KEY)
def remove_tag(self, handle, transaction):
"""
Remove the Tag specified by the database handle from the
database, preserving the change in the passed transaction.
"""
self._do_remove(handle, transaction, self.tag_map,
None, TAG_KEY)
self._do_remove(handle, transaction, TAG_KEY)
def is_empty(self):
"""

View File

@ -1181,23 +1181,7 @@ class DBAPI(DbGeneric):
ref_class_name])
# This function is followed by a commit.
def remove_person(self, handle, transaction):
"""
Remove the Person specified by the database handle from the database,
preserving the change in the passed transaction.
"""
if isinstance(handle, bytes):
handle = str(handle, "utf-8")
if self.readonly or not handle:
return
if handle in self.person_map:
person = Person.create(self.person_map[handle])
self.dbapi.execute("DELETE FROM person WHERE handle = ?;", [handle])
if not transaction.batch:
transaction.add(PERSON_KEY, TXNDEL, person.handle,
person.serialize(), None)
def _do_remove(self, handle, transaction, data_map, data_id_map, key):
def _do_remove(self, handle, transaction, obj_key):
if isinstance(handle, bytes):
handle = str(handle, "utf-8")
key2table = {
@ -1214,13 +1198,12 @@ class DBAPI(DbGeneric):
}
if self.readonly or not handle:
return
if handle in data_map:
self.dbapi.execute(
"DELETE FROM %s WHERE handle = ?;" % key2table[key],
[handle])
if self.has_handle(obj_key, handle):
sql = "DELETE FROM %s WHERE handle = ?;" % key2table[obj_key]
self.dbapi.execute(sql, [handle])
if not transaction.batch:
data = data_map[handle]
transaction.add(key, TXNDEL, handle, data, None)
data = self.get_raw_data(obj_key, handle)
transaction.add(obj_key, TXNDEL, handle, data, None)
def find_backlink_handles(self, handle, include_classes=None):
"""