Building with Builders
Yet another option is to use , which offers much richer query‑building options, particularly for nasty queries involving things like the union of multiple sub‑query results. More importantly, the interface dovetails nicely with the interface for executing queries. Hence, a common pattern for your content provider’s implementation is to create a , fill in some defaults, then allow it to build up (and optionally execute) the full query combining the defaults with what is provided to the content provider on the query request.
For example, here is a snippet of code from a content provider using :
Public query
New SQLiteQueryBuilder
SetTables getTableName
If isCollectionUri
SetProjectionMap getDefaultProjection
Else
AppendWhere getIdColumnName getPathSegments get
If isEmpty
GetDefaultSortOrder
Else
Query
Null null
SetNotificationUri getContext getContentResolver
Return
Content providers are explained in greater detail in Part 5 of this book, so some of this you will have to take on faith until then. Here, we see the following:
1. A is constructed.
2. It is told the table to use for the query ().
3. It is either told the default set of columns to return (), or is given a piece of a clause to identify a particular row in the table by an identifier extracted from the Uri supplied to the call ().
4. Finally, it is told to execute the query, blending the preset values with those supplied on the call to ().
Instead of having the execute the query directly, we could have called to have it generate and return the SQL statement we needed, which we could then execute ourselves.
Дата добавления: 2015-05-16; просмотров: 781;