Пример #1
0
    def rows(self, connection, filter_=None, limit=DEFAULT_LIMIT, simplify=None):
        """Retrieves rows from the table with the given filter applied"""

        comment = None
        order = []

        if simplify is None:
            simplify = False

        if simplify:
            comment = connection.comment(self.name)
            order = comment.order

        builder = QueryBuilder(connection, self, filter_=filter_, order=order, limit=limit, simplify=simplify)

        mapper = None
        if simplify:
            mapper = SimplifyMapper(self, comment=create_comment(self, comment, builder.counter, builder.aliases, None))

        try:
            result = connection.queryall(builder.build(), mapper=mapper)
        except (DataError, ProgrammingError, UnknownColumnException, UnicodeEncodeError):  # pragma: no cover
            raise
        except BaseException as e:  # pragma: no cover
            logger.error(e, exc_info=1)  # pragma: no cover
            import sys  # pragma: no cover

            # pylint: disable=raising-non-exception
            raise type(e), type(e)(u"{} (check comment on table {})".format(e.message, self.name)), sys.exc_info()[
                2
            ]  # pragma: no cover

        return map(lambda row: Row(self, row), result)
Пример #2
0
def create_values(connection, table, filter_):
    """Creates row values according to the given filter"""

    builder = QueryBuilder(connection, table, filter_=filter_.filter, limit=1, simplify=filter_.simplify)

    mapper = None
    keys = None
    if filter_.simplify:
        comment = create_comment(table, connection.comment(table.name), builder.counter, builder.aliases, None)

        keys = comment.display

        mapper = SimplifyMapper(table, comment=comment)

    result = connection.queryone(builder.build(), mapper)

    row = Row(table, result)

    logger.debug("Keys: %s", keys)
    if keys is None:
        keys = sorted(row.row.keys(), key=lambda key: "" if key == COMMENT_TITLE else tostring(key))
    logger.debug("Keys: %s", keys)

    values = forward_references(row, table, keys, builder.aliases)
    values += back_references(row, table, builder.aliases)

    return values
Пример #3
0
    def test_create_comment(self):
        """Tests the comment.create_comment function"""

        con = DbTestCase.connection
        user = con.table('user')
        user_comment = con.comment('user')
        c = Counter()

        self.assertEqual(
            '{_user_id}',  # comment.Comment('id', ),
            comment.create_comment(
                user,
                user_comment,
                c,
                {},
                '_user').id
        )

        pk, user.primary_key = user.primary_key, False
        self.assertEqual(
            '-',  # comment.Comment('id', ),
            comment.create_comment(
                user,
                user_comment,
                c,
                {},
                '_user').id
        )
        user.primary_key = pk

        user_comment.id = '{id}'
        self.assertEqual(
            '{_user_id}',  # comment.Comment('id', ),
            comment.create_comment(
                user,
                user_comment,
                c,
                {},
                '_user').id
        )
Пример #4
0
    def build(self):
        foreign_keys = self.table.foreign_keys()
        search_fields = []

        entity = aliased(self.table.entity(), name=self.alias)

        projection = map(lambda x: x, entity.columns)
        joins = {self.table.name: entity}

        if self.simplify:
            # Add referenced tables from comment to be linked
            comment = create_comment(
                self.table,
                self.connection.comment(self.table.name),
                self.counter,
                self.aliases,
                self.alias)

            add_references(self.table.name, foreign_keys, joins, comment)

            logger.debug('Joins: %s', joins)

            if not self.order:
                self.order = [self.table.primary_key]

            logger.debug('Order: %s', self.order)

            keys = dict(map(lambda k: (str(k), k), comment.columns.keys()))
            if comment.search:
                for s in comment.search:
                    search_fields.append(s.format(**keys))

            if not search_fields:
                search_fields.append(comment.title.format(**keys))

            logger.debug('Search fields: %s', search_fields)

            replace_filter(
                self.filter, self.table, entity, comment, search_fields)

        logger.debug('Aliases: %s', self.aliases)

        filters = []
        if self.filter:
            add_filters(self.filter, filters, self.table, joins)

        orders = []
        if self.order:
            for order in self.order:
                orders.append(entity.columns[order])

        # Create a session
        session = Session(self.connection.engine())

        # Create query
        if self.simplify:
            alias_format = '{col.table.name}_{col.name}'
        else:
            alias_format = '{col.name}'
        logger.debug('Projection: %s', projection)
        query = session.query(*map(create_label(alias_format), projection))
        logger.debug('Query (init): %s', query)

        # Add found joins
        # Aliased joins
        joins = dict(filter(
            lambda (k, v): k != entity.original.name,
            joins.iteritems()))
        logger.debug('Joins: %s', joins)
        for _, join in joins.iteritems():
            query = query.outerjoin(join)
            for column in join.columns.keys():
                col = join.columns[column]
                query = query.add_column(create_label(alias_format)(col))
        logger.debug('Query (joins): %s', query)

        # Add filters
        logger.debug('Filters: %s', filters)
        for f in filters:
            query = query.filter(f)
        logger.debug('Query (filter): %s', query)

        # Add orders
        for order in orders:
            query = query.order_by(order)
        logger.debug('Query (order): %s', query)

        logger.debug('Slice: 0, %d', self.limit)

        # For Markus: don't slice if limit is less than 1!
        if self.limit > 0:
            return query.slice(0, self.limit)

        return query