Conditional Compilation for Oracle Database 10g (
Page 1 of 4 )
In this fifth part of a nine-part series on PL/SQL, you'll learn how conditional compilation lets you compile selected parts of a program when you're working with Oracle Database 10g. It is excerpted from chapter 20 of the book Oracle PL/SQL Programming, Fourth Edition, written by Steven Feuerstein and Bill Pribyl (O'Reilly; ISBN: 0596009771). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
Conditional Compilation
Introduced in Oracle Database 10g Release 2, conditional compilation allows the compiler to compile selected parts of a program based on conditions you provide with the $IF directive.
Conditional compilation will come in very handy when you need to:
-
Write a program that will run under different versions of Oracle, taking advantage of features specific to those versions. More specifically, you want to take advantage of new features of Oracle where available, but you also need that program to compile and run in older versions. Without conditional compilation, you would have to maintain multiple files or use complex SQL*Plus substitution variable logic.
-
Run certain code during testing and debugging, but then omit that code from the production code. Prior to conditional compilation, you would need to either comment out lines of code or add some overhead to the processing of your application—even in production.
-
Install/compile different elements of your application based on user requirements, such as the components for which a user is licensed. Conditional compilation greatly simplifies the maintenance of a code base with this complexity.
You implement conditional compilation by placing compiler directives (commands) in your source code. When your program is compiled, the PL/SQL preprocessor evaluates the directives and selects those portions of your code that should be compiled. This pared-down source code is then passed to the compiler for compilation.
There are three types of directives:
Selection directives
Use the $IF directive to evaluate expressions and
determine which code should be included or avoided.
Inquiry directives
Use the $$identifier syntax to refer to conditional
compilation flags. These inquiry directives can be
referenced within an $IF directive or used
independently in your code.
Error directives
Use the $ERROR directive to report compilation errors
based on conditions evaluated when the
preprocessor prepares your code for compilation.
First we’ll look at some simple examples, then delve more deeply into the capabilities of each directive. We’ll also learn how to use two packages related to conditional compilation, DBMS_DB_VERSION and DBMS_PREPROCESSOR.