mirror of
https://github.com/cuberite/SQLiteCpp.git
synced 2025-08-08 03:36:06 -04:00
Fix Savepoint comments and file formatting
Improved Savepoint description, it was mostly copy-pasted from Transaction with missing bits of information Conform to the SQLiteCpp code style, mainly braces have to be located on their own line
This commit is contained in:
parent
343299a31d
commit
4a9cc0adce
@ -14,28 +14,31 @@
|
|||||||
|
|
||||||
#include <SQLiteCpp/Exception.h>
|
#include <SQLiteCpp/Exception.h>
|
||||||
|
|
||||||
namespace SQLite {
|
namespace SQLite
|
||||||
|
{
|
||||||
|
|
||||||
// Foward declaration
|
// Forward declaration
|
||||||
class Database;
|
class Database;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief RAII encapsulation of a SQLite Savepoint.
|
* @brief RAII encapsulation of a SQLite Savepoint.
|
||||||
*
|
*
|
||||||
* A Savepoint is a way to group multiple SQL statements into an atomic
|
* SAVEPOINTs are a method of creating Transactions, similar to BEGIN and COMMIT,
|
||||||
* secure operation; either it succeeds, with all the changes committed to the
|
* except that the SAVEPOINT and RELEASE commands are named and may be nested..
|
||||||
* database file, or if it fails, all the changes are rolled back to the initial
|
*
|
||||||
* state at the start of the savepoint.
|
* Resource Acquisition Is Initialization (RAII) means that the Savepoint
|
||||||
|
* begins in the constructor and is rolled back in the destructor (unless committed before), so that there is
|
||||||
|
* no need to worry about memory management or the validity of the underlying SQLite Connection.
|
||||||
*
|
*
|
||||||
* This method also offers big performances improvements compared to
|
* This method also offers big performances improvements compared to
|
||||||
* individually executed statements.
|
* individually executed statements.
|
||||||
*
|
*
|
||||||
* Caveats:
|
* Caveats:
|
||||||
*
|
*
|
||||||
* 1) Calling COMMIT or commiting a parent transaction or RELEASE on a parent
|
* 1) Calling COMMIT or committing a parent transaction or RELEASE on a parent
|
||||||
* savepoint will cause this savepoint to be released.
|
* savepoint will cause this savepoint to be released.
|
||||||
*
|
*
|
||||||
* 2) Calling ROLLBACK or rolling back a parent savepoint will cause this
|
* 2) Calling ROLLBACK TO or rolling back a parent savepoint will cause this
|
||||||
* savepoint to be rolled back.
|
* savepoint to be rolled back.
|
||||||
*
|
*
|
||||||
* 3) This savepoint is not saved to the database until this and all savepoints
|
* 3) This savepoint is not saved to the database until this and all savepoints
|
||||||
@ -43,19 +46,15 @@ class Database;
|
|||||||
*
|
*
|
||||||
* See also: https://sqlite.org/lang_savepoint.html
|
* See also: https://sqlite.org/lang_savepoint.html
|
||||||
*
|
*
|
||||||
* Thread-safety: a Transaction object shall not be shared by multiple threads,
|
* Thread-safety: a Savepoint object shall not be shared by multiple threads, because:
|
||||||
* because:
|
* 1) in the SQLite "Thread Safe" mode, "SQLite can be safely used by multiple threads
|
||||||
*
|
* provided that no single database connection is used simultaneously in two or more threads."
|
||||||
* 1) in the SQLite "Thread Safe" mode, "SQLite can be safely used by multiple
|
* 2) the SQLite "Serialized" mode is not supported by SQLiteC++,
|
||||||
* threads provided that no single database connection is used simultaneously in
|
* because of the way it shares the underling SQLite precompiled statement
|
||||||
* two or more threads."
|
* in a custom shared pointer (See the inner class "Statement::Ptr").
|
||||||
*
|
|
||||||
* 2) the SQLite "Serialized" mode is not supported by SQLiteC++, because of the
|
|
||||||
* way it shares the underling SQLite precompiled statement in a custom shared
|
|
||||||
* pointer (See the inner class "Statement::Ptr").
|
|
||||||
*/
|
*/
|
||||||
|
class Savepoint
|
||||||
class Savepoint {
|
{
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Begins the SQLite savepoint
|
* @brief Begins the SQLite savepoint
|
||||||
@ -73,7 +72,7 @@ class Savepoint {
|
|||||||
Savepoint& operator=(const Savepoint&) = delete;
|
Savepoint& operator=(const Savepoint&) = delete;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Safely rollback the savepoint if it has not been commited.
|
* @brief Safely rollback the savepoint if it has not been committed.
|
||||||
*/
|
*/
|
||||||
~Savepoint();
|
~Savepoint();
|
||||||
|
|
||||||
@ -83,7 +82,7 @@ class Savepoint {
|
|||||||
void release();
|
void release();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Rollback the savepoint
|
* @brief Rollback to the savepoint, but don't release it.
|
||||||
*/
|
*/
|
||||||
void rollback();
|
void rollback();
|
||||||
|
|
||||||
@ -92,4 +91,5 @@ class Savepoint {
|
|||||||
std::string msName; ///< Name of the Savepoint
|
std::string msName; ///< Name of the Savepoint
|
||||||
bool mbReleased = false; ///< True when release has been called
|
bool mbReleased = false; ///< True when release has been called
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace SQLite
|
} // namespace SQLite
|
||||||
|
@ -16,11 +16,13 @@
|
|||||||
#include <SQLiteCpp/Savepoint.h>
|
#include <SQLiteCpp/Savepoint.h>
|
||||||
#include <SQLiteCpp/Statement.h>
|
#include <SQLiteCpp/Statement.h>
|
||||||
|
|
||||||
namespace SQLite {
|
namespace SQLite
|
||||||
|
{
|
||||||
|
|
||||||
// Begins the SQLite savepoint
|
// Begins the SQLite savepoint
|
||||||
Savepoint::Savepoint(Database& aDatabase, const std::string& aName)
|
Savepoint::Savepoint(Database& aDatabase, const std::string& aName)
|
||||||
: mDatabase(aDatabase), msName(aName) {
|
: mDatabase(aDatabase), msName(aName)
|
||||||
|
{
|
||||||
// workaround because you cannot bind to SAVEPOINT
|
// workaround because you cannot bind to SAVEPOINT
|
||||||
// escape name for use in query
|
// escape name for use in query
|
||||||
Statement stmt(mDatabase, "SELECT quote(?)");
|
Statement stmt(mDatabase, "SELECT quote(?)");
|
||||||
@ -32,33 +34,46 @@ Savepoint::Savepoint(Database& aDatabase, const std::string& aName)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Safely rollback the savepoint if it has not been committed.
|
// Safely rollback the savepoint if it has not been committed.
|
||||||
Savepoint::~Savepoint() {
|
Savepoint::~Savepoint()
|
||||||
if (!mbReleased) {
|
{
|
||||||
try {
|
if (!mbReleased)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
rollback();
|
rollback();
|
||||||
release();
|
release();
|
||||||
} catch (SQLite::Exception&) {
|
}
|
||||||
// Never throw an exception in a destructor: error if already rolled
|
catch (SQLite::Exception&)
|
||||||
// back or released, but no harm is caused by this.
|
{
|
||||||
|
// Never throw an exception in a destructor: error if already released,
|
||||||
|
// but no harm is caused by this.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Release the savepoint and commit
|
// Release the savepoint and commit
|
||||||
void Savepoint::release() {
|
void Savepoint::release()
|
||||||
if (!mbReleased) {
|
{
|
||||||
|
if (!mbReleased)
|
||||||
|
{
|
||||||
mDatabase.exec(std::string("RELEASE SAVEPOINT ") + msName);
|
mDatabase.exec(std::string("RELEASE SAVEPOINT ") + msName);
|
||||||
mbReleased = true;
|
mbReleased = true;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
throw SQLite::Exception("Savepoint already released.");
|
throw SQLite::Exception("Savepoint already released.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rollback the savepoint
|
// Rollback to the savepoint, but don't release it
|
||||||
void Savepoint::rollback() {
|
void Savepoint::rollback()
|
||||||
if (!mbReleased) {
|
{
|
||||||
|
if (!mbReleased)
|
||||||
|
{
|
||||||
mDatabase.exec(std::string("ROLLBACK TO SAVEPOINT ") + msName);
|
mDatabase.exec(std::string("ROLLBACK TO SAVEPOINT ") + msName);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
throw SQLite::Exception("Savepoint already released.");
|
throw SQLite::Exception("Savepoint already released.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user