Enterprise Manager: File System > Triggers


Triggers are a powerful feature of the BBj file system and database that allows developers to execute BBj code when certain operations occur on a file. Triggers can be useful for:

  • Validating data before writing it to a file.

  • Validating permissions before allowing a user to read or write to a file.

  • Performing data manipulation before writing it to a file or database table.

  • And much more.

Developers can use the Enterprise Manager to create and modify triggers using a graphical interface or using the CREATE TRIGGER statement to manage file triggers programmatically via SQL.

Configuring and Managing Triggers

Triggers in BBj exist at the file level. This means that triggers fire regardless of whether the operation on the file is a READ RECORD coming from a BBj program, or from an SQL operation by a third party ODBC/JDBC application.

To configure triggers in the Enterprise Manager, first select the File System->Triggers item from the navigator. The trigger editor panel appears in the right pane:

The top portion of the panel displays a list of the configured databases and a list of mounted directories. Mounted Directories refer to the directories for which the administrator has chosen to show triggers. To be able to access triggers on files in a particular directory (if they are not part of a configured BBj database), BBj needs to know that it should look in required directories for files with triggers.

Adding a Trigger to a Database Table

  1. Select the database from the list of databases under the Databases item.

  2. Double click the table to add a trigger or add triggers to multiple tables by selecting the desired tables and click the right-arrow button.

Mounting a Trigger Directory

This is only necessary when an administrator needs to manage triggers on files that are not part of a configured BBj database.

  • Select the Mounted Directories item.

  • Click the green [+] button in the toolbar.

  • Choose the directory to add.

After mounting the appropriate directory, add a trigger as follows:

  • Locate the file in the list of Files Without Triggers.

  • Double click the file to add a trigger or add triggers to multiple files by selecting the desired files and click the right-arrow button.

Configuring a Trigger

To configure a trigger, double-click the trigger from the list which opens the Trigger Editor.

The trigger editor provides a quick way to manage the various types of triggers on a particular file. The following describes each of the settings.

General Trigger Settings

Enabled Enable or disable the use of triggers on the file. This must be enabled for any triggers on the file to be available. NOTE: When triggers are enabled on a file, if there is any kind of problem with the configuration or if the trigger code does not operate properly, the file may not be accessible until the problem is resolved or triggers are disabled.
Config File Location of a specific config.bbx file to use for this particular set of triggers.  Leave blank to use the default config.bbx.
Working Directory Working directory for the running interpreter used by the trigger to execute the trigger code.
Run As User User account to use for running the interpreter used for executing the trigger code.  Leave blank to use the default user.
Password Password for the" run as user."
Classpath An optional BBj Session Specific Classpath to use for the triggers.

Configuring Each Type of Trigger

Trigger Types

Most operations possible on data files may also have triggers attached to them. The following describes the different operations that can occur on a file and thus have triggers attached to them.

Read

Read triggers occur when a read operation is performed on a file. This includes SQL fetch operations, BBj READ and READ RECORD calls, SELECT verb calls, and EXTRACT calls.

Key

Key triggers occur when one of the BBj language functions KEY, KEYF, KEYL, KEYN, or KEYP are called.

Write

Write triggers occur when a write operation of any kind occurs. This includes SQL UPDATE and INSERT operations, and BBj WRITE and WRITE RECORD calls.

Remove

Remove triggers occur when a record is removed from a file. These include SQL DELETE operations as well as BBj REMOVE calls.

Erase

Erase triggers occur when a file is erased. These include SQL DROP table operations and BBj ERASE calls.

Open

Open triggers occur when a file is opened. These occur any time the file is opened regardless of whether it was from an SQL statement or a BBj OPEN call.

Close

Close triggers occur when a file is closed. These will occur when SQL operations are finished with a file, and when the BBj CLOSE call is executed.

Trigger Operation Properties

Enabled

Enable or disable the specific type of trigger for the file. If a specific type is enabled and misconfigured in some way, the file will not be accessible.

<Type>Program

The BBj code to execute when the trigger fires. For information on how to program triggers, see, Programming Triggers.

Click to View

Read-Only Code

Displays the current source code for the trigger operation located in the file specified in Program. NOTE: The source code viewer in the Enterprise Manager is read-only. BASIS advises using the BASIS IDE to edit trigger source code files to gain the benefit of syntax highlighting and other IDE features.

Timing Types

Each trigger fires at a particular point in the process of performing a particular file operation. The following describes the three times a trigger may fire:

Before

A before trigger is executed BEFORE the actual file operation occurs. This is useful, for example, when the data needs to validated before allowing a write.

After

An after trigger is executed AFTER the actual file operation occurs. After triggers have access to the data that was accessed during the actual file operation and can manipulate this data before it is returned to the client. One useful example would be an after read trigger that will decrypt data stored in the table before returning the data.

Instead Of

Instead of triggers execute INSTEAD OF the actual file operation. These are useful if operations on that file should perform another operation such as get the data from a third party DBMS.

See Also

Programming Triggers