2016-06-03 20:31:00 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include "Transaction.hpp"
|
|
|
|
#include "Connection.hpp"
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
2016-06-15 22:49:25 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-11-22 15:16:32 +01:00
|
|
|
SQMODE_DECL_TYPENAME(Typename, _SC("SqLiteTransaction"))
|
2016-06-15 22:49:25 +02:00
|
|
|
|
2016-06-03 20:31:00 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Transaction::Transaction(const Connection & db)
|
|
|
|
: Transaction(db.GetHandle())
|
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-06-15 22:49:25 +02:00
|
|
|
Transaction::Transaction(const ConnRef & db)
|
|
|
|
: m_Handle(db), m_Committed(false)
|
2016-06-03 20:31:00 +02:00
|
|
|
{
|
|
|
|
// Was the specified database connection valid?
|
2016-06-15 22:49:25 +02:00
|
|
|
if (!m_Handle)
|
2016-06-03 20:31:00 +02:00
|
|
|
{
|
|
|
|
STHROWF("Invalid connection handle");
|
|
|
|
}
|
|
|
|
// Attempt to begin transaction
|
2016-06-15 22:49:25 +02:00
|
|
|
m_Handle->mStatus = sqlite3_exec(m_Handle->mPtr, "BEGIN", nullptr, nullptr, nullptr);
|
|
|
|
// Validate the result
|
|
|
|
if (m_Handle->mStatus != SQLITE_OK)
|
2016-06-03 20:31:00 +02:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
STHROWF("Unable to begin transaction [%s]", m_Handle->ErrMsg());
|
2016-06-03 20:31:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Transaction::~Transaction()
|
|
|
|
{
|
|
|
|
// Was this transaction successfully committed?
|
|
|
|
if (m_Committed)
|
|
|
|
{
|
|
|
|
return; // We're done here!
|
|
|
|
}
|
|
|
|
// Attempt to roll back changes because this failed to commit
|
2016-06-15 22:49:25 +02:00
|
|
|
m_Handle->mStatus = sqlite3_exec(m_Handle->mPtr, "ROLLBACK", nullptr, nullptr, nullptr);
|
|
|
|
// Validate the result
|
|
|
|
if (m_Handle->mStatus != SQLITE_OK)
|
2016-06-03 20:31:00 +02:00
|
|
|
{
|
2016-11-13 08:31:23 +01:00
|
|
|
// We cannot throw exceptions in destructor
|
|
|
|
SqMod_LogErr("Unable to rollback transaction [%s]", m_Handle->ErrMsg());
|
2016-06-03 20:31:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
bool Transaction::Commit()
|
|
|
|
{
|
|
|
|
// We shouldn't even be here if there wasn't a valid connection but let's be sure
|
2016-06-15 22:49:25 +02:00
|
|
|
if (!m_Handle)
|
2016-06-03 20:31:00 +02:00
|
|
|
{
|
|
|
|
STHROWF("Invalid database connection");
|
|
|
|
}
|
|
|
|
// Was this transaction already committed?
|
|
|
|
else if (m_Committed)
|
|
|
|
{
|
|
|
|
STHROWF("Transaction was already committed");
|
|
|
|
}
|
|
|
|
// Attempt to commit the change during this transaction
|
2016-06-15 22:49:25 +02:00
|
|
|
m_Handle->mStatus = sqlite3_exec(m_Handle->mPtr, "COMMIT", nullptr, nullptr, nullptr);
|
|
|
|
// Validate the result
|
|
|
|
if (m_Handle->mStatus != SQLITE_OK)
|
2016-06-03 20:31:00 +02:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
STHROWF("Unable to commit transaction [%s]", m_Handle->ErrMsg());
|
2016-06-03 20:31:00 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Committed = true; // Everything was committed successfully
|
|
|
|
}
|
|
|
|
// Return the result
|
|
|
|
return m_Committed;
|
|
|
|
}
|
|
|
|
|
2016-06-15 22:49:25 +02:00
|
|
|
// ================================================================================================
|
|
|
|
void Register_Transaction(Table & sqlns)
|
|
|
|
{
|
|
|
|
sqlns.Bind(_SC("Transaction"),
|
2016-11-22 15:16:32 +01:00
|
|
|
Class< Transaction, NoCopy< Transaction > >(sqlns.GetVM(), Typename::Str)
|
2016-06-15 22:49:25 +02:00
|
|
|
// Constructors
|
|
|
|
.Ctor< const Connection & >()
|
|
|
|
// Meta-methods
|
2016-11-22 15:16:32 +01:00
|
|
|
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
|
2016-06-15 22:49:25 +02:00
|
|
|
.Func(_SC("_tostring"), &Transaction::ToString)
|
|
|
|
// Properties
|
|
|
|
.Prop(_SC("IsValid"), &Transaction::IsValid)
|
|
|
|
.Prop(_SC("Committed"), &Transaction::Commited)
|
|
|
|
// Member Methods
|
|
|
|
.Func(_SC("Commit"), &Transaction::Commit)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-06-03 20:31:00 +02:00
|
|
|
} // Namespace:: SqMod
|