DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Enhancing Angular Directives: Implementing Permission-Based Conditional Rendering With Else Case
  • CORS Anywhere on Pure NGINX Config
  • About Using Regexp in Nginx Map
  • Improving ui-select Control

Trending

  • Cloud Security and Privacy: Best Practices to Mitigate the Risks
  • How To Introduce a New API Quickly Using Quarkus and ChatGPT
  • Docker Base Images Demystified: A Practical Guide
  • Why Database Migrations Take Months and How to Speed Them Up

An Introduction to Conditional Compilation

Look at an introduction to conditional compilation and see why you might want to use it as well as how to put it to use.

By 
Steven Feuerstein user avatar
Steven Feuerstein
·
May. 17, 19 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
7.5K Views

Join the DZone community and get the full member experience.

Join For Free

This is the first post in a series on conditional compilation. 

Conditional compilation allows the compiler to compile selected parts of a program based on conditions you specify using $ syntax in PL/SQL. When you see statements like $IF, $ELSE, $END, and $ERROR in your PL/SQL code, you are looking at conditional compilations, sometimes also referred to as "ifdef" processing.

There's a really good chance you've never taken advantage of conditional compilation in PL/SQL, so I thought I'd write up a few posts about why you might want to use it and then how to put it to use.

Conditional compilation comes in very handy when you need to do any of the following:

  • Compile and run your PL/SQL code base on different versions of Oracle, taking advantage of features specific to those versions.
  • Run certain code during testing and debugging, but then omit that code from the production code. Or vice versa.
  • Install/compile different elements of your application based on user requirements, such as the components for which a user is licensed.
  • Expose usually private subprograms in the package specification to allow for direct testing on those subprograms.

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.

The preprocessor checks the value of the database parameter, PLSQL_CCFLAGS, to see if any application-specific conditional compilation flags have been set.

There Are 3 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.

I'll show you a simple example of each of these directives, then point you to additional resources. Future blog posts will go into detail on specific use cases, as well as two packages related to conditional compilation, DBMS_DB_VERSION and DBMS_PREPROCESSOR.

In the following block, I use $IF, $ELSE and DBMS_DB_VERSION to determine if I should include the UDF prima (new to Oracle Database 12c), which improves the performance of functions called from within SQL statements:

CREATE OR REPLACE FUNCTION my_function (n IN NUMBER)
   RETURN VARCHAR2
IS
$IF DBMS_DB_VERSION.VER_LE_11_2
$THEN
   /* UDF pragma not available till 12.1 */
$ELSE
   PRAGMA UDF;
$END 
BEGIN
   RETURN TO_CHAR (n);
END;
/

Next up: use my own application-specific inquiry directive, along with one provided by Oracle:

ALTER SESSION SET PLSQL_CCFLAGS = 'commit_off:true'
/

CREATE OR REPLACE PROCEDURE flexible_commits
IS
BEGIN
$IF $$commit_off
$THEN
   DBMS_OUTPUT.PUT_LINE ('Commit disabled in $$PLSQL_UNIT');
$ELSE
   COMMIT;
$END   
END;
/

Finally, I use $ERROR to force a compilation error if anyone tries to compile this code on a version earlier than 12.1.

CREATE OR REPLACE PROCEDURE uses_the_latest_and_greatest
AUTHID DEFINER
IS
BEGIN
   $IF DBMS_DB_VERSION.VER_LE_12_1
   $THEN
      $ERROR 'This program requires Oracle Databse 12.1 or higher.' $END
   $END
   NULL;
END;
/

Resources

  • Comprehensive white paper: a great starting place — and required reading — for anyone planning on using conditional compilation in production code

  • Conditional compilation scripts on LiveSQL

  • Tim Hall (Oracle-BASE) coverage of conditional compilation

  • Conditional compilation documentation

  • My Oracle Magazine article on this topic

Directive (programming)

Published at DZone with permission of Steven Feuerstein, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Enhancing Angular Directives: Implementing Permission-Based Conditional Rendering With Else Case
  • CORS Anywhere on Pure NGINX Config
  • About Using Regexp in Nginx Map
  • Improving ui-select Control

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: