60 lines
1.4 KiB
C++
60 lines
1.4 KiB
C++
/**
|
|
* @file CSQLiteTransaction.cpp
|
|
* @ingroup SQLiteCpp
|
|
* @brief A CSQLiteTransaction is way to group multiple SQL statements into an atomic secured operation.
|
|
*
|
|
* Copyright (c) 2012-2013 Sebastien Rombauts (sebastien.rombauts@gmail.com)
|
|
*
|
|
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
|
|
* or copy at http://opensource.org/licenses/MIT)
|
|
*/
|
|
#include "pch.h"
|
|
#include "CSQLite/CSQLiteTransaction.h"
|
|
|
|
#include "CSQLite/CSQLiteDB.h"
|
|
|
|
// Begins the SQLite CSQLiteTransaction
|
|
CSQLiteTransaction::CSQLiteTransaction(CSQLiteDB & aDatabase, bool startnow) :
|
|
mDatabase(aDatabase),
|
|
mbCommited(false)
|
|
{
|
|
if(startnow)
|
|
{
|
|
mDatabase.exec("BEGIN");
|
|
mbCommited = false;
|
|
}else{
|
|
mbCommited = true;
|
|
}
|
|
}
|
|
|
|
// Safely rollback the CSQLiteTransaction if it has not been committed.
|
|
CSQLiteTransaction::~CSQLiteTransaction()
|
|
{
|
|
if (false == mbCommited)
|
|
{
|
|
try
|
|
{
|
|
mDatabase.exec("ROLLBACK");
|
|
}
|
|
catch (CSQLiteException&)
|
|
{
|
|
// Never throw an exception in a destructor: error if already rollbacked, but no harm is caused by this.
|
|
}
|
|
}
|
|
}
|
|
|
|
// Commit the CSQLiteTransaction.
|
|
void CSQLiteTransaction::commit()
|
|
{
|
|
if (false == mbCommited)
|
|
{
|
|
mDatabase.exec("COMMIT");
|
|
mbCommited = true;
|
|
}
|
|
else
|
|
{
|
|
throw CSQLiteException("CSQLiteTransaction already commited.");
|
|
}
|
|
}
|
|
|