- What is a trigger?
- Available types of triggers
- Handling the triggers through programming
- The WLanguage functions
- Creating a trigger
- Creating a procedure run by a trigger
- Automated tests and triggers
- Triggers and data file aliases
A trigger is a WLanguage procedure automatically called by the HFSQL engine whenever a modification is performed (addition, deletion, modification, ...) on a record of a data file.
A trigger is used to associate a WLanguage process with one or more HFSQL functions (HAdd
, ...) without having to worry about the location of these functions in the source code. The triggers associated with the HFSQL functions used are run even if it is a window or a code created by a wizard.
Using a trigger allows you to:
- Reduce the code size. For example, using a trigger allows you to group the processes associated with one or more HFSQL functions (check for duplicates, consistency of data, ...).
- Make the code easier to read.
- Simplify the future evolutions of the application.
The triggers can be applied to all types of data files:
- Native accesses (Oracle, AS/400, ...)
- OLE DB
- With an HFSQL Client/Server database, you also have the ability to define server triggers. For more details, see Server triggers.
- When data files are modified via a query, server triggers, simple triggers or client triggers are fired.
Available types of triggers
Two types of triggers are available:
- The "BEFORE" triggers:
A "BEFORE" trigger is called:
- before running an HFSQL function (HAdd, HModify, HDelete, ...).
- before running a function for managing the Table controls based on a data file.
A "BEFORE" trigger can be used for example to check the consistency of data for the items of a record. With this type of trigger, an HFSQL variable can be initialized in order to cancel the execution of the associated HFSQL function.
- The "AFTER" triggers:
An "AFTER" trigger is called:
- after running an HFSQL function (except if the program was interrupted during the execution of this function).
- after running a function for managing the Table controls based on a data file.
An "AFTER" trigger can be used to manage the process of errors for example.
Handling the triggers through programming
The WLanguage functions
Several WLanguage functions are used to handle the triggers:
Creating a trigger
To create a "BEFORE" trigger or an "AFTER" trigger, use HDescribeTrigger
in the initialization process of the project. The trigger can be created:
The associated WLanguage procedure is run whenever a trigger is run.
Remark: A trigger is global to the project.
Creating a procedure run by a trigger
A trigger procedure accepts no parameter. However, some HFSQL state variables are positioned before each call:
|H.FileName||Character string: Logical name of the file whose trigger is enabled.|
|H.Action||Character initialized to "A" for a Before trigger and to "P" for an After trigger.|
|H.TriggerFunction||Character string: Name of HFSQL function that activated the trigger.|
|H.ToDo||During the execution of a before trigger:|
- cancel the execution of the HFSQL function by assigning "A" to the HFSQL state variable: H.ToDo = "A".
In this case, the action is not performed and the function (HAdd, HModify, etc.) returns True (no error).
- cancel the execution of the current HFSQL function by assigning "E" to the HFSQL state variable: H.ToDo = "E".
In this case, the action is not performed and the function (HAdd, HModify, etc.) returns False. The error message is as follows: "The action on XXX file was interrupted by the trigger".
: When a "BEFORE" trigger and an "AFTER" trigger are associated with an HFSQL function, if "BEFORE" cancels the execution of the HFSQL function (by setting H.ToDo to "A"), "AFTER" is not run.
In the procedure code, the MyFile
keyword gets and handles the name of the file on which the trigger was fired. For an "AFTER" trigger, if the HFSQL command was not run properly, the procedure associated with the trigger is not run.
Automated tests and triggers
If the automated tests activate triggers that use dialog boxes (YesNo
, ...), these dialog boxes must be disabled when the test is run by InAutomaticTestMode
Triggers and data file aliases
Triggers can be defined on data file aliases.
Training (WINDEV): WD Trigger
This page is also available for…
Click [Add] to post a comment