# QueryableSource Protocol Reference

# QueryableSource

    public protocol QueryableSource : AnyObject

Encapsulates containers which manages an internal `NSManagedObjectContext`, such as `DataStack`s and transactions, that can be used for querying values. CoreStore provides implementations for this protocol and should be used as a read-only abstraction.

- `queryValue(_:_:_:)`

  Queries aggregate values as specified by the `QueryClause`s. Requires at least a `Select` clause, and optional `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.

  A query differs from a fetch in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.

  #### Declaration

  Swift

      func queryValue<D, U>(_ from: From<D>, _ selectClause: Select<D, U>, _ queryClauses: QueryClause...) -> U? where D : DynamicObject, U : QueryableAttributeType

  #### Parameters

  `from` | a `From` clause indicating the entity type
  `selectClause` | a `Select<U>` clause indicating the properties to fetch, and with the generic type indicating the return type.
  `queryClauses` | a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.

  #### Return Value

  the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.

- `queryValue(_:_:_:)`

  Queries aggregate values as specified by the `QueryClause`s. Requires at least a `Select` clause, and optional `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.

  A query differs from a fetch in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.

  #### Declaration

  Swift

      func queryValue<D, U>(_ from: From<D>, _ selectClause: Select<D, U>, _ queryClauses: [QueryClause]) -> U? where D : DynamicObject, U : QueryableAttributeType

  #### Parameters

  `from` | a `From` clause indicating the entity type
  `selectClause` | a `Select<U>` clause indicating the properties to fetch, and with the generic type indicating the return type.
  `queryClauses` | a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.

  #### Return Value

  the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.

- `queryValue(_:)`

  Queries a property value or aggregate as specified by the `QueryChainableBuilderType` built from a chain of clauses.

  A query differs from a fetch in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.

      let averageAdultAge = dataStack.queryValue(
          From<MyPersonEntity>()
              .select(Int.self, .average(\.age))
              .where(\.age > 18)
      )

  #### Declaration

  Swift

      func queryValue<B>(_ clauseChain: B) -> B.ResultType? where B : QueryChainableBuilderType, B.ResultType : QueryableAttributeType

  #### Parameters

  `clauseChain` | a `QueryChainableBuilderType` indicating the property/aggregate to fetch and the series of queries for the request.

  #### Return Value

  the result of the the query as specified by the `QueryChainableBuilderType`

- `queryAttributes(_:_:_:)`

  Queries a dictionary of attribute values as specified by the `QueryClause`s. Requires at least a `Select` clause, and optional `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.

  A query differs from a fetch in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.

  #### Declaration

  Swift

      func queryAttributes<D>(_ from: From<D>, _ selectClause: Select<D, NSDictionary>, _ queryClauses: QueryClause...) -> [[String : Any]]? where D : DynamicObject

  #### Parameters

  `from` | a `From` clause indicating the entity type
  `selectClause` | a `Select<U>` clause indicating the properties to fetch, and with the generic type indicating the return type.
  `queryClauses` | a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.

  #### Return Value

  the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.

- `queryAttributes(_:_:_:)`

  Queries a dictionary of attribute values as specified by the `QueryClause`s. Requires at least a `Select` clause, and optional `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.

  A query differs from a fetch in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.

  #### Declaration

  Swift

      func queryAttributes<D>(_ from: From<D>, _ selectClause: Select<D, NSDictionary>, _ queryClauses: [QueryClause]) -> [[String : Any]]? where D : DynamicObject

  #### Parameters

  `from` | a `From` clause indicating the entity type
  `selectClause` | a `Select<U>` clause indicating the properties to fetch, and with the generic type indicating the return type.
  `queryClauses` | a series of `QueryClause` instances for the query request. Accepts `Where`, `OrderBy`, `GroupBy`, and `Tweak` clauses.

  #### Return Value

  the result of the the query. The type of the return value is specified by the generic type of the `Select<U>` parameter.

- `queryAttributes(_:)`

  Queries a dictionary of attribute values or as specified by the `QueryChainableBuilderType` built from a chain of clauses.

  A query differs from a fetch in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.

      let results = source.queryAttributes(
          From<MyPersonEntity>()
              .select(
                  NSDictionary.self,
                  .attribute(\.age, as: "age"),
                  .count(\.age, as: "numberOfPeople")
              )
              .groupBy(\.age)
      )
      for dictionary in results! {
          let age = dictionary["age"] as! Int
          let count = dictionary["numberOfPeople"] as! Int
          print("There are \(count) people who are \(age) years old."
      }

  #### Declaration

  Swift

      func queryAttributes<B>(_ clauseChain: B) -> [[String : Any]]? where B : QueryChainableBuilderType, B.ResultType == NSDictionary

  #### Parameters

  `clauseChain` | a `QueryChainableBuilderType` indicating the properties to fetch and the series of queries for the request.

  #### Return Value

  the result of the the query as specified by the `QueryChainableBuilderType`

- `unsafeContext()`

  The internal `NSManagedObjectContext` managed by this `QueryableSource`. Using this context directly should typically be avoided, and is provided by CoreStore only for extremely specialized cases.

  #### Declaration

  Swift

      func unsafeContext() -> NSManagedObjectContext