From 0a0a450b458868a6bb2208309713f66022e47163 Mon Sep 17 00:00:00 2001 From: Doug Blank Date: Sat, 30 Apr 2016 16:48:11 -0400 Subject: [PATCH] DB-API: get_family_handles(sort_handles=True) now fixed --- gramps/plugins/database/dbapi.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/gramps/plugins/database/dbapi.py b/gramps/plugins/database/dbapi.py index 7e8e4393a..a4c289b1f 100644 --- a/gramps/plugins/database/dbapi.py +++ b/gramps/plugins/database/dbapi.py @@ -377,11 +377,21 @@ class DBAPI(DbGeneric): If sort_handles is True, the list is sorted by surnames. """ if sort_handles: - self.dbapi.execute("""SELECT DISTINCT family.handle FROM family - JOIN person - ON family.father_handle = person.handle - ORDER BY person.primary_name__surname_list__0__surname, - person.primary_name__first_name;""") + self.dbapi.execute("""SELECT f.handle FROM + (SELECT family.* + FROM family LEFT JOIN + person AS father + ON family.father_handle = father.handle LEFT JOIN + person AS mother + on family.mother_handle = mother.handle + order by (case when father.handle is null + then mother.primary_name__surname_list__0__surname + else father.primary_name__surname_list__0__surname + end), + (case when family.handle is null + then mother.primary_name__first_name + else father.primary_name__first_name + end)) AS f;""") else: self.dbapi.execute("SELECT handle FROM family;") rows = self.dbapi.fetchall() @@ -627,7 +637,7 @@ class DBAPI(DbGeneric): emit = "family-add" self.dbapi.execute("""INSERT INTO family (handle, gramps_id, father_handle, mother_handle, blob_data) VALUES(?, ?, ?, ?, ?);""", - [family.handle, + [family.handle, family.gramps_id, family.father_handle, family.mother_handle, @@ -1112,7 +1122,7 @@ class DBAPI(DbGeneric): if order_by is None: query = "SELECT blob_data FROM %s;" % class_.__name__.lower() else: - order_phrases = ["%s %s" % (self._hash_name(class_.__name__, class_.get_field_alias(field)), direction) + order_phrases = ["%s %s" % (self._hash_name(class_.__name__, class_.get_field_alias(field)), direction) for (field, direction) in order_by] query = "SELECT blob_data FROM %s ORDER BY %s;" % (class_.__name__.lower(), ", ".join(order_phrases)) self.dbapi.execute(query) @@ -1617,7 +1627,7 @@ class DBAPI(DbGeneric): # do a select on all; if it works, then it is ok; else, check them all table_name = table.lower() try: - fields = [self._hash_name(table, field) for (field, ptype) in + fields = [self._hash_name(table, field) for (field, ptype) in self.get_table_func(table,"class_func").get_secondary_fields()] if fields: self.dbapi.execute("select %s from %s limit 1;" % (", ".join(fields), table_name)) @@ -1759,7 +1769,7 @@ class DBAPI(DbGeneric): order_by - [(field, "ASC" | "DESC"), ...] """ if order_by: - order_clause = ", ".join(["%s %s" % (self._hash_name(table, field), dir) + order_clause = ", ".join(["%s %s" % (self._hash_name(table, field), dir) for (field, dir) in order_by]) return "ORDER BY " + order_clause else: @@ -1830,8 +1840,8 @@ class DBAPI(DbGeneric): ["NOT", where] order_by - [[fieldname, "ASC" | "DESC"], ...] """ - secondary_fields = ([self._hash_name(table, field) for (field, ptype) in - self.get_table_func(table,"class_func").get_secondary_fields()] + + secondary_fields = ([self._hash_name(table, field) for (field, ptype) in + self.get_table_func(table,"class_func").get_secondary_fields()] + ["handle"]) # handle is a sql field, but not listed in secondaries # If no fields, then we need objects: # Check to see if where matches SQL fields: