def getItemReferences(cls, oid, type=None): if type is None: return getSession().query(Reference).filter(Reference.source == oid) else: return getSession().query(Reference).filter( sqlalchemy.and_( Reference.source == oid, Reference.type == type))
def getItemBackReferences(cls, oid, type=None): if type is None: return getSession().query(Reference).filter( Reference.destination == oid).all() else: return getSession().query(Reference).filter( sqlalchemy.and_( Reference.destination == oid, Reference.type == type)).all()
def apply(self, item): # run custom code if hasattr(self.factory, 'applyBehavior'): self.factory.applyBehavior(item, self) # apply behavior oid = item.oid session = getSession() ob = session.query(SQLBehavior).filter( sqlalchemy.and_( SQLBehavior.oid == oid, SQLBehavior.name == self.name)).first() if ob is not None: raise StorageException('Behavior already applied: %s'%self.name) if self.iswrapper: session.add(SQLBehavior(oid, self.name, -1)) else: num = len(self.getItemBehaviors(oid)) + 1 session.add(SQLBehavior(oid, self.name, num)) session.flush() # apply schema if self.schema is not None: item.applySchema(self.schema)
def __getitem__(self, oid): rel = getSession().query(self.Type).filter( self.Type.oid == oid).first() if rel is None: raise KeyError(oid) rel.type = self.name return rel
def schemas(self): if self._v_schemas is not None: return self._v_schemas schemas = [r[0] for r in getSession().query( SQLSchema.name).filter(SQLSchema.oid == self.oid)] self._v_schemas = schemas return schemas
def insert(self, source, destination, **kw): oid = uuid.uuid1().hex session = getSession() session.add(Reference(oid, self.name, source, destination)) item = self.Type(oid, self.name, source, destination, **kw) session.add(item) session.flush() return self.get(oid)
def getItem(cls, oid): item = hooks.cache.getItem(oid) if item is hooks.UNSET: item = getSession().query(Item).filter(Item.oid == oid).first() hooks.cache.setItem(oid, item) if item is None: raise KeyError(oid) return item
def behaviors(self): if self._v_behaviors is not None: return self._v_behaviors behaviors = [r[0] for r in getSession().query( SQLBehavior.name).filter( SQLBehavior.oid==self.oid).order_by(SQLBehavior.inst_id)] self._v_behaviors = behaviors return behaviors
def remove(self, oid): session = getSession() ob = session.query(Reference).filter(Reference.oid == oid).first() if ob is None: raise KeyError(oid) session.delete(ob) ob = session.query(self.Type).filter(self.Type.oid == oid).first() if ob is not None: session.delete(ob) session.flush()
def _register(name, schema, klass, type, title, description): sob = Schema(name, schema, klass, title, description) for tp in type: getSiteManager().registerUtility(sob, tp, name) # register in internal registry registry.registerSchema(sob) session = hooks.getSession() if session is not None: hooks.getMetadata().create_all() session.flush()
def _register(name, schema, klass, title, description): if klass is not None: rel = Relation(name, klass, title, description) else: rel = buildRelation(name, schema, title, description) registry.registerRelation(rel, schema) # create table session = hooks.getSession() if session is not None: hooks.getMetadata().create_all() session.flush()
def insertItem(cls, type=''): if type: type = getBehavior(type).name oid = uuid.uuid1().hex item = Item(oid, type) session = getSession() session.add(item) if type: item.applyBehavior(type) session.flush() return Item.getItem(oid)
def remove(self): # fixme: it too explicite, it do a lot of db queries # this method has to be simplier self.type = '' # first remove behavior, because behavior can remove some of schemas behaviors = self.behaviors while behaviors: self.removeBehavior(behaviors[0]) behaviors = self.behaviors for schId in self.schemas: self.removeSchema(schId) hooks.cache.delItem(self.oid) session = getSession() session.delete(self) session.flush()
def getReferences(self, source=None, destination=None, **kw): session = getSession() filter = [] if source is not None: filter.append(self.Type.source == source) if destination is not None: filter.append(self.Type.destination == destination) for name, value in kw.items(): if hasattr(self.Type, name): filter.append(getattr(self.Type, name)==value) if filter: for rel in session.query(self.Type).filter( sqlalchemy.and_(*filter)): yield rel else: for rel in session.query(self.Type): yield rel
def remove(self, item): oid = item.oid session = getSession() # remove behaivor record ob = session.query(SQLBehavior).filter( sqlalchemy.and_( SQLBehavior.oid == oid, SQLBehavior.name == self.name)).first() if ob is None: raise StorageException('Behavior is not applied: %s'%self.name) session.delete(ob) # remove schema if self.schema is not None: item.removeSchema(self.schema) session.flush() if hasattr(self.factory, 'removeBehavior'): self.factory.removeBehavior(item, self)
def listItems(cls, type): return getSession().query(Item).filter( Item.type == getBehavior(type).name)
def getItemBehaviors(self, oid): return [r[0] for r in getSession().query( SQLBehavior.name).filter( SQLBehavior.oid==oid).order_by(SQLBehavior.inst_id)]
def query(self, *args): session = getSession() return session.query(self.Type).filter(*args)
def getBehaviorOIDs(self): for r in getSession().query( SQLBehavior.oid).filter(SQLBehavior.name == self.name): yield r[0]