def create(session): with open("skill.json", 'r') as fp: skill = load_json(fp) entity.Entity(session).create("skill", skill) with open("operator.json", 'r') as fp: operator = load_json(fp) entity.Entity(session).create("operator", operator) with open("operator_skill.json", 'r') as fp: data = load_json(fp) relation.Relation(session).create("operator_skill", data)
def update(self, name, relation): if self.count(name) != 1: return "Relation %s does not exist!" % name, 404 # change record of relation name = relation["name"] type = relation["type"] source = relation["source"] destination = relation["destination"] foreignkey = relation["foreignkey"] associationforeinkey = relation["associationforeinkey"] r1 = self.session.query(meta.Relation).filter_by(name=name).first() e1 = self.session.query(meta.Entity).filter_by(name=source).one() if r1.type == "many2many" and type != r1.type: association_table = "%s_%s_table" % (r1.source, r1.destination) os.remove("./module/" + association_table + ".py") r1.entity = e1 r1.name = name r1.type = type r1.source = source r1.destination = destination r1.foreignkey = foreignkey r1.associationforeinkey = associationforeinkey # update relation self.session.commit() # update module class e1 = entity.Entity(self.session) e1.genarator(source) e1.genarator(destination) # genarate relationship in module class self.genarator(relation["name"]) return "ok!"
def delete(self, entity, field_id): if self.count(field_id) != 1: print("Field %s does not exist!" % field_id) return f1 = self.session.query( meta.Field).filter_by(field_id=field_id).first() self.session.delete(f1) self.session.commit() # update module class entity.Entity(self.session).genarator(entity)
def create(self, name, relation): if self.count(name) != 0: return "Relation %s existed!" % name, 400 # save relation into database self.write_db(relation) # update module class e1 = entity.Entity(self.session) e1.genarator(relation["source"]) e1.genarator(relation["destination"]) # genarate relationship in module class self.genarator(relation["name"]) return "ok!"
def entity_update(): engine = create_engine("postgresql://*****:*****@localhost/metadata", encoding='utf-8') Base.metadata.create_all(engine) Session_class = sessionmaker(bind=engine) session = Session_class() with open("operator_test.json", 'r') as fp: data = load_json(fp) en = entity.Entity(session) en.update(data) print(data["name"]) print(en.read(data["name"]))
def delete(self, name): if self.count(name) != 1: return "Relation %s does not exist!" % name, 404 r1 = self.session.query(meta.Relation).filter_by(name=name).first() self.session.delete(r1) e1 = entity.Entity(self.session) e1.genarator(r1.source) e1.genarator(r1.destination) # update module class if r1.type == "many2many": association_table = "%s_%s_table" % (r1.source, r1.destination) os.remove("../module/" + association_table + ".py") self.session.commit() return "ok!"
def update(self, entity, field_id, data): # change record of relation if self.count(field_id) != 1: print("Field %s does not exist!" % field_id) return f1 = self.session.query(meta.Field).filter_by(field_id=field_id).one() f1.name = data["name"] f1.type = data["type"] f1.primary = data["primary"] f1.default_value = data["default_value"] f1.is_not_null = data["is_not_null"] # update entity self.session.commit() # update module class entity.Entity(self.session).genarator(entity)
#!/usr/bin/env python3 #-*- coding: UTF-8 -*- from generator import entity,relation,field from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from module import Base from os import walk engine = create_engine("postgresql://*****:*****@localhost/metadata", encoding='utf-8') Session_class = sessionmaker(bind=engine) session = Session_class() en = entity.Entity(session) def init(): for dirpath, dirs, files in walk('../module'): for name in files: if name == "__init__.py": continue elif name.split(".")[0][-4:] == "table": __import__("module.%s" % name.split(".")[0], globals(), locals(), [name.split(".")[0]]) else: __import__("module.%s" % name.split(".")[0], globals(), locals(), [name.split(".")[0].capitalize()]) def create_entity(entity_name, body) -> str: init() result = en.create(entity_name, body) __import__("module.%s" % entity_name, globals(), locals(), [entity_name.capitalize()]) Base.metadata.create_all(engine) return result
def create(self, entity, data): # save relation into database self.write_db(entity, data) # update module class entity.Entity(self.session).genarator(entity)