DataQuery
open class DataQuery: ObjectBase
Encapsulates an OData query request. A query can be defined by setting query properties, or by calling query functions which provide a fluent interface.
Example using proxy classes
open func dataQueryExample() throws -> Void {
defer {
DebugConsole.traceOut("example.NorthwindProxyClient.dataQueryExample")
}
do {
DebugConsole.traceIn("example.NorthwindProxyClient.dataQueryExample")
do {
let service = self.service
let query = DataQuery()
.select(Customer.customerID, Customer.companyName,
Customer.contactName)
.filter(Customer.address.toLower().contains("king"))
.orderBy(Customer.companyName)
let customers = try service.fetchCustomers(matching: query)
self.showCustomers(customers)
}
}
}
Example using dynamic API
open func dataQueryExample() throws -> Void {
defer {
DebugConsole.traceOut("example.NorthwindClient.dataQueryExample")
}
do {
DebugConsole.traceIn("example.NorthwindClient.dataQueryExample")
do {
let service = self.service
let customersEntitySet = service.entitySet(withName: "Customers")
let customerEntityType = customersEntitySet.entityType
let customerIDProperty = customerEntityType.property(withName: "CustomerID")
let companyNameProperty = customerEntityType.property(withName: "CompanyName")
let contactNameProperty = customerEntityType.property(withName: "ContactName")
let addressProperty = customerEntityType.property(withName: "Address")
let query = DataQuery()
.select(customerIDProperty, companyNameProperty,
contactNameProperty)
.from(customersEntitySet).filter(addressProperty.toLower()
.contains("king"))
.orderBy(companyNameProperty)
let customers = try service.executeQuery(query).entityList()
self.showCustomers(customers)
}
}
}
-
The request path for this query. Intended for use by servers. Clients should use API calls (e.g.
from
) to create request paths.Declaration
Swift
final public var requestPath: String?
-
The query string for this query. Intended for use by servers. Clients should use API calls (e.g.
select
,expand
,filter
,orderBy
) to create query strings.Declaration
Swift
final public var queryString: String?
-
The data format for this query’s result.
See also
DataFormat
.Declaration
Swift
final public var dataFormat: Int?
-
The entity set expected for the result of this query. To execute a query, either this must be non-
nil
, ormethodCall
must be non-nil
.See also
from
.Declaration
Swift
final public var entitySet: EntitySet?
-
The entity type expected for the result of this query. Can be used for inference of
entitySet
, if only one entity set in the data model uses this type.Declaration
Swift
final public var entityType: EntityType?
-
Derived type for limiting query results.
Declaration
Swift
final public var derivedType: StructureType?
-
The method call for this query. To execute a query, either this must be non-
nil
, orentitySet
must be non-nil
.See also
invoke
.Declaration
Swift
final public var methodCall: DataMethodCall?
-
Property path for accessing individual properties of an entity.
See also
load
.Declaration
Swift
final public var propertyPath: DataPath?
-
Set to
true
if a single result is expected. This may be required ifurl
has been explicitly set.Declaration
Swift
final public var expectSingle: Bool = false
-
The properties to select from target entities.
See also
select
.Declaration
Swift
final public var selectItems: SelectItemList?
-
true
if all properties are selected (using*
).See also
selectAll
.Declaration
Swift
final public var allSelected: Bool = false
-
true
if all key properties should be selected.See also
selectKey
.Declaration
Swift
final public var keySelected: Bool = false
-
The properties to expand from target entities.
See also
expand
.Declaration
Swift
final public var expandItems: ExpandItemList?
-
The OData transformation items as described in OData Data Aggregation.
Declaration
Swift
final public var transformValues: TransformValueList?
-
A group transformation item set through groupBy method
Declaration
Swift
final public var groupTransform: GroupTransform?
-
The search text to locate in the target entities.
See also
search
.Declaration
Swift
final public var searchText: String?
-
The filter criteria for the target entities.
See also
filter
.Declaration
Swift
final public var queryFilter: QueryFilter?
-
The sort criteria for the target entities.
See also
orderBy
,thenBy
.Declaration
Swift
final public var sortItems: SortItemList?
-
If
true
, the service should return only the number of available results matching the query.See also
count
.Declaration
Swift
final public var countOnly: Bool = false
-
If
true
, the service should return the total number of available results as an inline count in the result list.See also
inlineCount
.Declaration
Swift
final public var countInline: Bool = false
-
If
true
, the client should use streamed parsing of the response.See also
stream
.Declaration
Swift
final public var streamResponse: Bool = false
-
If
true
, the client should use delta parsing of the response.See also
withChangeTracking
.Declaration
Swift
final public var deltaResponse: Bool = false
-
The delta time (
deltaToken
as UTC date/time).Declaration
Swift
final public var deltaTime: GlobalDateTime?
-
The delta token for use in change tracking.
Declaration
Swift
final public var deltaToken: String?
-
Does the client want the server to track changes?
See also
withChangeTracking
.Declaration
Swift
final public var trackChanges: Bool = false
-
The skip token for use in server-driven paging.
Declaration
Swift
final public var skipToken: String?
-
The number of initial results to skip.
See also
skip
.Declaration
Swift
final public var skipCount: Int?
-
The number of initial results to return.
See also
top
.Declaration
Swift
final public var topCount: Int?
-
Parameter alias values.
Declaration
Swift
final public var aliasValues: DataValueMap?
-
Custom query options.
Declaration
Swift
final public var customOptions: StringMap?
-
Default initializer.
Declaration
Swift
override public init()
-
This function is an alias for
filter
.Declaration
Swift
open func `where`(_ test: QueryFilter) -> DataQuery
Parameters
test
The query filter.
Return Value
This query.
-
Adds aggregate transformation items (to
transformValues
) for transforming the result of a query sent to an entity set. This method is only for adding aggregate transformation items (in contrast to ‘transform’ method) for readibility purposes.Example for aggregations
open func aggregationExample() throws -> Void { let service = self.service let employeeEntitySet = service.entitySet(withName: "Employees") let employeeEntityType = employeeEntitySet.entityType let ordersProperty = employeeEntityType.property(withName: "Orders") let orderDetails = ordersProperty.entityType.property(withName: "Order_Details") let ordersQuantity = orderDetails.entityType.property(withName: "Quantity") let query = DataQuery() let aggregateTransform = ordersProperty.path(ordersQuantity).sum("SumQuantity") _ = query.aggregate(aggregateTransform) try self.testApplyQueryResult(query: query, expectedDynamicPropertyName: "SumQuantity") let queryCount = DataQuery() let countAggregate = AggregateValue.count(as: "AllEmployees") _ = queryCount.aggregate(countAggregate) try self.testApplyQueryResult(query: queryCount, expectedDynamicPropertyName: nil) }
Example for aggregations
open func aggregationExample() throws -> Void { let query = DataQuery() let aggregateTransform = Employee.orders.path(Order.orderDetails) .path(OrderDetail.quantity).sum("SumQuantity") _ = query.aggregate(aggregateTransform) try self.testApplyQueryResult(query: query, expectedDynamicPropertyName: "SumQuantity") let queryCount = DataQuery() let countAggregate = AggregateValue.count(as: "AllEmployees") _ = queryCount.aggregate(countAggregate) try self.testApplyQueryResult(query: queryCount, expectedDynamicPropertyName: nil) }
Declaration
Swift
open func aggregate(_ items: AggregateValue...) -> DataQuery
Return Value
This query.
-
Bind a value before
invoke
of a bound action/function.Declaration
Swift
open func bind(_ value: DataValue?) -> DataQuery
Parameters
value
The value to be bound. Note: currently, only binding to an
EntityValue
is supported.Return Value
This query.
-
Check if this query is properly configured from the client’s perspective. Note: This does not guarantee that the server will be able to execute it.
Throws
DataQueryException
if the query is definitely not valid.Declaration
Swift
open func check() throws -> Void
-
Set
countOnly
totrue
to request only the number of matching query results.Example using proxy classes
open func navigationCountExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindProxyClient.navigationCountExample") } do { DebugConsole.traceIn("example.NorthwindProxyClient.navigationCountExample") do { let service = self.service let customer = try service.fetchCustomer(matching: DataQuery() .expand(Customer.orders).top(1)) let orders = customer.orders let query = DataQuery().load(customer, Customer.orders).count() let count = try service.executeQuery(query).count() assert(count == Int64(orders.count)) } } }
Example using proxy classes
open func collectionCountExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindProxyClient.collectionCountExample") } do { DebugConsole.traceIn("example.NorthwindProxyClient.collectionCountExample") do { let service = self.service let customers = try service.fetchCustomers() let query = DataQuery() .from(NorthwindServiceMetadata.EntitySets.customers).count() let count = try service.executeQuery(query).count() assert(count == Int64(customers.count)) } } }
Example using dynamic API
open func collectionCountExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindClient.collectionCountExample") } do { DebugConsole.traceIn("example.NorthwindClient.collectionCountExample") do { let service = self.service let customersEntitySet = service.entitySet(withName: "Customers") let customers = try service.executeQuery(DataQuery() .from(customersEntitySet)) .entityList() let query = DataQuery().from(customersEntitySet).count() let count = try service.executeQuery(query).count() assert(count == Int64(customers.length)) } } }
Example using dynamic API
open func navigationCountExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindClient.navigationCountExample") } do { DebugConsole.traceIn("example.NorthwindClient.navigationCountExample") do { let service = self.service let customersEntitySet = service.entitySet(withName: "Customers") let customerEntityType = customersEntitySet.entityType let ordersProperty = customerEntityType.property(withName: "Orders") let customer = try service.executeQuery(DataQuery() .from(customersEntitySet).expand(ordersProperty).top(1)) .entityList().first() let orders = ordersProperty.entityList(from: customer) let query = DataQuery().load(customer, ordersProperty).count() let count = try service.executeQuery(query).count() assert(count == Int64(orders.length)) } } }
Declaration
Swift
open func count() -> DataQuery
Return Value
This query.
-
Add a custom option to
customOptions
.Declaration
Swift
open func custom(_ name: String, _ value: String) -> DataQuery
Parameters
name
Option name.
value
Option value.
Return Value
This query.
-
Add properties (to
expandItems
) for expanding from the target entity.Example using proxy classes
open func expandExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindProxyClient.expandExample") } do { DebugConsole.traceIn("example.NorthwindProxyClient.expandExample") do { let service = self.service let query = DataQuery() .select(Customer.customerID, Customer.companyName, Customer.contactName) .filter(Customer.customerID.equal("ALFKI") .or(Customer.customerID.equal("ANATR"))) .expand(Customer.orders).orderBy(Customer.companyName) let customers = try service.fetchCustomers(matching: query) var countOrders = 0 for customer in customers { self.showCustomer(customer) let orders = customer.orders for order in orders { let orderID = order.orderID try Example.show(" Order ", Example.formatInt(orderID)) countOrders = countOrders + 1 } } assert(countOrders > 0) } } }
Example using dynamic API
open func expandExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindClient.expandExample") } do { DebugConsole.traceIn("example.NorthwindClient.expandExample") do { let service = self.service let customersEntitySet = service.entitySet(withName: "Customers") let customerEntityType = customersEntitySet.entityType let customerIDProperty = customerEntityType.property(withName: "CustomerID") let companyNameProperty = customerEntityType.property(withName: "CompanyName") let contactNameProperty = customerEntityType.property(withName: "ContactName") let ordersProperty = customerEntityType.property(withName: "Orders") let orderIDProperty = ordersProperty.itemEntityType.property(withName: "OrderID") let query = DataQuery() .select(customerIDProperty, companyNameProperty, contactNameProperty) .from(customersEntitySet).expand(ordersProperty) .filter(customerIDProperty.equal("ALFKI") .or(customerIDProperty.equal("ANATR"))) .orderBy(companyNameProperty) let customers = try service.executeQuery(query).entityList() var countOrders = 0 for customer in customers { self.showCustomer(customer) let orders = ordersProperty.entityList(from: customer) for order in orders { let orderID = orderIDProperty.intValue(from: order) try Example.show(" Order ", Example.formatInt(orderID)) countOrders = countOrders + 1 } } assert(countOrders > 0) } } }
Declaration
Swift
open func expand(_ items: PropertyPath...) -> DataQuery
Return Value
This query.
-
Add a property with a nested query to
expandItems
.Example using proxy classes
open func expandWithQueryExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindProxyClient.expandWithQueryExample") } do { DebugConsole.traceIn("example.NorthwindProxyClient.expandWithQueryExample") do { let service = self.service let query = DataQuery() .select(Customer.customerID, Customer.companyName, Customer.contactName) .filter(Customer.customerID.equal("ALFKI") .or(Customer.customerID.equal("ANATR"))) .expand(Customer.orders, withQuery: DataQuery() .select(Order.orderID) .expand(Order.orderDetails, withQuery: DataQuery().select(OrderDetail.quantity, OrderDetail.unitPrice))) .orderBy(Customer.companyName) let customers = try service.fetchCustomers(matching: query) var countDetails = 0 for customer in customers { self.showCustomer(customer) let orders = customer.orders for order in orders { let orderID = order.orderID var totalPrice = BigDecimal(0) for orderDetail in order.orderDetails { totalPrice = totalPrice.add(DecimalFunction.fromShort(orderDetail.quantity) .multiply(orderDetail.unitPrice)) countDetails = countDetails + 1 } try Example.show("Order ", Example.formatInt(orderID), ": total price ", Example.formatDecimal(totalPrice)) } } assert(countDetails > 0) } } }
Example using dynamic API
open func expandWithQueryExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindClient.expandWithQueryExample") } do { DebugConsole.traceIn("example.NorthwindClient.expandWithQueryExample") do { let service = self.service let customersEntitySet = service.entitySet(withName: "Customers") let customerEntityType = customersEntitySet.entityType let customerIDProperty = customerEntityType.property(withName: "CustomerID") let companyNameProperty = customerEntityType.property(withName: "CompanyName") let contactNameProperty = customerEntityType.property(withName: "ContactName") let ordersProperty = customerEntityType.property(withName: "Orders") let orderIDProperty = ordersProperty.itemEntityType.property(withName: "OrderID") let orderDetailsProperty = ordersProperty.itemEntityType.property(withName: "Order_Details") let quantityProperty = orderDetailsProperty.itemEntityType.property(withName: "Quantity") let unitPriceProperty = orderDetailsProperty.itemEntityType.property(withName: "UnitPrice") let query = DataQuery() .select(customerIDProperty, companyNameProperty, contactNameProperty) .from(customersEntitySet) .filter(customerIDProperty.equal("ALFKI") .or(customerIDProperty.equal("ANATR"))) .expand(ordersProperty, withQuery: DataQuery() .select(orderIDProperty) .expand(orderDetailsProperty, withQuery: DataQuery().select(quantityProperty, unitPriceProperty))) .orderBy(companyNameProperty) let customers = try service.executeQuery(query).entityList() for customer in customers { self.showCustomer(customer) let orders = ordersProperty.entityList(from: customer) for order in orders { let orderID = orderIDProperty.intValue(from: order) var totalPrice = BigDecimal(0) for orderDetail in orderDetailsProperty.entityList(from: order) { let quantity = quantityProperty.shortValue(from: orderDetail) let unitPrice = unitPriceProperty.decimalValue(from: orderDetail) totalPrice = totalPrice.add(DecimalFunction.fromShort(quantity) .multiply(unitPrice)) } try Example.show("Order ", Example.formatInt(orderID), ": total price ", Example.formatDecimal(totalPrice)) } } } } }
Declaration
Swift
open func expand(_ item: PropertyPath, withQuery query: DataQuery) -> DataQuery
Parameters
item
Item to be expanded.
query
Query to control expansion.
Return Value
This query.
-
Declaration
Swift
open func expandedProperties() -> StringSet
Return Value
The names of all properties expanded by this query.
-
Declaration
Swift
open func expandsProperty(_ property: Property) -> Bool
Parameters
property
Property to check for.
Return Value
true
if this query expandsproperty
from the target entity. -
Modify
queryFilter
to be and'ed withtest
(or set totest
ifqueryFilter
wasnil
).Example using proxy classes
open func queryWithFilterExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindProxyClient.queryWithFilterExample") } do { DebugConsole.traceIn("example.NorthwindProxyClient.queryWithFilterExample") do { let service = self.service let query = DataQuery() .select(Customer.customerID, Customer.companyName, Customer.contactName) .filter(Customer.country.equal("Germany") .and(Customer.contactName.greaterEqual("N"))) // Alternate syntax using convenience operators: // let query = DataQuery() // .select(Customer.customerID, Customer.companyName, Customer.contactName) // .filter(Customer.country == "Germany" && Customer.contactName >= "N") let customers = try service.fetchCustomers(matching: query) self.showCustomers(customers) } } }
Example using proxy classes
open func filterByDateExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindProxyClient.filterByDateExample") } do { DebugConsole.traceIn("example.NorthwindProxyClient.filterByDateExample") do { let service = self.service let oneWeekAgo = GlobalDateTime.now().plusDays(-7) let query = DataQuery() .filter(Order.orderDate.greaterThan(oneWeekAgo)) // Alternate syntax using convenience operators: // let query = DataQuery().filter(Order.orderDate >= oneWeekAgo) let orders = try service.fetchOrders(matching: query) self.showOrders(orders) } } }
Example using proxy classes
open func filterByEnumExample() throws -> Void { defer { DebugConsole.traceOut("example.TrippinProxyClient.filterByEnumExample") } do { DebugConsole.traceIn("example.TrippinProxyClient.filterByEnumExample") do { let service = self.service let query = DataQuery() .filter(TrippinPerson.gender.equal(TrippinServiceMetadata.EnumValues.TrippinPersonGender.female)) // Alternate syntax using convenience operators: // let query = DataQuery().filter(TrippinPerson.gender == TrippinPersonGender.female.enumValue) let people = try service.fetchPeople(matching: query) try self.showPeople(people) } } }
Example using dynamic API
open func queryWithFilterExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindClient.queryWithFilterExample") } do { DebugConsole.traceIn("example.NorthwindClient.queryWithFilterExample") do { let service = self.service let customersEntitySet = service.entitySet(withName: "Customers") let customerEntityType = customersEntitySet.entityType let customerIDProperty = customerEntityType.property(withName: "CustomerID") let companyNameProperty = customerEntityType.property(withName: "CompanyName") let contactNameProperty = customerEntityType.property(withName: "ContactName") let countryProperty = customerEntityType.property(withName: "Country") let query = DataQuery() .select(customerIDProperty, companyNameProperty, contactNameProperty) .from(customersEntitySet) .filter(countryProperty.equal("Germany") .and(contactNameProperty.greaterEqual("N"))) // Alternate syntax using convenience operators: // let query = DataQuery() // .select(customerIDProperty, companyNameProperty, contactNameProperty) // .from(customersEntitySet) // .filter(countryProperty == "Germany" && contactNameProperty >= "N") let customers = try service.executeQuery(query).entityList() self.showCustomers(customers) } } }
Example using dynamic API
open func filterByDateExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindClient.filterByDateExample") } do { DebugConsole.traceIn("example.NorthwindClient.filterByDateExample") do { let service = self.service let ordersEntitySet = service.entitySet(withName: "Orders") let orderEntityType = ordersEntitySet.entityType let orderDateProperty = orderEntityType.property(withName: "OrderDate") let oneWeekAgo = GlobalDateTime.now().plusDays(-7) let query = DataQuery().from(ordersEntitySet) .filter(orderDateProperty.greaterThan(oneWeekAgo)) // Alternate syntax using convenience operators: // let query = DataQuery().filter(orderDateProperty >= oneWeekAgo) let orders = try service.executeQuery(query).entityList() self.showOrders(orders) } } }
Example using dynamic API
open func filterByEnumExample() throws -> Void { defer { DebugConsole.traceOut("example.TrippinClient.filterByEnumExample") } do { DebugConsole.traceIn("example.TrippinClient.filterByEnumExample") do { let service = self.service let peopleEntitySet = service.entitySet(withName: "People") let personEntityType = peopleEntitySet.entityType let genderProperty = personEntityType.property(withName: "Gender") let genderEnumType = service.metadata.enumType(withName: "Microsoft.OData.Service.Sample.TrippinInMemory.Models.PersonGender") let female = genderEnumType.member(name: "Female") let query = DataQuery().from(peopleEntitySet) .filter(genderProperty.equal(female)) // Alternate syntax using convenience operators: // let query = DataQuery().filter(genderProperty == genderFemale) let people = try service.executeQuery(query).entityList() try self.showPeople(people) } } }
Declaration
Swift
open func filter(_ test: QueryFilter) -> DataQuery
Parameters
test
The query filter.
Return Value
This query.
-
Set the
entitySet
property to identify this query’s target entity set.Example using proxy classes
open func fromExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindProxyClient.fromExample") } do { DebugConsole.traceIn("example.NorthwindProxyClient.fromExample") do { let service = self.service let customers = try service.fetchCustomers() for customer in customers { self.showCustomer(customer) } } } }
Example using dynamic API
open func fromExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindClient.fromExample") } do { DebugConsole.traceIn("example.NorthwindClient.fromExample") do { let service = self.service let customersEntitySet = service.entitySet(withName: "Customers") let query = DataQuery().from(customersEntitySet) let customers = try service.executeQuery(query).entityList() for customer in customers { self.showCustomer(customer) } } } }
Declaration
Swift
open func from(_ entitySet: EntitySet) -> DataQuery
Parameters
entitySet
The target entity set.
Return Value
This query.
-
Set the
entitySet
property to identify this query’s target entity set, but only if theentitySet
property wasn’t previously set. This function is intended for use by generated proxy classes.Declaration
Swift
open func fromDefault(_ entitySet: EntitySet) -> DataQuery
Parameters
entitySet
The target entity set.
Return Value
This query.
-
Adds a group transformation item to the DataQuery with the property paths given in the parameter list. Note: If this method is called then all the transformation and aggregation items added to the query will be applied to the group transformation in the query.
Example for transformations
open func transformationExample() throws -> Void { let service = self.service let employeeEntitySet = service.entitySet(withName: "Employees") let employeeEntityType = employeeEntitySet.entityType let employeeReportsToProperty = employeeEntityType.property(withName: "ReportsTo") let employee1Property = employeeEntityType.property(withName: "Employee1") let ordersProperty = employeeEntityType.property(withName: "Orders") let ordersQuantity = ordersProperty.entityType.property(withName: "Quantity") let countryProperty = employeeEntityType.property(withName: "Country") let queryGroup = DataQuery() let filterGroupTransform = FilterTransform.of(QueryOperator.equal(countryProperty, StringValue.of("Budapest"))) let groupTransform = GroupTransform.groupBy(employeeReportsToProperty) .aggregate(ordersProperty.path(ordersQuantity).sum("SumQuantity")) _ = queryGroup.transform(groupTransform) try self.testApplyQueryResult(query: queryGroup, expectedDynamicPropertyName: "SumQuantity") let groupTransformForQuery = DataQuery().groupBy(employeeReportsToProperty) .aggregate(ordersProperty.path(ordersQuantity).sum("SumQuantity")) .transform(filterGroupTransform) try self.testApplyQueryResult(query: groupTransformForQuery, expectedDynamicPropertyName: "SumQuantity") let queryTopPercent = DataQuery() let topPercentTransform = try TopTransform.percent(amount: Double(50), value: employeeReportsToProperty) _ = queryTopPercent.transform(topPercentTransform) try self.testApplyQueryResult(query: queryTopPercent, expectedDynamicPropertyName: nil) let queryExpand = DataQuery() let expandTransform = ExpandTransform.withFilter(ordersProperty, filter: FilterTransform.of(QueryOperator.equal(employeeReportsToProperty, IntValue.of(1000)))) _ = queryExpand.transform(expandTransform) try self.testApplyQueryResult(query: queryExpand, expectedDynamicPropertyName: nil) let expandWithExpandTransform = ExpandTransform.withExpand(employee1Property, expandItem: expandTransform) let queryExpand1 = DataQuery() _ = queryExpand1.transform(expandWithExpandTransform) try self.testApplyQueryResult(query: queryExpand1, expectedDynamicPropertyName: nil) }
Example for transformations
open func transformationExample() throws -> Void { let queryGroup = DataQuery() let groupTransform = GroupTransform.groupBy(Employee.reportsTo) .aggregate(Employee.orders.path(Order.orderDetails) .path(OrderDetail.quantity).sum("SumQuantity")) _ = queryGroup.transform(groupTransform) try self.testApplyQueryResult(query: queryGroup, expectedDynamicPropertyName: "SumQuantity") let filterGroupTransform = FilterTransform.of(QueryOperator.equal(Employee.country, StringValue.of("Budapest"))) let groupTransformForQuery = DataQuery().groupBy(Employee.reportsTo) .aggregate(Employee.orders.path(Order.orderDetails) .path(OrderDetail.quantity).sum("SumQuantity")) .transform(filterGroupTransform) try self.testApplyQueryResult(query: groupTransformForQuery, expectedDynamicPropertyName: "SumQuantity") let queryTopPercent = DataQuery() let topPercentTransform = try TopTransform.percent(amount: Double(50), value: Employee.reportsTo) _ = queryTopPercent.transform(topPercentTransform) try self.testApplyQueryResult(query: queryTopPercent, expectedDynamicPropertyName: nil) let queryExpand = DataQuery() let expandTransform = ExpandTransform.withFilter(Employee.orders, filter: FilterTransform.of(QueryOperator.equal(Employee.reportsTo, IntValue.of(1000)))) _ = queryExpand.transform(expandTransform) try self.testApplyQueryResult(query: queryExpand, expectedDynamicPropertyName: nil) let expandWithExpandTransform = ExpandTransform.withExpand(Employee.employee1, expandItem: expandTransform) let queryExpand1 = DataQuery() _ = queryExpand1.transform(expandWithExpandTransform) try self.testApplyQueryResult(query: queryExpand1, expectedDynamicPropertyName: nil) }
Declaration
Swift
open func groupBy(_ items: PropertyPath...) -> DataQuery
Return Value
This query.
-
Set
countInline
totrue
to request an inline count in the server’s response.Example using proxy classes
open func inlineCountExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindProxyClient.inlineCountExample") } do { DebugConsole.traceIn("example.NorthwindProxyClient.inlineCountExample") do { let service = self.service let query = DataQuery() .from(NorthwindServiceMetadata.EntitySets.customers).skip(20).top(10) .inlineCount() let result = try service.executeQuery(query) let customers = try Customer.array(from: result.entityList()) let count = try result.inlineCount() assert(customers.count == 10) assert(count > Int64(10)) } } }
Example using dynamic API
open func inlineCountExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindClient.inlineCountExample") } do { DebugConsole.traceIn("example.NorthwindClient.inlineCountExample") do { let service = self.service let customersEntitySet = service.entitySet(withName: "Customers") let query = DataQuery().from(customersEntitySet).skip(20).top(10) .inlineCount() let result = try service.executeQuery(query) let customers = try result.entityList() let count = try result.inlineCount() assert(customers.length == 10) assert(count > Int64(10)) } } }
Declaration
Swift
open func inlineCount() -> DataQuery
Return Value
This query.
-
Set
methodCall
to associate this query with an action/function invocation.See also
bind
, for setting the binding parameter for a bound action/function.Example using proxy classes
open func invokeExample() throws -> Void { defer { DebugConsole.traceOut("example.TrippinProxyClient.invokeExample") } do { DebugConsole.traceIn("example.TrippinProxyClient.invokeExample") do { let service = self.service let person = try service.personWithMostFriends() try self.showPerson(person) } } }
Example using dynamic API
open func invokeExample() throws -> Void { defer { DebugConsole.traceOut("example.TrippinClient.invokeExample") } do { DebugConsole.traceIn("example.TrippinClient.invokeExample") do { let service = self.service let functionImport = service.dataMethod(withName: "GetPersonWithMostFriends") let query = DataQuery().invoke(functionImport) let person = try service.executeQuery(query).requiredEntity() try self.showPerson(person) } } }
Declaration
Swift
open func invoke(_ method: DataMethod, _ parameters: ParameterList = ParameterList.empty) -> DataQuery
Parameters
method
Data method to be called (action or function).
parameters
Method parameters.
Return Value
This query.
-
Set
entitySet
andentityKey
to configure this query to load an existing entity. Equivalent to callingfrom(entity.entitySet).withKey(entity.entityKey)
.Declaration
Swift
open func load(_ entity: EntityValue, _ path: PropertyPath? = nil) -> DataQuery
Parameters
entity
The entity to be loaded.
path
Optionally specify
propertyPath
.Return Value
This query.
-
Add to
sortItems
a value for result ordering.Example using proxy classes
open func orderByExample() throws -> Void { let service = self.service let avila = GeographyPoint.with(latitude: 40.6567, longitude: -4.6812) let query = DataQuery().top(3).orderBy(Artist.placeOfBirth.geoDistance(avila)) let artists = try service.fetchArtists(matching: query) self.showArtists(artists) }
Example using dynamic API
open func orderByExample() throws -> Void { let service = self.service let artistsEntitySet = service.entitySet(withName: "Artists") let artistEntityType = artistsEntitySet.entityType let placeOfBirthProperty = artistEntityType.property(withName: "placeOfBirth") let avila = GeographyPoint.with(latitude: 40.6567, longitude: -4.6812) let query = DataQuery().top(3).from(artistsEntitySet) .orderBy(placeOfBirthProperty.geoDistance(avila)) let artists = try service.executeQuery(query).entityList() self.showArtists(artists) }
Declaration
Swift
open func orderBy(_ value: QueryValue, _ order: SortOrder = SortOrder.ascending) -> DataQuery
Parameters
value
Value for ordering.
order
The sort order (defaults to ascending).
Return Value
This query.
-
Set
pageSize
to specify the page-size for server-driven paging of results. Note: the server is not required to respect this setting. But if this setting is used, the client should expect to have to follow next-links.Example using proxy classes
open func queryWithPageExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindProxyClient.queryWithPageExample") } do { DebugConsole.traceIn("example.NorthwindProxyClient.queryWithPageExample") do { let service = self.service let expected = try service.executeQuery(DataQuery() .from(NorthwindServiceMetadata.EntitySets.customers).count()) .count() var found = 0 var query = DataQuery() .from(NorthwindServiceMetadata.EntitySets.customers).page(5) repeat { let result = try service.executeQuery(query) let customers = try Customer.array(from: result.entityList()) self.showCustomers(customers) found = found + customers.count query = try result.nextQuery() } while query.url != nil assert(Int64(found) == expected) } } }
Example using dynamic API
open func queryWithPageExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindClient.queryWithPageExample") } do { DebugConsole.traceIn("example.NorthwindClient.queryWithPageExample") do { let service = self.service let customersEntitySet = service.entitySet(withName: "Customers") var query = DataQuery().from(customersEntitySet).page(5) repeat { let result = try service.executeQuery(query) let customers = try result.entityList() self.showCustomers(customers) query = try result.nextQuery() } while query.url != nil } } }
Declaration
Swift
open func page(_ size: Int) -> DataQuery
Parameters
size
Client-requested page size for use by server-driven paging.
Return Value
This query.
-
Set
propertyPath
to identify this query’s target path.Declaration
Swift
open func path(_ target: DataPath) -> DataQuery
Parameters
target
The target path.
Return Value
This query.
-
Set
propertyPath
to identify this query’s target property.Declaration
Swift
open func property(_ target: Property) -> DataQuery
Parameters
target
The target property.
Return Value
This query.
-
Set
searchText
for free-text searching.Example using proxy classes
open func searchExample() throws -> Void { let service = self.service let query = DataQuery().search("Juice") let products = try service.fetchProducts(matching: query) self.showProducts(products) }
Example using dynamic API
open func searchExample() throws -> Void { let service = self.service let productsEntitySet = service.entitySet(withName: "Products") let query = DataQuery().from(productsEntitySet).search("Juice") let products = try service.executeQuery(query).entityList() self.showProducts(products) }
Declaration
Swift
open func search(_ text: String) -> DataQuery
Parameters
text
The search expression.
Return Value
This query.
-
Add properties (to
selectItems
) for selecting from the target entity.Example using proxy classes
open func selectExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindProxyClient.selectExample") } do { DebugConsole.traceIn("example.NorthwindProxyClient.selectExample") do { let service = self.service let query = DataQuery() .select(Customer.customerID, Customer.companyName, Customer.contactName) let customers = try service.fetchCustomers(matching: query) for customer in customers { self.showCustomer(customer) } } } }
Example using dynamic API
open func selectExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindClient.selectExample") } do { DebugConsole.traceIn("example.NorthwindClient.selectExample") do { let service = self.service let customersEntitySet = service.entitySet(withName: "Customers") let customerEntityType = customersEntitySet.entityType let customerIDProperty = customerEntityType.property(withName: "CustomerID") let companyNameProperty = customerEntityType.property(withName: "CompanyName") let contactNameProperty = customerEntityType.property(withName: "ContactName") let query = DataQuery() .select(customerIDProperty, companyNameProperty, contactNameProperty) .from(customersEntitySet) let customers = try service.executeQuery(query).entityList() for customer in customers { self.showCustomer(customer) } } } }
Declaration
Swift
open func select(_ items: PropertyPath...) -> DataQuery
Return Value
This query.
-
Set
allSelected
totrue
.Declaration
Swift
open func selectAll() -> DataQuery
Return Value
This query.
-
Set
keySelected
totrue
.Declaration
Swift
open func selectKey() -> DataQuery
Return Value
This query.
-
Declaration
Swift
open func selectsProperty(_ property: Property) -> Bool
Parameters
property
Property to check for.
Return Value
true
if this query selectsproperty
from the target entity. -
Set
skipCount
to specify the number of initial results to return.Example using proxy classes
open func queryWithSkipExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindProxyClient.queryWithSkipExample") } do { DebugConsole.traceIn("example.NorthwindProxyClient.queryWithSkipExample") do { let service = self.service let query = DataQuery().skip(20).top(10) let customers = try service.fetchCustomers(matching: query) self.showCustomers(customers) assert(customers.count == 10) } } }
Example using dynamic API
open func queryWithSkipExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindClient.queryWithSkipExample") } do { DebugConsole.traceIn("example.NorthwindClient.queryWithSkipExample") do { let service = self.service let customersEntitySet = service.entitySet(withName: "Customers") let query = DataQuery().from(customersEntitySet).skip(20).top(10) let customers = try service.executeQuery(query).entityList() self.showCustomers(customers) assert(customers.length == 10) } } }
Declaration
Swift
open func skip(_ count: Int) -> DataQuery
Parameters
count
Number of initial results for query execution to skip.
Return Value
This query.
-
See also
orderBy
,thenBy
.Declaration
Swift
open func sortedByProperties() -> StringSet
Return Value
The names of all properties used for sorting by this query.
-
Set
streamResponse
totrue
to request streamed response processing.Declaration
Swift
open func stream() -> DataQuery
Return Value
This query.
-
Add to
sortItems
a value for result ordering. This function is an alias fororderBy
. By convention to improve readability when multiple ordering values are required, the first ordering value is added usingorderBy
, and subsequent ordering values are added usingthenBy
.Example using proxy classes
open func thenByExample() throws -> Void { defer { DebugConsole.traceOut("example.MediaProxyClient.thenByExample") } do { DebugConsole.traceIn("example.MediaProxyClient.thenByExample") do { let service = self.service let query = DataQuery().orderBy(Artist.lastName) .thenBy(Artist.firstName) let artists = try service.fetchArtists(matching: query) self.showArtists(artists) } } }
Example using dynamic API
open func thenByExample() throws -> Void { defer { DebugConsole.traceOut("example.MediaClient.thenByExample") } do { DebugConsole.traceIn("example.MediaClient.thenByExample") do { let service = self.service let artistsEntitySet = service.entitySet(withName: "Artists") let artistEntityType = artistsEntitySet.entityType let lastNameProperty = artistEntityType.property(withName: "lastName") let firstNameProperty = artistEntityType.property(withName: "firstName") let query = DataQuery().from(artistsEntitySet) .orderBy(lastNameProperty).thenBy(firstNameProperty) let artists = try service.executeQuery(query).entityList() self.showArtists(artists) } } }
Declaration
Swift
open func thenBy(_ value: QueryValue, _ order: SortOrder = SortOrder.ascending) -> DataQuery
Parameters
value
Value for ordering.
order
The sort order (defaults to ascending).
Return Value
This query.
-
Declaration
Swift
override open func toString() -> String
Return Value
A string representation of this object.
-
Set
topCount
to specify the maximum number of results to return.Example using proxy classes
open func queryWithTopExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindProxyClient.queryWithTopExample") } do { DebugConsole.traceIn("example.NorthwindProxyClient.queryWithTopExample") do { let service = self.service let query = DataQuery().top(10) let customers = try service.fetchCustomers(matching: query) self.showCustomers(customers) assert(customers.count == 10) } } }
Example using dynamic API
open func queryWithTopExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindClient.queryWithTopExample") } do { DebugConsole.traceIn("example.NorthwindClient.queryWithTopExample") do { let service = self.service let customersEntitySet = service.entitySet(withName: "Customers") let query = DataQuery().from(customersEntitySet).top(10) let customers = try service.executeQuery(query).entityList() self.showCustomers(customers) assert(customers.length == 10) } } }
Declaration
Swift
open func top(_ count: Int) -> DataQuery
Parameters
count
Maximum number of results for query execution to return.
Return Value
This query.
-
Adds transformation items (to
transformValues
) for transforming the result of a query sent to an entity set.Example for transformations
open func transformationExample() throws -> Void { let service = self.service let employeeEntitySet = service.entitySet(withName: "Employees") let employeeEntityType = employeeEntitySet.entityType let employeeReportsToProperty = employeeEntityType.property(withName: "ReportsTo") let employee1Property = employeeEntityType.property(withName: "Employee1") let ordersProperty = employeeEntityType.property(withName: "Orders") let ordersQuantity = ordersProperty.entityType.property(withName: "Quantity") let countryProperty = employeeEntityType.property(withName: "Country") let queryGroup = DataQuery() let filterGroupTransform = FilterTransform.of(QueryOperator.equal(countryProperty, StringValue.of("Budapest"))) let groupTransform = GroupTransform.groupBy(employeeReportsToProperty) .aggregate(ordersProperty.path(ordersQuantity).sum("SumQuantity")) _ = queryGroup.transform(groupTransform) try self.testApplyQueryResult(query: queryGroup, expectedDynamicPropertyName: "SumQuantity") let groupTransformForQuery = DataQuery().groupBy(employeeReportsToProperty) .aggregate(ordersProperty.path(ordersQuantity).sum("SumQuantity")) .transform(filterGroupTransform) try self.testApplyQueryResult(query: groupTransformForQuery, expectedDynamicPropertyName: "SumQuantity") let queryTopPercent = DataQuery() let topPercentTransform = try TopTransform.percent(amount: Double(50), value: employeeReportsToProperty) _ = queryTopPercent.transform(topPercentTransform) try self.testApplyQueryResult(query: queryTopPercent, expectedDynamicPropertyName: nil) let queryExpand = DataQuery() let expandTransform = ExpandTransform.withFilter(ordersProperty, filter: FilterTransform.of(QueryOperator.equal(employeeReportsToProperty, IntValue.of(1000)))) _ = queryExpand.transform(expandTransform) try self.testApplyQueryResult(query: queryExpand, expectedDynamicPropertyName: nil) let expandWithExpandTransform = ExpandTransform.withExpand(employee1Property, expandItem: expandTransform) let queryExpand1 = DataQuery() _ = queryExpand1.transform(expandWithExpandTransform) try self.testApplyQueryResult(query: queryExpand1, expectedDynamicPropertyName: nil) }
Example for transformations
open func transformationExample() throws -> Void { let queryGroup = DataQuery() let groupTransform = GroupTransform.groupBy(Employee.reportsTo) .aggregate(Employee.orders.path(Order.orderDetails) .path(OrderDetail.quantity).sum("SumQuantity")) _ = queryGroup.transform(groupTransform) try self.testApplyQueryResult(query: queryGroup, expectedDynamicPropertyName: "SumQuantity") let filterGroupTransform = FilterTransform.of(QueryOperator.equal(Employee.country, StringValue.of("Budapest"))) let groupTransformForQuery = DataQuery().groupBy(Employee.reportsTo) .aggregate(Employee.orders.path(Order.orderDetails) .path(OrderDetail.quantity).sum("SumQuantity")) .transform(filterGroupTransform) try self.testApplyQueryResult(query: groupTransformForQuery, expectedDynamicPropertyName: "SumQuantity") let queryTopPercent = DataQuery() let topPercentTransform = try TopTransform.percent(amount: Double(50), value: Employee.reportsTo) _ = queryTopPercent.transform(topPercentTransform) try self.testApplyQueryResult(query: queryTopPercent, expectedDynamicPropertyName: nil) let queryExpand = DataQuery() let expandTransform = ExpandTransform.withFilter(Employee.orders, filter: FilterTransform.of(QueryOperator.equal(Employee.reportsTo, IntValue.of(1000)))) _ = queryExpand.transform(expandTransform) try self.testApplyQueryResult(query: queryExpand, expectedDynamicPropertyName: nil) let expandWithExpandTransform = ExpandTransform.withExpand(Employee.employee1, expandItem: expandTransform) let queryExpand1 = DataQuery() _ = queryExpand1.transform(expandWithExpandTransform) try self.testApplyQueryResult(query: queryExpand1, expectedDynamicPropertyName: nil) }
Declaration
Swift
open func transform(_ items: TransformValue...) -> DataQuery
Return Value
This query.
-
The relative URL for this query; a combination of
requestPath
andqueryString
if either is non-nil
, ornil
if the URL is intended to be derived from other properties. Note: It is not recommended for clients to explicitly set the URL, except by using a URL fromEntityValueList.deltaLink
orEntityValueList.nextLink
.See also
from
,select
,expand
,filter
,orderBy
.Declaration
Swift
open var url: String?
-
Set
url
fromdeltaLink
and settrackChanges
totrue
. Also setdeltaResponse
totrue
ifdeltaLink
is non-null, otherwisefalse
.Declaration
Swift
open func withChangeTracking(deltaLink: String? = nil) -> DataQuery
Parameters
deltaLink
Delta link (absolute or, preferably, relative to the service root URL).
Return Value
This query.
-
Set
entityKey
to locate an entity by its primary key.Example using proxy classes
open func queryWithKeyExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindProxyClient.queryWithKeyExample") } do { DebugConsole.traceIn("example.NorthwindProxyClient.queryWithKeyExample") do { let service = self.service let query = DataQuery() .select(Customer.customerID, Customer.companyName, Customer.contactName) .withKey(Customer.key(customerID: "QUEEN")) let customer = try service.fetchCustomer(matching: query) self.showCustomer(customer) } } }
Example using dynamic API
open func queryWithKeyExample() throws -> Void { defer { DebugConsole.traceOut("example.NorthwindClient.queryWithKeyExample") } do { DebugConsole.traceIn("example.NorthwindClient.queryWithKeyExample") do { let service = self.service let customersEntitySet = service.entitySet(withName: "Customers") let customerEntityType = customersEntitySet.entityType let customerIDProperty = customerEntityType.property(withName: "CustomerID") let companyNameProperty = customerEntityType.property(withName: "CompanyName") let contactNameProperty = customerEntityType.property(withName: "ContactName") let query = DataQuery() .select(customerIDProperty, companyNameProperty, contactNameProperty) .from(customersEntitySet) .withKey(EntityKey() .withProperty(customerIDProperty, value: StringValue.of("QUEEN"))) let customer = try service.executeQuery(query).requiredEntity() self.showCustomer(customer) } } }
Declaration
Swift
open func withKey(_ key: EntityKey) -> DataQuery
Parameters
key
Entity key.
Return Value
This query.
-
Set
derivedType
to limit results to a derived complex/entity type.Declaration
Swift
open func withType(_ type: StructureType) -> DataQuery
Parameters
type
Complex/entity type.
Return Value
This query.
-
Set
url
. Note: It is not recommended for clients to explicitly set the URL, except by using a URL fromEntityValueList.deltaLink
orEntityValueList.nextLink
.Declaration
Swift
open func withURL(_ url: String?) -> DataQuery
Parameters
url
Query URL (absolute or, preferably, relative to the service root URL).
Return Value
This query.