Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG,...

114
ABSOLUTELY TYPICAL - THE WHOLE STORY ON TYPES AND HOW THEY POWER PL/SQL INTEROPERABILITY Lucas Jellema, AMIS On Types and Objects, Collections and OO UKOUG, December 2011, Birmingham, UK

description

This session will convince database developers that types in the Oracle Database are worth their salt - and more. With the recent improvements in 11gR2, the pieces are available to complete the puzzle of structured and modern programming with a touch of OO and more importantly to create a decoupled, reusable API that exposes services based on tables and views to clients that speak SQL, AQ, PL/SQL, Types, XML or RESTful, through SQL*Net, JDBC or HTTP. This session shows through many demonstrations how types and collections are defined, how they are used between SQL and PL/SQL and how they can be converted to and from XML and JSON and how they drive Native WebServices as well as RESTful services based on the Embedded PL/SQL Gateway. Everyone doing PL/SQL programming will benefit immediately from this session.Every Database Developer should be aware of Types and Collections. For structured programming, for optimal SQL to PL/SQL integration and for interoperability to client application. This session introduces Types and Collections, their OO capabilities, the conversion to XML and JSON, their use in Native and RESTful WebServices and the pivotal role they can play in encapsulation and decoupling.

Transcript of Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG,...

Page 1: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

ABSOLUTELY TYPICAL - THE WHOLE STORY ON TYPES AND HOW THEY POWER PL/SQL INTEROPERABILITY

Lucas Jellema, AMIS

On Types and Objects, Collections and OOUKOUG, December 2011, Birmingham, UK

Page 2: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

OVERVIEW

• History of Types• Areas of use• Type• True OO• Collections [of Types]• Table and Multiset• Interaction with the outside world

– XML, JSON– Web Services

Page 3: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

STANDARD TYPES IN ORACLE DATABASE• Standard as in ‘installed out of the box’

– Number, Char, Date– Varchar2– RAW, LONG– SDO_GEOMETRY, …– BLOB, CLOB, TIMESTAMP, INTERVAL– XMLType – DICOM (Medical Records)

Page 4: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

THE DATABASE SUFFIX

• 12c• 11g• 10g• 9i• 8i• 8.0

– That is 8.o (as in oh, not as in zero!)• And: 8 really is• OO in Oracle turned out to mean

– Define user defined types to use as SQL and PL/SQL data structure programming constructs• that have some OO characteristics as well• that can be used to define table columns for

oooo

Page 5: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

AREAS OF USE FOR TYPES

• Structured programming in PL/SQL• Interaction SQL and PL/SQL

– Elegant, performing, organized• Interaction with the outside world

– Java, SOA Suite, other consumers• OO-like storage of nested type structures• Do OO design and application development

Page 6: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

EXAMPLE OF TYPE DEFINITION

• Types are manipulated through DDL statements: – CREATE OR REPLACE TYPE

– Object Types have properties (attributes)– Object Types are similar to PL/SQL records– Object Types live in Data Dictionary as primary

database objects

Type as Object

PERSON_T

Page 7: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

INSTANTIATING AN OBJECT

• A variable based on a type (as object) needs to be instantiated before it can be assigned or read

• An instance of a type == an object

Page 8: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

FUNCTION ACTING ON TYPE

Page 9: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

CALLING FUNCTION WITH TYPE PARAMETER FROM SQL• Types can be instantiated from within SQL

statements and passed to PL/SQL functions

– SQL*Plus and other tools typically know how to render types in query results

Page 10: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

SELECT OBJECT TYPE FROM SQL

Page 11: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

SCALAR SUBQUERY

• Inline query that returns a single result– One row, one column

select ename, job, sal, (select count(empno) from emp s where s.mgr= e1.empno)from emp e1

select ename, sal from empwhere sal between (select avg(sal) from emp where job = 'SALESMAN') and (select avg(sal) from emp where job = 'ANALYST')

Employees with their job and sal and the number of staff they manage

Employees who earn between the average salary for salesmen and analysts

Page 12: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

WORKAROUND TO RETRIEVE MULTIPLE VALUES: CONCATENATION OF VALUES

