Esempio n. 1
0
def query_with_labels(query, schema):
    """Create a new query that labels columns according to the SQLAlchemy model.
    Properties that are excluded by `schema` will be ignored.

    :param query: Original SQLAlchemy query
    :param schema: Optional schema specifying properties to exclude
    :returns: Query with labeled entities
    """
    exclude = getattr(schema.Meta, 'exclude', ())
    entities = [
        entity for entity in query_entities(query) if entity.key not in exclude
    ]
    return query.with_entities(*entities)
Esempio n. 2
0
def query_with_labels(query, schema):
    """Create a new query that labels columns according to the SQLAlchemy model.
    Properties that are excluded by `schema` will be ignored.

    :param query: Original SQLAlchemy query
    :param schema: Optional schema specifying properties to exclude
    :returns: Query with labeled entities
    """
    exclude = getattr(schema.Meta, 'exclude', ())
    entities = [
        entity for entity in query_entities(query)
        if entity.key not in exclude
    ]
    return query.with_entities(*entities)
Esempio n. 3
0
def query_with_labels(query, schema, sort_columns=False):
    """Create a new query that labels columns according to the SQLAlchemy
    model.  Properties that are excluded by `schema` will be ignored.

    Furthermore, if a "relationships" attribute is set on the schema (via the
    Meta options object), those relationships will be followed to include the
    specified nested fields in the output.  By default, only the fields
    defined on the model mapped directly to columns in the corresponding table
    will be included.

    :param query: Original SQLAlchemy query
    :param schema: Optional schema specifying properties to exclude
    :param sort_columns: Optional flag to sort the column labels by name
    :returns: Query with labeled entities
    """
    exclude = getattr(schema.Meta, 'exclude', ())
    relationships = getattr(schema.Meta, 'relationships', [])
    joins = []
    entities = [
        entity for entity in query_entities(query)
        if entity.key not in exclude
    ]

    for relationship in relationships:
        if relationship.position == -1:
            entities.append(relationship.column.label(relationship.label))
        else:
            entities.insert(
                relationship.position,
                relationship.column.label(relationship.label)
            )

        if relationship.field not in joins:
            joins.append(relationship.field)

    if sort_columns:
        entities.sort(key=lambda x: x.name)

    if joins:
        query = query.join(*joins).with_entities(*entities)
    else:
        query = query.with_entities(*entities)

    return query