mirror of
https://github.com/yweber/lodel2.git
synced 2026-08-12 23:12:22 +02:00
EmField add_column code factorization
This commit is contained in:
parent
166dc6ad75
commit
3f8aff6813
2 changed files with 7 additions and 56 deletions
|
|
@ -96,61 +96,12 @@ class EmField(EmComponent):
|
|||
# @param emField EmField: the object representing the field
|
||||
# @return True in case of success, False if not
|
||||
def add_field_column_to_class_table(self):
|
||||
field_type = "%s%s" % (get_field_type(self.fieldtype).sql_column(), " DEFAULT 0" if self.fieldtype == 'integer' else '')
|
||||
field_class_table = self.get_class_table()
|
||||
|
||||
sql_engine = sqlutils.getEngine()
|
||||
conn = sql_engine.connect()
|
||||
meta_data = sqlutils.meta(sql_engine)
|
||||
table = sql.Table(field_class_table, meta_data)
|
||||
new_column = self.create_column(name=self.name, type_=field_type)
|
||||
ddl = AddColumn(table, new_column)
|
||||
sql_query = ddl.compile(dialect=sql_engine.dialect)
|
||||
sql_query = str(sql_query)
|
||||
logger.debug("Executing SQL : '%s'" % sql_query)
|
||||
ret = bool(conn.execute(sql_query))
|
||||
return ret
|
||||
|
||||
def create_column(self, **kwargs):
|
||||
if not 'name' in kwargs or ('type' not in kwargs and 'type_' not in kwargs):
|
||||
pass
|
||||
|
||||
if 'type_' not in kwargs and 'type' in kwargs:
|
||||
kwargs['type_'] = self.sql_to_sqla_type(kwargs['type'])
|
||||
del kwargs['type']
|
||||
|
||||
if 'extra' in kwargs:
|
||||
for extra_name in kwargs['extra']:
|
||||
kwargs[extra_name] = kwargs['extra']['name']
|
||||
del kwargs['extra']
|
||||
|
||||
if 'foreignkey' in kwargs:
|
||||
foreign_key = sql.ForeignKey(kwargs['foreignkey'])
|
||||
del kwargs['foreignkey']
|
||||
else:
|
||||
foreign_key = None
|
||||
|
||||
if 'primarykey' in kwargs:
|
||||
kwargs['primary_key'] = kwargs['primarykey']
|
||||
del kwargs['primarykey']
|
||||
|
||||
result = sql.Column(**kwargs)
|
||||
|
||||
if foreign_key is not None:
|
||||
result.append_foreign_key(foreign_key)
|
||||
|
||||
return result
|
||||
|
||||
def sql_to_sqla_type(self, strtype):
|
||||
if 'VARCHAR' in strtype:
|
||||
check_length = re.search(re.compile('VARCHAR\(([\d]+)\)', re.IGNORECASE), strtype)
|
||||
column_length = int(check_length.groups()[0]) if check_length else None
|
||||
return sql.VARCHAR(lengh=column_length)
|
||||
else:
|
||||
try:
|
||||
return getattr(sql, strtype)
|
||||
except AttributeError:
|
||||
raise NameError("Unknown type '%s'" % strtype)
|
||||
dbe = self.db_engine()
|
||||
fieldtype = get_field_type(self.fieldtype)
|
||||
new_column = sql.Column(name=self.name, **(fieldtype.sqlalchemy_args()) )
|
||||
class_table = sql.Table(self.get_class_table(), sqlutils.meta(dbe))
|
||||
ddl = AddColumn(class_table, new_column)
|
||||
return sqlutils.ddl_execute(ddl, dbe)
|
||||
|
||||
## get_class_table (Function)
|
||||
#
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue