From e02bf841ad93f8094628da19ce16cc5759aff117 Mon Sep 17 00:00:00 2001 From: asfernandes Date: Wed, 11 Feb 2015 15:12:18 +0000 Subject: [PATCH] Move Helper class from UDR to the main interface header. Use proper (argh) ref. counting in the ExternalContext::get* methods. --- examples/udr/Functions.cpp | 2 +- examples/udr/Triggers.cpp | 8 +-- src/include/firebird/Interface.h | 88 +++++++++++++++++++++++++++++ src/include/firebird/UdrCppEngine.h | 57 ++----------------- src/jrd/ExtEngineManager.cpp | 8 ++- 5 files changed, 103 insertions(+), 60 deletions(-) diff --git a/examples/udr/Functions.cpp b/examples/udr/Functions.cpp index 434973d7ba..83be8aee6a 100644 --- a/examples/udr/Functions.cpp +++ b/examples/udr/Functions.cpp @@ -57,7 +57,7 @@ FB_UDR_BEGIN_FUNCTION(wait_event) delete [] s; ISC_STATUS_ARRAY statusVector = {0}; - isc_db_handle dbHandle = getIscDbHandle(status, context); + isc_db_handle dbHandle = Helper::getIscDbHandle(status, context); ISC_ULONG counter = 0; FbException::check(isc_wait_for_event( diff --git a/examples/udr/Triggers.cpp b/examples/udr/Triggers.cpp index fe0562f19a..847a54d819 100644 --- a/examples/udr/Triggers.cpp +++ b/examples/udr/Triggers.cpp @@ -73,8 +73,8 @@ FB_UDR_BEGIN_TRIGGER(replicate) , triggerMetadata(metadata->getTriggerMetadata(status)) { ISC_STATUS_ARRAY statusVector = {0}; - isc_db_handle dbHandle = getIscDbHandle(status, context); - isc_tr_handle trHandle = getIscTrHandle(status, context); + isc_db_handle dbHandle = Helper::getIscDbHandle(status, context); + isc_tr_handle trHandle = Helper::getIscTrHandle(status, context); isc_stmt_handle stmtHandle = 0; FbException::check(isc_dsql_allocate_statement( @@ -224,8 +224,8 @@ FB_UDR_BEGIN_TRIGGER(replicate_persons) , triggerMetadata(metadata->getTriggerMetadata(status)) { ISC_STATUS_ARRAY statusVector = {0}; - isc_db_handle dbHandle = getIscDbHandle(status, context); - isc_tr_handle trHandle = getIscTrHandle(status, context); + isc_db_handle dbHandle = Helper::getIscDbHandle(status, context); + isc_tr_handle trHandle = Helper::getIscTrHandle(status, context); isc_stmt_handle stmtHandle = 0; FbException::check(isc_dsql_allocate_statement( diff --git a/src/include/firebird/Interface.h b/src/include/firebird/Interface.h index a469e353c3..9f6512833e 100644 --- a/src/include/firebird/Interface.h +++ b/src/include/firebird/Interface.h @@ -231,6 +231,94 @@ namespace Firebird } }; +#ifdef FB_API_VER // internal hack + class Helper + { + public: + template + static isc_db_handle getIscDbHandle(StatusType* status, IAttachment* attachment) + { + if (!attachment) + return 0; + + ISC_STATUS_ARRAY statusVector = {0}; + isc_db_handle handle = 0; + + fb_get_database_handle(statusVector, &handle, attachment); + + if (!handle) + { + status->setErrors(statusVector); + StatusType::checkException(status); + } + + return handle; + } + + template + static isc_db_handle getIscDbHandle(StatusType* status, IExternalContext* context) + { + IAttachment* attachment = context->getAttachment(status); + + if (!attachment) + return 0; + + try + { + isc_db_handle handle = getIscDbHandle(status, attachment); + attachment->release(); + return handle; + } + catch (...) + { + attachment->release(); + throw; + } + } + + template + static isc_tr_handle getIscTrHandle(StatusType* status, ITransaction* transaction) + { + if (!transaction) + return 0; + + ISC_STATUS_ARRAY statusVector = {0}; + isc_tr_handle handle = 0; + + fb_get_transaction_handle(statusVector, &handle, transaction); + + if (!handle) + { + status->setErrors(statusVector); + StatusType::checkException(status); + } + + return handle; + } + + template + static isc_tr_handle getIscTrHandle(StatusType* status, IExternalContext* context) + { + ITransaction* transaction = context->getTransaction(status); + + if (!transaction) + return 0; + + try + { + isc_tr_handle handle = getIscTrHandle(status, transaction); + transaction->release(); + return handle; + } + catch (...) + { + transaction->release(); + throw; + } + } + }; +#endif // FB_API_VER + // Additional API function. // Should be used only in non-plugin modules. // All plugins including providers should use passed at init time interface instead. diff --git a/src/include/firebird/UdrCppEngine.h b/src/include/firebird/UdrCppEngine.h index d2c1a1ee3e..f804820226 100644 --- a/src/include/firebird/UdrCppEngine.h +++ b/src/include/firebird/UdrCppEngine.h @@ -218,57 +218,8 @@ namespace Firebird template class Procedure; -class Helper -{ -public: - template - static isc_db_handle getIscDbHandle(StatusType* status, IExternalContext* context) - { - IAttachment* attachment = context->getAttachment(status); - - if (!attachment) - return 0; - - ISC_STATUS_ARRAY statusVector = {0}; - isc_db_handle handle = 0; - - fb_get_database_handle(statusVector, &handle, attachment); - - if (!handle) - { - status->setErrors(statusVector); - StatusType::checkException(status); - } - - return handle; - } - - template - static isc_tr_handle getIscTrHandle(StatusType* status, IExternalContext* context) - { - ITransaction* transaction = context->getTransaction(status); - - if (!transaction) - return 0; - - ISC_STATUS_ARRAY statusVector = {0}; - isc_tr_handle handle = 0; - - fb_get_transaction_handle(statusVector, &handle, transaction); - - if (!handle) - { - status->setErrors(statusVector); - StatusType::checkException(status); - } - - return handle; - } -}; - - template -class ResultSet : public IExternalResultSetImpl, public Helper +class ResultSet : public IExternalResultSetImpl { public: ResultSet(IExternalContext* aContext, Procedure* aProcedure, @@ -295,7 +246,7 @@ protected: template -class Function : public IExternalFunctionImpl, public Helper +class Function : public IExternalFunctionImpl { public: FB__UDR_COMMON_TYPE(InMessage); @@ -314,7 +265,7 @@ public: template -class Procedure : public IExternalProcedureImpl, public Helper +class Procedure : public IExternalProcedureImpl { public: FB__UDR_COMMON_TYPE(InMessage); @@ -333,7 +284,7 @@ public: template -class Trigger : public IExternalTriggerImpl, public Helper +class Trigger : public IExternalTriggerImpl { public: FB__UDR_COMMON_TYPE(FieldsMessage); diff --git a/src/jrd/ExtEngineManager.cpp b/src/jrd/ExtEngineManager.cpp index 722e22059c..d320264ebb 100644 --- a/src/jrd/ExtEngineManager.cpp +++ b/src/jrd/ExtEngineManager.cpp @@ -640,13 +640,17 @@ IExternalEngine* ExtEngineManager::ExternalContextImpl::getEngine(CheckStatusWra return engine; } -Firebird::IAttachment* ExtEngineManager::ExternalContextImpl::getAttachment(CheckStatusWrapper* /*status*/) +Firebird::IAttachment* ExtEngineManager::ExternalContextImpl::getAttachment( + CheckStatusWrapper* /*status*/) { + externalAttachment->addRef(); return externalAttachment; } -Firebird::ITransaction* ExtEngineManager::ExternalContextImpl::getTransaction(CheckStatusWrapper* /*status*/) +Firebird::ITransaction* ExtEngineManager::ExternalContextImpl::getTransaction( + CheckStatusWrapper* /*status*/) { + externalTransaction->addRef(); return externalTransaction; }