PredicateQuery
public class PredicateQuery
A compiled database query, similar to a N1QL or SQL query.
-
Creates a database query from the component pieces of a SELECT statement, all of which are optional except FROM.
Declaration
Swift
public init(from db: Database, where wherePredicate: Predicate? = nil, groupBy: [PredicateExpression]? = nil, having: Predicate? = nil, returning: [PredicateExpression]? = nil, distinct: Bool = false, orderBy: [SortDescriptor]? = nil)
Parameters
db
The database.
wherePredicate
The where predicate.
groupBy
The groupby expressions.
having
THe having predicate.
returning
The returning values.
distinct
The distinct flag.
orderBy
The order by as an array of the SortDescriptor objects.
-
The database being queried.
Declaration
Swift
public let database: Database
-
The number of result rows to skip; corresponds to the OFFSET property of a SQL or N1QL query. This can be useful for
paging
through a large query, but skipping many rows is slow. Defaults to 0.Declaration
Swift
public var offset: UInt = 0
-
The maximum number of rows to return; corresponds to the LIMIT property of a SQL or N1QL query. Defaults to unlimited.
Declaration
Swift
public var limit: UInt = UInt.max
-
Values to substitute for placeholder parameters defined in the query. Defaults to nil. The dictionary’s keys are parameter names, and values are the values to use. All parameters must be given values before running the query, or it will fail.
Declaration
Swift
public var parameters: [String : Any] = [:]
-
Checks whether the query is valid without running it.
Throws
An error if the query is not valid.Declaration
Swift
public func check() throws
-
Returns a string describing the implementation of the compiled query. This is intended to be read by a developer for purposes of optimizing the query, especially to add database indexes. It’s not machine-readable and its format may change.
As currently implemented, the result is two or more lines separated by newline characters:
- The first line is the SQLite SELECT statement.
The subsequent lines are the output of SQLite’s
EXPLAIN QUERY PLAN
command applied to that statement; for help interpreting this, see https://www.sqlite.org/eqp.html . The most important thing to know is that if you seeSCAN TABLE
, it means that SQLite is doing a slow linear scan of the documents instead of using an index.Throws
An error if the query is not valid.
Declaration
Swift
public func explain() throws -> String
Return Value
The compilied query description.
-
Runs the query, using the current settings (skip, limit, parameters), returning an enumerator that returns result rows one at a time. You can run the query any number of times, and you can even have multiple enumerators active at once. The results come from a snapshot of the database taken at the moment -run: is called, so they will not reflect any changes made to the database afterwards.
Throws
An error on a failure.Declaration
Swift
public func run() throws -> QueryIterator
Return Value
The QueryIterator object.
-
A convenience method equivalent to -run: except that its enumerator returns Documents directly, not QueryRows.
Throws
An error on a failure.Declaration
Swift
public func allDocuments() throws -> DocumentIterator
Return Value
The DocumentIterator.