PLSQL_s15_l03

28
1 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Database Programming with PL/SQL Using Conditional Compilation

description

orcl

Transcript of PLSQL_s15_l03

  • 5/25/2018 PLSQL_s15_l03

    1/28

    1 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    Database Programming with

    PL/SQLUsing Conditional Compilation

  • 5/25/2018 PLSQL_s15_l03

    2/28

    2 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    Objectives

    This lesson covers the following objectives:

    Describe the benefits of conditional compilation

    Create and conditionally compile a PL/SQL programcontaining selection, inquiry, and error directives

    Create and conditionally compile a PL/SQL programwhich calls the DBMS_DB_VERSIONserver-supplied

    package

    Using Conditional Compilation

  • 5/25/2018 PLSQL_s15_l03

    3/28

    3 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    Purpose

    Imagine you are creating a presentation as part of aschool project. You create it on your home PC usingMicrosoft Office 2007, which has some nice new

    features such as 3-D graphics display.

    When its finished, you will present your work to your

    class at school, but until the day of the presentation

    you wont know if the classroom PC will have Office2007 or an older version such as Office 2003, whichcant display 3-D graphics.

    Using Conditional Compilation

  • 5/25/2018 PLSQL_s15_l03

    4/28

    4 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    Purpose (cont.)

    You want to include 3-D graphics in your presentation,but you dont want it to fail while you are presenting to

    your class. Conditional compilation can prevent an

    embarrassing moment.

    Using Conditional Compilation

  • 5/25/2018 PLSQL_s15_l03

    5/28

    5 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    What is Conditional Compilation?

    Wouldnt it be great if you could somehow create your

    PowerPoint presentation so that if you show it usingOffice 2007, the 3-D graphics are displayed, but if you

    show it using Office 2003, the 2-D graphics aredisplayed instead?

    That way, it wont fail regardless of the Office version

    you use, and you automatically get the benefit of thenew features if theyre available.

    Using Conditional Compilation

  • 5/25/2018 PLSQL_s15_l03

    6/28

    6 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    What is Conditional Compilation? (cont.)

    You can do exactly this in PL/SQL by using ConditionalCompilation.

    Using Conditional Compilation

  • 5/25/2018 PLSQL_s15_l03

    7/287 Copyright 2013, Oracle and/or its affiliates. All rights

    reserved.

    What is Conditional Compilation? (cont.)

    Conditional Compilation allows you to include somesource code in your PL/SQL program that may becompiled or may be ignored (like a comment is

    ignored), depending on the values of an initializationparameter, the version of the Oracle software you areusing, the value of a global package constant, or anyother condition that you choose to set.

    You control conditional compilation by includingdirectivesin your source code. Directives are keywordsthat start with a single or double dollar sign ($ or $$).

    Using Conditional Compilation

  • 5/25/2018 PLSQL_s15_l03

    8/288 Copyright 2013, Oracle and/or its affiliates. All rights

    reserved.

    Conditional Compilation and Microsoft Office

    You cant really use PL/SQL with MS Office, so this is

    not a real example, but lets pretend:

    $IF,$THEN, $ELSE and$END are selection

    directives.

    Using Conditional Compilation

    CREATE OR REPLACE PROCEDURE lets_pretend IS

    BEGIN...

    $IF MS_OFFICE_VERSION >= '2007'$THENinclude_3_d_graphic;

    $ELSE

    include_2_d_graphic;

    $END

    ...END lets_pretend;

  • 5/25/2018 PLSQL_s15_l03

    9/28

    9 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    Conditional Compilation and Oracle Versions

    You cant test which Office version youre using, but

    youcan test which Oracle version youre using. This isa real subprogram:

    Using Conditional Compilation

    CREATE OR REPLACE FUNCTION lets_be_real

    RETURN NUMBER

    $IF DBMS_DB_VERSION.VERSION >= 11 $THEN

    DETERMINISTIC

    $END

    IS BEGIN

    NULL; -- real source code here !

    END lets_be_real;

  • 5/25/2018 PLSQL_s15_l03

    10/28

    10 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    Conditional Compilation and Oracle Versions

    (cont.)Deterministic functions are new in Oracle at Version11. This code includes the word DETERMINISTICif we

    compile the function on Version 11 or later, and is

    ignored if we compile on Version 10 or earlier.

    Using Conditional Compilation

    CREATE OR REPLACE FUNCTION lets_be_real

    RETURN NUMBER

    $IF DBMS_DB_VERSION.VERSION >= 11 $THEN

    DETERMINISTIC

    $END

    IS BEGINNULL; -- real source code here !

    END lets_be_real;

  • 5/25/2018 PLSQL_s15_l03

    11/28

    11 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    What is in the Data Dictionary Now?

    After compiling the function on the previous slide, whatis stored in the Data Dictionary? USER_SOURCE

    contains your complete source code, including the

    compiler directives:

    Using Conditional Compilation

    SELECT text FROM USER_SOURCE

    WHERE type = 'FUNCTION' and name = 'LETS_BE_REAL'

    ORDER BY line;

  • 5/25/2018 PLSQL_s15_l03

    12/28

    12 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    Seeing Which Code has been Compiled

    If you want to see which code has actually beenincluded in your compiled program, you use theDBMS_PREPROCESSOROracle-supplied package:

    This function was compiled using Oracle Version 11:

    Using Conditional Compilation

    BEGIN

    DBMS_PREPROCESSOR.PRINT_POST_PROCESSED_SOURCE

    ('FUNCTION','','LETS_BE_REAL');

    END;

  • 5/25/2018 PLSQL_s15_l03

    13/28

    13 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    Using Selection Directives

    There are five selection directives: $IF, $THEN,$ELSE, $ELSIFand $END(not $ENDIF). Their logic isthe same as IF, THEN, ELSEand so on, but they

    control which code is included at compile time, notwhat happens at execution time.

    Notice that $END does not end with a semicolon(;)unlike END;

    Using Conditional Compilation

    ...

    $IF condition $THENstatement(s);

    $ELSE statement(s);

    $ELSIF statement(s);

    $END

    ...

  • 5/25/2018 PLSQL_s15_l03

    14/28

    14 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    Using Selection Directives Example

    You have created a bodiless package that declares anumber of global constants:

    Now, you want to create a subprogram that declaresan explicit cursor whose WHEREclause will depend on

    the value of the Boolean package constant.

    Using Conditional Compilation

    CREATE OR REPLACE PACKAGE tax_code_pack IS

    new_tax_code CONSTANT BOOLEAN := TRUE; -- but could be FALSE...

    END tax_code_pack;

  • 5/25/2018 PLSQL_s15_l03

    15/28

    15 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    Using Selection Directives Example (cont.)

    Now lets look at the contents of the Data Dictionary.Remember, the package set NEW_TAX_CODEto TRUE.

    Using Conditional Compilation

    CREATE OR REPLACE PROCEDURE get_emps IS

    CURSOR get_emps_curs ISSELECT * FROM employees

    WHERE

    $IF tax_code_pack.new_tax_code $THEN

    salary > 20000;

    $ELSE

    salary > 50000;

    $ENDBEGIN

    FOR v_emps IN get_emps_curs LOOP

    ... /* real code here */

    END LOOP;

    END get_emps;

  • 5/25/2018 PLSQL_s15_l03

    16/28

    16 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    Using Selection Directives Example (cont.)

    Whats in the Data Dictionary?

    Using Conditional Compilation

    SELECT text FROM USER_SOURCE

    WHERE type = 'PROCEDURE' and name = 'GET_EMPS'

    ORDER BY line;

  • 5/25/2018 PLSQL_s15_l03

    17/28

    17 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    Using Selection Directives Example (cont.)

    And what code was compiled?

    Using Conditional Compilation

    BEGIN

    DBMS_PREPROCESSOR.PRINT_POST_PROCESSED_SOURCE

    ('PROCEDURE','','GET_EMPS');

    END;

  • 5/25/2018 PLSQL_s15_l03

    18/28

    18 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    The PLSQL_CCFLAGSInitialization

    ParameterYou may want to use the selection directives, such as$IF,to test for a condition that has nothing to do with

    global package constants or Oracle software versions.

    For example, you may want to include extra code tohelp you debug a PL/SQL program, but once the errorshave been corrected, you do not want to include this

    code in the final version because it will slow down theperformance. You can control this using thePLSQL_CCFLAGSinitialization parameter.

    Using Conditional Compilation

  • 5/25/2018 PLSQL_s15_l03

    19/28

    19 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    The PLSQL_CCFLAGSParameter (cont.)

    PLSQL_CCFLAGSallows you to set values for

    variables, and then test those variables in your PL/SQLprogram.

    You define the variables and assign values to themusing PLSQL_CCFLAGS. Then, you test the values of

    the variables in your PL/SQL program using inquiry

    directives. An inquiry directive is the name of thevariable prefixed by a double dollar sign ($$).

    Using Conditional Compilation

  • 5/25/2018 PLSQL_s15_l03

    20/28

    20 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    Using PLSQL_CCFLAGSand Inquiry

    DirectivesFirst, set the value of the parameter:

    Then compile your PL/SQL program:

    After you have debugged the program, remove thedebugging code by:

    Using Conditional Compilation

    ALTER SESSION SET PLSQL_CCFLAGS = 'debug:true';

    CREATE OR REPLACE PROCEDURE testproc IS BEGIN

    ...

    $IF $$debug $THEN

    DBMS_OUTPUT.PUT_LINE('This code was executed');

    $END

    ...

    END testproc;

  • 5/25/2018 PLSQL_s15_l03

    21/28

    21 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    Using PLSQL_CCFLAGSand Inquiry

    Directives (cont.)After you have debugged the program, remove thedebugging code by:

    and compile your procedure one more time to removethe debugging code.

    Using Conditional Compilation

    ALTER SESSION SET PLSQL_CCFLAGS = 'debug:false';

  • 5/25/2018 PLSQL_s15_l03

    22/28

    22 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    Using PLSQL_CCFLAGSand Inquiry

    Directives (cont.)DEBUG is not a keyword: you can use any name youlike, and it can be a number or a character string, not

    just a Boolean.

    Using Conditional Compilation

    ALTER SESSION SET PLSQL_CCFLAGS = 'testflag:1';

    CREATE OR REPLACE PROCEDURE testproc IS BEGIN

    ...

    $IF $$testflag > 0 $THEN

    DBMS_OUTPUT.PUT_LINE('This code was executed');

    $END

    ...

    END testproc;

  • 5/25/2018 PLSQL_s15_l03

    23/28

    23 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    Using PLSQL_CCFLAGSand Inquiry

    Directives (cont.)You can set more than one variable, and then testthem either together or separately:

    Using Conditional Compilation

    ALTER SESSION SET PLSQL_CCFLAGS =

    'firstflag:1, secondflag:false';

    CREATE OR REPLACE PROCEDURE testproc IS BEGIN

    ...

    $IF $$firstflag > 0 AND NOT $$secondflag $THEN

    DBMS_OUTPUT.PUT_LINE('Testing both variables');

    $ELSIF $$secondflag $THEN

    DBMS_OUTPUT.PUT_LINE('Testing one variable');$END

    ...

    END testproc;

  • 5/25/2018 PLSQL_s15_l03

    24/28

    24 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    Using PLSQL_CCFLAGSand Inquiry

    Directives (cont.)You can see which settings of PLSQL_CCFLAGSwere

    used to compile a program by queryingUSER_PLSQL_OBJECT_SETTINGS.

    Using Conditional Compilation

    SELECT name, plsql_ccflags

    FROM USER_PLSQL_OBJECT_SETTINGS

    WHERE name = 'TESTPROC';

  • 5/25/2018 PLSQL_s15_l03

    25/28

    25 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    Using PLSQL_CCFLAGSand Inquiry

    Directives (cont.)And, as always, you can see what was included in thecompiled program using DBMS_PREPROCESSOR.

    Using Conditional Compilation

    BEGIN

    DBMS_PREPROCESSOR.PRINT_POST_PROCESSED_SOURCE('PROCEDURE','','TESTPROC');

    END;

  • 5/25/2018 PLSQL_s15_l03

    26/28

    26 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    Using DBMS_DB_VERSION

    DBMS_DB_VERSIONis a bodiless package that defines

    a number of constants, including:

    VERSION(the current Oracle software version)

    VER_LE_11(= TRUE if the current Oracle softwareis version 11 or earlier)

    VER_LE_10(= TRUE if the current Oracle software

    is version 10 or earlier). So:

    Is exactly the same as:

    Using Conditional Compilation

    $IF DBMS_DB_VERSION.VER_LE_11 $THEN ...

    $IF DBMS_DB_VERSION.VERSION

  • 5/25/2018 PLSQL_s15_l03

    27/28

    27 Copyright 2013, Oracle and/or its affiliates. All rightsreserved.

    Terminology

    Key terms used in this lesson included:

    Conditional compilation

    DBMS_DB_VERSION

    DBMS_PREPROCESSOR

    Inquiry and selection directives

    PLSQL_CCFLAGS

    Using Conditional Compilation

  • 5/25/2018 PLSQL_s15_l03

    28/28

    28 Copyright 2013, Oracle and/or its affiliates. All rights

    Summary

    In this lesson, you should have learned how to:

    Describe the benefits of conditional compilation

    Create and conditionally compile a PL/SQL programcontaining selection, inquiry, and error directives

    Create and conditionally compile a PL/SQL programwhich calls the DBMS_DB_VERSIONserver-supplied

    package

    Using Conditional Compilation