Exemplo n.º 1
0
 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))
Exemplo n.º 2
0
 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()
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
    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
Exemplo n.º 5
0
    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
Exemplo n.º 6
0
    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)
Exemplo n.º 7
0
    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
Exemplo n.º 8
0
    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
Exemplo n.º 9
0
    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()
Exemplo n.º 10
0
    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()
Exemplo n.º 11
0
    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()
Exemplo n.º 12
0
    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)
Exemplo n.º 13
0
    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()
Exemplo n.º 14
0
    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
Exemplo n.º 15
0
    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)
Exemplo n.º 16
0
 def listItems(cls, type):
     return getSession().query(Item).filter(
         Item.type == getBehavior(type).name)
Exemplo n.º 17
0
 def getItemBehaviors(self, oid):
     return [r[0] for r in getSession().query(
             SQLBehavior.name).filter(
             SQLBehavior.oid==oid).order_by(SQLBehavior.inst_id)]
Exemplo n.º 18
0
 def query(self, *args):
     session = getSession()
     return session.query(self.Type).filter(*args)
Exemplo n.º 19
0
 def getBehaviorOIDs(self):
     for r in getSession().query(
         SQLBehavior.oid).filter(SQLBehavior.name == self.name):
         yield r[0]