Query

open class Query : OptionalRustObject

This class allows you to construct a query, bind values to variables and run those queries against a mentat DB.

This class cannot be created directly, but must be created through Mentat.query(String:).

The types of values you can bind are

  • Int64
  • Entid
  • Keyword
  • Bool
  • Double
  • Date
  • String
  • UUID.

Each bound variable must have a corresponding value in the query string used to create this query.

let query = """
           [:find ?name ?cat
            :in ?type
            :where
            [?c :community/name ?name]
            [?c :community/type ?type]
            [?c :community/category ?cat]]
           """
mentat.query(query: query)
       .bind(varName: "?type", toKeyword: ":community.type/website")
       .run { result in
            ...
        }

Queries can be run and the results returned in a number of different formats. Individual result values are returned as TypedValues and the format differences relate to the number and structure of those values. The result format is related to the format provided in the query string.

  • Rel - This is the default run function and returns a list of rows of values. Queries that wish to have Rel results should format their query strings: let query = """ [: find ?a ?b ?c : where ... ] """ mentat.query(query: query) .run { result in ... }
  • Scalar - This returns a single value as a result. This can be optional, as the value may not be present. Queries that wish to have Scalar results should format their query strings: let query = """ [: find ?a . : where ... ] """ mentat.query(query: query) .runScalar { result in ... }
  • Coll - This returns a list of single values as a result. Queries that wish to have Coll results should format their query strings: let query = """ [: find [?a ...] : where ... ] """ mentat.query(query: query) .runColl { result in ... }
  • Tuple - This returns a single row of values. Queries that wish to have Tuple results should format their query strings: let query = """ [: find [?a ?b ?c] : where ... ] """ mentat.query(query: query) .runTuple { result in ... }
  • Binds a Int64 value to the provided variable name.

    Throws

    PointerError.pointerConsumed if the underlying raw pointer has already consumed, which will occur if the query has already been executed.

    Declaration

    Swift

    open func bind(varName: String, toLong value: Int64) throws -> Query

    Parameters

    varName

    The name of the variable in the format ?name.

    value

    The value to be bound

    Return Value

    This Query such that further function can be called.

  • Binds a Entid value to the provided variable name.

    Throws

    PointerError.pointerConsumed if the underlying raw pointer has already consumed, which will occur if the query has already been executed.

    Declaration

    Swift

    open func bind(varName: String, toReference value: Entid) throws -> Query

    Parameters

    varName

    The name of the variable in the format ?name.

    value

    The value to be bound

    Return Value

    This Query such that further function can be called.

  • Binds a String value representing a keyword for an attribute to the provided variable name. Keywords take the format :namespace/name.

    Throws

    PointerError.pointerConsumed if the underlying raw pointer has already consumed, which will occur if the query has already been executed.

    Declaration

    Swift

    open func bind(varName: String, toReference value: String) throws -> Query

    Parameters

    varName

    The name of the variable in the format ?name.

    value

    The value to be bound

    Return Value

    This Query such that further function can be called.

  • Binds a keyword String value to the provided variable name. Keywords take the format :namespace/name.

    Throws

    PointerError.pointerConsumed if the underlying raw pointer has already consumed, which will occur if the query has already been executed.

    Declaration

    Swift

    open func bind(varName: String, toKeyword value: String) throws -> Query

    Parameters

    varName

    The name of the variable in the format ?name.

    value

    The value to be bound

    Return Value

    This Query such that further function can be called.

  • Binds a Bool value to the provided variable name.

    Throws

    PointerError.pointerConsumed if the underlying raw pointer has already consumed, which will occur if the query has already been executed.

    Declaration

    Swift

    open func bind(varName: String, toBoolean value: Bool) throws -> Query

    Parameters

    varName

    The name of the variable in the format ?name.

    value

    The value to be bound

    Return Value

    This Query such that further function can be called.

  • Binds a Double value to the provided variable name.

    Throws

    PointerError.pointerConsumed if the underlying raw pointer has already consumed, which will occur if the query has already been executed.

    Declaration

    Swift

    open func bind(varName: String, toDouble value: Double) throws -> Query

    Parameters

    varName

    The name of the variable in the format ?name.

    value

    The value to be bound

    Return Value

    This Query such that further function can be called.

  • Binds a Date value to the provided variable name.

    Throws

    PointerError.pointerConsumed if the underlying raw pointer has already consumed, which will occur if the query has already been executed.

    Declaration

    Swift

    open func bind(varName: String, toDate value: Date) throws -> Query

    Parameters

    varName

    The name of the variable in the format ?name.

    value

    The value to be bound

    Return Value

    This Query such that further function can be called.

  • Binds a String value to the provided variable name.

    Throws

    PointerError.pointerConsumed if the underlying raw pointer has already consumed, which will occur if the query has already been executed.

    Declaration

    Swift

    open func bind(varName: String, toString value: String) throws -> Query

    Parameters

    varName

    The name of the variable in the format ?name.

    value

    The value to be bound

    Return Value

    This Query such that further function can be called.

  • Binds a UUID value to the provided variable name.

    Throws

    PointerError.pointerConsumed if the underlying raw pointer has already consumed, which will occur if the query has already been executed.

    Declaration

    Swift

    open func bind(varName: String, toUuid value: UUID) throws -> Query

    Parameters

    varName

    The name of the variable in the format ?name.

    value

    The value to be bound

    Return Value

    This Query such that further function can be called.

  • Execute the query with the values bound associated with this Query and call the provided callback function with the results as a list of rows of TypedValues.

    Throws

    QueryError.executionFailed if the query fails to execute. This could be because the provided query did not parse, or that variable we incorrectly bound, or that the query provided was not Rel.

    Throws

    PointerError.pointerConsumed if the underlying raw pointer has already consumed, which will occur if the query has previously been executed.

    Declaration

    Swift

    open func run(callback: @escaping (RelResult?) -> Void) throws

    Parameters

    callback

    the function to call with the results of this query

  • Execute the query with the values bound associated with this Query and call the provided callback function with the result as a single TypedValue.

    Throws

    QueryError.executionFailed if the query fails to execute. This could be because the provided query did not parse, that variable we incorrectly bound, or that the query provided was not Scalar.

    Throws

    PointerError.pointerConsumed if the underlying raw pointer has already consumed, which will occur if the query has previously been executed.

    Declaration

    Swift

    open func runScalar(callback: @escaping (TypedValue?) -> Void) throws

    Parameters

    callback

    the function to call with the results of this query

  • Execute the query with the values bound associated with this Query and call the provided callback function with the result as a list of single TypedValues.

    Throws

    QueryError.executionFailed if the query fails to execute. This could be because the provided query did not parse, that variable we incorrectly bound, or that the query provided was not Coll.

    Throws

    PointerError.pointerConsumed if the underlying raw pointer has already consumed, which will occur if the query has previously been executed.

    Declaration

    Swift

    open func runColl(callback: @escaping (ColResult?) -> Void) throws

    Parameters

    callback

    the function to call with the results of this query

  • Execute the query with the values bound associated with this Query and call the provided callback function with the result as a list of single TypedValues.

    Throws

    QueryError.executionFailed if the query fails to execute. This could be because the provided query did not parse, that variable we incorrectly bound, or that the query provided was not Tuple.

    Throws

    PointerError.pointerConsumed if the underlying raw pointer has already consumed, which will occur if the query has previously been executed.

    Declaration

    Swift

    open func runTuple(callback: @escaping (TupleResult?) -> Void) throws

    Parameters

    callback

    the function to call with the results of this query

  • Undocumented

    Declaration

    Swift

    override open func cleanup(pointer: OpaquePointer)