2009-10-21 02:42:38 +02:00
|
|
|
/*
|
|
|
|
* The contents of this file are subject to the Initial
|
|
|
|
* Developer's Public License Version 1.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed AS IS,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing rights
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code was created by Adriano dos Santos Fernandes
|
|
|
|
* for the Firebird Open Source RDBMS project.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 Adriano dos Santos Fernandes <adrianosf@uol.com.br>
|
|
|
|
* and all contributors signed below.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef JRD_EXT_ENGINE_MANAGER_H
|
|
|
|
#define JRD_EXT_ENGINE_MANAGER_H
|
|
|
|
|
|
|
|
#include "FirebirdApi.h"
|
|
|
|
#include "FirebirdExternalApi.h"
|
2010-10-13 12:39:52 +02:00
|
|
|
#include "../common/common.h"
|
2009-10-21 02:42:38 +02:00
|
|
|
#include "../common/classes/array.h"
|
|
|
|
#include "../common/classes/fb_string.h"
|
|
|
|
#include "../common/classes/GenericMap.h"
|
|
|
|
#include "../common/classes/MetaName.h"
|
2010-11-21 04:47:29 +01:00
|
|
|
#include "../common/classes/NestConst.h"
|
2009-10-21 02:42:38 +02:00
|
|
|
#include "../common/classes/auto.h"
|
|
|
|
#include "../common/classes/rwlock.h"
|
2011-05-19 18:24:46 +02:00
|
|
|
#include "../common/classes/ImplementHelper.h"
|
2010-11-21 04:47:29 +01:00
|
|
|
///#include "../dsql/Nodes.h"
|
2009-10-21 02:42:38 +02:00
|
|
|
|
|
|
|
struct dsc;
|
|
|
|
|
|
|
|
namespace Jrd {
|
|
|
|
|
|
|
|
class thread_db;
|
|
|
|
class jrd_prc;
|
2011-05-07 21:52:44 +02:00
|
|
|
class jrd_tra;
|
2009-10-21 02:42:38 +02:00
|
|
|
class Attachment;
|
|
|
|
class Database;
|
|
|
|
class Format;
|
|
|
|
class Trigger;
|
2009-12-21 18:43:01 +01:00
|
|
|
class Function;
|
2010-11-21 04:47:29 +01:00
|
|
|
class ValueExprNode;
|
2009-10-21 02:42:38 +02:00
|
|
|
class ValueImpl;
|
|
|
|
class ValuesImpl;
|
|
|
|
struct impure_value;
|
|
|
|
struct record_param;
|
|
|
|
|
|
|
|
|
|
|
|
class ExtEngineManager : public Firebird::PermanentStorage
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
class AttachmentImpl;
|
|
|
|
template <typename T> class ContextManager;
|
|
|
|
class TransactionImpl;
|
|
|
|
|
2011-05-19 18:24:46 +02:00
|
|
|
class RoutineMetadata : public Firebird::VersionedIface<Firebird::IRoutineMetadata, FB_ROUTINE_METADATA_VERSION>,
|
|
|
|
public Firebird::PermanentStorage
|
2011-05-15 17:37:11 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
RoutineMetadata(MemoryPool& pool)
|
|
|
|
: PermanentStorage(pool),
|
|
|
|
package(pool),
|
|
|
|
name(pool),
|
|
|
|
entryPoint(pool),
|
|
|
|
body(pool),
|
|
|
|
triggerTable(pool),
|
|
|
|
triggerType(Firebird::ExternalTrigger::Type(0))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char* FB_CARG getPackage(Firebird::IStatus* /*status*/) const
|
|
|
|
{
|
|
|
|
return package.nullStr();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char* FB_CARG getName(Firebird::IStatus* /*status*/) const
|
|
|
|
{
|
|
|
|
return name.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char* FB_CARG getEntryPoint(Firebird::IStatus* /*status*/) const
|
|
|
|
{
|
|
|
|
return entryPoint.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char* FB_CARG getBody(Firebird::IStatus* /*status*/) const
|
|
|
|
{
|
|
|
|
return body.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char* FB_CARG getTriggerTable(Firebird::IStatus* /*status*/) const
|
|
|
|
{
|
|
|
|
return triggerTable.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Firebird::ExternalTrigger::Type FB_CARG getTriggerType(Firebird::IStatus* /*status*/) const
|
|
|
|
{
|
|
|
|
return triggerType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
Firebird::MetaName package;
|
|
|
|
Firebird::MetaName name;
|
|
|
|
Firebird::string entryPoint;
|
|
|
|
Firebird::string body;
|
|
|
|
Firebird::MetaName triggerTable;
|
|
|
|
Firebird::ExternalTrigger::Type triggerType;
|
|
|
|
};
|
|
|
|
|
2009-10-21 02:42:38 +02:00
|
|
|
class ExternalContextImpl : public Firebird::ExternalContext
|
|
|
|
{
|
|
|
|
friend class AttachmentImpl;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ExternalContextImpl(thread_db* tdbb, Firebird::ExternalEngine* aEngine);
|
|
|
|
virtual ~ExternalContextImpl();
|
|
|
|
|
|
|
|
void releaseTransaction();
|
|
|
|
void setTransaction(thread_db* tdbb);
|
|
|
|
|
|
|
|
virtual Firebird::ExternalEngine* FB_CALL getEngine(Firebird::Error* error);
|
2011-05-07 21:52:44 +02:00
|
|
|
virtual Firebird::IAttachment* FB_CALL getAttachment(Firebird::Error* error);
|
|
|
|
virtual Firebird::ITransaction* FB_CALL getTransaction(Firebird::Error* error);
|
|
|
|
virtual const char* FB_CALL getUserName();
|
|
|
|
virtual const char* FB_CALL getDatabaseName();
|
2009-10-21 02:42:38 +02:00
|
|
|
virtual const Firebird::Utf8* FB_CALL getClientCharSet();
|
|
|
|
virtual int FB_CALL obtainInfoCode();
|
|
|
|
virtual void* FB_CALL getInfo(int code);
|
|
|
|
virtual void* FB_CALL setInfo(int code, void* value);
|
|
|
|
|
|
|
|
private:
|
|
|
|
Firebird::ExternalEngine* engine;
|
|
|
|
Attachment* internalAttachment;
|
2011-05-07 21:52:44 +02:00
|
|
|
jrd_tra* internalTransaction;
|
|
|
|
Firebird::IAttachment* externalAttachment;
|
|
|
|
Firebird::ITransaction* externalTransaction;
|
2009-10-21 02:42:38 +02:00
|
|
|
Firebird::GenericMap<Firebird::NonPooled<int, void*> > miscInfo;
|
|
|
|
Firebird::MetaName clientCharSet;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EngineAttachment
|
|
|
|
{
|
|
|
|
EngineAttachment(Firebird::ExternalEngine* aEngine, Jrd::Attachment* aAttachment)
|
|
|
|
: engine(aEngine),
|
|
|
|
attachment(aAttachment)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool greaterThan(const EngineAttachment& i1, const EngineAttachment& i2)
|
|
|
|
{
|
|
|
|
return (i1.engine > i2.engine) ||
|
|
|
|
(i1.engine == i2.engine && i1.attachment > i2.attachment);
|
|
|
|
}
|
|
|
|
|
|
|
|
Firebird::ExternalEngine* engine;
|
|
|
|
Jrd::Attachment* attachment;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EngineAttachmentInfo
|
|
|
|
{
|
|
|
|
EngineAttachmentInfo()
|
|
|
|
: engine(NULL),
|
2009-10-30 11:50:59 +01:00
|
|
|
context(NULL),
|
|
|
|
adminCharSet(0)
|
2009-10-21 02:42:38 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Firebird::ExternalEngine* engine;
|
|
|
|
Firebird::AutoPtr<ExternalContextImpl> context;
|
|
|
|
USHORT adminCharSet;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
class Function
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Function(thread_db* tdbb, ExtEngineManager* aExtManager,
|
|
|
|
Firebird::ExternalEngine* aEngine,
|
2011-05-15 17:37:11 +02:00
|
|
|
RoutineMetadata* aMetadata,
|
2009-10-21 02:42:38 +02:00
|
|
|
Firebird::ExternalFunction* aFunction,
|
2009-12-21 18:43:01 +01:00
|
|
|
const Jrd::Function* aUdf);
|
2009-10-21 02:42:38 +02:00
|
|
|
~Function();
|
|
|
|
|
2010-11-21 04:47:29 +01:00
|
|
|
void execute(thread_db* tdbb, const Firebird::Array<NestConst<ValueExprNode> >& args,
|
|
|
|
impure_value* impure) const;
|
2009-10-21 02:42:38 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
ExtEngineManager* extManager;
|
|
|
|
Firebird::ExternalEngine* engine;
|
2011-05-15 17:37:11 +02:00
|
|
|
Firebird::AutoPtr<RoutineMetadata> metadata;
|
2009-10-21 02:42:38 +02:00
|
|
|
Firebird::ExternalFunction* function;
|
2009-12-21 18:43:01 +01:00
|
|
|
const Jrd::Function* udf;
|
2009-10-21 02:42:38 +02:00
|
|
|
Database* database;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ResultSet;
|
|
|
|
|
|
|
|
class Procedure
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Procedure(thread_db* tdbb, ExtEngineManager* aExtManager,
|
|
|
|
Firebird::ExternalEngine* aEngine,
|
2011-05-15 17:37:11 +02:00
|
|
|
RoutineMetadata* aMetadata,
|
2009-10-21 02:42:38 +02:00
|
|
|
Firebird::ExternalProcedure* aProcedure,
|
|
|
|
const jrd_prc* aPrc);
|
|
|
|
~Procedure();
|
|
|
|
|
2009-12-27 23:05:22 +01:00
|
|
|
ResultSet* open(thread_db* tdbb, ValuesImpl* inputParams, ValuesImpl* outputParams) const;
|
2009-10-21 02:42:38 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
ExtEngineManager* extManager;
|
|
|
|
Firebird::ExternalEngine* engine;
|
2011-05-15 17:37:11 +02:00
|
|
|
Firebird::AutoPtr<RoutineMetadata> metadata;
|
2009-10-21 02:42:38 +02:00
|
|
|
Firebird::ExternalProcedure* procedure;
|
|
|
|
const jrd_prc* prc;
|
|
|
|
Database* database;
|
|
|
|
|
|
|
|
friend class ResultSet;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ResultSet
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ResultSet(thread_db* tdbb, ValuesImpl* inputParams, ValuesImpl* outputParams,
|
2009-12-27 23:05:22 +01:00
|
|
|
const Procedure* aProcedure);
|
2009-10-21 02:42:38 +02:00
|
|
|
~ResultSet();
|
|
|
|
|
|
|
|
bool fetch(thread_db* tdbb);
|
|
|
|
|
|
|
|
private:
|
2009-12-27 23:05:22 +01:00
|
|
|
const Procedure* procedure;
|
2011-05-09 12:15:19 +02:00
|
|
|
Attachment* attachment;
|
2009-10-21 02:42:38 +02:00
|
|
|
bool firstFetch;
|
|
|
|
EngineAttachmentInfo* attInfo;
|
|
|
|
Firebird::ExternalResultSet* resultSet;
|
|
|
|
USHORT charSet;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Trigger
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Trigger(thread_db* tdbb, ExtEngineManager* aExtManager,
|
|
|
|
Firebird::ExternalEngine* aEngine,
|
2011-05-15 17:37:11 +02:00
|
|
|
RoutineMetadata* aMetadata,
|
2009-10-21 02:42:38 +02:00
|
|
|
Firebird::ExternalTrigger* aTrigger,
|
|
|
|
const Jrd::Trigger* aTrg);
|
|
|
|
~Trigger();
|
|
|
|
|
|
|
|
void execute(thread_db* tdbb, Firebird::ExternalTrigger::Action action,
|
2010-10-12 13:36:51 +02:00
|
|
|
record_param* oldRpb, record_param* newRpb) const;
|
2009-10-21 02:42:38 +02:00
|
|
|
|
|
|
|
private:
|
2009-10-31 02:46:06 +01:00
|
|
|
int setValues(thread_db* tdbb, Firebird::MemoryPool& pool,
|
2009-10-22 01:48:07 +02:00
|
|
|
Firebird::AutoPtr<ValuesImpl>& values, Firebird::Array<dsc*>& descs,
|
2010-10-12 13:36:51 +02:00
|
|
|
record_param* rpb) const;
|
2009-10-21 02:42:38 +02:00
|
|
|
|
|
|
|
ExtEngineManager* extManager;
|
|
|
|
Firebird::ExternalEngine* engine;
|
2011-05-15 17:37:11 +02:00
|
|
|
Firebird::AutoPtr<RoutineMetadata> metadata;
|
2009-10-21 02:42:38 +02:00
|
|
|
Firebird::ExternalTrigger* trigger;
|
|
|
|
const Jrd::Trigger* trg;
|
|
|
|
Database* database;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2009-10-30 11:50:59 +01:00
|
|
|
explicit ExtEngineManager(MemoryPool& p)
|
2009-10-21 02:42:38 +02:00
|
|
|
: PermanentStorage(p),
|
|
|
|
engines(p),
|
|
|
|
enginesAttachments(p)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ExtEngineManager();
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void initialize();
|
|
|
|
|
|
|
|
public:
|
|
|
|
void closeAttachment(thread_db* tdbb, Attachment* attachment);
|
|
|
|
|
2009-12-21 18:43:01 +01:00
|
|
|
Function* makeFunction(thread_db* tdbb, const Jrd::Function* udf,
|
2009-10-21 02:42:38 +02:00
|
|
|
const Firebird::MetaName& engine, const Firebird::string& entryPoint,
|
|
|
|
const Firebird::string& body);
|
|
|
|
Procedure* makeProcedure(thread_db* tdbb, const jrd_prc* prc,
|
|
|
|
const Firebird::MetaName& engine, const Firebird::string& entryPoint,
|
|
|
|
const Firebird::string& body);
|
|
|
|
Trigger* makeTrigger(thread_db* tdbb, const Jrd::Trigger* trg,
|
|
|
|
const Firebird::MetaName& engine, const Firebird::string& entryPoint,
|
|
|
|
const Firebird::string& body, Firebird::ExternalTrigger::Type type);
|
|
|
|
|
|
|
|
private:
|
|
|
|
Firebird::ExternalEngine* getEngine(thread_db* tdbb,
|
|
|
|
const Firebird::MetaName& name);
|
|
|
|
EngineAttachmentInfo* getEngineAttachment(thread_db* tdbb,
|
|
|
|
const Firebird::MetaName& name);
|
|
|
|
EngineAttachmentInfo* getEngineAttachment(thread_db* tdbb,
|
|
|
|
Firebird::ExternalEngine* engine, bool closing = false);
|
2009-11-14 23:21:11 +01:00
|
|
|
void setupAdminCharSet(thread_db* tdbb, Firebird::ExternalEngine* engine,
|
|
|
|
EngineAttachmentInfo* attInfo);
|
2009-10-21 02:42:38 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef Firebird::GenericMap<Firebird::Pair<
|
|
|
|
Firebird::Left<Firebird::MetaName, Firebird::ExternalEngine*> > > EnginesMap;
|
|
|
|
typedef Firebird::GenericMap<Firebird::Pair<Firebird::NonPooled<
|
|
|
|
EngineAttachment, EngineAttachmentInfo*> >, EngineAttachment> EnginesAttachmentsMap;
|
|
|
|
|
|
|
|
Firebird::RWLock enginesLock;
|
|
|
|
EnginesMap engines;
|
|
|
|
EnginesAttachmentsMap enginesAttachments;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Jrd
|
|
|
|
|
|
|
|
#endif // JRD_EXT_ENGINE_MANAGER_H
|