Query

  • A reusable query returning entities or their IDs.

    You can hold on to a Query once it is set up and re-query it e.g. using find().

    Use the block-based query method to state conditions using operator overloads, like:

    let query = box.query { Person.age > 21 && Person.name.startsWith(“M”) }.build()

    If you want to return aggregate results or just property values and not whole entities, use property(_:) to obtain a PropertyQuery.

    See more

    Declaration

    Swift

    public class Query<E: EntityInspectable & __EntityRelatable>: CustomDebugStringConvertible
    where E == E.EntityBindingType.EntityType
  • Query for values of a specific property instead of object instances.

    Create using Query.property(_:). Use Query if you want to obtain entities.

    PropertyQuery will respect the conditions of its base Query. So if you want to find the average age of all Persons above 30, this is how you can write it:

     let query = try personBox.query { Person.age > 29 }.build()
     let ageQuery = query.property(Person.age)
     let averageAge = try ageQuery.average()
    

    Note

    Property values do currently not consider any sorting order defined in the main Query object.
    See more

    Declaration

    Swift

    public class PropertyQuery<E: EntityInspectable & __EntityRelatable, T: EntityPropertyTypeConvertible>
        where E == E.EntityBindingType.EntityType
  • Representation of comparison expression conditions of a Query.

    All operators that target entity properties will produce this:

    personBox.query { Person.age > 21 }
    

    You can apply a short name (called alias, see PropertyAlias) to later identify specific expressions using the .= operator. This is useful to change the values. The example above with an alias would read:

    let query = try personBox.query { "AgeRestriction" .= Person.age > 21 }.build()
    // Change the condition to `Person.age > 18`
    query.setParameter("AgeRestriction", to: 18)
    

    You can form conditions and use them later, like:

     let alcoholRestriction: QueryCondition<Person> =
             "MinAge" .= Person.age > 21
             && "MaxAge" .= Person.age < 80
     // ...
     let alcoholAllowedQuery = try personBox.query {
         return alcoholRestriction
             || (Person.firstName == "Johnny" && Person.lastName == "Walker")
     }.build()
     try alcoholAllowedQuery.setParameter("MinAge", to: 18)
    
    See more

    Declaration

    Swift

    public class PropertyQueryCondition<E: EntityInspectable & __EntityRelatable, T: EntityPropertyTypeConvertible>:
        QueryCondition<E>
    where E == E.EntityBindingType.EntityType
  • Base representation of conditions of a Query when you use the block-based variant.

    The boolean operators && and || use this type directly. You combine two QueryConditions with the boolean operators into a combined condition, forming a tree structure. Conditions that target entity properties, like value comparisons and equality checks, are of type PropertyQueryCondition.

    You can form conditions and use them later, like:

    let alcoholRestriction: QueryCondition<Person> = Person.age > 18 && Person.age < 80
    // ...
    let alcoholAllowedQuery = try personBox.query {
        return alcoholRestriction
            || (Person.firstName == "Johnny" && Person.lastName == "Walker")
    }.build()
    
    See more

    Declaration

    Swift

    public class QueryCondition<E: EntityInspectable & __EntityRelatable>
    where E == E.EntityBindingType.EntityType
  • As a convenience, you can register a short name (called an alias) for a query condition, and later modify its values.

    Example of the operator in the block-based syntax:

    let query1 = try personBox.query { "AgeRestriction" .= Person.age > 21 }.build()
    let query2 = try personBox.query { "AgeRestriction" .= Person.age > 18 }.build()
    try query1.setParameter("AgeRestriction", to: 18)
    // Now query1 and query2 produce the same results
    

    Currently, aliases do not work for non-block-based queries.

    Note

    setParameter does not perform type checks for you. Do not use a String variant for an integer parameter.
    See more

    Declaration

    Swift

    public class PropertyAlias<E: EntityInspectable & __EntityRelatable, T: EntityPropertyTypeConvertible>:
        QueryCondition<E>
    where E == E.EntityBindingType.EntityType
  • This operator is used in Box.query()‘s block to define a named alias for a query.

    Declaration

    Swift

    public func .= <E, T>(alias: String, condition: PropertyQueryCondition<E, T>) -> PropertyAlias<E, T> where E : EntityInspectable, E : __EntityRelatable, E == E.EntityBindingType.EntityType, T : EntityPropertyTypeConvertible

    Parameters

    alias

    The short name for condition.

    condition

    The condition the short name should be applied to.

    Return Value

    An object representing the short name/condition association that you can return from the block.