Tidy up secondary column code

This commit is contained in:
Nick Hall 2017-02-22 22:13:18 +00:00
parent a8e6f7b9a3
commit 6bf8870295

View File

@ -913,24 +913,21 @@ class DBAPI(DbGeneric):
Create secondary columns. Create secondary columns.
""" """
LOG.info("Creating secondary columns...") LOG.info("Creating secondary columns...")
for table in self.get_table_func(): for cls in (Person, Family, Event, Place, Repository, Source,
if not hasattr(self.get_table_func(table, "class_func"), Citation, Media, Note, Tag):
"get_secondary_fields"): table_name = cls.__name__.lower()
continue for field, schema_type, max_length in cls.get_secondary_fields():
table_name = table.lower()
for field, schema_type, max_length in self.get_table_func(
table, "class_func").get_secondary_fields():
sql_type = self._sql_type(schema_type, max_length) sql_type = self._sql_type(schema_type, max_length)
try: try:
# test to see if it exists: # test to see if it exists:
self.dbapi.execute("SELECT %s FROM %s LIMIT 1" self.dbapi.execute("SELECT %s FROM %s LIMIT 1"
% (field, table_name)) % (field, table_name))
LOG.info(" Table %s, field %s is up to date", LOG.info(" Table %s, field %s is up to date",
table, field) table_name, field)
except: except:
# if not, let's add it # if not, let's add it
LOG.info(" Table %s, field %s was added", LOG.info(" Table %s, field %s was added",
table, field) table_name, field)
self.dbapi.execute("ALTER TABLE %s ADD COLUMN %s %s" self.dbapi.execute("ALTER TABLE %s ADD COLUMN %s %s"
% (table_name, field, sql_type)) % (table_name, field, sql_type))
@ -941,8 +938,7 @@ class DBAPI(DbGeneric):
Does not commit. Does not commit.
""" """
table = obj.__class__.__name__ table = obj.__class__.__name__
fields = self.get_table_func(table, "class_func").get_secondary_fields() fields = [field[0] for field in obj.get_secondary_fields()]
fields = [field for (field, schema_type, max_length) in fields]
sets = [] sets = []
values = [] values = []
for field in fields: for field in fields:
@ -965,10 +961,10 @@ class DBAPI(DbGeneric):
table_name = table.lower() table_name = table.lower()
self.dbapi.execute("UPDATE %s SET %s where handle = ?" self.dbapi.execute("UPDATE %s SET %s where handle = ?"
% (table_name, ", ".join(sets)), % (table_name, ", ".join(sets)),
self._sql_cast_list(table, sets, values) self._sql_cast_list(values)
+ [obj.handle]) + [obj.handle])
def _sql_cast_list(self, table, fields, values): def _sql_cast_list(self, values):
""" """
Given a list of field names and values, return the values Given a list of field names and values, return the values
in the appropriate type. in the appropriate type.