mirror of
https://github.com/cuberite/SQLiteCpp.git
synced 2025-08-06 10:46:03 -04:00
Add a Transaction::rollback() method
This commit is contained in:
parent
fc92d9cf92
commit
cc0044a603
@ -86,6 +86,11 @@ public:
|
|||||||
*/
|
*/
|
||||||
void commit();
|
void commit();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Rollback the transaction
|
||||||
|
*/
|
||||||
|
void rollback();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Database& mDatabase; ///< Reference to the SQLite Database Connection
|
Database& mDatabase; ///< Reference to the SQLite Database Connection
|
||||||
bool mbCommited = false; ///< True when commit has been called
|
bool mbCommited = false; ///< True when commit has been called
|
||||||
|
@ -44,7 +44,7 @@ Transaction::Transaction(Database& aDatabase, TransactionBehavior behavior) :
|
|||||||
Transaction::Transaction(Database &aDatabase) :
|
Transaction::Transaction(Database &aDatabase) :
|
||||||
mDatabase(aDatabase)
|
mDatabase(aDatabase)
|
||||||
{
|
{
|
||||||
mDatabase.exec("BEGIN");
|
mDatabase.exec("BEGIN TRANSACTION");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Safely rollback the transaction if it has not been committed.
|
// Safely rollback the transaction if it has not been committed.
|
||||||
@ -54,7 +54,7 @@ Transaction::~Transaction()
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
mDatabase.exec("ROLLBACK");
|
mDatabase.exec("ROLLBACK TRANSACTION");
|
||||||
}
|
}
|
||||||
catch (SQLite::Exception&)
|
catch (SQLite::Exception&)
|
||||||
{
|
{
|
||||||
@ -68,7 +68,7 @@ void Transaction::commit()
|
|||||||
{
|
{
|
||||||
if (false == mbCommited)
|
if (false == mbCommited)
|
||||||
{
|
{
|
||||||
mDatabase.exec("COMMIT");
|
mDatabase.exec("COMMIT TRANSACTION");
|
||||||
mbCommited = true;
|
mbCommited = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -77,5 +77,17 @@ void Transaction::commit()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Rollback the transaction
|
||||||
|
void Transaction::rollback()
|
||||||
|
{
|
||||||
|
if (false == mbCommited)
|
||||||
|
{
|
||||||
|
mDatabase.exec("ROLLBACK TRANSACTION");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw SQLite::Exception("Transaction already committed.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace SQLite
|
} // namespace SQLite
|
||||||
|
@ -99,8 +99,8 @@ TEST(Transaction, commitRollback)
|
|||||||
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"third\")"));
|
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"third\")"));
|
||||||
EXPECT_EQ(2, db.getLastInsertRowid());
|
EXPECT_EQ(2, db.getLastInsertRowid());
|
||||||
|
|
||||||
// Execute a manual rollback (no real use case I can think of, so no rollback() method)
|
// Execute a manual rollback
|
||||||
db.exec("ROLLBACK");
|
transaction.rollback();
|
||||||
|
|
||||||
// end of scope: the automatic rollback should not raise an error because it is harmless
|
// end of scope: the automatic rollback should not raise an error because it is harmless
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user