21#include "cbl/CBLQuery.h"
36 class Query :
private RefCounted {
54 internal::check(q, error);
64 inline std::vector<std::string>
columnNames()
const;
213 std::vector<std::string> cols;
215 for (
unsigned i = 0; i < n ; ++i) {
217 cols.push_back(name.asString());
225 internal::check(rs, error);
226 return ResultSet::adopt(rs);
242 ,_query(std::move(query))
249 throw std::runtime_error(
"Not allowed to call on uninitialized ChangeListeners");
251 return getResults(_query,
token());
258 internal::check(rs, error);
259 return ResultSet::adopt(rs);
274 return ChangeListener::getResults(_query, _token);
314 return Query(*
this, language, queryString);
#define CBL_REFCOUNTED_BOILERPLATE(CLASS, SUPER, C_TYPE)
Definition Base.hh:160
#define CBL_ASSUME_NONNULL_END
#define CBL_ASSUME_NONNULL_BEGIN
A Couchbase Lite database, which is a container for collections of documents.
Definition Database.hh:147
CBLDatabase *_Nullable ref() const
Returns a pointer to the underlying C object (CBLDatabase), or NULL if this is a null reference.
Definition Database.hh:411
Query createQuery(QueryLanguage language, std::string_view queryString)
Creates a new query by compiling the input string.
Definition Query.hh:313
static void call(void *_Nullable context, Args... args)
Definition Base.hh:228
CBLListenerToken *_Nullable token() const
Definition Base.hh:221
std::function< void(Args...)> Callback
The type of the user callback that this token holds.
Definition Base.hh:180
The change passed to a live query's listener callback, giving access to the updated results.
Definition Query.hh:267
ResultSet results()
Returns the query's results as of this change.
Definition Query.hh:273
friend class Query
Definition Query.hh:283
Query query()
Returns the query that produced this change.
Definition Query.hh:278
Change(const Change &src)
Copy constructor.
Definition Query.hh:270
The token returned by Query::addChangeListener for a live query.
Definition Query.hh:232
ChangeListener(Query query, Callback cb)
Creates a change listener token bound to a specific query and callback.
Definition Query.hh:240
ChangeListener()
Creates an empty, uninitialized change listener token.
Definition Query.hh:235
ResultSet results()
Returns the most recent results computed by the live query.
Definition Query.hh:247
A database query.
Definition Query.hh:36
std::vector< std::string > columnNames() const
Returns the column names that will appear in the query results.
Definition Query.hh:211
CBLQuery *_Nullable ref() const
Returns a pointer to the underlying C object (CBLQuery), or NULL if this is a null reference.
Definition Query.hh:108
fleece::Dict parameters() const
Returns the query's current parameter bindings, if any.
Definition Query.hh:78
Query() noexcept
Constructs a null reference (one that points to no object).
Definition Query.hh:108
ChangeListener addChangeListener(ListenerToken< Change >::Callback callback)
Registers a change listener callback to the query, turning it into a "live query" until the listener ...
Definition Query.hh:291
ResultSet execute()
Runs the query, returning the results.
Definition Query.hh:222
std::string explain()
Returns information about the query, including the translated SQLite form, and the search strategy.
Definition Query.hh:87
Query(const Database &db, CBLQueryLanguage language, std::string_view queryString)
Creates a new query by compiling the input string.
Definition Query.hh:51
void setParameters(fleece::Dict parameters)
Assigns values to the query's parameters.
Definition Query.hh:75
A single query result; ResultSet::iterator iterates over these.
Definition Query.hh:113
fleece::Value operator[](int i) const
A subscript operator that returns value of a column of the current result, given its (zero-based) num...
Definition Query.hh:143
fleece::Value valueForKey(std::string_view key) const
Returns the value of a column of the current result, given its column name.
Definition Query.hh:138
CBLResultSet *_Nullable _ref
Definition Query.hh:150
friend class ResultSetIterator
Definition Query.hh:151
alloc_slice toJSON() const
Returns the current result as a JSON dictionary string.
Definition Query.hh:122
Result(CBLResultSet *_Nullable ref)
Definition Query.hh:149
fleece::Value valueAtIndex(unsigned i) const
Returns the value of a column of the current result, given its (zero-based) numeric index.
Definition Query.hh:130
fleece::Value operator[](std::string_view key) const
A subscript operator that returns the value of a column of the current result, given its column name.
Definition Query.hh:146
uint64_t count() const
Returns the number of columns in the current result.
Definition Query.hh:117
The results of a query.
Definition Query.hh:155
iterator end()
Returns an iterator marking the end of the results.
Definition Query.hh:307
friend class Query
Definition Query.hh:172
ResultSet() noexcept
Constructs a null reference (one that points to no object).
Definition Query.hh:173
iterator begin()
Returns an iterator positioned at the first result.
Definition Query.hh:303
ResultSetIterator iterator
Definition Query.hh:157
Single-pass iterator over a ResultSet, yielding each Result in turn.
Definition Query.hh:178
const Result & operator*() const
Returns the current Result.
Definition Query.hh:181
ResultSetIterator()
Definition Query.hh:197
ResultSetIterator & operator++()
Advances to the next result, or to end-of-results if there are no more.
Definition Query.hh:191
friend class ResultSet
Definition Query.hh:206
const Result & operator->() const
Allows access to the current Result via ->.
Definition Query.hh:183
ResultSet _rs
Definition Query.hh:204
bool operator!=(const ResultSetIterator &i) const
Returns true if the iterators differ.
Definition Query.hh:188
ResultSetIterator(ResultSet rs)
Definition Query.hh:198
Result _result
Definition Query.hh:205
bool operator==(const ResultSetIterator &i) const
Returns true if both iterators reference the same underlying result set position.
Definition Query.hh:186
FLEECE_PUBLIC FLStringResult FLValue_ToJSON(FLValue FL_NULLABLE)
struct CBLListenerToken CBLListenerToken
FLDict _cbl_nullable CBLQuery_Parameters(const CBLQuery *query)
CBLQuery * CBLResultSet_GetQuery(const CBLResultSet *rs)
void CBLQuery_SetParameters(CBLQuery *query, FLDict parameters)
_cbl_warn_unused FLSliceResult CBLQuery_Explain(const CBLQuery *)
FLSlice CBLQuery_ColumnName(const CBLQuery *, unsigned columnIndex)
FLValue _cbl_nullable CBLResultSet_ValueForKey(const CBLResultSet *, FLString key)
FLDict CBLResultSet_ResultDict(const CBLResultSet *)
_cbl_warn_unused CBLResultSet *_cbl_nullable CBLQuery_Execute(CBLQuery *, CBLError *_cbl_nullable outError)
_cbl_warn_unused bool CBLResultSet_Next(CBLResultSet *)
struct CBLResultSet CBLResultSet
_cbl_warn_unused CBLQuery *_cbl_nullable CBLDatabase_CreateQuery(const CBLDatabase *db, CBLQueryLanguage language, FLString queryString, int *_cbl_nullable outErrorPos, CBLError *_cbl_nullable outError)
FLValue _cbl_nullable CBLResultSet_ValueAtIndex(const CBLResultSet *, unsigned index)
_cbl_warn_unused CBLListenerToken * CBLQuery_AddChangeListener(CBLQuery *query, CBLQueryChangeListener listener, void *_cbl_nullable context)
_cbl_warn_unused CBLResultSet *_cbl_nullable CBLQuery_CopyCurrentResults(const CBLQuery *query, CBLListenerToken *listener, CBLError *_cbl_nullable outError)
unsigned CBLQuery_ColumnCount(const CBLQuery *)
struct CBLRefCounted CBLRefCounted
const struct _FLDict * FLDict
const struct _FLValue * FLValue
fleece::slice slice
Convenience alias for fleece::slice, a non-owning view of a byte range.
Definition Base.hh:49
fleece::alloc_slice alloc_slice
Convenience alias for fleece::alloc_slice, an owning byte buffer.
Definition Base.hh:51