Query

public class Query

A database query. A Query instance can be constructed by calling one of the select class methods.

  • Returns the Parameters object used for setting values to the query parameters defined in the query. All parameters defined in the query must be given values before running the query, or the query will fail.

    Declaration

    Swift

    public var parameters: Parameters?
  • Create a SELECT statement instance that you can use further (e.g. calling the from() function) to construct the complete query statement.

    Declaration

    Swift

    public static func select(_ results: SelectResult...) -> Select

    Parameters

    results

    The array of the SelectResult object for specifying the returned values.

    Return Value

    A Select object.

  • Create a SELECT DISTINCT statement instance that you can use further (e.g. calling the from() function) to construct the complete query statement.

    Declaration

    Swift

    public static func selectDistinct(_ results: SelectResult...) -> Select

    Parameters

    results

    The array of the SelectResult object for specifying the returned values.

    Return Value

    A Select distinct object.

  • Executes the query. The 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 failure, or if the query is invalid.

    Declaration

    Swift

    public func execute() throws -> ResultSet

    Return Value

    The ResultSet object representing the query result.

  • 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.

  • Throws

    An error if the query is not valid.

    Declaration

    Swift

    public func explain() throws -> String

    Return Value

    The implementation detail of the compiled query.

  • Adds a query change listener. Changes will be posted on the main queue.

    Declaration

    Swift

    @discardableResult public func addChangeListener(
            _ listener: @escaping (QueryChange) -> Void) -> ListenerToken

    Parameters

    listener

    The listener to post changes.

    Return Value

    An opaque listener token object for removing the listener.

  • Adds a query change listener with the dispatch queue on which changes will be posted. If the dispatch queue is not specified, the changes will be posted on the main queue.

    Declaration

    Swift

    @discardableResult public func addChangeListener(withQueue queue: DispatchQueue?,
            _ listener: @escaping (QueryChange) -> Void) -> ListenerToken

    Parameters

    queue

    The dispatch queue.

    listener

    The listener to post changes.

    Return Value

    An opaque listener token object for removing the listener.

  • Removes a change listener wih the given listener token.

    Declaration

    Swift

    public func removeChangeListener(withToken token: ListenerToken)

    Parameters

    token

    The listener token.

  • Declaration

    Swift

    public var description: String