Use of packaged constants within a selection directive allows you to easily synchronize multiple program units around a specific conditional compilation setting. This is possible because Oracle’s automatic dependency management is applied to selection directives. In other words, if program unit PROG contains a selection directive that references package PKG, then PROG is marked as dependent on PKG. When the specification of PKG is recompiled, all program units using the packaged constant are marked invalid and must be recompiled. Suppose I want to use conditional compilation to automatically include or exclude debugging and tracing logic in my code base. I define a package specification to hold the required constants: /* File on web: cc_debug.pks */ debug_active CONSTANT BOOLEAN := TRUE; I then use these constants in procedure calc_totals: CREATE OR REPLACE PROCEDURE calc_totals During development, the debug_active constant is initialized to TRUE. When it is time to move the code to production, I change the flag to FALSE and recompile the package. The calc_totals program and all other programs with similar selection directives are marked invalid and must then be recompiled. Program-Specific Settings with Inquiry Directives Packaged constants are useful for coordinating settings across multiple program units. Inquiry directives, drawn from the compilation settings of individual programs, are a better fit when you need different settings applied to different programs. Once you have compiled a program with a particular set of values, it will retain those values until the next compilation (either from a file or a simple recompilation using the ALTER...COMPILE statement). Furthermore, a program is guaranteed to be recompiled with the same postprocessed source as was selected at the time of the previous compilation if all of the following conditions are TRUE:
This capability is demonstrated by the cc_reuse_settings.sql script, whose output is shown below. I first set the value of app_debug to TRUE and then compile a program with that setting, A query against USER_PLSQL_OBJECT_SETTINGS shows that this value is now associated with the program unit: /* File on web: cc_reuse_settings.sql */ SQL> ALTER SESSION SET plsql_ccflags = 'app_debug:TRUE'; SQL> CREATE OR REPLACE PROCEDURE test_ccflags SQL> SELECT name, plsql_ccflags NAME PLSQL_CCFLAGS I now alter the session, setting $$app_debug to evaluate to FALSE. I compile a new program with this setting: SQL> ALTER SESSION SET plsql_ccflags = 'app_debug:FALSE'; SQL> CREATE OR REPLACE PROCEDURE test_ccflags_new Then I recompile my existing program with REUSE SETTINGS: SQL> ALTER PROCEDURE test_ccflags COMPILE REUSE SETTINGS; A query against the data dictionary view now reveals that my settings are different for each program: SQL> SELECT name, plsql_ccflags NAME PLSQL_CCFLAGS
blog comments powered by Disqus |
|
|
|
|
|
|
|