select ename, job, sal, substr(sal_aggs, 1, instr(sal_aggs,'x') -1) avg_sal_in_job, substr(sal_aggs, instr(sal_aggs,'x') +1) max_sal_in_jobfrom ( select ename , job , sal , ( select avg(sal)||'x'||max(sal) from emp where job = e1.job ) sal_aggs from emp e1 )order by job, sal desc

Page 13: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

MUCH BETTER WORK AROUND: ADT

select ename, job, sal, emps.sal_aggs.value1 avg_sal_in_job, emps.sal_aggs.value2 max_sal_in_jobfrom ( select ename , job , sal

, ( select multivalue_return_type

( avg(sal), max(sal), null) from emp where job = e1.job ) sal_aggs from emp e1 ) empsorder by job, sal desc

create typemultivalue_return_typeas object( value1 number(20), value2 number(20), value3 number(20))

Page 14: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

TYPES AND THE DBA

• Before Oracle Database 11g – altering type definitions was not easy– Drop, Alter or Replace of a Type could only be done if no

references existed to the type from other types or tables

– That usually meant all types associated with an application were dropped and recreated – to alter one!

• Starting in 11g the DROP and REPLACE/ALTER TYPE operations can specify FORCE to ignore dependencies– Helps to overcome frowning, type-averse DBAs!

• Note: Data Dictionary Views are available to tell on types

• Types are editionable objects in terms of 11gR2 Edition Based Redefinition and can evolve gracefully

Page 15: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

COMPLEX TYPES

• Attribute of a Type (as Object) can be based on an ADT aka UDT: Object or Collection– Limitation: Self-reference is not allowed

Type as Object

PERSON_T

Type as Object

SOCIAL_PROFILE_T

Page 16: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

SOCIAL PROFILES STORED IN TABLES• Two options for storing social profiles:

– The normal, explicit relational way:

– The nested tableapproach:

Page 17: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

INSTANTIATING NESTED OBJECT IN SQL• Instantiate ‘look up object’ using Scalar

Subquery

• Instantiate ‘look up object’ from (outer) joined columns

Page 18: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

COLLECTIONS IN ORACLE

• Oracle supports in-memory collections of type instances– Can be of standard type (number, varchar2,

date,…) or complex User Defined Type (aka ADT)• Three kinds of collections

– Associative Array (PL/SQL only) – {key,value} pairs, very similar to Map in Java and Hashtable in C#

– VARRAY (the variable array of fixed size)– Nested Table

• These collections are similar, yet quite distinct– Definition and instantiation– Similar operations (count, first, next, delete, …)– Sparse vs. Dense– SQL Interaction support

Page 19: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

CREATING AND WORKING WITH AN ASSOCIATIVE ARRAY (PL/SQL ONLY)

Page 20: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

CREATING AND WORKING WITH A NESTED TABLE

Page 21: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

COLLECTION OPERATIONS

• Exists – to check if element with/at index exists

• Extend (only for Nested Table)• Count• First• Last• Next• Prior• Delete• Trim

Page 22: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

COMPLEX TYPES

Type as Object

PERSON_T

Type as Object

SOCIAL_PROFILE_T

Type as Object

EMAIL_ADDRESS_T

Type as Table of email_address_t

EMAIL_ADDRESS_TBL

Page 23: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

CONSTRUCTING A COMPLEX OBJECT IN PL/SQL

Page 24: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

CONSTRUCTING A COMPLEX OBJECT IN PL/SQL

Page 25: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

TRUE OO: SUBTYPES AND INHERITANCE• Types can be created as subtypes of another

Type• A subtype has multiple identities:

– EMPLOYEE_T is both PERSON_T and EMPLOYEE_T– Anything that can handle a PERSON_T can also

handle an EMPLOYEE_T and a CUSTOMER_T

Type as Object

PERSON_T

Type as Object

CUSTOMER_T

Type as Object

EMPLOYEE_T

Page 26: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

CREATION OF TYPE HIERARCHY

Type as Object

EMPLOYEE_T

Page 27: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

CREATE INSTANCES OF SUBTYPE IN PL/SQL

Page 28: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

