FirebirdSQL logo

Firebird API

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

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.

Constants

The resulting Firebird.pas file is missing isc_* constants. Theseconstants for C/C++ languages can be found underhttps://github.com/FirebirdSQL/firebird/blob/B3_0_Release/src/include/consts_pub.h.To obtain constants for the Pascal language, we use the AWK script forsyntax transformations. On Windows you will need to install Gawk forWindows or use the Windows Subsystem for Linux (available atWindows 10). This is done with the following command:

awk -f Pascal.Constants.awk consts_pub.h > const.pas

TThe contents of the resulting file must be copied into the empty constsection of the Firebird.pas file immediately after implementation.The file Pascal.Constants.awk can be found athttps://github.com/FirebirdSQL/firebird/tree/B3_0_Release/src/misc/pascal.

Life time management

Firebird interfaces are not based on the COM specification, sotheir lifetime is managed differently.

There are two interfaces in Firebird that deal with lifetime management:IDisposable and IReferenceCounted. The latter is especially active whencreating other interfaces: IPlugin counts links, like many other interfacesused by plug-ins. These include interfaces that describe the databaseconnection, transaction management, and SQL statements.

You don’t always need the extra overhead of a reference-counted interface. Forexample, IMaster, the main interface that calls functions available to therest of the API, has an unlimited lifetime by definition. For other APIs, thelifetime is strictly determined by the lifetime of the parent interface;interface ISatus is notmultithreaded. For interfaces with limited lifetimes, it is useful to have aneasy way to destroy them, i.e. the dispose() function.

Tip
Clue

If you don’t know how an object is destroyed, look up its hierarchy if it hasthe IReferenceCounted interface. For reference-counted interfaces, uponcompletion of work with the object, it is necessary to decrement the referencecount by calling the release() method.

Example 1. Important

Some methods of interfaces derived from IReferenceCounted release the interfaceafter successful completion. There is no need to call release() after calling such methods.

This is done for historical reasons, because similar functions from the ISC API freed the corresponding handle.

Here is a list of such methods:

  • IAttachment interface

    • detach(status: IStatus) - disconnect the connection to the database. On success, releases the interface.

    • dropDatabase(status: IStatus) - drop database. On success, releases the interface.

  • Interface ITransaction

    • commit(status: IStatus) - transaction confirmation. On success, releases the interface.

    • rollback(status: IStatus) - transaction rollback. On success, releases the interface.

  • IStation interface

    • free(status: IStatus) - removes a prepared statement. On success, releases the interface.

  • IResultSet interface

    • close(status: IStatus) closes the cursor. On success, releases the interface.

  • IBlob interface

    • cancel(status: IStatus) - cancels all changes made to the temporary BLOB (if any) and closes the BLOB. On success, releases the interface.

    • close(status: IStatus) - saves all changes made to the temporary BLOB (if any) and closes the BLOB. On success, releases the interface.

  • Interface IService

    • detach(status: IStatus) - disconnect the connection with the service manager. On success, releases the interface.

  • IEvents interface

    • cancel(status: IStatus) - cancels event subscription. On success, releases the interface.