Lucas Jellema, AMISABSOLUTELY TYPICAL -THE WHOLE STORY ON TYPES AND HOWTHEY POWER PL/SQL INTEROPERABILITYOn Types and Obje...
OVERVIEW•   History of Types•   Areas of use•   Type•   True OO•   Collections [of Types]•   Table and Multiset•   Interac...
STANDARD TYPES IN ORACLE DATABASE• Standard as in „installed out of the box‟   – Number, Char, Date   – Varchar2   – RAW, ...
THE DATABASE SUFFIX• 12c• 11g• 10g• 9i• 8i• 8.0   – That is 8.o (as in oh, not as in zero!)                   o• And: 8 re...
AREAS OF USE FOR TYPES• Structured programming in PL/SQL• Interaction SQL and PL/SQL   – Elegant, performing, organized• I...
EXAMPLE OF TYPE DEFINITION• Types are manipulated through             Type as Object  DDL statements:   – CREATE OR REPLAC...
INSTANTIATING AN OBJECT• A variable based on a type (as object) needs to be  instantiated before it can be assigned or rea...
FUNCTION ACTING ON TYPE
CALLING FUNCTION WITH TYPEPARAMETER FROM SQL• Types can be instantiated from within SQL statements  and passed to PL/SQL f...
SELECT OBJECT TYPE FROM SQL
SCALAR SUBQUERY           • Inline query that returns a single result              – One row, one columnselect ename      ...
WORKAROUND TO RETRIEVE MULTIPLE             VALUES: CONCATENATION OF VALUESselect ename,       job,       sal,       subst...
MUCH BETTER WORK AROUND: ADTselect ename                                                 create type,       job           ...
TYPES AND THE DBA• Before Oracle Database 11g – altering type definitions  was not easy   – Drop, Alter or Replace of a Ty...
COMPLEX TYPES• Attribute of a Type (as Object) can be based on an ADT  aka UDT: Object or Collection   – Limitation: Self-...
SOCIAL PROFILES STORED IN TABLES• Two options for storing social profiles:   – The normal, explicit     relational way:   ...
INSTANTIATING NESTED OBJECT IN SQL• Instantiate „look up object‟ using Scalar Subquery• Instantiate „look up object‟ from ...
COLLECTIONS IN ORACLE• Oracle supports in-memory collections of type  instances   – Can be of standard type (number, varch...
CREATING AND WORKING WITH ANASSOCIATIVE ARRAY (PL/SQL ONLY)
CREATING AND WORKING WITH ANESTED TABLE
COLLECTION OPERATIONS•   Exists – to check if element with/at index exists•   Extend (only for Nested Table)•   Count•   F...
COMPLEX TYPES   Type as Object                             Type as Object     PERSON_T                           SOCIAL_PR...
CONSTRUCTING A COMPLEX OBJECTIN PL/SQL
CONSTRUCTING A COMPLEX OBJECTIN PL/SQL
TRUE OO: SUBTYPES AND INHERITANCE• Types can be created as subtypes of another Type• A subtype has multiple identities:   ...
CREATION OF TYPE HIERARCHYType as ObjectEMPLOYEE_T
CREATE INSTANCES OF SUBTYPE INPL/SQL
OO OPERATORS IN TYPE HIERARCHY• TREAT (AS type) – downcast an instance to a specific  subtype – to access specialized subt...
MEMBER FUNCTIONS• A type can have operations   – Specification and Implementation (in the Body)   – Acting on the members ...
CONSTRUCTOR FUNCTIONS• A type is instantiated through a call to one of its  constructor methods• A type can have multiple,...
MULTIPLE USER DEFINED CONSTRUCTORS
MEMBER FUNCTIONS• Types can have member functions – similar to  functions in a package   – Just like package can act on pa...
MEMBER FUNCTION DISPLAY_LABEL
OVERRIDING MEMBER FUNCTIONS  • Sub types can override member functions from super     – When the function is invoked on a ...
EXAMPLE OF OVERRIDING MEMBERFUNCTION              Type PERSON_T as Object                        Type CUSTOMER_T as Object
MAP AND ORDER MEMBER FUNCTIONSORTING COLLECTIONS• Map Member Function translates an object to a scalar  data type that Ora...
MAP MEMBER FUNCTION IN ACTION -SORTING IN SQL• Map function dictates:   – Women come first, then sort by birth date (young...
ORDER MEMBER FUNCTION USED TOORDER BY IN SQL• Order member function to compare objects   – returns -1 when self comes befo...
ORDER MEMBER FUNCTION IN ACTION
USER DEFINED AGGREGATION FUNCTIONSIN SQL  select avg(hiredate) from emp    ORA-00932: inconsistent datatypes:         expe...
IMPLEMENTING USER DEFINEDAGGREGATE• Implement an object with the following operations:   – ODCIAggregateInitialize   – ODC...
IMPLEMENTING USER DEFINEDAGGREGATE• Register new aggregate function implemented by  the object   function dateavg (input d...
FUNCTION CHAINING• Dot notation (navigation) is used to call a function on  the result of a function (on the result…)   – ...
BULK COLLECT TO PREVENT MULTIPLEROW-BY-ROW PL/SQL TO SQL ACCESS• Traditional cursor based approaches for fetching data  fr...
BULK COLLECT EXAMPLE
CREATE A COLLECTION IN SQL – COLLECTOPERATOR• COLLECT is an aggregation function (like MAX, SUM,  COUNT) that aggregates i...
SELECT COLLECT INTO PL/SQL VARIABLE
MULTISET TURNING A (SUB) QUERY RESULTSET ON THE FLY INTO A NESTED TABLE• MULTISET can be used inside SQL Queries to conver...
MULTISET EXAMPLE
MULTISET OPERATORS ACTING ON NESTEDTABLES IN SQL AND/OR PL/SQL• C1 = C2   – Comparison based on named type, cardinality an...
MULTISET OPERATORS ACTING ON NESTEDTABLES IN SQL AND PL/SQL• C1 SUBMULTISET C2 evaluates to true when the  members of C1 a...
COULD BE ANYTHING• The Any… Types are like generic placeholders for data  that is only known at run time   – The contents ...
CREATING AND PROCESSINGA TABLE OF THINGS – STEP 1
CREATING AND PROCESSINGA TABLE OF THINGS – STEP 2
Process collection as query resultTABLE
TABLE FUNCTIONS• In queries: select from collections as if they were a  table using the TABLE operator
JUST BECAUSE WE CAN - POINTLESSCOMBINATION OF COLLECT AND TABLE
SORTING COLLECTIONSLEVERAGE SQL TO DO THE ORDER BY                 Build up collection of people                 in variab...
EXTREME ENCAPSULATION• View on top of table(collection)   – With instead of trigger handling DML   – Data queried from NUM...
EXTREME ENCAPSULATION
FOR GENERATING ROWS FOR SPECIAL     DOMAINS OF ALLOWABLE VALUES     • Have TABLE operate on an in-line created collectionw...
PIPING ROWS USING COLLECTIONS ANDTHE „TABLE FUNCTION‟• A Table Function normally returns the entire collection  at once• H...
THE SLOW ALPHABET…create or replacefunction alphabetreturn letter_tableis  l_alphabet letter_table:= letter_table();begin ...
THE PIPELINED SLOW ALPHABET…create or replacefunction alphabet return letter_tablepipelinedis  l_alphabet letter_table:= l...
MORE PERFORMANCE REQUIRESPARALLEL
MORE PERFORMANCE REQUIRESPARALLEL
DO NOT DO IT…ON YOUR OWN• Parallel means: multiple resources contributing to a  task at the same time• Introduce parallell...
PANCAKE PARTY
BETTER PERFORMING PANCAKEPARTY
PIPELINED PANCAKE PARTY: BESTPERFORMANCE
PIPELINED PANCAKE PARTY• Parallel Pipelined: multiple different resources working  on different components of the task in ...
PANCAKE PARTY IN PL/SQL
PANCAKE PARTY IN PL/SQL
PANCAKE PARTY PIPELINED IN PL/SQL
PIPELINED PANCAKE BA-EAT-KING
INTRODUCING XML• Structured ASCII• Esperanto• Cross technology – interoperability• Tools & frameworks for common operation...
WHAT IS XML? eXtensible Markup Language – for exchange of data Plain text / ASCII files Structured messages    Human r...
TYPE TO HOLD XML DATA IN ORACLE         XMLType
CONSTRUCTING XML IN DATABASEAPPLICATIONS• Many ways to produce XML content in Oracle   – XMLType – from text, types, curso...
CONVERTING TYPE STRUCTURE TOXMLTYPE• XMLType can be instantiated based on a single  complex User Defined Type instance   –...
CONVERTING NESTED TABLE TO XMLTYPE
CONVERT XMLTYPE TO NESTED USERDEFINED TYPE• XMLType supports conversion directly in a –  potentially nested – user defined...
XML CAN BE SOMEWHAT HEAVY• Size of XML documents compared to the actual data  content of those documents is „XXL‟• Complex...
JavaScript Object NotationLightweight data-interchange format
Name : Value
Name : Value,Name : Value,Name : Value
,                   ,Name : Value,       Name : Value,       Name : Value,Name : Value,       Name : Value,       Name : V...
{"ACCOUNTING" : {    "EMPLOYEES" : [    {"ENAME" : "KING",      "JOB" : "PRESIDENT",      "SAL" : 5000    },    {"ENAME" :...
<department name="ACCOUNTING">  <employees>    <employee>      <ename>KING</ename>      <job>PRESIDENT</job>      <sal>500...
PL/JSON EXAMPLE – CREATING ANDQUERYING JSON
MANIPULATION OF JSON OBJECT
FROM TYPES TO XML TO JSON
FROM JSON TO XML TO TYPES
PUBLISH PACKAGE AS HTTP-BASED APIUSING DBMS_EPG                                                       http• Hide database ...
SIMPLE DBMS_EPG EXAMPLE                            sys            scott                    scott
RESTFUL WEBSERVICES• Simple, stateless, light weight, http-based message  exchange protocol   – Use standard get, put, pos...
RESTFUL ARCHITECTURE           RESTful PL/SQL API       exposed through dbms_epg
RESTFUL RESOURCE NAVIGATION
PUBLISH REST-FUL HTTP API –DIRECTLY ON TOP OF THE RDBMS
PUBLISH REST-FUL HTTP API –DIRECTLY ON TOP OF THE RDBMS
JSON RESPONSE – WROUGHT FROMTYPES AND COLLECTIONS
TURNING ANY OBJECT INTO JSON
CONSUMING REST-FUL SERVICES• Invoking a REST-ful service is simply making an HTTP  request to some URL• Can be done throug...
SOAP WEB SERVICES• All messages (input and output) are XML• The message consists of two parts inside an envelope  (a SOAP ...
ORACLE RDBMS 11G –NATIVE DATABASE WEB SERVICES• Database Schema can be published                        WS/SOAP  through n...
STUFF INC• Challenge: publish SOAP Web Services to expose data  services based on an Oracle Database   – Read data and Man...
XML                                                                  Relational/Oracle Type                           JEE ...
PL/SQL MUSIC API EXPRESSED INTYPES AND COLLECTIONS  Type         Package
FOR JAVA DEVELOPERS BENEFIT:SOAP WebService   WRAP TYPE IN XMLTYPE                                    Java Class       Jav...
TO ACCOMMODATE FRAMEWORKSSOAP WebService   THAT DO NOT UNDERSTAND XMLTYPE                               Java Class    Java...
USING THE OSB OR SOA SUITE DATABASE                          ADAPTER – USER DEFINED TYPES ARE FINE                        ...
JUKEBOX SERVICE - COMPLETE                         Validate   Enrich    Transform Route & Operate       SOAP WebService   ...
SUMMARY• Long live types!• Structured programming in PL/SQL   – Multiple input and output parameters, step by step      re...
Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)
Upcoming SlideShare
Loading in …5
×

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

6,745 views

Published on

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.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
6,745
On SlideShare
0
From Embeds
0
Number of Embeds
3,499
Actions
Shares
0
Downloads
107
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • Process
  • http://technology.amis.nl/blog/6122/oracle-rdbms-11gr2-drop-and-replace-user-defined-types-even-when-there-are-dependencies
  • http://technology.amis.nl/blog/1217/sorting-plsql-collections-the-quite-simple-way-part-two-have-the-sql-engine-do-the-heavy-liftinghttp://technology.amis.nl/blog/1214/sorting-plsql-collections-the-hard-way-the-intermediate-way-and-the-quite-simple-way-part-one
  • Compare ECT:Unload all containers, than start moving out (train?)Unload one, put on truck and start driving; then unload next one
  • http://download.oracle.com/docs/cd/E11882_01/appdev.112/e16659/xdb13gen.htm#i1027254XSU – PL/SQL API http://download.oracle.com/docs/cd/E11882_01/appdev.112/e10708/adx_j_xsu.htm#BABGGGAH
  • http://download.oracle.com/docs/cd/E11882_01/appdev.112/e16659/xdb13gen.htm#CHEHAECDhttp://nuijten.blogspot.com/2010/12/creating-xmltype-based-on-object-types.htmlhttp://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/t_xml.htm#BABFEGCBhttp://askanantha.blogspot.com/2008/07/xml-type-conversions-in-oracle.htmlhttp://technology.amis.nl/blog/6131/oracle-sql-and-plsql-juggling-with-user-defined-types-adt-and-xmltype-for-creating-an-adt-based-xml-service-api
  • JavascriptPhpWeb wereld
  • Very compact, with lots of information.
  • Same information, now represented in XML – lot more text
  • Available Open Source projects for JSON in combination with PL/SQL
  • Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)

    1. 1. Lucas Jellema, AMISABSOLUTELY TYPICAL -THE WHOLE STORY ON TYPES AND HOWTHEY POWER PL/SQL INTEROPERABILITYOn Types and Objects, Collections and OOUKOUG, December 2011, Birmingham, UK
    2. 2. 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
    3. 3. 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)
    4. 4. THE DATABASE SUFFIX• 12c• 11g• 10g• 9i• 8i• 8.0 – That is 8.o (as in oh, not as in zero!) o• And: 8 really is o o• 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
    5. 5. 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
    6. 6. EXAMPLE OF TYPE DEFINITION• Types are manipulated through Type as Object DDL statements: – CREATE OR REPLACE TYPE PERSON_T – Object Types have properties (attributes) – Object Types are similar to PL/SQL records – Object Types live in Data Dictionary as primary database objects
    7. 7. 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
    8. 8. FUNCTION ACTING ON TYPE
    9. 9. CALLING FUNCTION WITH TYPEPARAMETER 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
    10. 10. SELECT OBJECT TYPE FROM SQL
    11. 11. SCALAR SUBQUERY • Inline query that returns a single result – One row, one columnselect ename Employees with their job and sal and the, job number of staff they manage, sal, (select count(empno) from emp s where s.mgr= e1.empno)from emp e1select ename Employees who earn between the average, sal salary for salesmen and analystsfrom empwhere sal between (select avg(sal) from emp where job = SALESMAN) and (select avg(sal) from emp where job = ANALYST)
    12. 12. WORKAROUND TO RETRIEVE MULTIPLE VALUES: CONCATENATION OF VALUESselect 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
    13. 13. MUCH BETTER WORK AROUND: ADTselect ename create type, job multivalue_return_type, sal as object, emps.sal_aggs.value1 avg_sal_in_job ( value1 number(20), emps.sal_aggs.value2 max_sal_in_job , value2 number(20)from ( select ename , value3 number(20) , 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
    14. 14. 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
    15. 15. 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 Type as Object PERSON_T SOCIAL_PROFILE_T
    16. 16. SOCIAL PROFILES STORED IN TABLES• Two options for storing social profiles: – The normal, explicit relational way: – The nested table approach:
    17. 17. INSTANTIATING NESTED OBJECT IN SQL• Instantiate „look up object‟ using Scalar Subquery• Instantiate „look up object‟ from (outer) joined columns
    18. 18. 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
    19. 19. CREATING AND WORKING WITH ANASSOCIATIVE ARRAY (PL/SQL ONLY)
    20. 20. CREATING AND WORKING WITH ANESTED TABLE
    21. 21. COLLECTION OPERATIONS• Exists – to check if element with/at index exists• Extend (only for Nested Table)• Count• First• Last• Next• Prior• Delete• Trim
    22. 22. COMPLEX TYPES Type as Object Type as Object PERSON_T SOCIAL_PROFILE_T Type as Table of email_address_t EMAIL_ADDRESS_TBL Type as Object EMAIL_ADDRESS_T
    23. 23. CONSTRUCTING A COMPLEX OBJECTIN PL/SQL
    24. 24. CONSTRUCTING A COMPLEX OBJECTIN PL/SQL
    25. 25. 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 Type as Object EMPLOYEE_T CUSTOMER_T
    26. 26. CREATION OF TYPE HIERARCHYType as ObjectEMPLOYEE_T
    27. 27. CREATE INSTANCES OF SUBTYPE INPL/SQL
    28. 28. 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
    29. 29. 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 Type PERSON_T as – Normal member functions Object <public methods> Type Body PERSON_T <implementation of private and public methods>
    30. 30. 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
    31. 31. MULTIPLE USER DEFINED CONSTRUCTORS
    32. 32. 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)
    33. 33. MEMBER FUNCTION DISPLAY_LABEL
    34. 34. 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 function Type PERSON_T as ObjectType EMPLOYEE_T as Object Type CUSTOMER_T as Object
    35. 35. EXAMPLE OF OVERRIDING MEMBERFUNCTION Type PERSON_T as Object Type CUSTOMER_T as Object
    36. 36. 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!
    37. 37. MAP MEMBER FUNCTION IN ACTION -SORTING IN SQL• Map function dictates: – Women come first, then sort by birth date (young before old)
    38. 38. ORDER MEMBER FUNCTION USED TOORDER 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
    39. 39. ORDER MEMBER FUNCTION IN ACTION
    40. 40. USER DEFINED AGGREGATION FUNCTIONSIN SQL select avg(hiredate) from emp ORA-00932: inconsistent datatypes: expected NUMBER got DATE• 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
    41. 41. IMPLEMENTING USER DEFINEDAGGREGATE• Implement an object with the following operations: – ODCIAggregateInitialize – ODCIAggregateIterate – ODCIAggregateTerminate – ODCIAggregateMerge • Optional, to enable parallelism create type DateAvgImpl as object ( count number , sumdates NUMBER , function ODCIAggregateInitialize , function ODCIAggregateIterate , function ODCIAggregateTerminate , function ODCIAggregateMerge )
    42. 42. IMPLEMENTING USER DEFINEDAGGREGATE• Register new aggregate function implemented by the object function dateavg (input date) return date PARALLEL_ENABLE AGGREGATE USING DATEAVGIMPL• Use the new function in queries select DateAvg(hiredate) from emp• “Analytic Function Friendly”
    43. 43. 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
    44. 44. BULK COLLECT TO PREVENT MULTIPLEROW-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
    45. 45. BULK COLLECT EXAMPLE
    46. 46. CREATE A COLLECTION IN SQL – COLLECTOPERATOR• COLLECT is an aggregation function (like MAX, SUM, COUNT) that aggregates into nested tables – Group by can be used to create ‘clusters’
    47. 47. SELECT COLLECT INTO PL/SQL VARIABLE
    48. 48. MULTISET TURNING A (SUB) QUERY RESULTSET 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)
    49. 49. MULTISET EXAMPLE
    50. 50. MULTISET OPERATORS ACTING ON NESTEDTABLES 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
    51. 51. MULTISET OPERATORS ACTING ON NESTEDTABLES 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)
    52. 52. 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
    53. 53. CREATING AND PROCESSINGA TABLE OF THINGS – STEP 1
    54. 54. CREATING AND PROCESSINGA TABLE OF THINGS – STEP 2
    55. 55. Process collection as query resultTABLE
    56. 56. TABLE FUNCTIONS• In queries: select from collections as if they were a table using the TABLE operator
    57. 57. JUST BECAUSE WE CAN - POINTLESSCOMBINATION OF COLLECT AND TABLE
    58. 58. SORTING COLLECTIONSLEVERAGE SQL TO DO THE ORDER BY Build up collection of people in variable l_people
    59. 59. 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
    60. 60. EXTREME ENCAPSULATION
    61. 61. FOR GENERATING ROWS FOR SPECIAL DOMAINS OF ALLOWABLE VALUES • Have TABLE operate on an in-line created collectionwith countries as( select column_value country from table( string_table(Belgium,France ,Egypt,Italy ) • To save)on repeated) – select value from dual UNION ALL select value …select countryfrom countrieswith gender_values as( select column_value gender from table( string_table(MALE, FEMALE,‘UNKNOWN )))select genderfrom gender_values
    62. 62. PIPING ROWS USING COLLECTIONS ANDTHE „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
    63. 63. THE SLOW ALPHABET…create or replacefunction 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
    64. 64. THE PIPELINED SLOW ALPHABET…create or replacefunction 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!
    65. 65. MORE PERFORMANCE REQUIRESPARALLEL
    66. 66. MORE PERFORMANCE REQUIRESPARALLEL
    67. 67. 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
    68. 68. PANCAKE PARTY
    69. 69. BETTER PERFORMING PANCAKEPARTY
    70. 70. PIPELINED PANCAKE PARTY: BESTPERFORMANCE
    71. 71. 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
    72. 72. PANCAKE PARTY IN PL/SQL
    73. 73. PANCAKE PARTY IN PL/SQL
    74. 74. PANCAKE PARTY PIPELINED IN PL/SQL
    75. 75. PIPELINED PANCAKE BA-EAT-KING
    76. 76. 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
    77. 77. 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>
    78. 78. TYPE TO HOLD XML DATA IN ORACLE XMLType
    79. 79. CONSTRUCTING XML IN DATABASEAPPLICATIONS• 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
    80. 80. CONVERTING TYPE STRUCTURE TOXMLTYPE• XMLType can be instantiated based on a single complex User Defined Type instance – Not (directly) on a nested table
    81. 81. CONVERTING NESTED TABLE TO XMLTYPE
    82. 82. CONVERT XMLTYPE TO NESTED USERDEFINED TYPE• XMLType supports conversion directly in a – potentially nested – user defined type – Provided element names match names of type, subtypes and attributes
    83. 83. 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 alternatives is on: structured, contract, standardized, facilities, lighter weight!
    84. 84. JavaScript Object NotationLightweight data-interchange format
    85. 85. Name : Value
    86. 86. Name : Value,Name : Value,Name : Value
    87. 87. , ,Name : Value, Name : Value, Name : Value,Name : Value, Name : Value, Name : Value,Name : Value Name : Value Name : Value
    88. 88. {"ACCOUNTING" : { "EMPLOYEES" : [ {"ENAME" : "KING", "JOB" : "PRESIDENT", "SAL" : 5000 }, {"ENAME" : "MILLER", "JOB" : "CLERK", "SAL" : 1300 }] }}
    89. 89. <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>
    90. 90. PL/JSON EXAMPLE – CREATING ANDQUERYING JSON
    91. 91. MANIPULATION OF JSON OBJECT
    92. 92. FROM TYPES TO XML TO JSON
    93. 93. FROM JSON TO XML TO TYPES
    94. 94. PUBLISH PACKAGE AS HTTP-BASED APIUSING DBMS_EPG http• 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
    95. 95. SIMPLE DBMS_EPG EXAMPLE sys scott scott
    96. 96. 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
    97. 97. RESTFUL ARCHITECTURE RESTful PL/SQL API exposed through dbms_epg
    98. 98. RESTFUL RESOURCE NAVIGATION
    99. 99. PUBLISH REST-FUL HTTP API –DIRECTLY ON TOP OF THE RDBMS
    100. 100. PUBLISH REST-FUL HTTP API –DIRECTLY ON TOP OF THE RDBMS
    101. 101. JSON RESPONSE – WROUGHT FROMTYPES AND COLLECTIONS
    102. 102. TURNING ANY OBJECT INTO JSON
    103. 103. 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
    104. 104. 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
    105. 105. ORACLE RDBMS 11G –NATIVE DATABASE WEB SERVICES• Database Schema can be published WS/SOAP 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 – http and https is supported – Limited control over WSDL & XSD – Use case: internal, cross technology, WS enabled client, no ESB or Application Server available
    106. 106. 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
    107. 107. XML Relational/Oracle Type JEE Server Database Native DB WebService HTTP http EPG ADF BC JSON/ CSV SDO /SDO WS XML PL/SQL XML & XSD SOA WS JAX-WS package Ref Cursor Suite JPublisher DB Types & Coll WS JDBC XML AQ Types Adapters AQ JMS Queue JMS utl_file, View JMS BFILE,Oracle EJB EJB/JPA URITYPEService Pojo Other Table (Email, FTP/File, XML Bus File XMPP/Chat) DB FTP UMS Chat/IM XMPP Server File/FTP Server Email Server
    108. 108. PL/SQL MUSIC API EXPRESSED INTYPES AND COLLECTIONS Type Package
    109. 109. FOR JAVA DEVELOPERS BENEFIT:SOAP WebService WRAP TYPE IN XMLTYPE Java Class Java Class JAX-WS ESB Architecture pattern: The XMLType Oracle JDBC Type Package Mediator Decouple XMLType Java Class exchange Validate, Enrich, Transform, Route, …
    110. 110. TO ACCOMMODATE FRAMEWORKSSOAP WebService THAT DO NOT UNDERSTAND XMLTYPE Java Class Java Class JAX-WS Oracle JDBC XMLType String Type CLOB Mediator XMLType Package Java Class JDBC exchange
    111. 111. 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 Validate Enrich Transform Route & Operate SOAP WebService UserXML Proxy Service Defined Business Type Type PackageHTTP ServiceSOAP JDBC Database Adapter
    112. 112. JUKEBOX SERVICE - COMPLETE Validate Enrich Transform Route & Operate SOAP WebService UserXML Proxy Service Defined Business Type Type PackageHTTP ServiceSOAP JDBC Database Adapter
    113. 113. 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

    ×