mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2026-07-29 20:07:13 +02:00
Untested revised implementation of the SQLite module.
This commit is contained in:
@@ -5,6 +5,14 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQInteger Transaction::Typename(HSQUIRRELVM vm)
|
||||
{
|
||||
static const SQChar name[] = _SC("SqSQLiteTransaction");
|
||||
sq_pushstring(vm, name, sizeof(name));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Transaction::Transaction(const Connection & db)
|
||||
: Transaction(db.GetHandle())
|
||||
@@ -13,18 +21,20 @@ Transaction::Transaction(const Connection & db)
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Transaction::Transaction(const ConnHnd & db)
|
||||
: m_Connection(db), m_Committed(false)
|
||||
Transaction::Transaction(const ConnRef & db)
|
||||
: m_Handle(db), m_Committed(false)
|
||||
{
|
||||
// Was the specified database connection valid?
|
||||
if (!m_Connection)
|
||||
if (!m_Handle)
|
||||
{
|
||||
STHROWF("Invalid connection handle");
|
||||
}
|
||||
// Attempt to begin transaction
|
||||
else if ((m_Connection = sqlite3_exec(m_Connection, "BEGIN", nullptr, nullptr, nullptr)) != SQLITE_OK)
|
||||
m_Handle->mStatus = sqlite3_exec(m_Handle->mPtr, "BEGIN", nullptr, nullptr, nullptr);
|
||||
// Validate the result
|
||||
if (m_Handle->mStatus != SQLITE_OK)
|
||||
{
|
||||
STHROWF("Unable to begin transaction [%s]", m_Connection.ErrMsg());
|
||||
STHROWF("Unable to begin transaction [%s]", m_Handle->ErrMsg());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,9 +47,11 @@ Transaction::~Transaction()
|
||||
return; // We're done here!
|
||||
}
|
||||
// Attempt to roll back changes because this failed to commit
|
||||
if ((m_Connection = sqlite3_exec(m_Connection, "ROLLBACK", nullptr, nullptr, nullptr)) != SQLITE_OK)
|
||||
m_Handle->mStatus = sqlite3_exec(m_Handle->mPtr, "ROLLBACK", nullptr, nullptr, nullptr);
|
||||
// Validate the result
|
||||
if (m_Handle->mStatus != SQLITE_OK)
|
||||
{
|
||||
STHROWF("Unable to rollback transaction [%s]", m_Connection.ErrMsg());
|
||||
STHROWF("Unable to rollback transaction [%s]", m_Handle->ErrMsg());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +59,7 @@ Transaction::~Transaction()
|
||||
bool Transaction::Commit()
|
||||
{
|
||||
// We shouldn't even be here if there wasn't a valid connection but let's be sure
|
||||
if (!m_Connection)
|
||||
if (!m_Handle)
|
||||
{
|
||||
STHROWF("Invalid database connection");
|
||||
}
|
||||
@@ -57,9 +69,11 @@ bool Transaction::Commit()
|
||||
STHROWF("Transaction was already committed");
|
||||
}
|
||||
// Attempt to commit the change during this transaction
|
||||
else if ((m_Connection = sqlite3_exec(m_Connection, "COMMIT", nullptr, nullptr, nullptr)) != SQLITE_OK)
|
||||
m_Handle->mStatus = sqlite3_exec(m_Handle->mPtr, "COMMIT", nullptr, nullptr, nullptr);
|
||||
// Validate the result
|
||||
if (m_Handle->mStatus != SQLITE_OK)
|
||||
{
|
||||
STHROWF("Unable to commit transaction [%s]", m_Connection.ErrMsg());
|
||||
STHROWF("Unable to commit transaction [%s]", m_Handle->ErrMsg());
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -69,4 +83,22 @@ bool Transaction::Commit()
|
||||
return m_Committed;
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
void Register_Transaction(Table & sqlns)
|
||||
{
|
||||
sqlns.Bind(_SC("Transaction"),
|
||||
Class< Transaction, NoCopy< Transaction > >(sqlns.GetVM(), _SC("SqSQLiteTransaction"))
|
||||
// Constructors
|
||||
.Ctor< const Connection & >()
|
||||
// Meta-methods
|
||||
.SquirrelFunc(_SC("_typename"), &Transaction::Typename)
|
||||
.Func(_SC("_tostring"), &Transaction::ToString)
|
||||
// Properties
|
||||
.Prop(_SC("IsValid"), &Transaction::IsValid)
|
||||
.Prop(_SC("Committed"), &Transaction::Commited)
|
||||
// Member Methods
|
||||
.Func(_SC("Commit"), &Transaction::Commit)
|
||||
);
|
||||
}
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
||||
Reference in New Issue
Block a user