def test(self): family = db.get_family_from_gramps_id("F0001") s = Struct(family.to_struct(), db) self.assertEqual(s["gramps_id"], "F0001") s["gramps_id"] = "TEST" self.assertEqual(s["gramps_id"], "TEST") self.assertEqual(s.father_handle.primary_name.first_name, "Allen Carl") s["father_handle.primary_name.first_name"] = "Edward" self.assertEqual(s["father_handle.primary_name.first_name"], "Edward")
def do_query(self, items, table): """ Perform the query on the items in the named table. """ # table: a class that has .row(1, 2, 3, ...) with self.database.get_transaction_class()("QueryQuickview", self.database, batch=True) as trans: ROWNUM = 0 env = self.make_env() for item in items: if item is None: continue row = [] row_env = [] # "col[0]" in WHERE clause will return first column of selection: env["col"] = row_env env["ROWNUM"] = ROWNUM env["object"] = item struct = Struct(item.to_struct(), self.database) env.set_struct(struct) for col in self.columns: try: value = eval(col, env) except: value = None row.append(value) # allow col[#] reference: row_env.append(value) # an alias? if col in self.aliases: env[self.aliases[col]] = value # Should we include this row? if self.where: try: result = eval(self.where, env) except: continue else: if self.action in ["DELETE", "UPDATE"]: result = True else: result = any([col != None for col in row]) # are they all None? # If result, then append the row if result: if (self.limit is None) or (self.limit[0] <= ROWNUM < self.limit[1]): if self.action == "SELECT": if not self.flat: # Join by rows: products = [] columns = [] count = 0 for col in row: if ((isinstance(col, Struct) and isinstance(col.struct, list) and len(col.struct) > 0) or (isinstance(col, list) and len(col) > 0)): products.append(col) columns.append(count) count += 1 if len(products) > 0: current = self.clean(row, self.columns) for items in itertools.product(*products): for i in range(len(items)): current[ columns[i]] = self.stringify( items[i]) table.row( *current, link=(item.__class__.__name__, item.handle)) self.select += 1 else: table.row(*self.clean(row, self.columns), link=(item.__class__.__name__, item.handle)) self.select += 1 else: table.row(*self.clean(row, self.columns), link=(item.__class__.__name__, item.handle)) self.select += 1 elif self.action == "UPDATE": # update table set col=val, col=val where expr; table.row(*self.clean(row, self.columns), link=(item.__class__.__name__, item.handle)) self.select += 1 for i in range(len(self.setcolumns)): struct.setitem(self.setcolumns[i], eval(self.values[i], env), trans=trans) elif self.action == "DELETE": table.row(*self.clean(row, self.columns)) self.select += 1 self.database.remove_instance(item, trans) else: raise AttributeError("unknown command: '%s'", self.action) ROWNUM += 1 if (self.limit is not None) and (ROWNUM >= self.limit[1]): break