SlideShare a Scribd company logo
SAGE Computing Services
Customised Oracle Training Workshops and Consulting
Creative Conditional Compilation
… and “raising the bar” with yourPL/SQL
Sco tt We sle y
Systems Consultant
The example everybody’s seen…
FUNCTION qty_booked(p_resource IN VARCHAR2
,p_date IN DATE)
RETURN NUMBER
$IF dbms_db_version.ver_le_10 $THEN
$ELSE RESULT_CACHE $END
IS
li_total PLS_INTEGER := 0;
BEGIN
SELECT SUM(b.qty)
INTO li_total
FROM bookings b, events e
WHERE p_date BETWEEN e.start_date AND e.end_date
AND b.resource = p_resource;
RETURN li_total
END qty_booked;
The example everybody’s seen…
FUNCTION qty_booked(p_resource IN VARCHAR2FUNCTION qty_booked(p_resource IN VARCHAR2
,p_date IN DATE),p_date IN DATE)
RETURN NUMBERRETURN NUMBER
$IF dbms_db_version.ver_le_10 $THEN
$ELSE RESULT_CACHE $END
ISIS
li_total PLS_INTEGER := 0;li_total PLS_INTEGER := 0;
BEGINBEGIN
SELECT SUM(b.qty)SELECT SUM(b.qty)
INTO li_totalINTO li_total
FROM bookings b, events eFROM bookings b, events e
WHERE p_date BETWEEN e.start_date AND e.end_dateWHERE p_date BETWEEN e.start_date AND e.end_date
AND b.resource = p_resource;AND b.resource = p_resource;
RETURN li_totalRETURN li_total
END qty_booked;END qty_booked;
• PL/SQL User’s Guide & Reference
10g Release 2
– Fundamentals of the PL/SQL Language
• Conditional Compilation
Availability
• 11g
• 10g Release 2
– Enabled out of the box
• 10.1.0.4 – Once patched, enabled by default
– Disable using “_parameter”
• 9.2.0.6 – Once patched, disabled by default
– Enable using “_parameter”
Catch 22
INDICES OF
Catch 22
Conditional
Compilation
Patch
INDICES OF
Facilitates removal of unnecessary code at compile time
Performance
Readability
Accuracy Testing
It's cool!
Selection Directives
$IF boolean_static_expression $THEN text
[ $ELSIF boolean_static_expression $THEN text ]
[ $ELSE text ]
$END
Inquiry Directives
DBMS_OUTPUT.PUT_LINE($$PLSQL_LINE);
ALTER SESSION SET PLSQL_CCFLAGS='max_sentence:100';
IF sentence > $$max_sentence THEN
Error Directives
$IF $$PLSQL_OPTIMIZE_LEVEL != 2
$THEN
$ERROR 'intensive_program must be compiled with
maximum optimisation'
$END
$END
Semantics
First demo
cc1a
cc1b
cc1c
cc1d
Inquiry Directives
<< anon >>
BEGIN
DBMS_OUTPUT.PUT_LINE('Unit:'||$$PLSQL_UNIT);
DBMS_OUTPUT.PUT_LINE('Line:'||$$PLSQL_LINE);
END anon;
/
Unit:
Line:4
> CREATE OR REPLACE PROCEDURE sw_test IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Unit:'||$$PLSQL_UNIT);
DBMS_OUTPUT.PUT_LINE('Line:'||$$PLSQL_LINE);
END sw_test;
/
Procedure created.
> exec sw_test
Unit:SW_TEST
Line:4
ALTER SESSION SET PLSQL_CCFLAGS = 'max_sentence:100';
Session altered.
> BEGIN
IF p_sentence > $$max_sentence THEN
DBMS_OUTPUT.PUT_LINE('Parole Available');
ELSE
DBMS_OUTPUT.PUT_LINE('Life');
END IF;
END;
/
Life
ALTER SYSTEM SET PLSQL_CCFLAGS =
'VARCHAR2_SIZE:100, DEF_APP_ERR:-20001';
DECLARE
lc_variable_chr VARCHAR2($$VARCHAR2_SIZE);
e_def_app_err EXCEPTION;
PRAGMA EXCEPTION_INIT (e_def_app_err, $$DEF_APP_ERR);
BEGIN
--> rest of your code
END anon;
/
Reuse Settings
ALTER SESSION SET PLSQL_CCFLAGS = 'MY_PI:314';
CREATE OR REPLACE PROCEDURE universe_alpha IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Alpha pi = '||$$my_pi/100);
END;
CREATE OR REPLACE PROCEDURE universe_gamma IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Gamma pi = '||$$my_pi/100);
END;
CREATE OR REPLACE PROCEDURE universe_oz IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Oz pi = '||$$my_pi/100);
END;ALTER PROCEDURE universe_alpha COMPILE
PLSQL_CCFLAGS = 'MY_PI:289'
REUSE SETTINGS; ALTER PROCEDURE universe_gamma COMPILE
PLSQL_CCFLAGS = 'MY_PI:423'
REUSE SETTINGS;
> BEGIN
universe_alpha;
universe_gamma;
universe_oz;
END;
/
Alpha pi = 2.89
Gamma pi = 4.23
Oz pi = 3.14
Some actual examples?
Using new version code today
$IF dbms_db_version.ver_le_10 $THEN
-- version 10 and earlier code
$ELSIF dbms_db_version.ver_le_11 $THEN
-- version 11 code
$ELSE
-- version 12 and later code
$END
10.1 vs 10.2 dbms_output
CREATE OR REPLACE PROCEDURE sw_debug (p_text IN
VARCHAR2) IS
$IF $$sw_debug_on $THEN
l_text VARCHAR2(32767);
$END
BEGIN
$IF $$sw_debug_on $THEN
-- Let’s provide debugging info
$IF dbms_db_version.ver_le_10_1 $THEN
-- We have to truncate for <= 10.1
l_text := SUBSTR(p_text, 1 ,200);
$ELSE
l_text := p_text;
$END
DBMS_OUTPUT.PUT_LINE(p_text);
$ELSE
-- No debugging
NULL;
$END
END sw_debug;
CREATE OR REPLACE PROCEDURE sw_debug (p_text INCREATE OR REPLACE PROCEDURE sw_debug (p_text IN
VARCHAR2) ISVARCHAR2) IS
$IF $$sw_debug_on $THEN
l_text VARCHAR2(32767);
$END
BEGINBEGIN
$IF $$sw_debug_on $THEN
-- Let’s provide debugging info
$IF dbms_db_version.ver_le_10_1 $THEN
-- We have to truncate for <= 10.1
l_text := SUBSTR(p_text, 1 ,200);
$ELSE
l_text := p_text;
$END
DBMS_OUTPUT.PUT_LINE(p_text);
$ELSE$ELSE
-- No debugging-- No debugging
NULL;NULL;
$END$END
END sw_debug;END sw_debug;
CREATE OR REPLACE PROCEDURE sw_debug (p_text INCREATE OR REPLACE PROCEDURE sw_debug (p_text IN
VARCHAR2) ISVARCHAR2) IS
$IF $$sw_debug_on $THEN$IF $$sw_debug_on $THEN
l_text VARCHAR2(32767);l_text VARCHAR2(32767);
$END$END
BEGINBEGIN
$IF $$sw_debug_on $THEN$IF $$sw_debug_on $THEN
-- Let’s provide debugging info-- Let’s provide debugging info
$IF dbms_db_version.ver_le_10_1 $THEN
-- We have to truncate for <= 10.1
l_text := SUBSTR(p_text, 1 ,255);
$ELSE$ELSE
l_text := p_text;l_text := p_text;
$END$END
DBMS_OUTPUT.PUT_LINE(p_text);DBMS_OUTPUT.PUT_LINE(p_text);
$ELSE$ELSE
-- No debugging-- No debugging
NULL;NULL;
$END$END
END sw_debug;END sw_debug;
10g vs 11g result_cache
FUNCTION quantity_ordered
(p_item_id IN items.item_id%TYPE)
RETURN NUMBER
$IF dbms_version.ver_le_10 $THEN
-- nothing
$ELSE
RESULT_CACHE
$END
IS
BEGIN
...
9i vs 10g Bulk Insert
CREATE OR REPLACE PACKAGE BODY sw_bulk_insert IS
PROCEDURE sw_insert (sw_tab IN t_sw_tab) IS
BEGIN
$IF dbms_db_version.ver_le_9 $THEN
DECLARE
l_dense t_sw_tab;
ln_index PLS_INTEGER := sw_tab.FIRST;
BEGIN
<< dense_loop >>
WHILE (l_index IS NOT NULL) LOOP
l_dense(l_dense.COUNT + 1) := sw_tab(l_index);
l_index := sw_tab.NEXT(l_index);
END LOOP dense_loop;
FORALL i IN 1..l_dense.COUNT
INSERT INTO sw_table VALUES l_dense(i);
END;
$ELSE
FORALL i IN INDICES OF sw_tab
INSERT INTO sw_table
VALUES sw_tab(i);
$END
END sw_insert;
END sw_bulk_insert;
CREATE OR REPLACE PACKAGE BODY sw_bulk_insert IS
PROCEDURE sw_insert (sw_tab IN t_sw_tab) IS
BEGIN
$IF dbms_db_version.ver_le_9 $THEN
DECLARE
l_dense t_sw_tab;
ln_index PLS_INTEGER := sw_tab.FIRST;
BEGIN
<< dense_loop >>
WHILE (l_index IS NOT NULL) LOOP
l_dense(l_dense.COUNT + 1) := sw_tab(l_index);
l_index := sw_tab.NEXT(l_index);
END LOOP dense_loop;
FORALL i IN 1..l_dense.COUNT
INSERT INTO sw_table VALUES l_dense(i);
END;
$ELSE$ELSE
FORALL i IN INDICES OF sw_tabFORALL i IN INDICES OF sw_tab
INSERT INTO sw_tableINSERT INTO sw_table
VALUES sw_tab(i);VALUES sw_tab(i);
$END$END
END sw_insert;
END sw_bulk_insert;
CREATE OR REPLACE PACKAGE BODY sw_bulk_insert IS
PROCEDURE sw_insert (sw_tab IN t_sw_tab) IS
BEGIN
$IF dbms_db_version.ver_le_9 $THEN$IF dbms_db_version.ver_le_9 $THEN
DECLAREDECLARE
l_dense t_sw_tab;l_dense t_sw_tab;
ln_index PLS_INTEGER := sw_tab.FIRST;ln_index PLS_INTEGER := sw_tab.FIRST;
BEGINBEGIN
<< dense_loop >><< dense_loop >>
WHILE (l_index IS NOT NULL) LOOPWHILE (l_index IS NOT NULL) LOOP
l_dense(l_dense.COUNT + 1) := sw_tab(l_index);l_dense(l_dense.COUNT + 1) := sw_tab(l_index);
l_index := sw_tab.NEXT(l_index);l_index := sw_tab.NEXT(l_index);
END LOOP dense_loop;END LOOP dense_loop;
FORALL i IN 1..l_dense.COUNTFORALL i IN 1..l_dense.COUNT
INSERT INTO sw_table VALUES l_dense(i);INSERT INTO sw_table VALUES l_dense(i);
END;END;
$ELSE
FORALL i IN INDICES OF sw_tab
INSERT INTO sw_table
VALUES sw_tab(i);
$END
END sw_insert;
END sw_bulk_insert;
Paradigm Examples
Latent debugging code
CREATE OR REPLACE PACKAGE pkg_debug IS
debug_flag CONSTANT BOOLEAN := FALSE;
END pkg_debug;
/
CREATE OR REPLACE PROCEDURE sw_proc IS
BEGIN
$IF pkg_debug.debug_flag $THEN
dbms_output.put_line ('Debug=T');
$ELSE
dbms_output.put_line ('Debug=F');
$END
END sw_proc;
/
Assertions
“Assertions should be used to document
logically impossible situations —
Development tool
Testing Aid
In-line Documentation
if the ‘impossible’ occurs,
then something fundamental is clearly wrong.
This is distinct from error handling.”
Run-time Cost
Latent Assertions
“The removal of assertions from production code is
almost always done automatically.
It usually is done via conditional compilation.”
$IF $$asserting
OR CC_assertion.asserting
$THEN
IF p_param != c_pi*r*r THEN
raise_application_error(..
END IF;
$END
-- individual program unit
-- entire application
Testing subprograms only in package body
CREATE PACKAGE universe IS
PROCEDURE create_sun;
PROCEDURE create_planets;
-- CC test procedure
PROCEDURE test_orbit;
END universe;
CREATE PACKAGE BODY universe IS
-- Private
PROCEDURE orbit IS .. END;
-- Public
PROCEDURE create_sun IS .. END;
PROCEDURE create_planets IS .. END;
-- Testers
PROCEDURE test_orbit IS
BEGIN
$IF $$testing $THEN
orbit;
$ELSE
RAISE program_error;
$END
END test_orbit;
END universe;
CREATE PACKAGE universe IS
PROCEDURE create_sun;
PROCEDURE create_planets;
-- CC test sequence
PROCEDURE test_run;
END universe;
CREATE PACKAGE BODY universe IS
-- Private
PROCEDURE orbit IS .. END;
-- Public
PROCEDURE create_sun IS .. END;
PROCEDURE create_planets IS .. END;
-- Test sequence
PROCEDURE test_run IS
BEGIN
$IF $$testing $THEN
create_sun;
create_planets;
orbit;
$ELSE
RAISE program_error;
$END
END test_run;
END universe;
Mock objects
FUNCTION get_emp(p_emp_id IN emp.emp_id%TYPE)
RETURN t_emp IS
l_emp t_emp;
BEGIN
$IF $$mock_emp $THEN
l_emp.emp_name := 'Scott';
..
RETURN l_emp;
$ELSE
SELECT *
FROM emp
INTO l_emp
WHERE emp_id = p_emp_id;
RETURN l_emp;
$END
END get_emp;
Comparing competing implementations
during prototyping
PROCEDURE xyz IS
$IF $$alternative = 1 $THEN
-- varray declaration
$$ELSIF $$alternative = 2 $THEN
-- nested table declaration
$END
BEGIN
$IF $$alternative = 1 $THEN
-- simple varray solution
$$ELSIF $$alternative = 2 $THEN
-- elegant nested table solution
$END
END xyz;
PROCEDURE xyz ISPROCEDURE xyz IS
$IF $$alternative = 1 $THEN$IF $$alternative = 1 $THEN
-- varray declaration-- varray declaration
$$ELSIF $$alternative = 2 $THEN$$ELSIF $$alternative = 2 $THEN
-- nested table declaration-- nested table declaration
$END$END
BEGINBEGIN
$IF $$alternative = 1 $THEN$IF $$alternative = 1 $THEN
-- simple varray solution-- simple varray solution
$$ELSIF $$alternative = 2 $THEN$$ELSIF $$alternative = 2 $THEN
-- elegant nested table solution-- elegant nested table solution
$END$END
END xyz;END xyz;
$IF $$alternative = 1 $THEN
-- first verbose solution that came
to mind
$$ELSIF $$alternative = 2 $THEN
-- some crazy idea you came up with
at 3am you need to try out
$END
Component Based Installation
PACKAGE BODY core IS
PROCEDURE execute_component(p_choice IN VARCHAR2) IS
BEGIN
CASE p_choice -- Base is always installed.
WHEN 'base' THEN base.main();
$IF CC_licence.cheap_installed $THEN
WHEN 'cheap' THEN cheap.main();
$END
...
$IF CC_licence.pricey_installed $THEN
WHEN 'pricey' THEN pricey.main();
$END
END CASE;
EXCEPTION WHEN case_not_found THEN
dbms_output.put_line('Component '||p_choice||' is
not installed.');
END execute_component;
END core;
Get It Right with the Error Directive
$IF $$PLSQL_OPTIMIZE_LEVEL != 2
$THEN
$ERROR 'intensive_program must be compiled with
maximum optimisation'
$END
$END
BEGIN
...
/*
*
* Note to self: Must remember to finish this bit
*
*/
...
END;
BEGIN
...
-- Jacko: this doesn’t work, fix before moving to prod!
...
END;
1 CREATE PROCEDURE process_court_outcome IS
2 BEGIN
3 IF lr_victim.age > 18 THEN
4 send_to_prison(lr_victim);
5 ELSE
6 $ERROR
7 'Waiting for business to advise '||
8 $$PLSQL_UNIT||' line: '||$$PLSQL_LINE
9 $END
10 END IF;
11 END process_court_outcome;
12 /
Warning: Procedure created with compilation errors.
SQL> sho err
LINE/COL ERROR
-------- ----------------------------------------
6/6 PLS-00179: $ERROR: Waiting for business to
advise PROCESS_COURT_OUTCOME line: 8
Post-processed Source
Demo: cc2
Smaller, Faster Run-Time Code Is in Your Future
Good Practices
Inquiry directives have null values for normal behaviour
$IF $$cc_flag = 'x' $THEN
cc_code;
$END
Choose restriction type carefully
$IF $$debug_on
OR module_y.cc_debug
$THEN
sw_debug('Error');
$END
but there’s always this...
SQL> define debug=/*
begin
&debug
select 'conditionally'
from dual;
-- */
select 'always'
from dual;
end;
/
SAGE Computing Services
Customised Oracle Training Workshops and Consulting
Questions and Answers?
Presentations are available from our website:
http: //www. sag e co m puting . co m . au
e nq uirie s@ sag e co m puting . co m . au
sco tt. we sle y@ sag e co m puting . co m . au
http: //triang le -circle -sq uare . blo g spo t. co m

More Related Content

What's hot

Refactoring using Codeception
Refactoring using CodeceptionRefactoring using Codeception
Refactoring using Codeception
Jeroen van Dijk
 
Clear php reference
Clear php referenceClear php reference
Clear php reference
Damien Seguy
 
PHP Conference Asia 2016
PHP Conference Asia 2016PHP Conference Asia 2016
PHP Conference Asia 2016
Britta Alex
 
Bad test, good test
Bad test, good testBad test, good test
Bad test, good test
Seb Rose
 
Unit-Testing Bad-Practices by Example
Unit-Testing Bad-Practices by ExampleUnit-Testing Bad-Practices by Example
Unit-Testing Bad-Practices by ExampleBenjamin Eberlei
 
Unittesting Bad-Practices by Example
Unittesting Bad-Practices by ExampleUnittesting Bad-Practices by Example
Unittesting Bad-Practices by Example
Benjamin Eberlei
 
Test driven development_for_php
Test driven development_for_phpTest driven development_for_php
Test driven development_for_php
Lean Teams Consultancy
 
Symfony World - Symfony components and design patterns
Symfony World - Symfony components and design patternsSymfony World - Symfony components and design patterns
Symfony World - Symfony components and design patterns
Łukasz Chruściel
 
Storytelling By Numbers
Storytelling By NumbersStorytelling By Numbers
Storytelling By Numbers
Michael King
 
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers CracowForget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Kacper Gunia
 
What's new with PHP7
What's new with PHP7What's new with PHP7
What's new with PHP7
SWIFTotter Solutions
 
Dutch php a short tale about state machine
Dutch php   a short tale about state machineDutch php   a short tale about state machine
Dutch php a short tale about state machine
Łukasz Chruściel
 
SfCon: Test Driven Development
SfCon: Test Driven DevelopmentSfCon: Test Driven Development
SfCon: Test Driven Development
Augusto Pascutti
 
Php Enums
Php EnumsPhp Enums
Event Sourcing with php
Event Sourcing with phpEvent Sourcing with php
Event Sourcing with php
Sébastien Houzé
 
Dependency Injection in PHP
Dependency Injection in PHPDependency Injection in PHP
Dependency Injection in PHP
Kacper Gunia
 
Zero to SOLID
Zero to SOLIDZero to SOLID
Zero to SOLID
Vic Metcalfe
 
PhpSpec 2.0 ilustrated by examples
PhpSpec 2.0 ilustrated by examplesPhpSpec 2.0 ilustrated by examples
PhpSpec 2.0 ilustrated by examples
Marcello Duarte
 
Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnit
mfrost503
 
Building a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing StrategiesBuilding a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing Strategies
CiaranMcNulty
 

What's hot (20)

Refactoring using Codeception
Refactoring using CodeceptionRefactoring using Codeception
Refactoring using Codeception
 
Clear php reference
Clear php referenceClear php reference
Clear php reference
 
PHP Conference Asia 2016
PHP Conference Asia 2016PHP Conference Asia 2016
PHP Conference Asia 2016
 
Bad test, good test
Bad test, good testBad test, good test
Bad test, good test
 
Unit-Testing Bad-Practices by Example
Unit-Testing Bad-Practices by ExampleUnit-Testing Bad-Practices by Example
Unit-Testing Bad-Practices by Example
 
Unittesting Bad-Practices by Example
Unittesting Bad-Practices by ExampleUnittesting Bad-Practices by Example
Unittesting Bad-Practices by Example
 
Test driven development_for_php
Test driven development_for_phpTest driven development_for_php
Test driven development_for_php
 
Symfony World - Symfony components and design patterns
Symfony World - Symfony components and design patternsSymfony World - Symfony components and design patterns
Symfony World - Symfony components and design patterns
 
Storytelling By Numbers
Storytelling By NumbersStorytelling By Numbers
Storytelling By Numbers
 
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers CracowForget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers Cracow
 
What's new with PHP7
What's new with PHP7What's new with PHP7
What's new with PHP7
 
Dutch php a short tale about state machine
Dutch php   a short tale about state machineDutch php   a short tale about state machine
Dutch php a short tale about state machine
 
SfCon: Test Driven Development
SfCon: Test Driven DevelopmentSfCon: Test Driven Development
SfCon: Test Driven Development
 
Php Enums
Php EnumsPhp Enums
Php Enums
 
Event Sourcing with php
Event Sourcing with phpEvent Sourcing with php
Event Sourcing with php
 
Dependency Injection in PHP
Dependency Injection in PHPDependency Injection in PHP
Dependency Injection in PHP
 
Zero to SOLID
Zero to SOLIDZero to SOLID
Zero to SOLID
 
PhpSpec 2.0 ilustrated by examples
PhpSpec 2.0 ilustrated by examplesPhpSpec 2.0 ilustrated by examples
PhpSpec 2.0 ilustrated by examples
 
Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnit
 
Building a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing StrategiesBuilding a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing Strategies
 

Similar to Oracle PL/SQL - Creative Conditional Compilation

Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Kang-min Liu
 
Tidy Up Your Code
Tidy Up Your CodeTidy Up Your Code
Tidy Up Your Code
Abbas Ali
 
A New View of Database Views
A New View of Database ViewsA New View of Database Views
A New View of Database Views
Michael Rosenblum
 
ETL Patterns with Postgres
ETL Patterns with PostgresETL Patterns with Postgres
ETL Patterns with Postgres
Martin Loetzsch
 
PHPSpec BDD for PHP
PHPSpec BDD for PHPPHPSpec BDD for PHP
PHPSpec BDD for PHP
Marcello Duarte
 
Prepared Statement 올바르게 사용하기
Prepared Statement 올바르게 사용하기Prepared Statement 올바르게 사용하기
Prepared Statement 올바르게 사용하기
Kangjun Heo
 
Building and Distributing PostgreSQL Extensions Without Learning C
Building and Distributing PostgreSQL Extensions Without Learning CBuilding and Distributing PostgreSQL Extensions Without Learning C
Building and Distributing PostgreSQL Extensions Without Learning C
David Wheeler
 
PL/SQL
PL/SQLPL/SQL
PL/SQL
Vaibhav0
 
Xenogenetics for PL/SQL - infusing with Java best practices
Xenogenetics for PL/SQL - infusing with Java best practicesXenogenetics for PL/SQL - infusing with Java best practices
Xenogenetics for PL/SQL - infusing with Java best practices
Lucas Jellema
 
Creating "Secure" PHP Applications, Part 1, Explicit Code & QA
Creating "Secure" PHP Applications, Part 1, Explicit Code & QACreating "Secure" PHP Applications, Part 1, Explicit Code & QA
Creating "Secure" PHP Applications, Part 1, Explicit Code & QAarchwisp
 
PHP Unit Testing
PHP Unit TestingPHP Unit Testing
PHP Unit Testing
Tagged Social
 
Extbase and Beyond
Extbase and BeyondExtbase and Beyond
Extbase and Beyond
Jochen Rau
 
Curscatalyst
CurscatalystCurscatalyst
CurscatalystKar Juan
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworksdiego_k
 
Mocking Demystified
Mocking DemystifiedMocking Demystified
Mocking Demystified
Marcello Duarte
 
Sqlite perl
Sqlite perlSqlite perl
Sqlite perl
Ashoka Vanjare
 
Presentation1
Presentation1Presentation1
Presentation1
Rahadyan Gusti
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data Objects
Wez Furlong
 
Why is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosWhy is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenarios
Divante
 

Similar to Oracle PL/SQL - Creative Conditional Compilation (20)

Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Tidy Up Your Code
Tidy Up Your CodeTidy Up Your Code
Tidy Up Your Code
 
A New View of Database Views
A New View of Database ViewsA New View of Database Views
A New View of Database Views
 
ETL Patterns with Postgres
ETL Patterns with PostgresETL Patterns with Postgres
ETL Patterns with Postgres
 
PHPSpec BDD for PHP
PHPSpec BDD for PHPPHPSpec BDD for PHP
PHPSpec BDD for PHP
 
Prepared Statement 올바르게 사용하기
Prepared Statement 올바르게 사용하기Prepared Statement 올바르게 사용하기
Prepared Statement 올바르게 사용하기
 
Building and Distributing PostgreSQL Extensions Without Learning C
Building and Distributing PostgreSQL Extensions Without Learning CBuilding and Distributing PostgreSQL Extensions Without Learning C
Building and Distributing PostgreSQL Extensions Without Learning C
 
PL/SQL
PL/SQLPL/SQL
PL/SQL
 
Xenogenetics for PL/SQL - infusing with Java best practices
Xenogenetics for PL/SQL - infusing with Java best practicesXenogenetics for PL/SQL - infusing with Java best practices
Xenogenetics for PL/SQL - infusing with Java best practices
 
Xenogenetics
XenogeneticsXenogenetics
Xenogenetics
 
Creating "Secure" PHP Applications, Part 1, Explicit Code & QA
Creating "Secure" PHP Applications, Part 1, Explicit Code & QACreating "Secure" PHP Applications, Part 1, Explicit Code & QA
Creating "Secure" PHP Applications, Part 1, Explicit Code & QA
 
PHP Unit Testing
PHP Unit TestingPHP Unit Testing
PHP Unit Testing
 
Extbase and Beyond
Extbase and BeyondExtbase and Beyond
Extbase and Beyond
 
Curscatalyst
CurscatalystCurscatalyst
Curscatalyst
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
 
Mocking Demystified
Mocking DemystifiedMocking Demystified
Mocking Demystified
 
Sqlite perl
Sqlite perlSqlite perl
Sqlite perl
 
Presentation1
Presentation1Presentation1
Presentation1
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data Objects
 
Why is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosWhy is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenarios
 

More from Scott Wesley

Oracle Text in APEX
Oracle Text in APEXOracle Text in APEX
Oracle Text in APEX
Scott Wesley
 
Being Productive in IT
Being Productive in ITBeing Productive in IT
Being Productive in IT
Scott Wesley
 
Oracle APEX Performance
Oracle APEX PerformanceOracle APEX Performance
Oracle APEX Performance
Scott Wesley
 
Oracle Forms to APEX conversion tool
Oracle Forms to APEX conversion toolOracle Forms to APEX conversion tool
Oracle Forms to APEX conversion tool
Scott Wesley
 
Utilising Oracle documentation effectively
Utilising Oracle documentation effectivelyUtilising Oracle documentation effectively
Utilising Oracle documentation effectively
Scott Wesley
 
Oracle 11g new features for developers
Oracle 11g new features for developersOracle 11g new features for developers
Oracle 11g new features for developers
Scott Wesley
 
Oracle PL/SQL Bulk binds
Oracle PL/SQL Bulk bindsOracle PL/SQL Bulk binds
Oracle PL/SQL Bulk binds
Scott Wesley
 
Oracle SQL Model Clause
Oracle SQL Model ClauseOracle SQL Model Clause
Oracle SQL Model Clause
Scott Wesley
 

More from Scott Wesley (8)

Oracle Text in APEX
Oracle Text in APEXOracle Text in APEX
Oracle Text in APEX
 
Being Productive in IT
Being Productive in ITBeing Productive in IT
Being Productive in IT
 
Oracle APEX Performance
Oracle APEX PerformanceOracle APEX Performance
Oracle APEX Performance
 
Oracle Forms to APEX conversion tool
Oracle Forms to APEX conversion toolOracle Forms to APEX conversion tool
Oracle Forms to APEX conversion tool
 
Utilising Oracle documentation effectively
Utilising Oracle documentation effectivelyUtilising Oracle documentation effectively
Utilising Oracle documentation effectively
 
Oracle 11g new features for developers
Oracle 11g new features for developersOracle 11g new features for developers
Oracle 11g new features for developers
 
Oracle PL/SQL Bulk binds
Oracle PL/SQL Bulk bindsOracle PL/SQL Bulk binds
Oracle PL/SQL Bulk binds
 
Oracle SQL Model Clause
Oracle SQL Model ClauseOracle SQL Model Clause
Oracle SQL Model Clause
 

Recently uploaded

Quantitative Data AnalysisReliability Analysis (Cronbach Alpha) Common Method...
Quantitative Data AnalysisReliability Analysis (Cronbach Alpha) Common Method...Quantitative Data AnalysisReliability Analysis (Cronbach Alpha) Common Method...
Quantitative Data AnalysisReliability Analysis (Cronbach Alpha) Common Method...
2023240532
 
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
dwreak4tg
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
oz8q3jxlp
 
Adjusting OpenMP PageRank : SHORT REPORT / NOTES
Adjusting OpenMP PageRank : SHORT REPORT / NOTESAdjusting OpenMP PageRank : SHORT REPORT / NOTES
Adjusting OpenMP PageRank : SHORT REPORT / NOTES
Subhajit Sahu
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Subhajit Sahu
 
Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
jerlynmaetalle
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
mbawufebxi
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
Timothy Spann
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
axoqas
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
ewymefz
 
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptxData_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
AnirbanRoy608946
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
74nqk8xf
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
v3tuleee
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
Oppotus
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
rwarrenll
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
Roger Valdez
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
ewymefz
 
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
pchutichetpong
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
balafet
 

Recently uploaded (20)

Quantitative Data AnalysisReliability Analysis (Cronbach Alpha) Common Method...
Quantitative Data AnalysisReliability Analysis (Cronbach Alpha) Common Method...Quantitative Data AnalysisReliability Analysis (Cronbach Alpha) Common Method...
Quantitative Data AnalysisReliability Analysis (Cronbach Alpha) Common Method...
 
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
 
Adjusting OpenMP PageRank : SHORT REPORT / NOTES
Adjusting OpenMP PageRank : SHORT REPORT / NOTESAdjusting OpenMP PageRank : SHORT REPORT / NOTES
Adjusting OpenMP PageRank : SHORT REPORT / NOTES
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
 
Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
 
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptxData_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
 
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
 

Oracle PL/SQL - Creative Conditional Compilation

  • 1. SAGE Computing Services Customised Oracle Training Workshops and Consulting Creative Conditional Compilation … and “raising the bar” with yourPL/SQL Sco tt We sle y Systems Consultant
  • 2.
  • 3. The example everybody’s seen… FUNCTION qty_booked(p_resource IN VARCHAR2 ,p_date IN DATE) RETURN NUMBER $IF dbms_db_version.ver_le_10 $THEN $ELSE RESULT_CACHE $END IS li_total PLS_INTEGER := 0; BEGIN SELECT SUM(b.qty) INTO li_total FROM bookings b, events e WHERE p_date BETWEEN e.start_date AND e.end_date AND b.resource = p_resource; RETURN li_total END qty_booked;
  • 4. The example everybody’s seen… FUNCTION qty_booked(p_resource IN VARCHAR2FUNCTION qty_booked(p_resource IN VARCHAR2 ,p_date IN DATE),p_date IN DATE) RETURN NUMBERRETURN NUMBER $IF dbms_db_version.ver_le_10 $THEN $ELSE RESULT_CACHE $END ISIS li_total PLS_INTEGER := 0;li_total PLS_INTEGER := 0; BEGINBEGIN SELECT SUM(b.qty)SELECT SUM(b.qty) INTO li_totalINTO li_total FROM bookings b, events eFROM bookings b, events e WHERE p_date BETWEEN e.start_date AND e.end_dateWHERE p_date BETWEEN e.start_date AND e.end_date AND b.resource = p_resource;AND b.resource = p_resource; RETURN li_totalRETURN li_total END qty_booked;END qty_booked;
  • 5. • PL/SQL User’s Guide & Reference 10g Release 2 – Fundamentals of the PL/SQL Language • Conditional Compilation
  • 6. Availability • 11g • 10g Release 2 – Enabled out of the box • 10.1.0.4 – Once patched, enabled by default – Disable using “_parameter” • 9.2.0.6 – Once patched, disabled by default – Enable using “_parameter”
  • 9. Facilitates removal of unnecessary code at compile time Performance Readability Accuracy Testing It's cool!
  • 10. Selection Directives $IF boolean_static_expression $THEN text [ $ELSIF boolean_static_expression $THEN text ] [ $ELSE text ] $END Inquiry Directives DBMS_OUTPUT.PUT_LINE($$PLSQL_LINE); ALTER SESSION SET PLSQL_CCFLAGS='max_sentence:100'; IF sentence > $$max_sentence THEN Error Directives $IF $$PLSQL_OPTIMIZE_LEVEL != 2 $THEN $ERROR 'intensive_program must be compiled with maximum optimisation' $END $END Semantics
  • 14. > CREATE OR REPLACE PROCEDURE sw_test IS BEGIN DBMS_OUTPUT.PUT_LINE('Unit:'||$$PLSQL_UNIT); DBMS_OUTPUT.PUT_LINE('Line:'||$$PLSQL_LINE); END sw_test; / Procedure created. > exec sw_test Unit:SW_TEST Line:4
  • 15. ALTER SESSION SET PLSQL_CCFLAGS = 'max_sentence:100'; Session altered.
  • 16. > BEGIN IF p_sentence > $$max_sentence THEN DBMS_OUTPUT.PUT_LINE('Parole Available'); ELSE DBMS_OUTPUT.PUT_LINE('Life'); END IF; END; / Life
  • 17. ALTER SYSTEM SET PLSQL_CCFLAGS = 'VARCHAR2_SIZE:100, DEF_APP_ERR:-20001'; DECLARE lc_variable_chr VARCHAR2($$VARCHAR2_SIZE); e_def_app_err EXCEPTION; PRAGMA EXCEPTION_INIT (e_def_app_err, $$DEF_APP_ERR); BEGIN --> rest of your code END anon; /
  • 19. ALTER SESSION SET PLSQL_CCFLAGS = 'MY_PI:314'; CREATE OR REPLACE PROCEDURE universe_alpha IS BEGIN DBMS_OUTPUT.PUT_LINE('Alpha pi = '||$$my_pi/100); END; CREATE OR REPLACE PROCEDURE universe_gamma IS BEGIN DBMS_OUTPUT.PUT_LINE('Gamma pi = '||$$my_pi/100); END; CREATE OR REPLACE PROCEDURE universe_oz IS BEGIN DBMS_OUTPUT.PUT_LINE('Oz pi = '||$$my_pi/100); END;ALTER PROCEDURE universe_alpha COMPILE PLSQL_CCFLAGS = 'MY_PI:289' REUSE SETTINGS; ALTER PROCEDURE universe_gamma COMPILE PLSQL_CCFLAGS = 'MY_PI:423' REUSE SETTINGS; > BEGIN universe_alpha; universe_gamma; universe_oz; END; / Alpha pi = 2.89 Gamma pi = 4.23 Oz pi = 3.14
  • 21. Using new version code today $IF dbms_db_version.ver_le_10 $THEN -- version 10 and earlier code $ELSIF dbms_db_version.ver_le_11 $THEN -- version 11 code $ELSE -- version 12 and later code $END
  • 22. 10.1 vs 10.2 dbms_output
  • 23. CREATE OR REPLACE PROCEDURE sw_debug (p_text IN VARCHAR2) IS $IF $$sw_debug_on $THEN l_text VARCHAR2(32767); $END BEGIN $IF $$sw_debug_on $THEN -- Let’s provide debugging info $IF dbms_db_version.ver_le_10_1 $THEN -- We have to truncate for <= 10.1 l_text := SUBSTR(p_text, 1 ,200); $ELSE l_text := p_text; $END DBMS_OUTPUT.PUT_LINE(p_text); $ELSE -- No debugging NULL; $END END sw_debug;
  • 24. CREATE OR REPLACE PROCEDURE sw_debug (p_text INCREATE OR REPLACE PROCEDURE sw_debug (p_text IN VARCHAR2) ISVARCHAR2) IS $IF $$sw_debug_on $THEN l_text VARCHAR2(32767); $END BEGINBEGIN $IF $$sw_debug_on $THEN -- Let’s provide debugging info $IF dbms_db_version.ver_le_10_1 $THEN -- We have to truncate for <= 10.1 l_text := SUBSTR(p_text, 1 ,200); $ELSE l_text := p_text; $END DBMS_OUTPUT.PUT_LINE(p_text); $ELSE$ELSE -- No debugging-- No debugging NULL;NULL; $END$END END sw_debug;END sw_debug;
  • 25. CREATE OR REPLACE PROCEDURE sw_debug (p_text INCREATE OR REPLACE PROCEDURE sw_debug (p_text IN VARCHAR2) ISVARCHAR2) IS $IF $$sw_debug_on $THEN$IF $$sw_debug_on $THEN l_text VARCHAR2(32767);l_text VARCHAR2(32767); $END$END BEGINBEGIN $IF $$sw_debug_on $THEN$IF $$sw_debug_on $THEN -- Let’s provide debugging info-- Let’s provide debugging info $IF dbms_db_version.ver_le_10_1 $THEN -- We have to truncate for <= 10.1 l_text := SUBSTR(p_text, 1 ,255); $ELSE$ELSE l_text := p_text;l_text := p_text; $END$END DBMS_OUTPUT.PUT_LINE(p_text);DBMS_OUTPUT.PUT_LINE(p_text); $ELSE$ELSE -- No debugging-- No debugging NULL;NULL; $END$END END sw_debug;END sw_debug;
  • 26. 10g vs 11g result_cache FUNCTION quantity_ordered (p_item_id IN items.item_id%TYPE) RETURN NUMBER $IF dbms_version.ver_le_10 $THEN -- nothing $ELSE RESULT_CACHE $END IS BEGIN ...
  • 27. 9i vs 10g Bulk Insert
  • 28. CREATE OR REPLACE PACKAGE BODY sw_bulk_insert IS PROCEDURE sw_insert (sw_tab IN t_sw_tab) IS BEGIN $IF dbms_db_version.ver_le_9 $THEN DECLARE l_dense t_sw_tab; ln_index PLS_INTEGER := sw_tab.FIRST; BEGIN << dense_loop >> WHILE (l_index IS NOT NULL) LOOP l_dense(l_dense.COUNT + 1) := sw_tab(l_index); l_index := sw_tab.NEXT(l_index); END LOOP dense_loop; FORALL i IN 1..l_dense.COUNT INSERT INTO sw_table VALUES l_dense(i); END; $ELSE FORALL i IN INDICES OF sw_tab INSERT INTO sw_table VALUES sw_tab(i); $END END sw_insert; END sw_bulk_insert;
  • 29. CREATE OR REPLACE PACKAGE BODY sw_bulk_insert IS PROCEDURE sw_insert (sw_tab IN t_sw_tab) IS BEGIN $IF dbms_db_version.ver_le_9 $THEN DECLARE l_dense t_sw_tab; ln_index PLS_INTEGER := sw_tab.FIRST; BEGIN << dense_loop >> WHILE (l_index IS NOT NULL) LOOP l_dense(l_dense.COUNT + 1) := sw_tab(l_index); l_index := sw_tab.NEXT(l_index); END LOOP dense_loop; FORALL i IN 1..l_dense.COUNT INSERT INTO sw_table VALUES l_dense(i); END; $ELSE$ELSE FORALL i IN INDICES OF sw_tabFORALL i IN INDICES OF sw_tab INSERT INTO sw_tableINSERT INTO sw_table VALUES sw_tab(i);VALUES sw_tab(i); $END$END END sw_insert; END sw_bulk_insert;
  • 30. CREATE OR REPLACE PACKAGE BODY sw_bulk_insert IS PROCEDURE sw_insert (sw_tab IN t_sw_tab) IS BEGIN $IF dbms_db_version.ver_le_9 $THEN$IF dbms_db_version.ver_le_9 $THEN DECLAREDECLARE l_dense t_sw_tab;l_dense t_sw_tab; ln_index PLS_INTEGER := sw_tab.FIRST;ln_index PLS_INTEGER := sw_tab.FIRST; BEGINBEGIN << dense_loop >><< dense_loop >> WHILE (l_index IS NOT NULL) LOOPWHILE (l_index IS NOT NULL) LOOP l_dense(l_dense.COUNT + 1) := sw_tab(l_index);l_dense(l_dense.COUNT + 1) := sw_tab(l_index); l_index := sw_tab.NEXT(l_index);l_index := sw_tab.NEXT(l_index); END LOOP dense_loop;END LOOP dense_loop; FORALL i IN 1..l_dense.COUNTFORALL i IN 1..l_dense.COUNT INSERT INTO sw_table VALUES l_dense(i);INSERT INTO sw_table VALUES l_dense(i); END;END; $ELSE FORALL i IN INDICES OF sw_tab INSERT INTO sw_table VALUES sw_tab(i); $END END sw_insert; END sw_bulk_insert;
  • 33. CREATE OR REPLACE PACKAGE pkg_debug IS debug_flag CONSTANT BOOLEAN := FALSE; END pkg_debug; / CREATE OR REPLACE PROCEDURE sw_proc IS BEGIN $IF pkg_debug.debug_flag $THEN dbms_output.put_line ('Debug=T'); $ELSE dbms_output.put_line ('Debug=F'); $END END sw_proc; /
  • 35. “Assertions should be used to document logically impossible situations — Development tool Testing Aid In-line Documentation if the ‘impossible’ occurs, then something fundamental is clearly wrong. This is distinct from error handling.” Run-time Cost
  • 37. “The removal of assertions from production code is almost always done automatically. It usually is done via conditional compilation.”
  • 38. $IF $$asserting OR CC_assertion.asserting $THEN IF p_param != c_pi*r*r THEN raise_application_error(.. END IF; $END -- individual program unit -- entire application
  • 39. Testing subprograms only in package body
  • 40. CREATE PACKAGE universe IS PROCEDURE create_sun; PROCEDURE create_planets; -- CC test procedure PROCEDURE test_orbit; END universe; CREATE PACKAGE BODY universe IS -- Private PROCEDURE orbit IS .. END; -- Public PROCEDURE create_sun IS .. END; PROCEDURE create_planets IS .. END; -- Testers PROCEDURE test_orbit IS BEGIN $IF $$testing $THEN orbit; $ELSE RAISE program_error; $END END test_orbit; END universe;
  • 41. CREATE PACKAGE universe IS PROCEDURE create_sun; PROCEDURE create_planets; -- CC test sequence PROCEDURE test_run; END universe; CREATE PACKAGE BODY universe IS -- Private PROCEDURE orbit IS .. END; -- Public PROCEDURE create_sun IS .. END; PROCEDURE create_planets IS .. END; -- Test sequence PROCEDURE test_run IS BEGIN $IF $$testing $THEN create_sun; create_planets; orbit; $ELSE RAISE program_error; $END END test_run; END universe;
  • 43. FUNCTION get_emp(p_emp_id IN emp.emp_id%TYPE) RETURN t_emp IS l_emp t_emp; BEGIN $IF $$mock_emp $THEN l_emp.emp_name := 'Scott'; .. RETURN l_emp; $ELSE SELECT * FROM emp INTO l_emp WHERE emp_id = p_emp_id; RETURN l_emp; $END END get_emp;
  • 45. PROCEDURE xyz IS $IF $$alternative = 1 $THEN -- varray declaration $$ELSIF $$alternative = 2 $THEN -- nested table declaration $END BEGIN $IF $$alternative = 1 $THEN -- simple varray solution $$ELSIF $$alternative = 2 $THEN -- elegant nested table solution $END END xyz; PROCEDURE xyz ISPROCEDURE xyz IS $IF $$alternative = 1 $THEN$IF $$alternative = 1 $THEN -- varray declaration-- varray declaration $$ELSIF $$alternative = 2 $THEN$$ELSIF $$alternative = 2 $THEN -- nested table declaration-- nested table declaration $END$END BEGINBEGIN $IF $$alternative = 1 $THEN$IF $$alternative = 1 $THEN -- simple varray solution-- simple varray solution $$ELSIF $$alternative = 2 $THEN$$ELSIF $$alternative = 2 $THEN -- elegant nested table solution-- elegant nested table solution $END$END END xyz;END xyz; $IF $$alternative = 1 $THEN -- first verbose solution that came to mind $$ELSIF $$alternative = 2 $THEN -- some crazy idea you came up with at 3am you need to try out $END
  • 47. PACKAGE BODY core IS PROCEDURE execute_component(p_choice IN VARCHAR2) IS BEGIN CASE p_choice -- Base is always installed. WHEN 'base' THEN base.main(); $IF CC_licence.cheap_installed $THEN WHEN 'cheap' THEN cheap.main(); $END ... $IF CC_licence.pricey_installed $THEN WHEN 'pricey' THEN pricey.main(); $END END CASE; EXCEPTION WHEN case_not_found THEN dbms_output.put_line('Component '||p_choice||' is not installed.'); END execute_component; END core;
  • 48. Get It Right with the Error Directive
  • 49. $IF $$PLSQL_OPTIMIZE_LEVEL != 2 $THEN $ERROR 'intensive_program must be compiled with maximum optimisation' $END $END
  • 50. BEGIN ... /* * * Note to self: Must remember to finish this bit * */ ... END;
  • 51. BEGIN ... -- Jacko: this doesn’t work, fix before moving to prod! ... END;
  • 52. 1 CREATE PROCEDURE process_court_outcome IS 2 BEGIN 3 IF lr_victim.age > 18 THEN 4 send_to_prison(lr_victim); 5 ELSE 6 $ERROR 7 'Waiting for business to advise '|| 8 $$PLSQL_UNIT||' line: '||$$PLSQL_LINE 9 $END 10 END IF; 11 END process_court_outcome; 12 / Warning: Procedure created with compilation errors. SQL> sho err LINE/COL ERROR -------- ---------------------------------------- 6/6 PLS-00179: $ERROR: Waiting for business to advise PROCESS_COURT_OUTCOME line: 8
  • 54. Smaller, Faster Run-Time Code Is in Your Future
  • 56. Inquiry directives have null values for normal behaviour $IF $$cc_flag = 'x' $THEN cc_code; $END
  • 57. Choose restriction type carefully $IF $$debug_on OR module_y.cc_debug $THEN sw_debug('Error'); $END
  • 59. SQL> define debug=/* begin &debug select 'conditionally' from dual; -- */ select 'always' from dual; end; /
  • 60. SAGE Computing Services Customised Oracle Training Workshops and Consulting Questions and Answers? Presentations are available from our website: http: //www. sag e co m puting . co m . au e nq uirie s@ sag e co m puting . co m . au sco tt. we sle y@ sag e co m puting . co m . au http: //triang le -circle -sq uare . blo g spo t. co m

Editor's Notes

  1. Some powerpoint pezzaz straight off the bat, let’s get into an example you’ve probably seen before so you all know what I’m talking about
  2. And one that Penny even details later in the conference. I’m going to talk in depth about the concept behind conditional compilation and delve into a few more uses for you.
  3. This funky bit of code here is the concept where going to talk about. As usual, I’ll fly through the semantics because that’s all available in the doco
  4. You don’t need to look far to find it. What I really want to concentrate on with you is more diverse applications of this concept, using programming principles that have been around for a while.
  5. Feature of 10gr2, but made available to previous releases via patchsets. Historians of Oracle Database might be interested to know that the introduction of PL/SQL conditional compilation was motivated by a request from Oracle’s Applications Division for a solution to the problem of code spanning multiple releases.
  6. Catch22 - So you must patch your previous db to make conditional compilation available to prepare for new release
  7. Catch22 - So you must patch your previous db to make conditional compilation available to prepare for new release
  8. I&amp;apos;ll basically go through all of these points in the seminar
  9. Note the lack of semicolon, end ‘if’;
  10. This demo really needs to highlight the difference between enquiry directive usage. Diff between $$x and static constant. Usage behind both examples can differ depending on what you are trying to accomplish. Example shown later in competing soln.
  11. Designers should choose carefully with their names, to minimise collision Accidental discovery Reserved words
  12. Even more valuable, I can use inquiry directives in places in my code where a variable is not allowed. Here are two examples Also note it’s system or session specific
  13. A quick word for developers to ensure the same compiler directives are used when functions recompiled later
  14. Let’s have a quick look at was this feature was made for
  15. A quick word for developers to ensure the same compiler directives are used when functions recompiled later
  16. Don’t forget the whole poor software engineering premise to this funky technology though - never recommend putting untested code in production. When you upgrade to 11g obviously you’d test the upgrade in a non-production environment as part of search for side effects or compilation issues. Just means you haven’t forgotten this new feature. We won’t flick switch to 11g on prod anyway.
  17. OK, you don&amp;apos;t upgrade often. How about these paradigms
  18. We saw this one in the demo…
  19. Ok, who’se heard of assertions?
  20. Ok, who’se heard of LATENT assertions?
  21. “In tests, a mock object behaves exactly like a real object with one crucial difference: the programmer will hard-code return values for its methods...”
  22. REALLY basic example here, the white paper example I saw was on it&amp;apos;s way towards some sophisticated testing, but there is so much potential here. for embedding test plans, particular for subprograms in packages
  23. Highlight not so much the code, but the concept behind this. Making sure code doesn’t get lost, competing examples don’t get lost and confused as commented code. Life may only be days/weeks, not production code. But the concept does lead on to the idea of component based installation for licensed software.
  24. Inquiry directives allow quite flexible playing around with other triggers or events elsewhere in your app that you can control your testing with.
  25. Much of a segue from the previous solution, but too deep a topic to go to any more detail today than a quick conceptual example
  26. Pick up any textbook that talks about conditional compilation.
  27. If all else fails
  28. Thanks Connor