OO OPERATORS IN TYPE HIERARCHY• TREAT (AS type) – downcast an instance to a

specific subtype – to access specialized subtype aspects

• IS OF ( [ONLY] type) – to identify type of an instance

Page 29: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

MEMBER FUNCTIONS

• A type can have operations– Specification and Implementation (in the Body)– Acting on the members – public and private – of

the object– Possibly overriding methods of the super-type

• Two types of operations– Constructor– Normal member functions

Type PERSON_T as Object

<public methods>

Type BodyPERSON_T

<implementation of private and

public methods>

Page 30: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

CONSTRUCTOR FUNCTIONS

• A type is instantiated through a call to one of its constructor methods

• A type can have multiple, overloaded user defined constructor functions– In addition to the

default system defined constructor that simply expects input for every public member

Page 31: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

MULTIPLE USER DEFINED CONSTRUCTORS

Page 32: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

MEMBER FUNCTIONS

• Types can have member functions – similar to functions in a package– Just like package can act on package state

(globals), member functions act on object (type instance)

Page 33: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

MEMBER FUNCTION DISPLAY_LABEL

Page 34: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

OVERRIDING MEMBER FUNCTIONS

• Sub types can override member functions from super– When the function is invoked on a PERSON_T

and PERSON_T happens to be a CUSTOMER_T, then the function as defined on CUSTOMER_T is executed

– Overriding subtype member function can invoke the overridden super typer functionType PERSON_T as Object

Type CUSTOMER_T as ObjectType EMPLOYEE_T as Object

Page 35: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

EXAMPLE OF OVERRIDING MEMBER FUNCTION

Type PERSON_T as Object

Type CUSTOMER_T as Object

Page 36: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

MAP AND ORDER MEMBER FUNCTIONSORTING COLLECTIONS

• Map Member Function translates an object to a scalar data type that Oracle knows how to compare and sort– Varchar2, Number, Date or even an UDT with

scalar attributes (or a Map function of its own)

• Note: Map member function is also used for equality comparison– In this case John Doe and Bill Doe are seen as equal!

Page 37: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

MAP MEMBER FUNCTION IN ACTION - SORTING IN SQL

• Map function dictates:– Women come first, then sort by birth date

(young before old)

Page 38: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

ORDER MEMBER FUNCTION USED TO ORDER BY IN SQL• Order member function to compare objects

– returns -1 when self comes before other (when sorted), 1 when self comes after and 0 when they draw

Page 39: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

ORDER MEMBER FUNCTION IN ACTION

Page 40: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

USER DEFINED AGGREGATION FUNCTIONS IN SQL

• The Oracle Data Cartridge offers User Extensibility framework for the database

• Data Cartridge provides: – user defined aggregate functions– domain indexes and functions

• used for Oracle Text and Oracle Spatial

– Pipelined Table Functions – the interface approach• Dynamic handling of AnyData objects

select avg(hiredate) from emp

ORA-00932: inconsistent datatypes: expected NUMBER got DATE

Page 41: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

IMPLEMENTING USER DEFINED AGGREGATE• Implement an object with the following

operations:– ODCIAggregateInitialize – ODCIAggregateIterate – ODCIAggregateTerminate – ODCIAggregateMerge

• Optional, to enable parallelism

create type DateAvgImplas object ( count number , sumdates NUMBER , function ODCIAggregateInitialize , function ODCIAggregateIterate , function ODCIAggregateTerminate , function ODCIAggregateMerge )

Page 42: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

IMPLEMENTING USER DEFINED AGGREGATE• Register new aggregate function implemented by

the object

• Use the new function in queries

• “Analytic Function Friendly”

function dateavg (input date) return datePARALLEL_ENABLEAGGREGATE USING DATEAVGIMPL

select DateAvg(hiredate) from emp

Page 43: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

FUNCTION CHAINING

• Dot notation (navigation) is used to call a function on the result of a function (on the result…)– if the result of the nested functions are types

Page 44: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

BULK COLLECT TO PREVENT MULTIPLE ROW-BY-ROW PL/SQL TO SQL ACCESS

