SAGE Computing Services Customised Oracle Training Workshops and Consulting Creative Conditional Compilation … and raising the bar with your PL/SQL Scott.

Slides:



Advertisements
Similar presentations
PL/SQL : Stop making the same performance mistakes
Advertisements

Owners Roles DB Objects Grant Revoke Grant Revoke Grant Revoke (*) with admin option Il ruolo …. È un set di system & object privileges Non appartiene.
Refreshing Materialized Views
Child Health Reporting System (CHRS) How to Submit VHSS Data
Construction process lasts until coding and testing is completed consists of design and implementation reasons for this phase –analysis model is not sufficiently.
Automating Software Module Testing for FAA Certification Usha Santhanam The Boeing Company.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
PL/SQL.
4 Oracle Data Integrator First Project – Simple Transformations: One source, one target 3-1.
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
Introduction to Recursion and Recursive Algorithms
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 1.
Oracle PL/SQL IV Exceptions Packages.
SQL Objects and PL/SQL. Who am I ?  Gary Myers  Oracle developer since 1994  Database Consultant with SMS M&T  Blogger since 2004 Now at blog.sydoracle.com.
Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS.
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
PL/SQL.
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
Gary MarsdenSlide 1University of Cape Town Statements & Expressions Gary Marsden Semester 2 – 2000.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
Advanced Package Concepts. 2 home back first prev next last What Will I Learn? Write packages that use the overloading feature Write packages that use.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts and Debugging.
DT211/3 Internet Application Development
PL/SQL Agenda: Basic PL/SQL block structure
1 CSE1301 Computer Programming: Lecture 15 Flowcharts, Testing and Debugging.
07/19/04 NorCal OAUG Training Day, Paper 2.4 John Peters, JRPJR, Inc.1 Oracle Workflow Notifications John Peters JRPJR, Inc.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Cursor and Exception Handling By Nidhi Bhatnagar.
Benefits of PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –List and explain the benefits of PL/SQL –List.
SAGE Computing Services Customised Oracle Training Workshops and Consulting Are you making the most of PL/SQL? Hints and tricks and things you may have.
My experience building a custom ETL system Problems, solutions and Oracle quirks or How scary Oracle can look for a Java developer.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
SAGE Computing Services Customised Oracle Training Workshops and Consulting 11g New Features … of the SQL & PL/SQL Variety Scott Wesley Systems Consultant.
Oracle10g Developer: PL/SQL Programming1 Objectives Programming fundamentals The PL/SQL block Define and declare variables Initialize variables The NOT.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 7 PL/SQL Packages.
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
Oracle Data Integrator Procedures, Advanced Workflows.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
Advanced SQL: Triggers & Assertions
What is a Package? A package is an Oracle object, which holds other objects within it. Objects commonly held within a package are procedures, functions,
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Implementing The Middle Tier These slides.
Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
COMP 430 Intro. to Database Systems
Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
UNIT - V STORED PROCEDURE.
Database Application Development
PL/SQL Package Week 8.
PL/SQL Scripting in Oracle:
Overview of Eclipse Lectures
Coding Concepts (Basics)
Procedures Oracle & MySQL
Prof. Arfaoui. COM390 Chapter 7
Database Application Development
Presentation transcript:

SAGE Computing Services Customised Oracle Training Workshops and Consulting Creative Conditional Compilation … and raising the bar with your PL/SQL Scott Wesley Systems Consultant

The example everybodys seen…

PL/SQL Users Guide & Reference 10g Release 2 –Fundamentals of the PL/SQL Language Conditional Compilation

Availability 11g Release 1 10g Release 2 –Enabled out of the box – Once patched, enabled by default –Disable using _parameter – 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 Semantics

Inquiry Directives

> 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; /

First Demo: Post-processed Source Demo: cc1.sql cc2.sql

Reuse Settings

ALTER SYSTEM 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

Second Demo: Directive Usage Demo: cc3.sql cc4.sql cc5.sql

Some versioning 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 -- Lets 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;

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 > 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;

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 ('Debugging Details'); $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; $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

BEGIN... /* * * Note to self: Must remember to finish this bit * */... END;

BEGIN Jacko: this doesnt 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 PLS-00179: $ERROR: Waiting for business to advise PROCESS_COURT_OUTCOME line: 8

CREATE OR REPLACE PACKAGE pkg_debug IS debug_flag CONSTANT BOOLEAN := FALSE; $IF $$CC_dev_notes $THEN PROCEDURE insert_dev_note(p_note IN VARCHAR2,p_unit IN VARCHAR2,p_line IN VARCHAR2) IS PRAGMA AUTONOMOUS TRANSACTION; BEGIN INSERT INTO dev_notes (note, unit, line) VALUES (p_note, p_unit, p_line); END insert_dev_note; $END END pkg_debug; /

CREATE PROCEDURE process_court_outcome IS BEGIN IF lr_victim.age > 18 THEN send_to_prison(lr_victim); ELSE pkg_debug.insert_dev_note ('Waiting for business to advise',$$PLSQL_UNIT,$$PLSQL_LINE); END IF; END process_court_outcome; /

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 theres always this...

SQL> define debug=/* begin &debug select 'conditionally' into :c1 from dual; -- */ select 'always' into :c2 from dual; end; /

SAGE Computing Services Customised Oracle Training Workshops and Consulting Questions and Answers? Presentations are available from our website: