Page 379
In the previous two lessons, you built two forms: one form based on a single table and a master-detail form. You also learned how to modify the default layout that is generated by the Forms Designer when a new block is created.
This is your final lesson on Oracle Forms. In it, you investigate the use of PL/SQL to construct application triggers that serve a variety of purposes. You also go through the steps that can be used to develop a multi-form application. Along with that, you learn how to construct a menu that will invoke different forms.
Page 380
NEW TERM
A trigger is one or more PL/SQL statements that execute when an event occurs. The event may be directly related to an action performed by the user, such as pressing a button. Or the event may be indirectly related to an action performed by the user, such as the period before a query is executed.
A trigger can be defined at three levels:
Often, a single form will have one or more triggers at each level.
In the previous lesson, you created a master-detail form named Department_Class. When you created each block, you checked a checkbox on the Layout tab to indicate that integrity constraints should be enforced. If you use the Object Navigator to expand the Department block, you will see three triggers that were created when you defined the Department block (see Figure 14.1):
Figure 14.1. Page 381
Block-level triggers
created to enforce
integrity.
How do these triggers enforce referential integrity? Let's look at the KEY-DELREC trigger in detail. This trigger is fired when the user presses the Delete key. Listing 14.1 contains the text of this trigger. Notice that the trigger is composed of two anonymous PL/SQL blocks. The first block determines if there are any classes associated with the department to be deleted, and, if there are, issues an error message and raises an exception. The second block is very similar to the first block; it determines if any instructors belong to the department to be deleted and, if so, takes the necessary actions. You can view a trigger by double-clicking it in the Object Navigator.
Listing 14.1. KEY-DELREC trigger code for the Department block.
declare cursor primary_cur is select `x' from FLUGLE.CLASS where DEPARTMENT_ID = :DEPARTMENT.DEPARTMENT_ID; primary_dummy char(1); begin if ( ( :DEPARTMENT.DEPARTMENT_ID is not null ) ) then open primary_cur; fetch primary_cur into primary_dummy; if ( primary_cur%found ) then message(`Cannot delete master record when matching detail Ârecords exist.'); close primary_cur; raise form_trigger_failure; end if; close primary_cur; end if; end; declare cursor primary_cur is select `x' from FLUGLE.COURSE where DEPARTMENT_ID = :DEPARTMENT.DEPARTMENT_ID; primary_dummy char(1); begin if ( ( :DEPARTMENT.DEPARTMENT_ID is not null ) ) then open primary_cur; fetch primary_cur into primary_dummy; if ( primary_cur%found ) then message(`Cannot delete master record when matching detail Ârecords exist.'); close primary_cur; raise form_trigger_failure; end if; close primary_cur; end if; end; declare cursor primary_cur is select `x' from FLUGLE.INSTRUCTOR where DEPARTMENT_ID = :DEPARTMENT.DEPARTMENT_ID; primary_dummy char(1);
continues
Page 382
Listing 14.1. continued
begin if ( ( :DEPARTMENT.DEPARTMENT_ID is not null ) ) then open primary_cur; fetch primary_cur into primary_dummy; if ( primary_cur%found ) then message(`Cannot delete master record when matching detail Ârecords exist.'); close primary_cur; raise form_trigger_failure; end if; close primary_cur; end if; end; delete_record;
Let's work through an example of how a trigger can be used in the Department_Class
form. If you look at the Class block, the Course_ID is displayed, but unless the user has mem-
orized the Course IDs, he or she has no way of knowing anything else about the course.
The user probably would benefit if the title of the course were displayed.
Page 383
Figure 14.2.
Adding a text item to
the Class block.
Figure 14.3.
Setting the properties
for the Course_Title
text item.
Page 384
Now that you have a place to display the course title, you still need a trigger that will cause it to be displayed.
Figure 14.4.
Creating a POST-
QUERY trigger for
the Department block.
NOTE |
Notice that a colon precedes all references to blocks and items. |