Couchbase Lite C
Couchbase Lite C API
|
A CBLQuery represents a compiled database query. More...
Typedefs | |
typedef struct CBLQuery | CBLQuery |
A compiled database query. More... | |
typedef struct CBLResultSet | CBLResultSet |
An iterator over the rows resulting from running a query. More... | |
Change listener | |
Adding a change listener to a query turns it into a "live query". When changes are made to documents, the query will periodically re-run and compare its results with the prior results; if the new results are different, the listener callback will be called.
| |
typedef void(* | CBLQueryChangeListener) (void *_cbl_nullable context, CBLQuery *query, CBLListenerToken *token) |
A callback to be invoked after the query's results have changed. More... | |
_cbl_warn_unused CBLListenerToken * | CBLQuery_AddChangeListener (CBLQuery *query, CBLQueryChangeListener listener, void *_cbl_nullable context) |
Registers a change listener callback with a query, turning it into a "live query" until the listener is removed (via CBLListener_Remove). More... | |
_cbl_warn_unused CBLResultSet *_cbl_nullable | CBLQuery_CopyCurrentResults (const CBLQuery *query, CBLListenerToken *listener, CBLError *_cbl_nullable outError) |
Returns the query's entire current result set, after it's been announced via a call to the listener's callback. More... | |
Query objects | |
_cbl_warn_unused CBLQuery *_cbl_nullable | CBLDatabase_CreateQuery (const CBLDatabase *db, CBLQueryLanguage language, FLString queryString, int *_cbl_nullable outErrorPos, CBLError *_cbl_nullable outError) |
Creates a new query by compiling the input string. More... | |
static const CBLQuery * | CBLQuery_Retain (const CBLQuery *t) |
static void | CBLQuery_Release (const CBLQuery *t) |
void | CBLQuery_SetParameters (CBLQuery *query, FLDict parameters) |
Assigns values to the query's parameters. More... | |
FLDict _cbl_nullable | CBLQuery_Parameters (const CBLQuery *query) |
Returns the query's current parameter bindings, if any. More... | |
_cbl_warn_unused CBLResultSet *_cbl_nullable | CBLQuery_Execute (CBLQuery *, CBLError *_cbl_nullable outError) |
Runs the query, returning the results. More... | |
_cbl_warn_unused FLSliceResult | CBLQuery_Explain (const CBLQuery *) |
Returns information about the query, including the translated SQLite form, and the search strategy. More... | |
unsigned | CBLQuery_ColumnCount (const CBLQuery *) |
Returns the number of columns in each result. More... | |
FLSlice | CBLQuery_ColumnName (const CBLQuery *, unsigned columnIndex) |
Returns the name of a column in the result. More... | |
Result sets | |
A It exposes one result at a time – as a collection of values indexed either by position or by name – and can be stepped from one result to the next. It's important to note that the initial position of the iterator is before the first result, so CBLResultSet_Next must be called first. Example: CBLResultSet *rs = CBLQuery_Execute(query, &error);
assert(rs);
while (CBLResultSet_Next(rs) {
FLValue aValue = CBLResultSet_ValueAtIndex(rs, 0);
...
}
CBLResultSet_Release(rs);
_cbl_warn_unused CBLResultSet *_cbl_nullable CBLQuery_Execute(CBLQuery *, CBLError *_cbl_nullable outError) Runs the query, returning the results. _cbl_warn_unused bool CBLResultSet_Next(CBLResultSet *) Moves the result-set iterator to the next result. struct CBLResultSet CBLResultSet An iterator over the rows resulting from running a query. Definition: CBLBase.h:218 static void CBLResultSet_Release(const CBLResultSet *t) Definition: CBLQuery.h:170 FLValue _cbl_nullable CBLResultSet_ValueAtIndex(const CBLResultSet *, unsigned index) Returns the value of a column of the current result, given its (zero-based) numeric index. | |
_cbl_warn_unused bool | CBLResultSet_Next (CBLResultSet *) |
Moves the result-set iterator to the next result. More... | |
FLValue _cbl_nullable | CBLResultSet_ValueAtIndex (const CBLResultSet *, unsigned index) |
Returns the value of a column of the current result, given its (zero-based) numeric index. More... | |
FLValue _cbl_nullable | CBLResultSet_ValueForKey (const CBLResultSet *, FLString key) |
Returns the value of a column of the current result, given its name. More... | |
FLArray | CBLResultSet_ResultArray (const CBLResultSet *) |
Returns the current result as an array of column values. More... | |
FLDict | CBLResultSet_ResultDict (const CBLResultSet *) |
Returns the current result as a dictionary mapping column names to values. More... | |
CBLQuery * | CBLResultSet_GetQuery (const CBLResultSet *rs) |
Returns the Query that created this ResultSet. More... | |
static const CBLResultSet * | CBLResultSet_Retain (const CBLResultSet *t) |
static void | CBLResultSet_Release (const CBLResultSet *t) |
A CBLQuery represents a compiled database query.
The query language is a large subset of the N1QL language from Couchbase Server, which you can think of as "SQL for JSON" or "SQL++".
Supported Query languages: N1QL
JSON language resembles a parse tree of N1QL. The JSON syntax is harder for humans, but much more amenable to machine generation, if you need to create queries programmatically or translate them from some other form.
typedef void(* CBLQueryChangeListener) (void *_cbl_nullable context, CBLQuery *query, CBLListenerToken *token) |
A callback to be invoked after the query's results have changed.
The actual result set can be obtained by calling CBLQuery_CopyCurrentResults, either during the callback or at any time thereafter.
context | The same context value that you passed when adding the listener. |
query | The query that triggered the listener. |
token | The token for obtaining the query results by calling CBLQuery_CopyCurrentResults. |
typedef struct CBLResultSet CBLResultSet |
An iterator over the rows resulting from running a query.
_cbl_warn_unused CBLQuery *_cbl_nullable CBLDatabase_CreateQuery | ( | const CBLDatabase * | db, |
CBLQueryLanguage | language, | ||
FLString | queryString, | ||
int *_cbl_nullable | outErrorPos, | ||
CBLError *_cbl_nullable | outError | ||
) |
Creates a new query by compiling the input string.
This is fast, but not instantaneous. If you need to run the same query many times, keep the CBLQuery around instead of compiling it each time. If you need to run related queries with only some values different, create one query with placeholder parameter(s), and substitute the desired value(s) with CBLQuery_SetParameters each time you run the query.
db | The database to query. |
language | The query language, JSON or N1QL. |
queryString | The query string. |
outErrorPos | If non-NULL, then on a parse error the approximate byte offset in the input expression will be stored here (or -1 if not known/applicable.) |
outError | On failure, the error will be written here. |
_cbl_warn_unused CBLListenerToken * CBLQuery_AddChangeListener | ( | CBLQuery * | query, |
CBLQueryChangeListener | listener, | ||
void *_cbl_nullable | context | ||
) |
Registers a change listener callback with a query, turning it into a "live query" until the listener is removed (via CBLListener_Remove).
When the first change listener is added, the query will run (in the background) and notify the listener(s) of the results when ready. After that, it will run in the background after the database changes, and only notify the listeners when the result set changes.
query | The query to observe. |
listener | The callback to be invoked. |
context | An opaque value that will be passed to the callback. |
unsigned CBLQuery_ColumnCount | ( | const CBLQuery * | ) |
Returns the number of columns in each result.
Returns the name of a column in the result.
The column name is based on its expression in the SELECT...
or WHAT:
section of the query. A column that returns a property or property path will be named after that property. A column that returns an expression will have an automatically-generated name like $1
. To give a column a custom name, use the AS
syntax in the query. Every column is guaranteed to have a unique name.
_cbl_warn_unused CBLResultSet *_cbl_nullable CBLQuery_CopyCurrentResults | ( | const CBLQuery * | query, |
CBLListenerToken * | listener, | ||
CBLError *_cbl_nullable | outError | ||
) |
Returns the query's entire current result set, after it's been announced via a call to the listener's callback.
query | The query being listened to. |
listener | The query listener that was notified. |
outError | If the query failed to run, the error will be stored here. |
_cbl_warn_unused CBLResultSet *_cbl_nullable CBLQuery_Execute | ( | CBLQuery * | , |
CBLError *_cbl_nullable | outError | ||
) |
Runs the query, returning the results.
To obtain the results you'll typically call CBLResultSet_Next in a while
loop, examining the values in the CBLResultSet each time around.
_cbl_warn_unused FLSliceResult CBLQuery_Explain | ( | const CBLQuery * | ) |
Returns information about the query, including the translated SQLite form, and the search strategy.
You can use this to help optimize the query: the word SCAN
in the strategy indicates a linear scan of the entire database, which should be avoided by adding an index. The strategy will also show which index(es), if any, are used.
FLDict _cbl_nullable CBLQuery_Parameters | ( | const CBLQuery * | query | ) |
Returns the query's current parameter bindings, if any.
|
inlinestatic |
Assigns values to the query's parameters.
These values will be substited for those parameters whenever the query is executed, until they are next assigned.
Parameters are specified in the query source as e.g. $PARAM
(N1QL) or ["$PARAM"]
(JSON). In this example, the parameters
dictionary to this call should have a key PARAM
that maps to the value of the parameter.
query | The query. |
parameters | The parameters in the form of a Fleece dictionary whose keys are the parameter names. (It's easiest to construct this by using the mutable API, i.e. calling FLMutableDict_New and adding keys/values.) |
CBLQuery * CBLResultSet_GetQuery | ( | const CBLResultSet * | rs | ) |
Returns the Query that created this ResultSet.
_cbl_warn_unused bool CBLResultSet_Next | ( | CBLResultSet * | ) |
Moves the result-set iterator to the next result.
Returns false if there are no more results.
|
inlinestatic |
FLArray CBLResultSet_ResultArray | ( | const CBLResultSet * | ) |
Returns the current result as an array of column values.
FLDict CBLResultSet_ResultDict | ( | const CBLResultSet * | ) |
Returns the current result as a dictionary mapping column names to values.
|
inlinestatic |
FLValue _cbl_nullable CBLResultSet_ValueAtIndex | ( | const CBLResultSet * | , |
unsigned | index | ||
) |
Returns the value of a column of the current result, given its (zero-based) numeric index.
This may return a NULL pointer, indicating MISSING
, if the value doesn't exist, e.g. if the column is a property that doesn't exist in the document.
FLValue _cbl_nullable CBLResultSet_ValueForKey | ( | const CBLResultSet * | , |
FLString | key | ||
) |
Returns the value of a column of the current result, given its name.
This may return a NULL pointer, indicating MISSING
, if the value doesn't exist, e.g. if the column is a property that doesn't exist in the document. (Or, of course, if the key is not a column name in this query.)