159356994 managing pl sql code

Upload: gustavo-gomez

Post on 16-Oct-2015

40 views

Category:

Documents


0 download

TRANSCRIPT

  • 5/26/2018 159356994 Managing Pl SQL Code

    1/32

    Copyright 2009, Oracle. All rights reserved.

    Managing PL/SQL Code

  • 5/26/2018 159356994 Managing Pl SQL Code

    2/32

    Copyright 2009, Oracle. All rights reserved.12 - 2

    Objectives

    After completing this lesson, you should be able to do thefollowing:

    Describe and use conditional compilation

    Hide PL/SQL source code using dynamic obfuscation and

    the Wrap utility

  • 5/26/2018 159356994 Managing Pl SQL Code

    3/32

    Copyright 2009, Oracle. All rights reserved.12 - 3

    Lesson Agenda

    Using conditional compilation Obfuscating PL/SQL code

  • 5/26/2018 159356994 Managing Pl SQL Code

    4/32

    Copyright 2009, Oracle. All rights reserved.12 - 4

    What Is Conditional Compilation?

    Enables you to customize the functionality in a PL/SQLapplication without removing any source code:

    Utilize the latest functionality with the latest database

    release or disable the new features to run the application

    against an older release of the database.

    Activate debugging or tracing functionality in the

    development environment and hide that functionality in the

    application while it runs at a production site.

    Reserved preprocessor control tokens

    $IF, $THEN, $ELSE,$ELSIF, $END, $$, $ERROR

  • 5/26/2018 159356994 Managing Pl SQL Code

    5/32

    Copyright 2009, Oracle. All rights reserved.12 - 5

    How Does Conditional Compilation Work?

    Inquiry directives:

    Use the $$token.

    Selection directives:

    Use the $IFtoken.

    Error directives:

    Use the $ERRORtoken.

    DBMS_DB_VERSION

    package

    DBMS_PREPROCESSOR

    package

  • 5/26/2018 159356994 Managing Pl SQL Code

    6/32

    Copyright 2009, Oracle. All rights reserved.12 - 6

    Using Selection Directives

    DECLARECURSOR cur IS SELECT employee_id FROMemployees WHERE$IF myapp_tax_package.new_tax_code $THEN

    salary > 20000;$ELSE

    salary > 50000;$ENDBEGIN

    OPEN cur;. . .END;

    $IF $THEN Text$ELSEIF $THEN Text. . .$ELSE Text$END

  • 5/26/2018 159356994 Managing Pl SQL Code

    7/32Copyright 2009, Oracle. All rights reserved.12 - 7

    PLSQL_CCFLAGSPLSQL_CODE_TYPE

    PLSQL_DEBUG

    PLSQL_OPTIMIZE_LEVEL

    PLSQL_WARNINGS

    NLS_LENGTH_SEMANTICSPLSQL_LINE

    PLSQL_UNIT

    Using Predefined and

    User-Defined Inquiry Directives

    PLSQL_CCFLAGS = 'plsql_ccflags:true,debug:true,debug:0';

    Predefined inquiry directives

    User-defined inquiry directives

  • 5/26/2018 159356994 Managing Pl SQL Code

    8/32Copyright 2009, Oracle. All rights reserved.12 - 8

    The PLSQL_CCFLAGSParameter

    and the Inquiry Directive

    Use the PLSQL_CCFLAGSparameter to control conditionalcompilation of each PL/SQL library unit independently.

    PLSQL_CCFLAGS = ':,:,...,:'

    ALTER SESSION SETPLSQL_CCFLAGS = 'plsql_ccflags:true, debug:true, debug:0';

    PLSQL_CCFLAGS

    initialization parameter Inquiry directive

  • 5/26/2018 159356994 Managing Pl SQL Code

    9/32Copyright 2009, Oracle. All rights reserved.12 - 9

    Displaying the PLSQL_CCFLAGSInitialization

    Parameter Setting

    SELECT name, type, plsql_ccflagsFROM user_plsql_object_settings

    . . .

  • 5/26/2018 159356994 Managing Pl SQL Code

    10/32Copyright 2009, Oracle. All rights reserved.12 - 10

    ALTER SESSION SET PLSQL_CCFLAGS = 'Tracing:true';CREATE OR REPLACE PROCEDURE P ISBEGIN$IF $$tracing $THEN

    DBMS_OUTPUT.PUT_LINE ('TRACING');$END

    END P;

    The PLSQL_CCFLAGSParameter

    and the Inquiry Directive: Example

    SELECT name, plsql_ccflagsFROM USER_PLSQL_OBJECT_SETTINGSWHERE name = 'P';

  • 5/26/2018 159356994 Managing Pl SQL Code

    11/32Copyright 2009, Oracle. All rights reserved.12 - 11

    Using Conditional Compilation Error

    Directives to Raise User-Defined Errors

    ALTER SESSION SET Plsql_CCFlags = ' Trace_Level:3 '/ CREATE PROCEDURE P ISBEGIN

    $IF $$Trace_Level = 0 $THEN ...;$ELSIF $$Trace_Level = 1 $THEN ...;$ELSIF $$Trace_Level = 2 $THEN ...;$else $error 'Bad: '||$$Trace_Level $END$END

    END P; /

    $ERROR varchar2_static_expression $END

    SHOW ERRORSErrors for PROCEDURE P:LINE/COL ERROR-------- ------------------------------------6/9 PLS-00179: $ERROR: Bad: 3

  • 5/26/2018 159356994 Managing Pl SQL Code

    12/32Copyright 2009, Oracle. All rights reserved.12 - 12

    Using Static Expressions

    with Conditional Compilation

    Booleanstatic expressions: TRUE, FALSE, NULL, IS NULL, IS NOT NULL

    > , < , >= ,

  • 5/26/2018 159356994 Managing Pl SQL Code

    13/32Copyright 2009, Oracle. All rights reserved.12 - 13

    The DBMS_DB_VERSIONPackage:

    Boolean Constants

    VER_LE_9

    VER_LE_9_1

    VER_LE_9_2

    VER_LE_10

    VER_LE_10_1

    VER_LE_10_2

    VER_LE_11

    VER_LE_11_1

    DBMS_DB_VERSION

    Package

    DBMS_DB_VERSION

    Boolean constants

    Oracle 10g Release 2

    TRUE?

  • 5/26/2018 159356994 Managing Pl SQL Code

    14/32Copyright 2009, Oracle. All rights reserved.12 - 14

    The DBMS_DB_VERSIONPackage Constants

    Name Value Description

    VERSIO N 10 Current version.

    RELEASE 2 Current release.

    VER_LE_9 FALSE Version

  • 5/26/2018 159356994 Managing Pl SQL Code

    15/32Copyright 2009, Oracle. All rights reserved.12 - 15

    Using Conditional Compilation

    with Database Versions: Example

    ALTER SESSION SET PLSQL_CCFLAGS = 'my_debug:FALSE, my_tracing:FALSE';CREATE PACKAGE my_pkg ASSUBTYPE my_real IS-- Check the database version, if >= 10g, use BINARY_DOUBLE data type,-- else use NUMBER data type$IF DBMS_DB_VERSION.VERSION < 10 $THEN NUMBER;$ELSE BINARY_DOUBLE;$END

    my_pi my_real; my_e my_real;END my_pkg;/CREATE PACKAGE BODY my_pkg ASBEGIN$IF DBMS_DB_VERSION.VERSION < 10 $THEN

    my_pi := 3.14016408289008292431940027343666863227;

    my_e := 2.71828182845904523536028747135266249775;$ELSEmy_pi := 3.14016408289008292431940027343666863227d;my_e := 2.71828182845904523536028747135266249775d;

    $ENDEND my_pkg;/

  • 5/26/2018 159356994 Managing Pl SQL Code

    16/32Copyright 2009, Oracle. All rights reserved.12 - 16

    Using Conditional Compilation

    with Database Versions: Example

    CREATE OR REPLACE PROCEDURE circle_area(p_radius my_pkg.my_real) ISv_my_area my_pkg.my_real;v_my_datatype VARCHAR2(30);

    BEGINv_my_area := my_pkg.my_pi * p_radius;DBMS_OUTPUT.PUT_LINE('Radius: ' || TO_CHAR(p_radius)

    || ' Area: ' || TO_CHAR(v_my_area) );$IF $$my_debug $THEN -- if my_debug is TRUE, run some debugging code

    SELECT DATA_TYPE INTO v_my_datatype FROM USER_ARGUMENTSWHERE OBJECT_NAME = 'CIRCLE_AREA' AND ARGUMENT_NAME = 'P_RADIUS';DBMS_OUTPUT.PUT_LINE('Datatype of the RADIUS argument is: ' ||v_my_datatype);

    $ENDEND; /

    CALL circle_area(50); -- Using Oracle Database 11gRelease 1

  • 5/26/2018 159356994 Managing Pl SQL Code

    17/32Copyright 2009, Oracle. All rights reserved.12 - 17

    Using DBMS_PREPROCESSORProcedures

    to Print or Retrieve Source Text

    -- Substitute ORA62 with your user account as neededCALLDBMS_PREPROCESSOR.PRINT_POST_PROCESSED_SOURCE('PACKAGE', 'ORA62', 'MY_PKG');

  • 5/26/2018 159356994 Managing Pl SQL Code

    18/32Copyright 2009, Oracle. All rights reserved.12 - 18

    Lesson Agenda

    Using conditional compilation Obfuscating PL/SQL code

  • 5/26/2018 159356994 Managing Pl SQL Code

    19/32Copyright 2009, Oracle. All rights reserved.12 - 19

    What Is Obfuscation?

    Obfuscation (or wrapping) of a PL/SQL unit is the processof hiding the PL/SQL source code.

    Wrapping can be done with the wrap utility and DBMS_DDL

    subprograms.

    The wrap utility is run from the command line and itprocesses an input SQL file, such as a SQL*Plus

    installation script.

    The DBMS_DDLsubprograms wrap a single PL/SQL unit,

    such as a single CREATE PROCEDUREcommand, that has

    been generated dynamically.

  • 5/26/2018 159356994 Managing Pl SQL Code

    20/32Copyright 2009, Oracle. All rights reserved.12 - 20

    Benefits of Obfuscating

    It prevents others from seeing your source code. Your source code is not visible through the USER_SOURCE,

    ALL_SOURCE, orDBA_SOURCEdata dictionary views.

    SQL*Plus can process the obfuscated source files.

    The Importand Exportutilities accept wrapped files.

  • 5/26/2018 159356994 Managing Pl SQL Code

    21/32Copyright 2009, Oracle. All rights reserved.12 - 21

    Whats New in Dynamic

    Obfuscating Since Oracle 10g?

    DBMS_DDL package

    DBMS_DDL.WRAPfunction

    CREATE_WRAPPED

    procedure

    Wraps the text and createsthe PL/SQL unit

    Provides the same functionalityas the CREATE_WRAPPEDprocedure

    but allows for larger inputs

  • 5/26/2018 159356994 Managing Pl SQL Code

    22/32Copyright 2009, Oracle. All rights reserved.12 - 22

    Nonobfuscated PL/SQL Code: Example

    BEGIN -- The ALL_SOURCE view family shows source code

    EXECUTE IMMEDIATE'CREATE OR REPLACE PROCEDURE P1 ISBEGINDBMS_OUTPUT.PUT_LINE (''I am not wrapped'');

    END P1;';

    END;/

    CALL p1();

    SELECT text FROM user_source

    WHERE name = 'P1' ORDER BY line;

  • 5/26/2018 159356994 Managing Pl SQL Code

    23/32Copyright 2009, Oracle. All rights reserved.12 - 23

    Obfuscated PL/SQL Code: Example

    BEGIN -- ALL_SOURCE view family obfuscates source codeDBMS_DDL.CREATE_WRAPPED ( 'CREATE OR REPLACE PROCEDURE P1 ISBEGINDBMS_OUTPUT.PUT_LINE (''I am wrapped now'');

    END P1;' );

    END;/

    CALL p1();

    SELECT text FROM user_sourceWHERE name = 'P1' ORDER BY line;

    . . .

  • 5/26/2018 159356994 Managing Pl SQL Code

    24/32Copyright 2009, Oracle. All rights reserved.12 - 24

    Dynamic Obfuscation: Example

    SET SERVEROUTPUT ON

    DECLAREc_code CONSTANT VARCHAR2(32767) :=' CREATE OR REPLACE PROCEDURE new_proc ASv_VDATE DATE;BEGINv_VDATE := SYSDATE;DBMS_OUTPUT.PUT_LINE(v_VDATE) ;

    END; ' ;

    BEGINDBMS_DDL.CREATE_WRAPPED (c_CODE);END;/

  • 5/26/2018 159356994 Managing Pl SQL Code

    25/32Copyright 2009, Oracle. All rights reserved.12 - 25

    The PL/SQL Wrapper Utility

    The PL/SQL wrapper is a stand-alone utility that hidesapplication internals by converting PL/SQL source code

    into portable object code.

    Wrapping has the following features:

    Platform independence

    Dynamic loading

    Dynamic binding

    Dependency checking

    Normal importing and exporting when invoked

  • 5/26/2018 159356994 Managing Pl SQL Code

    26/32Copyright 2009, Oracle. All rights reserved.12 - 26

    Running the Wrapper Utility

    Do not use spaces around the equal signs.

    The INAMEargument is required.

    The default extension for the input file is .sql, unless it is

    specified with the name.

    The ONAMEargument is optional.

    The default extension for output file is .plb, unless

    specified with the ONAMEargument.

    WRAP INAME=input_file_name [ONAME=output_file_name]

    WRAP INAME=demo_04_hello.sqlWRAP INAME=demo_04_helloWRAP INAME=demo_04_hello.sql ONAME=demo_04_hello.plb

    Examples

  • 5/26/2018 159356994 Managing Pl SQL Code

    27/32Copyright 2009, Oracle. All rights reserved.12 - 27

    Results of Wrapping

    -- Original PL/SQL source code in input file:

    CREATE PACKAGE banking ISmin_bal := 100;no_funds EXCEPTION;...

    END banking;/

    -- Wrapped code in output file:

    CREATE PACKAGE bankingwrapped

    012abc463e ...

    /

  • 5/26/2018 159356994 Managing Pl SQL Code

    28/32Copyright 2009, Oracle. All rights reserved.12 - 28

    Guidelines for Wrapping

    You must wrap only the package body, not the packagespecification.

    The wrapper can detect syntactic errors but cannot detect

    semantic errors.

    The output file should not be edited. You maintain theoriginal source code and wrap again as required.

    To ensure that all the important parts of your source code

    are obfuscated, view the wrapped file in a text editor

    before distributing it.

  • 5/26/2018 159356994 Managing Pl SQL Code

    29/32Copyright 2009, Oracle. All rights reserved.12 - 29

    DBMS_DDLPackage Versus the Wrap Utility

    Functionality DBMS_DDL Wrap Utility

    Code obfuscation Yes Yes

    Dynamic Obfuscation Yes No

    Obfuscate multiple

    programs at a time

    No Yes

  • 5/26/2018 159356994 Managing Pl SQL Code

    30/32Copyright 2009, Oracle. All rights reserved.12 - 30

    Quiz

    Conditional compilation enables you to customize thefunctionality in a PL/SQL application without removing any

    source code.

    1. True

    2. False

  • 5/26/2018 159356994 Managing Pl SQL Code

    31/32

    Copyright 2009, Oracle. All rights reserved.12 - 31

    Summary

    In this lesson, you should have learned how to: Describe and use conditional compilation

    Hide PL/SQL source code using dynamic obfuscation and

    the Wrap utility

  • 5/26/2018 159356994 Managing Pl SQL Code

    32/32

    Practice 12: Overview

    This practice covers the following topics: Creating a package and a procedure that uses conditional

    compilation

    Using the appropriate package to retrieve the

    postprocessed source text of the PL/SQL unit

    Obfuscating some PL/SQL code