Query

public class Query

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 wher: Predicate? = nil,
                    groupBy: [Expression]? = nil,
                    having: Predicate? = nil,
                    returning: [Expression]? = nil,
                    distinct: Bool = false,
                    orderBy: [SortDescriptor]? = nil)
  • 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.

    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 see "SCAN TABLE", it means that SQLite is doing a
    slow linear scan of the documents instead of using an index.
    

    Declaration

    Swift

    public func explain() throws -> String
  • 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.

    Declaration

    Swift

    public func run() throws -> QueryIterator
  • A convenience method equivalent to -run: except that its enumerator returns Documents directly, not QueryRows.

    Declaration

    Swift

    public func allDocuments() throws -> DocumentIterator