Query

  • A reusable query returning entities.

    You can store a Query once it is set up and re-evaluate it e.g. using find() or all().

    Use PropertyQuery instead if you want to return aggregate results. Use property(_:) to obtain a PropertyQuery.

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

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

    Declaration

    Swift

    public struct Query<E> where E : Entity
  • 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 = personBox.query { Person.age > 29 }
     let ageQuery = query.property(Person.age)
     let averageAge = 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, T> where E : Entity, T : EntityPropertyTypeConvertible
  • 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 = personBox.query { AgeRestriction .= Person.age > 21 } // 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 = personBox.query {
        return alcoholRestriction
            || (Person.firstName == "Johnny" && Person.lastName == "Walker")
    }
    alcoholAllowedQuery.setParameter("MinAge", to: 18)
    
    See more

    Declaration

    Swift

    public class PropertyQueryCondition<E, T> : QueryCondition<E> where E : Entity, T : EntityPropertyTypeConvertible
  • 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 = personBox.query {
        return alcoholRestriction
            || (Person.firstName == "Johnny" && Person.lastName == "Walker")
    }
    
    See more

    Declaration

    Swift

    public class QueryCondition<E> where E : Entity
  • 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 = personBox.query { "AgeRestriction" .= Person.age > 21 }
    let query2 = personBox.query { "AgeRestriction" .= Person.age > 18 }
    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, T> : QueryCondition<E> where E : Entity, T : EntityPropertyTypeConvertible
  • 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 : Entity, 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.