Query<T>for building queries that can fetch a subset of rows within a certain range.
offsetproperties of a
Query<T>. For example, if a table contains 100 rows, and you would like to grab 10 at a time, each query would have a value of 10 for its
fetchLimit. The first query would have an
offsetof 0, then 10, then 20, and so on. Especially when using
sortBy, this type of paging can be effective. One of the drawbacks to this type of paging is that it can skip or duplicate rows if rows are being added or deleted between fetches.
fetchLimit=2) starting at the first item (
offset=0), our first result set is the first two objects. The next page is the original offset plus the same limit - we grab the next two rows. But before the next page is fetched, a new object is inserted and its at an index that we already fetched. The next page would return
3:00pmagain. A similar problem occurs if a row is deleted when paging in this way.
1:30pm. The query filters out rows with a value less than the one it was sent, orders the remaining rows and then fetches the newest from the top.
Query.pageByuses this technique. Its usage is similar to
fetchLimit- otherwise you're just sorting and returning every row. You identify which property to page on by using a property selector. The second argument to
pageBydefines the order the rows will be sorted in.
pageByis null - meaning start from the beginning. Once the first set has been fetched, the
boundingValueis the value of the paging property in the last object returned.
ManagedObjectController<T>as an example.)
pageByquery will return an empty list of objects when no more values are left. If the number of objects remaining in the last page are less than the
fetchLimit, only those objects will be returned. For example, if there four more objects left and the
fetchLimitis 10, the number of objects returned will be four.
wheremethod is a safe and elegant way to add this criteria to a query. This method allows you to assign boolean expressions to the properties of the object being queried. Each expression is added to the WHERE clause of the generated SQL query. Here's an example of a query that finds a
idequal to 1:
SELECT _user.id, _user.name, ... FROM _user WHERE _user.id = 1.)
equalTo- see the documentation for
QueryExpression<T>for a complete list.
wheremultiple times. Each criteria is combined together with a logical 'and'. For example, the following query will find all users whose
nameis "Bob" and
identifiedByexpression for finding objects that belong to a specific object. For example, when finding all employees for a given company:
Query<T>can also fetch relationship properties. This allows queries to fetch entire model graphs and reduces the number of round-trips to a database.
asMap(). For example, consider the following definitions, where a
Query<User>will fetch the
tasksare not fetched, so the data returned looks like this:
join()method will tell a query to also include related objects. The following shows a fetch that gets users and their tasks:
set:argument takes a property selector that must select a
ManagedSet. (When fetching a has-one or belongs-to relationship, use the
join()returns a new
Tis the type of the joined object. That is, the above code could also be written as such:
Children. When fetching parents and joining their children, a
whereexpression on the join query impacts which children are returned, but does not impact which parents are returned. For example, the following query would fetch every parent, but would only include children who are greater than 1 years old:
haveAtLeastOneWhereis specific to has-many relationships. When selecting properties of a has-one or belongs-to relationship, you access the property directly:
max. Here's an example:
Query<T>also impact the
reducefunction. For example, applying a
Query.whereand then executing a
sumfunction will only sum the rows that meet the criteria of the where clause:
PersistentStore.execute. Note that the objects returned will be a
List<List<dynamic>>- a list of rows, for each a list of columns.
Stringthat is set as the query's where clause. A
QueryPredicatehas two properties, a format string and a
Map<String, dynamic>of parameter values. The
formatstring can (and should) parameterize any input values. Parameters are indicated in the format string using the
@token may contain
[A-Za-z0-9_]. The resulting where clause will be formed by replacing each token with the matching key in the parameters map. The value is not transformed in any way, so it must be the appropriate type for the column. If a key is not present in the
Map, an exception will be thrown. Extra keys will be ignored.