• Traditional cursor based approaches for fetching data from the database using SQL resulted in – Unnecessary SQL/PLSQL context switches

• By fetching multiple rows at once – the number of (expensive) switches can be reduced

• Several approaches exist – all rely on Collections– Bulk Collect– Select Collection

• Note: as of Oracle 10g the ‘normal’ cursor-for-loop also implicitly does fetching in bulk

Page 45: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

BULK COLLECT EXAMPLE

Page 46: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

CREATE A COLLECTION IN SQL – COLLECT OPERATOR• COLLECT is an aggregation function (like

MAX, SUM, COUNT) that aggregates into nested tables– Group by can be used to create ‘clusters’

Page 47: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

SELECT COLLECT INTO PL/SQL VARIABLE

Page 48: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

MULTISET TURNING A (SUB) QUERY RESULT SET ON THE FLY INTO A NESTED TABLE

• MULTISET can be used inside SQL Queries to convert a query result set into a collection (nested table)– On the fly (in the middle of a query)

Page 49: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

MULTISET EXAMPLE

Page 50: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

MULTISET OPERATORS ACTING ON NESTED TABLES IN SQL AND/OR PL/SQL

• C1 = C2– Comparison based on named type, cardinality and

comparison of individual elements• C1 <> C2 or C1 != C2

– Inverse of equality test• C1 IN C2 returns true if C1 is one of the

collections in C2 which is a collection of collections

• POWERMULTISET C1 generates all non-empty sub-multisets from collection C1– All possible combinations created from the

elements in C1, produced as collections of the same type as C1

• C1 IS EMPTY returns true if C1 is empty• CARDINALITY C1 returns the number of elements

in C1

Page 51: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

MULTISET OPERATORS ACTING ON NESTED TABLES IN SQL AND PL/SQL• C1 SUBMULTISET C2 evaluates to true when the

members of C1 are all found in C2• X MEMBER OF C1 evaluates to true when X is

found as a member of collection C1• C1 MULTISET UNION [DISTINCT] C2 – combining

two collections– Use DISTINCT to eliminate duplicate elements

• C1 MULTISET INTERSECT [DISTINCT] C2 – produces collection of elements that occur in both C1 and C2

• C1 MULTISET EXCEPT [DISTINCT] C2 – produces the collection of elements that appear in C1 and not in C2– Use ALL to allow (reduced number of )duplicates

• C1 IS A SET – tests if the collection C1 only has unique members (no duplicates)

Page 52: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

COULD BE ANYTHING

• The Any… Types are like generic placeholders for data that is only known at run time– The contents of Any… instances is accessible

through introspection

• AnyData – some thing, could be any thing (Object)

• AnyType – holds the definition of some thing• Like AnyData without the data – only meta data

• AnySet – some collection of things (all of the same type)– can be used for interface parameters to

communicate self-descriptive sets of data– Used in Pipelined Table Functions created through

Data Cartridge to return dynamically determined data structures

Page 53: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

CREATING AND PROCESSING A TABLE OF THINGS – STEP 1

Page 54: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

CREATING AND PROCESSING A TABLE OF THINGS – STEP 2

Page 55: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

TABLEProcess collection as query result

Page 56: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

TABLE FUNCTIONS

• In queries: select from collections as if they were a table using the TABLE operator

Page 57: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

JUST BECAUSE WE CAN - POINTLESS COMBINATION OF COLLECT AND TABLE

Page 58: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

SORTING COLLECTIONSLEVERAGE SQL TO DO THE ORDER BY

Build up collection of peoplein variable l_people

Page 59: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

EXTREME ENCAPSULATION

• View on top of table(collection)– With instead of trigger handling DML

– Data queried from NUMBERS is produced in PL/SQL – no underlying table involved

– Data manipulated in NUMBERS is processed in memory

Page 60: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

EXTREME ENCAPSULATION

Page 61: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

FOR GENERATING ROWS FOR SPECIAL DOMAINS OF ALLOWABLE VALUES

• Have TABLE operate on an in-line created collection

• To save on repeated– select value from dual UNION ALL select value

with countries as( select column_value country from table( string_table('Belgium','France' ,'Egypt','Italy' ) ))select countryfrom countries

