def _add_field(field:Field): session.add(field) try: session.commit() return field except Exception as e: session.rollback() logger.error('Failed to add a field {}. Error:{}'.format(field.name, e))
def add_schema(name:str, desc:str=None): schema = Schema() schema.name = name.strip() schema.desc = desc.strip() session.add(schema) try: session.commit() return schema except Exception as e: session.rollback() logger.error('Fail to add new schema {}. Error:{}'.format(name, e))
def add_field(schema_name, name, meta): schema = get_schema_by_name(schema_name) if not schema: raise ValueError('{} is not a Tablename'.format(schema_name)) # 解析meta,from ..models import FieldMeta meta_data = FieldMeta(meta) field = Field() field.name = name.strip() field.schema_id = schema.id field.meta = meta # 能解析成功说明符合格式要求 # ref_id 引用 if meta_data.reference: ref = get_field(meta_data.reference.schema, meta_data.reference.field) if not ref: raise TypeError('Wrong Reference {}.{}'.format(meta_data.reference.schema, meta_data.reference.field)) field.ref_id = ref.id # 判断字段是否已经使用 if not table_used(schema.id): # 未使用的逻辑表,直接加字段 return _add_field(field) # 已使用的逻辑表 if meta_data.nullable: # 可以为空,直接加字段 return _add_field(field) # 到这里已经有一个隐含条件即不可为空 if meta_data.unique: # 必须唯一 # 当前的条件是 对一个正在使用的逻辑表加字段不可以为空又要唯一,做不到 raise TypeError('This field is required an unique.') # 到这里的隐含条件是,不可以为空,但可以不唯一 if not meta_data.default: # 没有缺省值 raise TypeError('This field requires a default value.') else: # 为逻辑表所有记录增加字段,操作entity表 entities = session.query(Entity).filter((Entity.schema_id == schema.id) & (Entity.deleted == False)).all() for entity in entities: # value表新增记录 value = Value() value.entity_id = entity.id value.field = field value.value = meta_data.default session.add(value) return _add_field(field) # 到这里的隐含条件是,不可以为空,但可以不唯一 if not meta_data.default: # 没有缺省值 raise TypeError('This field requires a default value.') else: # 为逻辑表所有记录增加字段,操作entity表 for entity in iter_entities(schema.id): # value表新增记录 value = Value() value.entity_id = entity.id value.field = field value.value = meta_data.default session.add(value) return _add_field(field)
def delete_schema(id:int): try: schema = session.query(Schema).filter((Schema.id == id) & (Schema.deleted == False)) if schema: schema.deleted = True session.add(schema) try: session.commit() return schema except Exception as e: session.rollback() raise e else: raise ValueError('Wrong ID {}'.format(id)) except Exception as e: logger.error('Fail to del a schema.id = {}; error:{}'.format(id, e))