ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

This content has been translated automatically.  Click here  to view the French version.
Help / WLanguage / Managing databases / HFSQL / Managing triggers
  • What is a trigger?
  • Definition
  • Available types of triggers
  • Handling the triggers programmatically
  • The WLanguage functions
  • Creating a trigger
  • Creating a procedure run by a trigger
  • Automated tests and triggers
  • Triggers and data file aliases
WINDEV
WindowsLinuxJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac Catalyst
Others
Stored procedures
What is a trigger?

Definition

A trigger is a WLanguage procedure called automatically by the HFSQL engine each time a record in a data file is modified (added, deleted, edited, etc.).s
A trigger can be used to associate a WLanguage processing operation with one or more HFSQL functions (HAdd, HModify, HDelete, etc.) 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.
Note: Triggers also work with Table control based on a data file functions: TableAdd, TableAddLine, TableModify, TableModifyLine, TableDelete, etc.c
Using a trigger allows you to:
  • Reduce the code size. For example, processing associated with one or more HFSQL functions (duplicate check, data consistency test, etc.) can be grouped together using a trigger.r
  • Make the code easier to read.
  • Simplify the future evolutions of the application.
The triggers can be applied to all types of data files:
  • HFSQL
  • Native Access (Oracle, AS/400, etc.)
  • ODBC
  • OLE DB
Remarks:
  • 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 execution of an HFSQL function (HAdd, HModify, HDelete, etc.)..
    • 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 programmatically

The WLanguage functions

Several WLanguage functions are used to handle the triggers:
HActivateTriggerRe-enables a trigger that was disabled by HDeactivateTrigger.
HDeactivateTriggerDisables a trigger.
HDeleteTriggerDeletes a trigger.
HDescribeTriggerAdds or modifies a trigger on an HFSQL data file.
HListTriggerReturns the list of triggers applied to one or more HFSQL data files.
HSetTriggerEnables or disables the management of triggers.
HTriggerRecordBeforeRetrieves the value of the current record before the trigger(s) are executed.

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.
Note A trigger is global to the project.t

Creating a procedure run by a trigger

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 automation tests trigger triggers using dialog boxes (functions YesNo, Confirm, etc.), it is necessary to disable these dialog boxes during test execution using function InAutomaticTestMode.

Triggers and data file aliases

Triggers can be defined on data file aliases.
Related Examples:
WD Trigger Training (WINDEV): WD Trigger
[ + ] This example explains how to use the triggers with WINDEV.
A trigger is a WLanguage procedure automatically called by the HFSQL engine whenever a HFSQL function is run.

The following topics are presented in this example:
1/ how to branch a trigger on a HFSQL function
2/ the code that must be used in the function called by trigger.

Summary of the example supplied with WINDEV:
This application is used to enter the expenses for the contributors of a company.
Two modes can be used:
- the mode with read/write rights
- the mode with read-only rights.
The triggers will be used to control the access to the files according to the current mode.
In this example, the triggers are also used to write the different events into a log file.
This log file can be directly consulted in the application.
Minimum version required
  • Version 12
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 10/09/2024

Send a report | Local help