Exemple #1
0
 def _principal_by_name(cls, name: str) -> Principal:
     query = bakery(
         lambda session: session.query(cls.factory).filter(
             cls.factory.name == bindparam("name")
         )
     )
     return query(DBSession()).params(name=name).one()
Exemple #2
0
 def _principal_by_name(cls, name: str) -> Principal:
     query = bakery(
         lambda session: session.query(cls.factory).filter(
             cls.factory.name == bindparam("name")
         )
     )
     return query(DBSession()).params(name=name).one()
Exemple #3
0
    def root_id(self):
        """ Query for the one node without a parent and return its id.
        :result: The root node's id.
        :rtype: int
        """

        query = bakery(lambda session: session.query(Node).with_polymorphic(
            Node).add_columns(Node.id).enable_eagerloads(False).filter(
                Node.parent_id == None))

        return query(DBSession()).one().id
Exemple #4
0
    def root_id(self) -> int:
        """ Query for the one node without a parent and return its id.
        :result: The root node's id.
        :rtype: int
        """

        query = bakery(lambda session: session.query(Node)
                       .with_polymorphic(Node).add_columns(Node.id)
                       .enable_eagerloads(False).filter(Node.parent_id == None))

        return query(DBSession()).one().id
Exemple #5
0
    def __getitem__(self, path: Union[str, Iterable[str]]) -> "Node":
        db_session = DBSession()
        db_session._autoflush()

        # if not hasattr(path, '__iter__'):
        if isinstance(path, str):
            path = (path,)
        path = [p for p in path]

        # Optimization: don't query children if self._children already there:
        if "_children" in self.__dict__:
            rest = path[1:]
            try:
                [child] = filter(lambda ch: ch.name == path[0], self._children)
            except ValueError:
                raise KeyError(path)
            if rest:
                return child[rest]
            else:
                return child

        baked_query = bakery(lambda session: session.query(Node))

        if len(path) == 1:
            try:
                baked_query += lambda q: q.filter(
                    Node.name == bindparam("name"),
                    Node.parent_id == bindparam("parent_id"),
                )
                return (
                    baked_query(db_session)
                    .params(name=path[0], parent_id=self.id)
                    .one()
                )
            except NoResultFound:
                raise KeyError(path)

        # We have a path with more than one element, so let's be a
        # little clever about fetching the requested node:
        nodes = Node.__table__
        conditions = [nodes.c.id == self.id]
        alias = nodes
        for name in path:
            alias, old_alias = nodes.alias(), alias
            conditions.append(alias.c.parent_id == old_alias.c.id)
            conditions.append(alias.c.name == name)
        expr = select([alias.c.id], and_(*conditions))
        row = db_session.execute(expr).fetchone()
        if row is None:
            raise KeyError(path)
        return baked_query(db_session).get(row.id)
Exemple #6
0
    def __getitem__(self, path: Union[str, Iterable[str]]) -> "Node":
        db_session = DBSession()
        db_session._autoflush()

        # if not hasattr(path, '__iter__'):
        if isinstance(path, str):
            path = (path, )
        path = [p for p in path]

        # Optimization: don't query children if self._children already there:
        if "_children" in self.__dict__:
            rest = path[1:]
            try:
                [child] = filter(lambda ch: ch.name == path[0], self._children)
            except ValueError:
                raise KeyError(path)
            if rest:
                return child[rest]
            else:
                return child

        baked_query = bakery(lambda session: session.query(Node))

        if len(path) == 1:
            try:
                baked_query += lambda q: q.filter(
                    Node.name == bindparam("name"),
                    Node.parent_id == bindparam("parent_id"),
                )
                return (baked_query(db_session).params(
                    name=path[0], parent_id=self.id).one())
            except NoResultFound:
                raise KeyError(path)

        # We have a path with more than one element, so let's be a
        # little clever about fetching the requested node:
        nodes = Node.__table__
        conditions = [nodes.c.id == self.id]
        alias = nodes
        for name in path:
            alias, old_alias = nodes.alias(), alias
            conditions.append(alias.c.parent_id == old_alias.c.id)
            conditions.append(alias.c.name == name)
        expr = select([alias.c.id], and_(*conditions))
        row = db_session.execute(expr).fetchone()
        if row is None:
            raise KeyError(path)
        return baked_query(db_session).get(row.id)