with gender_values as( select column_value gender from table( string_table('MALE', 'FEMALE',‘UNKNOWN' )))select genderfrom gender_values

Page 62: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

PIPING ROWS USING COLLECTIONS AND THE ‘TABLE FUNCTION’ • A Table Function normally returns the entire

collection at once• However: it can also return collection

elements one at a time– That is called PIPELINED

• As soon as the function pipes a single element, processing that element can continue

• It’s bit like the /*+ FIRST_ROWS */ Hint

Page 63: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

THE SLOW ALPHABET…

create or replace function alphabetreturn letter_tableis l_alphabet letter_table:= letter_table();begin l_alphabet.extend(26); for i in 1..26 loop l_alphabet(i):= chr(64+i); dbms_lock.sleep(0.1); end loop; return l_alphabet;end alphabet;

Note: it is not just 2.79 seconds for all rows to appear: it takes that long for any row to appear

Page 64: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

THE PIPELINED SLOW ALPHABET…

create or replace function alphabet return letter_tablepipelinedis l_alphabet letter_table:= letter_table();begin l_alphabet.extend(26); for i in 1..26 loop l_alphabet(i):= chr(64+i); dbms_lock.sleep(0.1); pipe row (l_alphabet(i)); end loop; return;end alphabet;

Note: total processing time is just as long as before; however, the first row appears after less than 0.5 sec!

Page 65: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

MORE PERFORMANCE REQUIRES PARALLEL

Page 66: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

MORE PERFORMANCE REQUIRES PARALLEL

Page 67: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

DO NOT DO IT…ON YOUR OWN

• Parallel means: multiple resources contributing to a task at the same time

• Introduce parallellism into your database application– parallel query and DML– coordinated jobs (dbms_job, dbms_scheduler)– 11g: dbms_parallel_execute– Pipelined & Parallel table functions

Page 68: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

PANCAKE PARTY

Page 69: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

BETTER PERFORMING PANCAKE PARTY

Page 70: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

PIPELINED PANCAKE PARTY: BEST PERFORMANCE

Page 71: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

PIPELINED PANCAKE PARTY

• Parallel Pipelined: multiple different resources working on different components of the task in [potentially] different tiers– From batch processing to unit processing => pass

the unit on for further processing as soon as part of the task is done – to leverage resources (across tiers) in parallel

• Instead of baking the entire stack first and only then eating it…– … start eating as soon as the first one is done– Even the last guest ends earlier than without

pipelining provided he eats more than one pancake• (he eats his first pancake when it is done, not when

the stack is done)

– The first eater is done much sooner • first rows/all rows ≈ first pancake/all pancakes

Page 72: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

PANCAKE PARTY IN PL/SQL

Page 73: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

PANCAKE PARTY IN PL/SQL

Page 74: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

PANCAKE PARTY PIPELINED IN PL/SQL

Page 75: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

PIPELINED PANCAKE BA-EAT-KING

Page 76: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

INTRODUCING XML

• Structured ASCII• Esperanto• Cross technology – interoperability• Tools & frameworks for common operations

– Parsing, validation, construction, transformation and querying

• Standards: – XSD – design and contract– XPath - querying– XSLT - transformation– XQuery – querying and manipulation

Page 77: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

WHAT IS XML?

eXtensible Markup Language – for exchange of data

Plain text / ASCII files Structured messages

Human readable More important: machine interpreted

Meaning defined with tags Elements and attributes<?xml version="1.0" encoding="UTF-8"?><root element>

<element1 attributeName="value"> content</element1><element2 attributeName="value" /><element3> <element4>content</element4></element3>

</root element>

Page 78: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

TYPE TO HOLD XML DATA IN ORACLE

XMLType

Page 79: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

CONSTRUCTING XML IN DATABASE APPLICATIONS• Many ways to produce XML content in Oracle

– XMLType – from text, types, cursor, bfile contents, …

– SQL/XML (XMLAgg, XMLElement, XMLForest, …)– DBMS_XMLGEN– SYS_XMLGEN and SYS_XMLAGG– DBURIType and XDBURIType– XML SQL Utility (XSU) – command line, Java,

PL/SQL– fn:doc and fn:collection (replacing ora:view)

XMLType

Page 80: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

CONVERTING TYPE STRUCTURE TO XMLTYPE• XMLType can be instantiated based on a

single complex User Defined Type instance– Not (directly) on a nested table

Page 81: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

CONVERTING NESTED TABLE TO XMLTYPE

Page 82: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

CONVERT XMLTYPE TO NESTED USER DEFINED TYPE• XMLType supports conversion directly in a –

potentially nested – user defined type– Provided element names match names of type,

subtypes and attributes

Page 83: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

XML CAN BE SOMEWHAT HEAVY

• Size of XML documents compared to the actual data content of those documents is ‘XXL’

• Complex nested structure that requires parsing

• Turning XML into native data structures and vice versa is expensive (Marshalling/Unmarshalling )

• XML Programming facilities not available on all platforms and in all languages– Objective C, JavaScript, …

• The search for alternativesis on: structured, contract, standardized, facilities,lighter weight!

Page 84: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

JavaScript Object Notation

Lightweight data-interchange format

Page 85: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

Name : Value

Page 86: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

Name : Value,Name : Value,Name : Value

Page 87: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

Name : Value,Name : Value,Name : Value

Name : Value,Name : Value,Name : Value

Name : Value,Name : Value,Name : Value, ,

Page 88: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

{"ACCOUNTING" : { "EMPLOYEES" : [ {"ENAME" : "KING", "JOB" : "PRESIDENT", "SAL" : 5000 }, {"ENAME" : "MILLER", "JOB" : "CLERK", "SAL" : 1300 }] }}

Page 89: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

<department name="ACCOUNTING"> <employees> <employee> <ename>KING</ename> <job>PRESIDENT</job> <sal>5000</sal> </employee> <employee> <ename>MILLER</ename> <job>CLERK</job> <sal>1300</sal> </employee> </employees></department>

Page 90: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)
Page 91: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

PL/JSON EXAMPLE – CREATING AND QUERYING JSON

Page 92: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

MANIPULATION OF JSON OBJECT

Page 93: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

FROM TYPES TO XML TO JSON

Page 94: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

FROM JSON TO XML TO TYPES

Page 95: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

PUBLISH PACKAGE AS HTTP-BASED API USING DBMS_EPG• Hide database protocol

– Not its physical location nor the schema, and user authentication

• HTTP communication is truly cross technology– Browser, Java, .Net, JavaScript & RIA clients, …

• Is also possibly remote, across networks, firewalls etc. – easier than normal JDBC connections

• Can publish in various formats– Text, HTML, CSV, JSON, XML, RSS

• Use case:cross-technology, internal no WS*/ESB

http

Page 96: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

SIMPLE DBMS_EPG EXAMPLE

sys

scott

scott

Page 97: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

RESTFUL WEBSERVICES

• Simple, stateless, light weight, http-based message exchange protocol– Use standard get, put, post, delete operations– Use URL to pass resource reference

• Small, easy-to-understand messages• Typically XML or JSON based• Not using SOAP, or the WS*-stack• Often used directly from User Interface –

(AJAX enabled) JavaScript

Page 98: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

RESTFUL ARCHITECTURE

RESTful PL/SQL APIexposed through dbms_epg

httphttp

Page 99: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

RESTFUL RESOURCE NAVIGATION

Page 100: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

PUBLISH REST-FUL HTTP API – DIRECTLY ON TOP OF THE RDBMS

Page 101: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

PUBLISH REST-FUL HTTP API – DIRECTLY ON TOP OF THE RDBMS

Page 102: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

JSON RESPONSE – WROUGHT FROM TYPES AND COLLECTIONS

Page 103: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

TURNING ANY OBJECT INTO JSON

Page 104: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

CONSUMING REST-FUL SERVICES

• Invoking a REST-ful service is simply making an HTTP request to some URL

• Can be done through:– utl_http– httpuritype– Stored Java

• Result can be processed as just a string or as XML, JSON or other format– Libraries (PL/JSON) can help with special formats

Page 105: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

SOAP WEB SERVICES

• All messages (input and output) are XML• The message consists of two parts inside an

envelope (a SOAP XML wrapper)– The header with meta-data– The body with the contents to be handled by or

returned by the service• The WebService Definition Language (WSDL)

document describes the service• An XML Schema Document (XSD) describes

the structure of the XML messages– XSD is like an ERD or Table Design

Page 106: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

ORACLE RDBMS 11G – NATIVE DATABASE WEB SERVICES• Database Schema can be published

through native database web services– Each package corresponds with a WSDL

• With an operation for each Procedure or Function• Auto-mapping from PL/SQL input and output

parameters to WSDL (XSD) Types

– WSDL and XSD are dynamically generated– A generic Query Service (across schema) is

exposed• To execute user defined queries• With support for pagination and some formatting

WS/SOAP

– http and https is supported– Limited control over WSDL & XSD

– Use case: internal, cross technology, WS enabled client, no ESB or Application Server available

Page 107: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

STUFF INC

• Challenge: publish SOAP Web Services to expose data services based on an Oracle Database– Read data and Manipulate data

• Use as decoupled an approach as possible– Java developers do not

touch SQL or PL/SQL– Database developers are not

aware of web service context

• See white paper on OTN http://www.oracle.com/technetwork/articles/soa/jellema-esb-pattern-1385306.html for details

Page 108: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

Chat/IM XMPP Server

HTTP

JDBC

Other(Email, FTP/File,

XMPP/Chat)

SOA Suite

Oracle Service

Bus

DB

AQ

JMS

EJB

FileFTP

SDO

WS

http

PL/SQL package

Table

AQ

Native DB WebService

EPG

View

JEE Server Database

JAX-WS

ADF BC/SDO WS

EJB/JPA

Email ServerFile/FTP Server

UMS

XMLDB

XMLTypes

XMLXML & XSD

JSON/ CSV

Ref Cursor

Types & CollJPublisher

WS

utl_file, BFILE,

URITYPE

JMS Queue

XMLRelational/Oracle Type

JMS

Adapters

Pojo

Page 109: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

PL/SQL MUSIC API EXPRESSED IN TYPES AND COLLECTIONS

PackageType

Page 110: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

Package

XMLT

ype

Type

SOAP

Web

Serv

ice

JAX-WS

Java Class

Java ClassOracle JDBC

XMLType exchange

Java Class

Mediator

The ESB Architecture pattern:

DecoupleValidate, Enrich, Transform, Route, …

FOR JAVA DEVELOPERS BENEFIT: WRAP TYPE IN XMLTYPE

Page 111: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

SOAP

Web

Serv

ice

JAX-WS

Java Class

Java ClassOracle JDBC

XMLType exchange

Java Class

Mediator Package

XMLT

ype

Type

String

JDBC

CLO

B

TO ACCOMMODATE FRAMEWORKS THAT DO NOT UNDERSTAND XMLTYPE

Page 112: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

USING THE OSB OR SOA SUITE DATABASE ADAPTER – USER DEFINED TYPES ARE FINE

• Database adapter interacting with Type based PL/SQL API– Derives XSD from input and output parameters– Allows for perfect decoupling between PL/SQL

on one side of the fence and Web Service/XML on the other

PackageType

SOAP

Web

Serv

ice

Proxy Service

Validate Enrich Transform Route & Operate

XML

HTTPSOAP

Business Service

Database Adapter

User Defined

Type

JDBC

Page 113: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

PackageType

SOAP

Web

Serv

ice

Proxy Service

Validate Enrich Transform Route & Operate

XML

HTTPSOAP

Business Service

Database Adapter

User Defined

Type

JDBC

JUKEBOX SERVICE - COMPLETE

Page 114: Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

SUMMARY

• Long live types!• Structured programming in PL/SQL

– Multiple input and output parameters, step by step result constructions

• Integration between SQL and PL/SQL– Scalar Subquery, Bulk Collect and Collect

Aggregator– Table and Multiset operators– Pipelined Table Functions

• Integrating with the external world– Type turning XML and/or JSON– Native Database Web Services– Database Adapter in SOA Suite and Service Bus