QueryBuilder

public final class QueryBuilder<E: EntityInspectable & __EntityRelatable>
where E == E.EntityBindingType.EntityType

An object that describes a query. You usually interact with a QueryBuilder solely via Box’s query() method, but certain properties that can’t be modified on a query once it has been built (like sort order) can be configured on the QueryBuilder. Call build() on a QueryBuilder to create an actual query object that you can request objects from.

  • The type of entity this query builder operates on.

    Declaration

    Swift

    public typealias EntityType = E
  • Build a query from the information in this query builder, which can then be used to get actual query results from the database.

    Declaration

    Swift

    public func build() throws -> Query<EntityType>
  • Request that query results for an entity query be returned sorted by the given property. Can not be called after the query has been used the first time.

    Currently has no effect on the result of calling query.property(...).

    Declaration

    Swift

    public func ordered<T>(by property: Property<EntityType, T, Void>, flags: OrderFlags = [])
        -> QueryBuilder<EntityType>

    Parameters

    property

    The property by which to sort.

    flags

    Additional flags to control sort behaviour, like what to do with NIL values, or to sort descending instead of ascending.

  • Adds an and-relation to another entity referenced by a ToOne property of this entity to this query.

    Note

    in relational databases you would use a join for this.

    Declaration

    Swift

    func link<V: IdBase, R>(_ property: Property<E, V, R>,
                            conditions: @escaping () -> QueryCondition<R>)
        -> QueryBuilder<E>
        where R == R.EntityBindingType.EntityType

    Parameters

    property

    The ToOne relation property you wish to traverse for your query.

    conditions

    The query you want to execute on the referenced property.

    Return Value

    This object, so you can chain calls like orderBox.query().link(property: Order.customer) { Customer.name == "Sally Sparrow" }.build()

  • Adds an and-relation to another entity referenced by a ToMany property of this entity to this query.

    Note: in relational databases you would use a join for this.

    Declaration

    Swift

    func link<V: EntityInspectable>(_ property: ToManyProperty<V>,
                                    conditions: @escaping () -> QueryCondition<V>)
        -> QueryBuilder<E>
        where V == V.EntityBindingType.EntityType

    Parameters

    property

    The ToMany relation property you wish to traverse for your query.

    conditions

    The query you want to execute on the referenced property.

    Return Value

    This object, so you can chain calls like customerBox.query().and(property: Customer.orders) { Order.name == "Paint Supplies" }.build()