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) -> DataQuery

    Parameters

    index

    From zero to size - 1

    Return Value

    The DataQuery for the action call, if isAction(index) is true, otherwise throws undefined

  • Declaration

    Swift

    open func actionResult(for call: DataQuery, at index: Int = Int(Int32.min)) -> QueryResult

    Parameters

    call

    DataQuery containing the action call.

    index

    From 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 QueryResult for 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

    action

    Data query for this action invocation. After invocation, this may be passed to getActionResult.

    headers

    Request-specific headers.

    options

    Request-specific options.

  • Add QueryResult for an action call to this change set.

    Declaration

    Swift

    open func addActionResult(call: DataQuery, result: QueryResult, index: Int = Int(Int32.min))

    Parameters

    call

    DataQuery for action call, which must have been previously added to this change set using addAction or invokeAction.

    result

    Query result.

    index

    From 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

    entity

    Entity to be created.

    headers

    Request-specific headers.

    options

    Request-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

    from

    Source entity for the link to be created.

    property

    Source navigation property for the link to be created.

    to

    Target entity for the link to be created.

    headers

    Request-specific headers.

    options

    Request-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

    entity

    Entity to be created.

    parent

    Previously created parent entity.

    property

    Parent’s navigation property.

    headers

    Request-specific headers.

    options

    Request-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

    entity

    Entity to be deleted.

    headers

    Request-specific headers.

    options

    Request-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

    from

    Source entity for the link to be deleted.

    property

    Source navigation property for the link to be deleted.

    to

    Target entity for the link to be deleted.

    headers

    Request-specific headers.

    options

    Request-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) -> EntityValue

    Parameters

    index

    From zero to size - 1.

    Return Value

    The changed entity, if isEntity(index) is true, otherwise throws undefined. The EntityValue.isCreated, EntityValue.isUpdated and EntityValue.isDeleted properties can be accessed on the resulting entity value to determine the type of change.

  • Error if status does not represent a successful response.

    Declaration

    Swift

    public final var error: DataServiceError? { get set }
  • Declaration

    Swift

    open func headers(at index: Int) -> HTTPHeaders

    Parameters

    index

    From 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.bind and DataQuery.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) -> DataQuery

    Parameters

    method

    Action to be called.

    parameters

    Action parameters.

    headers

    Action call headers.

    options

    Action call options.

    Return Value

    DataQuery the action call. After invocation, this may be passed to getActionResult.

  • Declaration

    Swift

    open func isAction(at index: Int) -> Bool

    Parameters

    index

    From zero to size - 1

    Return Value

    true if index is a valid change index, and the change is for an action call; otherwise false.

  • Declaration

    Swift

    open func isEntity(at index: Int) -> Bool

    Parameters

    index

    From zero to size - 1.

    Return Value

    true if index is a valid change index, and the change is for a created, updated or deleted entity; otherwise false.

  • Declaration

    Swift

    open func isLink(at index: Int) -> Bool

    Parameters

    index

    From zero to size - 1.

    Return Value

    true if index is a valid change index, and the change is for a created, updated or deleted link; otherwise false.

  • 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) -> ChangedLink

    Parameters

    index

    From zero to size - 1.

    Return Value

    The changed link, if isLink(index) is true, otherwise throws undefined. The ChangedLink.isCreated, ChangedLink.isUpdated and ChangedLink.isDeleted properties can be accessed on the resulting changed link to determine the type of change.

  • Declaration

    Swift

    open func options(at index: Int) -> RequestOptions

    Parameters

    index

    From zero to size - 1.

    Return Value

    The request options for the change at index.

  • Call createEntity, if entity.isNew == true, otherwise call updateEntity.

    Declaration

    Swift

    open func saveEntity(_ entity: EntityValue, headers: HTTPHeaders? = nil, options: RequestOptions? = nil)

    Parameters

    entity

    Entity to be created or updated.

    headers

    Request-specific headers.

    options

    Request-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

    entity

    Entity to be updated.

    headers

    Request-specific headers.

    options

    Request-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

    from

    Source entity for the link to be updated.

    property

    Source navigation property for the link to be updated. This must be a one-to-one navigation property.

    to

    Target entity for the link to be updated.

    headers

    Request-specific headers.

    options

    Request-specific options.