Until August 2015 it never occurred to me that one could use non-PL/SQL code within conditional compilation blocks. Back then we discussed various template engine options as the foundation for oddgen – the Oracle community’s dictionary-driven code generator.
oddgen supports nowadays the in-database template engines FTLDB and tePLSQL. Both tools may access templates stored in PL/SQL packages using a selection directive. Here’s the package body of a generator using FTLDB:
CREATE OR REPLACE PACKAGE BODY ftldb_hello_world IS
$IF FALSE $THEN
--%begin generate_ftl
<#assign object_type = template_args[0]/>
<#assign object_name = template_args[1]/>
BEGIN
sys.dbms_output.put_line('Hello ${object_type} ${object_name}!');
END;
${"/"}
--%end generate_ftl
$END
FUNCTION generate(in_object_type IN VARCHAR2,
in_object_name IN VARCHAR2) RETURN CLOB IS
l_result CLOB;
l_args varchar2_nt;
BEGIN
l_args := NEW varchar2_nt(in_object_type, in_object_name);
l_result := ftldb_api.process_to_clob(in_templ_name => $PLSQL_UNIT || '%generate_ftl',
in_templ_args => l_args);
RETURN l_result;
END generate;
END ftldb_hello_world;
/
The template is stored within lines 4 to 11. It’s easy to see that the target code is PL/SQL, but the template itself contains various parts which do not comply with the PL/SQL language. The $IF on line 3 ensures that the template is compiled only when the condition is met. Never, in this case. You may be surprised, but yes, this trick works.
However, if I check this code with PL/SQL Cop for SQL Developer 1.0.12 I get the following result:
Bad. This version of PL/SQL Cop cannot parse this code successfully, since it expects valid PL/SQL code within the conditional compilation blocks. While it has some advantages to include conditional PL/SQL code in code analysis, it is simply worthless if the code cannot be parsed at all.
Therefore I released today new versions of all PL/SQL parser-based products supporting non-PL/SQL code within conditional compilation blocks. And the result in PL/SQL Cop for SQL Developer 1.0.13 is:
Good. You see, this version parses such code without problems. There are still some limitations regarding the support of conditional compilation in DECLARE sections, but I’m glad that the parser is becoming more and more complete.
So it is time to update PL/SQL Analyzer, PL/SQL Cop and PL/SQL Cop for SQL Developer.
Thanks oddgen for driving this improvement.