Transaction

public final class Transaction

Thread-safe Transaction object

  • Check whether is already in transaction.

    Declaration

    Swift

    public private(set) var isInTransaction: Bool = false
  • The number of changed rows in the most recent call.
    It should be called after executing successfully

    Declaration

    Swift

    public var changes: Int
  • The path of the related database.

    Declaration

    Swift

    public var path: String
  • tag

    The tag of the related database.

    Declaration

    Swift

    public var tag: Tag?
  • Prepare a specific sql.
    Note that you can use this interface to prepare a SQL that is not contained in the WCDB interface layer

    Throws

    Error

    Declaration

    Swift

    public func prepare(_ statement: Statement) throws -> CoreStatement

    Parameters

    statement

    WINQ statement

    Return Value

    CoreStatement

  • Exec a specific sql.
    Note that you can use this interface to execute a SQL that is not contained in the WCDB interface layer.

    Throws

    Error

    Declaration

    Swift

    public func exec(_ statement: Statement) throws

    Parameters

    statement

    WINQ statement

  • Check whether table exists

    Throws

    Error

    Declaration

    Swift

    public func isTableExists(_ table: String) throws -> Bool

    Parameters

    table

    The name of the table to be checked.

    Return Value

    True if table exists. False if table does not exist.

  • This interface is equivalent begin(.immediate)

    Throws

    Error

    Declaration

    Swift

    public func begin() throws
  • Separate interface of run(transaction:)
    You should call begin, commit, rollback and all other operations in same thread.
    To do a cross-thread transaction, use getTransaction.

    Throws

    Error

    Declaration

    Swift

    public func begin(_ mode: StatementTransaction.Mode) throws
  • Separate interface of run(transaction:)
    You should call begin, commit, rollback and all other operations in same thread. To do a cross-thread transaction, use getTransaction.

    Throws

    Error

    Declaration

    Swift

    public func commit() throws
  • Separate interface of run(transaction:) You should call begin, commit, rollback and all other operations in same thread.
    To do a cross-thread transaction, use getTransaction.

    Throws

    Error

    Declaration

    Swift

    public func rollback() throws
  • Run a transaction in closure

    try transaction.run(transaction: { () throws -> Void in 
        try transaction.insert(objects: objects, intoTable: table)
    })
    

    Throws

    Error

    Declaration

    Swift

    public func run(transaction: TransactionClosure) throws

    Parameters

    transaction

    Operation inside transaction

  • Run a controlable transaction in closure

    try transaction.run(controlableTransaction: { () throws -> Bool in 
        try transaction.insert(objects: objects, intoTable: table)
        return true // return true to commit transaction and return false to rollback transaction.
    })
    

    Throws

    Error

    Declaration

    Swift

    public func run(controlableTransaction: ControlableTransactionClosure) throws

    Parameters

    controlableTransaction

    Operation inside transaction

  • Run a embedded transaction in closure
    The embedded transaction means that it will run a transaction if it’s not in other transaction, otherwise it will be executed within the existing transaction.

    try transaction.run(embeddedTransaction: { () throws -> Void in 
        try transaction.insert(objects: objects, intoTable: table)
    })
    

    Throws

    Error

    Declaration

    Swift

    public func run(embeddedTransaction: TransactionClosure) throws

    Parameters

    embeddedTransaction

    Operation inside transaction