Firebird has had the ability to extendfeatures of the PSQL language by writing external functions - UDF (UserDefined Functions). UDF can be written on almost any compilableprogramming language.
Firebird 3.0 introduced a plugin architecture to extendFirebird features. One such plugin is External Engine(external engines). UDR mechanism (User Defined Routines - defineduser subroutine) adds a layer on top of the engine interface offirebird external. UDRs have the following advantages over UDFs:
you can write not only functions that return a scalar result, but alsostored procedures (both executable and selective), as well astriggers;
improved control of input and output parameters. In a number of cases(passing by descriptor) the types and other properties of the input parameters were not controlled at all, however, you could get these properties inside the UDF. UDRs provide a more uniform way of declaring input and output parameters, as is done with regular PSQLfunctions and procedures;
the context of the current connection or transaction is available in the UDR, whichallows you to perform some manipulations on the current database in thiscontext;
external procedures and functions (UDR) can be grouped in PSQL packages;
UDRs can be written in any programming language (optionalcompiled into object codes). This requires an appropriateexternal engine plugin. For example, there are plugins forwriting external modules in Java or any of the .NET languages.
In this guide, we will describe how to declare UDRs, their internalmechanisms, capabilities, and give examples of writing UDRs in Pascal.In addition, some aspects of using the new object-oriented API willbe touched upon.
Further in the various chapters of this manual, when using the termsexternal procedure, function or trigger, we will mean exactly UDR,not UDF.
Note
|
All our examples work on Delphi 2009 and older, as well as on FreePascal. All examples can be compiled in both Delphi and FreePascal unless otherwise noted. |
To write external procedures, functions or triggers on compiledprogramming languages, we need knowledge about the new objectoriented Firebird API. This manual does not include a completeFirebird API description. You can find it in thedocumentation catalog distributed with Firebird(doc/Using_OO_API.html
). For Russian-speaking users there isa translation of this document available athttps://github.com/sim1984/fbooapi.
Included files for various programming languages that contain APIs arenot distributed as part of the Firebird distribution for Windows,but you can extract them from compressed tarbar files distributedfor Linux (path inside the archive/opt/firebird/include/firebird/Firebird.pas
).
CLOOP - Cross Language Object Oriented Programming. This tool is notincluded with Firebird. It can be found in the source codehttps://github.com/FirebirdSQL/firebird/tree/B3_0_Release/extern/cloop.After the tool is built, you can generate an API for your programming language(IdlFbInterfaces.h
or Firebird.pas
) based on the interface descriptionfile include/firebird/FirebirdInterface.idl
.
For Object Pascal this is done with the following command:
cloop FirebirdInterface.idl pascal Firebird.pas Firebird --uses SysUtils \
--interfaceFile Pascal.interface.pas \
--implementationFile Pascal.implementation.pas \
--exceptionClass FbException --prefix I \
--functionsFile fb_get_master_interface.pas
The files Pascal.interface.pas
, Pascal.implementation.pas
andfb_get_master_interface.pas
can be found athttps://github.com/FirebirdSQL/firebird/tree/B3_0_Release/src/misc/pascal.
Note
|
Comment
In this case, for Firebird API interfaces will be addedprefix I, as it is accepted in Object Pascal. |