1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00

Transactor.

This commit is contained in:
Sandu Liviu Catalin 2021-04-10 17:19:37 +03:00
parent 623f8053f4
commit 6ee33a6dcd
2 changed files with 172 additions and 5 deletions

View File

@ -31,6 +31,7 @@ SQMOD_DECL_TYPENAME(SqBoolBinding, _SC("SqBoolBinding"))
SQMOD_DECL_TYPENAME(SqPcDataSession, _SC("SqDataSession")) SQMOD_DECL_TYPENAME(SqPcDataSession, _SC("SqDataSession"))
SQMOD_DECL_TYPENAME(SqPcDataStatement, _SC("SqDataStatement")) SQMOD_DECL_TYPENAME(SqPcDataStatement, _SC("SqDataStatement"))
SQMOD_DECL_TYPENAME(SqPcDataRecordSet, _SC("SqDataRecordSet")) SQMOD_DECL_TYPENAME(SqPcDataRecordSet, _SC("SqDataRecordSet"))
SQMOD_DECL_TYPENAME(SqPcDataTransaction, _SC("SqDataTransaction"))
SQMOD_DECL_TYPENAME(SqPcDataSessionPool, _SC("SqDataSessionPool")) SQMOD_DECL_TYPENAME(SqPcDataSessionPool, _SC("SqDataSessionPool"))
SQMOD_DECL_TYPENAME(SqPcDataStatementResult, _SC("SqDataStatementResult")) SQMOD_DECL_TYPENAME(SqPcDataStatementResult, _SC("SqDataStatementResult"))
@ -545,7 +546,13 @@ static void ProcessPocoData()
// Forward the callback with the result // Forward the callback with the result
if (!inst->mFunc.IsNull()) if (!inst->mFunc.IsNull())
{ {
inst->mFunc.Execute(inst->mStmt, inst->mRes.data()); try {
inst->mFunc.Execute(inst->mStmt, inst->mRes.data());
} catch (const Poco::Exception & e) {
LogErr("SqData.Process: %s", e.displayText().c_str());
} catch (const std::exception & e) {
LogErr("SqData.Process: %s", e.what());
}
} }
// Stop processing this result // Stop processing this result
inst->UnchainInstance(); inst->UnchainInstance();
@ -760,12 +767,32 @@ void Register_POCO_Data(HSQUIRRELVM vm, Table &)
.StaticFunc(_SC("GetName"), &SqDataSessionPool::GetName_) .StaticFunc(_SC("GetName"), &SqDataSessionPool::GetName_)
); );
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
ns.Bind(_SC("Transaction"),
Class< SqDataTransaction, NoCopy< SqDataTransaction > >(vm, SqPcDataTransaction::Str)
// Constructors
.Ctor< SqDataSession & >()
.Ctor< SqDataSession &, bool >()
// Meta-methods
.SquirrelFunc(_SC("_typename"), &SqPcDataTransaction::Fn)
// Properties
.Prop(_SC("Active"), &SqDataTransaction::IsActive)
.Prop(_SC("Isolation"), &SqDataTransaction::GetIsolation, &SqDataTransaction::SetIsolation)
// Member Methods
.Func(_SC("HasIsolation"), &SqDataTransaction::HasIsolation)
.Func(_SC("IsIsolation"), &SqDataTransaction::IsIsolation)
.FmtFunc(_SC("Execute"), &SqDataTransaction::Execute)
.FmtFunc(_SC("ExecuteList"), &SqDataTransaction::ExecuteList)
.CbFunc(_SC("Transact"), &SqDataTransaction::Transact)
.Func(_SC("Commit"), &SqDataTransaction::Commit)
.Func(_SC("Rollback"), &SqDataTransaction::Rollback)
);
// --------------------------------------------------------------------------------------------
ns.Func(_SC("Process"), ProcessPocoData); ns.Func(_SC("Process"), ProcessPocoData);
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
#ifdef SQMOD_POCO_HAS_SQLITE #ifdef SQMOD_POCO_HAS_SQLITE
ns.FmtFunc(_SC("SQLiteEscapeString"), SQLiteEscapeString); ns.FmtFunc(_SC("SQLiteEscapeString"), SQLiteEscapeString);
ns.FmtFunc(_SC("SQLiteEscapeStringEx"), SQLiteEscapeStringEx); ns.FmtFunc(_SC("SQLiteEscapeStringEx"), SQLiteEscapeStringEx);
#endif #endif
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
Register_POCO_Data_Binding< SQInteger, SqIntegerBinding >(vm, ns, _SC("IntBind")); Register_POCO_Data_Binding< SQInteger, SqIntegerBinding >(vm, ns, _SC("IntBind"));
Register_POCO_Data_Binding< String, SqStringBinding >(vm, ns, _SC("StrBind")); Register_POCO_Data_Binding< String, SqStringBinding >(vm, ns, _SC("StrBind"));

