Programming Oracle Triggers and Stored Procedures (3rd Edition) (Prentice Hall PTR Oracle Series)

Boolean expressions use the operators:

  • AND

  • OR

  • NOT

A Boolean expression can evaluate to TRUE, FALSE, or possibly not evaluate to anything. An expression does not evaluate when it contains a NULL ”this topic is covered in the next section, "Expressions with NULL."

Lengthy Boolean expressions can be difficult and time consuming to interpret. You can lessen the impact of hard-to-read logic if you wrap long Boolean expressions into a PL/SQL function that returns a BOOLEAN.

For example, within the body of a PL/SQL procedure, it could take some time to digest the following logic.

If NOT ((status = 'PART TIME' OR status = 'FULL TIME') AND (balance = 0) AND NOT (college_major = 'Undeclared')) THEN

The same logic, when embedded in a function, simplifies to the code. The following procedure encapsulates the Boolean logic into a function by the name :

registered_student_with_no_credit().

This is a long function name, but when used in the body of the PL/SQL program, conveys the Boolean test being performed.

PROCEDURE process_student_registration (. . .) IS -- Local function that encapsulates this logic. FUNCTION registered_student_with_no_credit RETURN BOOLEAN IS BEGIN RETURN NOT ((status = 'PART TIME' OR status = 'FULL TIME') AND (balance = 0) AND NOT (college_major = 'Undeclared')); END registered_student_with_no_credit; BEGIN . . . body of procedure . . . IF registered_student_with_no_credit THEN Do something . . . ELSE Do other thing END IF; END process_student_registration;

A programmer can look at the body of this procedure and get an idea of what the code does. A general understanding is that the code looks at students without credit. Logic is executed based on that test. The complex Boolean logic is still in the procedure, localized to a function. The programmer can dig into that logic if necessary. The encapsulation of the Boolean code allowed the procedure to be reviewed in steps.

Категории