//  // //  #### # # # -= SQLiteX =-  // // # # ## ## Transaction.cpp  // //  ## # ###  // //  ## # ### SQL transaction  // //  # # ## ##  // // #### ##### # # R1 (C)2005 Markus Ewald -> License.txt  // //  // #include "SQLiteX/Transaction.h" #include "SQLiteX/Errors.h" using namespace SQLiteX; // ############################################################################################# // // # SQLiteX::Transaction::Transaction() # // // ############################################################################################# // /** Initializes an SQLite transaction @param sName Name of the transaction; optional */ Transaction::Transaction(const std::string &sName) : m_sName(sName) {} // ############################################################################################# // // # SQLiteX::Transaction::~Transaction() # // // ############################################################################################# // /** Cleans up any memory reserved for variable bindings */ Transaction::~Transaction() {} // ############################################################################################# // // # SQLiteX::Transaction::add() # // // ############################################################################################# // /** Appends one or more SQL statements to the transaction @param sStatements Statements to be appended to the transaction */ void Transaction::add(const std::string &sStatements) { m_Queries.push_back(sStatements); } // ############################################################################################# // // # SQLiteX::Transaction::add() # // // ############################################################################################# // /** Appends one or more SQL statements to the transaction @param sStatements Statements to be appended to the transaction */ void Transaction::add(const Query &TheQuery) { m_Queries.push_back(TheQuery); } // ############################################################################################# // // # SQLiteX::Transaction::add() # // // ############################################################################################# // /** Executes the transaction @param SQLiteDB Database on which to operate; provided by an inverse call from the database */ void Transaction::execute(const SharedSQLite3 &SQLiteDB) { // Begin a new transaction checkError(SQLiteDB, ::sqlite3_exec( SQLiteDB, (std::string("BEGIN TRANSACTION ") + m_sName + ";\n").c_str(), 0, 0, 0 )); // Make sure we roll back the transaction in case of an error struct TransactionUnroller { TransactionUnroller(sqlite3 *pSQLiteDB) : m_pSQLiteDB(pSQLiteDB), m_bDismissed(false) {} ~TransactionUnroller() { if(!m_bDismissed) { ::sqlite3_exec(m_pSQLiteDB, "ROLLBACK;\n", 0, 0, 0); } } void dismiss() { m_bDismissed = true; } sqlite3 *m_pSQLiteDB; bool m_bDismissed; } rollback_Transaction(SQLiteDB); for( QueryDeque::iterator QueryIt = m_Queries.begin(); QueryIt != m_Queries.end(); ++QueryIt ) { QueryIt->execute(SQLiteDB); } checkError(SQLiteDB, ::sqlite3_exec(SQLiteDB, "COMMIT;\n", 0, 0, 0)); rollback_Transaction.dismiss(); }