View File

@ -9,6 +9,7 @@
#include <Poco/Data/Session.h> #include <Poco/Data/Session.h>
#include <Poco/Data/Statement.h> #include <Poco/Data/Statement.h>
#include <Poco/Data/RecordSet.h> #include <Poco/Data/RecordSet.h>
#include <Poco/Data/Transaction.h>
#include <Poco/Data/SessionPool.h> #include <Poco/Data/SessionPool.h>
#include <Poco/Data/LOB.h> #include <Poco/Data/LOB.h>
@ -474,6 +475,7 @@ using namespace Poco::Data::Keywords;
using Poco::Data::Session; using Poco::Data::Session;
using Poco::Data::Statement; using Poco::Data::Statement;
using Poco::Data::RecordSet; using Poco::Data::RecordSet;
using Poco::Data::Transaction;
using Poco::Data::SessionPool; using Poco::Data::SessionPool;
using Poco::Data::ReferenceBinding; using Poco::Data::ReferenceBinding;
@ -1930,4 +1932,142 @@ struct SqDataSessionPool : public SessionPool
} }
}; };
/* ------------------------------------------------------------------------------------------------
* Helps with transactions in domain logic.
*/
struct SqDataTransaction : public Transaction
{
/* --------------------------------------------------------------------------------------------
* Base constructor.
*/
explicit SqDataTransaction(SqDataSession & session)
: Transaction(session)
{
}
/* --------------------------------------------------------------------------------------------
* Base constructor.
*/
SqDataTransaction(SqDataSession & session, bool start)
: Transaction(session, start)
{
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
SqDataTransaction(const SqDataTransaction &) = delete;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
SqDataTransaction(SqDataTransaction &&) = delete;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~SqDataTransaction() = default;
/* --------------------------------------------------------------------------------------------
* Assignment operator.
*/
SqDataTransaction & operator = (const SqDataTransaction &) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment.
*/
SqDataTransaction & operator = (SqDataTransaction &&) = delete;
/* --------------------------------------------------------------------------------------------
* Returns false after the transaction has been committed or rolled back,
* true if the transaction is ongoing.
*/
SQMOD_NODISCARD bool IsActive()
{
return Transaction::isActive();
}
/* --------------------------------------------------------------------------------------------
* Sets the transaction isolation level.
*/
void SetIsolation(SQInteger ti)
{
Transaction::setIsolation(static_cast< Poco::UInt32 >(ti));
}
/* --------------------------------------------------------------------------------------------
* Returns the transaction isolation level.
*/
SQMOD_NODISCARD SQInteger GetIsolation()
{
return static_cast< SQInteger >(Transaction::getIsolation());
}
/* --------------------------------------------------------------------------------------------
* Returns true iff the transaction isolation level corresponding to the supplied bitmask is supported.
*/
SQMOD_NODISCARD bool HasIsolation(SQInteger ti)
{
return Transaction::hasIsolation(static_cast< Poco::UInt32 >(ti));
}
/* --------------------------------------------------------------------------------------------
* Returns true iff the transaction isolation level corresponds to the supplied bitmask.
*/
SQMOD_NODISCARD bool IsIsolation(SQInteger ti)
{
return Transaction::isIsolation(static_cast< Poco::UInt32 >(ti));
}
/* --------------------------------------------------------------------------------------------
* Executes and, if do_commit is true, commits the transaction.
* Passing true value for commit disables rollback during destruction of this Transaction object.
*/
SqDataTransaction & Execute(bool do_commit, StackStrF & sql)
{
Transaction::execute(sql.ToStr(), do_commit);
return *this;
}
/* --------------------------------------------------------------------------------------------
* Executes all the SQL statements supplied in the vector and,
* after the last one is sucesfully executed, commits the transaction.
*/
SqDataTransaction & ExecuteList(const SqVector< std::string > & sql)
{
Transaction::execute(sql.Valid());
return *this;
}
/* --------------------------------------------------------------------------------------------
* Executes the transactor and, unless transactor throws an exception, commits the transaction.
*/
SqDataTransaction & Transact(Function & cb)
{
auto t = [&](Session & s) {
cb(LightObj(reinterpret_cast< SqDataSession * >(&s), nullptr));
};
Transaction::transact(t);
return *this;
}
/* --------------------------------------------------------------------------------------------
* Commits the current transaction.
*/
SqDataTransaction & Commit()
{
Transaction::commit();
return *this;
}
/* --------------------------------------------------------------------------------------------
* Rolls back the current transaction.
*/
SqDataTransaction & Rollback()
{
Transaction::rollback();
return *this;
}
};
} // Namespace:: SqMod } // Namespace:: SqMod