Query
-
A reusable query returning entities or their IDs.
You can hold on to a
Queryonce it is set up and re-query it e.g. usingfind().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
See moreproperty(_:)to obtain aPropertyQuery.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(_:). UseQueryif you want to obtain entities.PropertyQuerywill respect the conditions of its baseQuery. So if you want to find the average age of allPersons 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()See moreNote
Property values do currently not consider any sorting order defined in the mainQueryobject.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:
See morelet 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)Declaration
Swift
public class PropertyQueryCondition<E: EntityInspectable & __EntityRelatable, T: EntityPropertyTypeConvertible>: QueryCondition<E> where E == E.EntityBindingType.EntityType -
Base representation of conditions of a
Querywhen you use the block-based variant.The boolean operators
&&and||use this type directly. You combine twoQueryConditionswith the boolean operators into a combined condition, forming a tree structure. Conditions that target entity properties, like value comparisons and equality checks, are of typePropertyQueryCondition.You can form conditions and use them later, like:
See morelet alcoholRestriction: QueryCondition<Person> = Person.age > 18 && Person.age < 80 // ... let alcoholAllowedQuery = try personBox.query { return alcoholRestriction || (Person.firstName == "Johnny" && Person.lastName == "Walker") }.build()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 resultsCurrently, aliases do not work for non-block-based queries.
See moreNote
setParameterdoes not perform type checks for you. Do not use a String variant for an integer parameter.Declaration
Swift
public class PropertyAlias<E: EntityInspectable & __EntityRelatable, T: EntityPropertyTypeConvertible>: QueryCondition<E> where E == E.EntityBindingType.EntityType
View on GitHub
Install in Dash
Query Reference