ChangeSet
open class ChangeSet : ObjectBase
Encapsulates an OData change set. A change set is used to group a set of entity or link changes into a single unit of work, like an atomic database transaction.
Example using proxy classes
open func applyChangesExample() throws -> Void {
let service = self.service
let suppliers = try service.fetchSuppliers(matching: DataQuery().top(2))
let products = try service.fetchProducts(matching: DataQuery().top(3))
let product1 = products[0].copy()
let product2 = products[1].copy()
let product3 = products[2].copy()
product1.productName = "Blueberry Muffins"
product2.productName = "Strawberry Yoghurt"
product3.productName = "Raspberry Pie"
let entityCreates = ChangeSet()
entityCreates.createEntity(product1)
entityCreates.createEntity(product2)
entityCreates.createEntity(product3)
try service.applyChanges(entityCreates)
let entityChanges = ChangeSet()
product2.productName = "Blackberry Yoghurt"
entityChanges.updateEntity(product2)
entityChanges.deleteEntity(product3)
try service.applyChanges(entityChanges)
let linkChanges = ChangeSet()
let supplier1 = suppliers[0]
let supplier2 = suppliers[1]
linkChanges.createLink(from: product1, property: Product.supplier,
to: supplier1)
linkChanges.updateLink(from: product1, property: Product.supplier,
to: supplier2)
linkChanges.deleteLink(from: product1, property: Product.supplier)
try service.applyChanges(linkChanges)
}
Example using dynamic API
open func applyChangesExample() throws -> Void {
let service = self.service
let suppliersEntitySet = service.entitySet(withName: "Suppliers")
let productsEntitySet = service.entitySet(withName: "Products")
let productEntityType = productsEntitySet.entityType
let productNameProperty = productEntityType.property(withName: "ProductName")
let supplierProperty = productEntityType.property(withName: "Supplier")
let suppliers = try service.executeQuery(DataQuery().from(suppliersEntitySet)
.top(2))
.entityList()
let products = try service.executeQuery(DataQuery().from(productsEntitySet)
.top(3))
.entityList()
let product1 = products.item(at: 0).copyEntity()
let product2 = products.item(at: 1).copyEntity()
let product3 = products.item(at: 1).copyEntity()
productNameProperty.setStringValue(in: product1, to: "Blueberry Yoghurt")
productNameProperty.setStringValue(in: product2, to: "Strawberry Yoghurt")
productNameProperty.setStringValue(in: product3, to: "Raspberry Pie")
let entityCreates = ChangeSet()
entityCreates.createEntity(product1)
entityCreates.createEntity(product2)
entityCreates.createEntity(product3)
try service.applyChanges(entityCreates)
let entityChanges = ChangeSet()
productNameProperty.setStringValue(in: product2, to: "Blackberry Yoghurt")
entityChanges.updateEntity(product2)
entityChanges.deleteEntity(product3)
try service.applyChanges(entityChanges)
let linkChanges = ChangeSet()
let supplier1 = suppliers.item(at: 0)
let supplier2 = suppliers.item(at: 1)
linkChanges.createLink(from: product1, property: supplierProperty,
to: supplier1)
linkChanges.updateLink(from: product1, property: supplierProperty,
to: supplier2)
linkChanges.deleteLink(from: product1, property: supplierProperty)
try service.applyChanges(linkChanges)
}
-
Default initializer.
Declaration
Swift
override public init() -
Declaration
Swift
open func action(at index: Int) -> DataQueryParameters
indexFrom zero to
size - 1Return Value
The DataQuery for the action call, if
isAction(index)istrue, otherwise throwsundefined -
Declaration
Swift
open func actionResult(for call: DataQuery, at index: Int = Int(Int32.min)) -> QueryResultParameters
callDataQuerycontaining the action call.indexFrom zero to
size - 1. Index for the query. Omit if unknown, in which case the query will be located by a linear search.Return Value
The
QueryResultfor an action call. -
Add an action call to the change set.
Declaration
Swift
open func addAction(_ action: DataQuery, headers: HTTPHeaders? = nil, options: RequestOptions? = nil)Parameters
actionData query for this action invocation. After invocation, this may be passed to
getActionResult.headersRequest-specific headers.
optionsRequest-specific options.
-
Add
QueryResultfor an action call to this change set.Declaration
Swift
open func addActionResult(call: DataQuery, result: QueryResult, index: Int = Int(Int32.min))Parameters
callDataQueryfor action call, which must have been previously added to this change set usingaddActionorinvokeAction.resultQuery result.
indexFrom zero to
size - 1. Index for the query. Omit if unknown, in which case the query will be located by a linear search. -
Add a pending created entity to the change set. The entity will be created when this change set is submitted.
Declaration
Swift
open func createEntity(_ entity: EntityValue, headers: HTTPHeaders? = nil, options: RequestOptions? = nil)Parameters
entityEntity to be created.
headersRequest-specific headers.
optionsRequest-specific options.
-
Add a pending created link to the change set. The link will be created when this change set is submitted.
Declaration
Swift
open func createLink(from: EntityValue, property: Property, to: EntityValue, headers: HTTPHeaders? = nil, options: RequestOptions? = nil)Parameters
fromSource entity for the link to be created.
propertySource navigation property for the link to be created.
toTarget entity for the link to be created.
headersRequest-specific headers.
optionsRequest-specific options.
-
Add a pending created entity to the change set, related to a parent entity via a parent navigation property. The entity will be created when this change set is submitted.
Example using proxy classes
open func createRelatedEntityInChangeSetExample() throws -> Void { let service = self.service let customers = try service.fetchCustomers(matching: DataQuery() .filter(Customer.customerID.equal("ALFKI"))) let orders = try service.fetchOrders(matching: DataQuery().top(2)) let changes = ChangeSet() let newCustomer = customers.first!.copy() changes.createEntity(newCustomer) let firstOrder = orders.first!.copy() let secondOrder = orders.last!.copy() changes.createRelatedEntity(firstOrder, in: newCustomer, property: Customer.orders) changes.createRelatedEntity(secondOrder, in: newCustomer, property: Customer.orders) try service.applyChanges(changes) }Example using dynamic API
open func createRelatedEntityInChangeSetExample() throws -> Void { let service = self.service let customersEntitySet = service.entitySet(withName: "Customers") let ordersEntitySet = service.entitySet(withName: "Orders") let customerEntityType = customersEntitySet.entityType let customerIDProperty = customerEntityType.property(withName: "CustomerID") let ordersProperty = customerEntityType.property(withName: "Orders") let customers = try service.executeQuery(DataQuery().from(customersEntitySet) .filter(customerIDProperty.equal("ALFKI"))) .entityList() let orders = try service.executeQuery(DataQuery().from(ordersEntitySet) .top(2)) .entityList() let changes = ChangeSet() let newCustomer = customers.first().copyEntity() changes.createEntity(newCustomer) let firstOrder = orders.first().copyEntity() let secondOrder = orders.last().copyEntity() changes.createRelatedEntity(firstOrder, in: newCustomer, property: ordersProperty) changes.createRelatedEntity(secondOrder, in: newCustomer, property: ordersProperty) try service.applyChanges(changes) }Declaration
Swift
open func createRelatedEntity(_ entity: EntityValue, in parent: EntityValue, property: Property, headers: HTTPHeaders? = nil, options: RequestOptions? = nil)Parameters
entityEntity to be created.
parentPreviously created parent entity.
propertyParent’s navigation property.
headersRequest-specific headers.
optionsRequest-specific options.
-
Add a pending deleted entity to the change set. The entity will be deleted when this change set is submitted.
Declaration
Swift
open func deleteEntity(_ entity: EntityValue, headers: HTTPHeaders? = nil, options: RequestOptions? = nil)Parameters
entityEntity to be deleted.
headersRequest-specific headers.
optionsRequest-specific options.
-
Add a pending deleted link to the change set. The link will be deleted when this change set is submitted.
Declaration
Swift
open func deleteLink(from: EntityValue, property: Property, to: EntityValue = EntityValue.ofType(EntityType.undefined), headers: HTTPHeaders? = nil, options: RequestOptions? = nil)Parameters
fromSource entity for the link to be deleted.
propertySource navigation property for the link to be deleted.
toTarget entity for the link to be deleted.
headersRequest-specific headers.
optionsRequest-specific options.
-
Example
open func showChangedEntities(changes: ChangeSet) throws -> Void { let n = changes.size do { var i = 0 var first_1077 = true while (true) { if first_1077 { first_1077 = false } else { i = i + 1 } if !(i < n) { break } if changes.isEntity(at: i) { let change = changes.entity(at: i) if change.isCreated { try Example.show("created entity") } else if change.isUpdated { try Example.show("updated entity") } else if change.isDeleted { try Example.show("deleted entity") } } } } }Declaration
Swift
open func entity(at index: Int) -> EntityValueParameters
indexFrom zero to
size - 1.Return Value
The changed entity, if
isEntity(index)istrue, otherwise throwsundefined. TheEntityValue.isCreated,EntityValue.isUpdatedandEntityValue.isDeletedproperties can be accessed on the resulting entity value to determine the type of change. -
Error if
statusdoes not represent a successful response.Declaration
Swift
public final var error: DataServiceError? { get set } -
Declaration
Swift
open func headers(at index: Int) -> HTTPHeadersParameters
indexFrom zero to
size - 1.Return Value
The HTTP headers for the change at
index. -
Add an action call to the change set. If the method is not an action throws a ‘UsageException’.
See also
DataQuery.bindandDataQuery.from, for setting the binding parameter for a bound action (either can be applied to the returned query).Declaration
Swift
open func invokeAction(method: DataMethod, parameters: ParameterList = ParameterList.empty, headers: HTTPHeaders? = nil, options: RequestOptions? = nil) -> DataQueryParameters
methodAction to be called.
parametersAction parameters.
headersAction call headers.
optionsAction call options.
Return Value
DataQuerythe action call. After invocation, this may be passed togetActionResult. -
Declaration
Swift
open func isAction(at index: Int) -> BoolParameters
indexFrom zero to
size - 1Return Value
trueifindexis a valid change index, and the change is for an action call; otherwisefalse. -
Declaration
Swift
open func isEntity(at index: Int) -> BoolParameters
indexFrom zero to
size - 1.Return Value
trueifindexis a valid change index, and the change is for a created, updated or deleted entity; otherwisefalse. -
Declaration
Swift
open func isLink(at index: Int) -> BoolParameters
indexFrom zero to
size - 1.Return Value
trueifindexis a valid change index, and the change is for a created, updated or deleted link; otherwisefalse. -
Example
open func showChangedLinks(changes: ChangeSet) throws -> Void { let n = changes.size do { var i = 0 var first_1101 = true while (true) { if first_1101 { first_1101 = false } else { i = i + 1 } if !(i < n) { break } if changes.isLink(at: i) { let change = changes.link(at: i) if change.isCreated { try Example.show("created link") } else if change.isUpdated { try Example.show("updated link") } else if change.isDeleted { try Example.show("deleted link") } } } } }Declaration
Swift
open func link(at index: Int) -> ChangedLinkParameters
indexFrom zero to
size - 1.Return Value
The changed link, if
isLink(index)istrue, otherwise throwsundefined. TheChangedLink.isCreated,ChangedLink.isUpdatedandChangedLink.isDeletedproperties can be accessed on the resulting changed link to determine the type of change. -
Declaration
Swift
open func options(at index: Int) -> RequestOptionsParameters
indexFrom zero to
size - 1.Return Value
The request options for the change at
index. -
Call
createEntity, ifentity.isNew == true, otherwise callupdateEntity.Declaration
Swift
open func saveEntity(_ entity: EntityValue, headers: HTTPHeaders? = nil, options: RequestOptions? = nil)Parameters
entityEntity to be created or updated.
headersRequest-specific headers.
optionsRequest-specific options.
-
The number of changes in this change set.
Declaration
Swift
open var size: Int { get } -
Response status (e.g. HTTP status code 200 = OK).
Declaration
Swift
public final var status: Int { get set } -
Add an updated entity to the change set. The entity will be updated when this change set is submitted.
Declaration
Swift
open func updateEntity(_ entity: EntityValue, headers: HTTPHeaders? = nil, options: RequestOptions? = nil)Parameters
entityEntity to be updated.
headersRequest-specific headers.
optionsRequest-specific options.
-
Add a pending updated link to the change set. The link will be updated when this change set is submitted.
Declaration
Swift
open func updateLink(from: EntityValue, property: Property, to: EntityValue, headers: HTTPHeaders? = nil, options: RequestOptions? = nil)Parameters
fromSource entity for the link to be updated.
propertySource navigation property for the link to be updated. This must be a one-to-one navigation property.
toTarget entity for the link to be updated.
headersRequest-specific headers.
optionsRequest-specific options.