http:// pl/sql: it’s all in the presentation! tim hall oracle ace director oracle ace of the year...

27
http://www.oracle- base.com PL/SQL: It’s all in the PL/SQL: It’s all in the presentation! presentation! Tim Hall Tim Hall Oracle ACE Director Oracle ACE Director Oracle ACE of the Year 2006 Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g) OCP DBA (7, 8, 8i, 9i, 10g, 11g) OCA PL/SQL Developer OCA PL/SQL Developer http://www.oracle-base.com Oracle PL/SQL Tuning (Rampant) Oracle PL/SQL Tuning (Rampant) Oracle Job Scheduling (Rampant) Oracle Job Scheduling (Rampant)

Upload: preston-hodge

Post on 27-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

PL/SQL: It’s all in the presentation!PL/SQL: It’s all in the presentation!

Tim HallTim Hall

Oracle ACE DirectorOracle ACE Director

Oracle ACE of the Year 2006Oracle ACE of the Year 2006OCP DBA (7, 8, 8i, 9i, 10g, 11g)OCP DBA (7, 8, 8i, 9i, 10g, 11g)

OCA PL/SQL DeveloperOCA PL/SQL Developer

http://www.oracle-base.com

Oracle PL/SQL Tuning (Rampant)Oracle PL/SQL Tuning (Rampant)

Oracle Job Scheduling (Rampant)Oracle Job Scheduling (Rampant)

Page 2: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

PL/SQL: It’s all in the presentation!PL/SQL: It’s all in the presentation!

• Physical organization of objectsPhysical organization of objects• Present queries from APIsPresent queries from APIs

– Ref cursorsRef cursors

– Table functions and pipeliningTable functions and pipelining

• Presenting data as web services and XMLPresenting data as web services and XML– SOAP web servicesSOAP web services

– XML over HTTPXML over HTTP

– REST web servicesREST web services

Page 3: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

Physical organization of objects Physical organization of objects

http://www.oracle-base.com

Schema Owner

API Owner

Login User

PL/SQL APIsPL/SQL APIs Views?Views?

TablesTables

APEXAPEXPHPPHP .NET.NETJavaJava

Page 4: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

Physical organization of objects Physical organization of objects

http://www.oracle-base.com

Schema Owner

API Owner

Login User

PL/SQL APIsPL/SQL APIs

TablesTables

APEXAPEXPHPPHP .NET.NETJavaJava

PL/SQL APIsPL/SQL APIsAPI Owner

Login User Login User

Page 5: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

Why break things up like this?Why break things up like this?

• All data access via presentation (API) layer.All data access via presentation (API) layer.– Better security.Better security.

– Hides processing complexity from clients.Hides processing complexity from clients.

– Presentation layer is sharable between applications.Presentation layer is sharable between applications.

– Easier to tune and trace.Easier to tune and trace.

– Hides schema changes from client applications.Hides schema changes from client applications.

• Table or Transactional APIs?Table or Transactional APIs?– Transactional APIs are important to me. APIs that perform Transactional APIs are important to me. APIs that perform

business functions and and are understandable by the business.business functions and and are understandable by the business.

– I feel table APIs are unnecessary, but if you like them use them.I feel table APIs are unnecessary, but if you like them use them.

– Don’t present table APIs to the outside world. Don’t present table APIs to the outside world.

Page 6: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

How do we implement it?How do we implement it?

• Use existing techniques to abstract the data:Use existing techniques to abstract the data:– Packaged procedures and functions for data processing.Packaged procedures and functions for data processing.

– Ref cursors and pipelined table functions for data presentation.Ref cursors and pipelined table functions for data presentation.

• schema_setup.sql

Page 7: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

Without using APIsWithout using APIs

basic_query.php

Page 8: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

Do views help?Do views help?

• Views do reduce complexity of code in client apps.Views do reduce complexity of code in client apps.• I prefer not to expose views to client developers.I prefer not to expose views to client developers.• Risk of client developers writing joins between views.Risk of client developers writing joins between views.• How would a view affect the previous client code?How would a view affect the previous client code?

web_view.sql

Page 9: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

Using a viewUsing a view

view_query.php

Page 10: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

Cursor variables (REF CURSOR)Cursor variables (REF CURSOR)

• What are they?What are they?– A pointer to current row in multi-row query.A pointer to current row in multi-row query.

• Why are they useful?Why are they useful?– Allow us to separate opening and processing of cursors.Allow us to separate opening and processing of cursors.

– Can be passed as parameters.Can be passed as parameters.

• Why is that useful to us?Why is that useful to us?– Allows us to pass resultsets to client applicationsAllows us to pass resultsets to client applications

• Is that all they can do?Is that all they can do?– No, but it gets boring pretty fast… No, but it gets boring pretty fast…

• Do we have to define REF CURSOR types?Do we have to define REF CURSOR types?– No. We can be lazy and use SYS_REFCURSOR type.No. We can be lazy and use SYS_REFCURSOR type.

Page 11: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

Returning a cursor from a functionReturning a cursor from a function

web_rc_api.sql

Page 12: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

Using ref cursorsUsing ref cursors

rc_query.php

Page 13: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

I don’t have a simple query. What so I do?I don’t have a simple query. What so I do?

• You could use a table function.You could use a table function.• What’s one of those?What’s one of those?

– Functions that return collections are known as table functions.Functions that return collections are known as table functions.

• How can that help me?How can that help me?– In combination with the TABLE function, they can be used in the In combination with the TABLE function, they can be used in the

FROM clause of a query like a regular table.FROM clause of a query like a regular table.tf_test.sql

• For regular table functions, the collection must be defined For regular table functions, the collection must be defined using database OBJECT types. using database OBJECT types.

• Is that all I need to know?Is that all I need to know?– Not really. You need to know about pipelining.Not really. You need to know about pipelining.

Page 14: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

Pipelining table functionsPipelining table functions

• A table function builds the entire collection before returning any A table function builds the entire collection before returning any data, while a pipelined table function “pipes” each row out as data, while a pipelined table function “pipes” each row out as soon as it is created.soon as it is created.

• How does that help me?How does that help me?– It reduces first row lag…It reduces first row lag…

ptf_schema.sql, , ptf_package.sql, , ptf_query.sql– As the collection is never fully resident in memory, pipelining can As the collection is never fully resident in memory, pipelining can

produce a considerable memory saving.produce a considerable memory saving.memory_usage.sql

– Since 9i R2, the types used to define the pipelined table function Since 9i R2, the types used to define the pipelined table function can be defined in a package, but this method can produce can be defined in a package, but this method can produce management problems, so I prefer the explicit method.management problems, so I prefer the explicit method.implicit_types.sql

• But how does that really help me?But how does that really help me?– You can use PL/SQL to build the row then pass it out.You can use PL/SQL to build the row then pass it out.

Page 15: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

Defining a Pipelined table FunctionDefining a Pipelined table Function

web_ptf_api.sql

Page 16: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

Using a pipelined table functionUsing a pipelined table function

ptf_query.php

Page 17: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

Can we improve on this?Can we improve on this?

• We can combine Pipelined Table Functions and Ref Cursors.We can combine Pipelined Table Functions and Ref Cursors.web_rc_ptf_api.sql

• How will that affect the client code?How will that affect the client code?

Page 18: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

RC & PTF together (rc_ptf_query.php)RC & PTF together (rc_ptf_query.php)

rc_ptf_query.php

Page 19: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

What have we shown?What have we shown?

• We can use APIs to hide complexity from client application We can use APIs to hide complexity from client application developers.developers.

• How do we present queries from our APIs?How do we present queries from our APIs?– Ref cursorsRef cursors

– Pipelined table functionsPipelined table functions

– Views?Views?

• Remember: The technology dictates will and won’t want to do Remember: The technology dictates will and won’t want to do with APIs, not just our ideals.with APIs, not just our ideals.

• Is that the only way we can present data from PL/SQL?Is that the only way we can present data from PL/SQL?– Certainly not…Certainly not…

Page 20: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

Web Services and XMLWeb Services and XML

• eGov AU: Craig Thomler's personal eGovernment and Gov 2.0 eGov AU: Craig Thomler's personal eGovernment and Gov 2.0 thoughts and speculations from an Australian perspectivethoughts and speculations from an Australian perspective

• Overcoming public sector hurdles to Gov 2.0Overcoming public sector hurdles to Gov 2.0http://egovau.blogspot.com/2009/10/overcoming-public-sector-hurdles-to-gov.html

• Choice quotes:Choice quotes:– "...people are generally most comfortable with the technologies "...people are generally most comfortable with the technologies

they grew up with...”they grew up with...”

– "...government systems are struggling in some areas to keep up "...government systems are struggling in some areas to keep up with the rate of change...”with the rate of change...”

– "If our systems can't support Gov 2.0 initiatives then it is unlikely "If our systems can't support Gov 2.0 initiatives then it is unlikely that our senior management will.”that our senior management will.”

Page 21: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

Web Services and XMLWeb Services and XML

• What does this have to do with PL/SQL?What does this have to do with PL/SQL?– PL/SQL allows you to create web applications, without the learning PL/SQL allows you to create web applications, without the learning

curve of Java or .NET.curve of Java or .NET.

– Oracle allows you to present existing PL/SQL code as web Oracle allows you to present existing PL/SQL code as web services with zero effort.services with zero effort.

– Oracle's web toolkit allows you to present data as XML really Oracle's web toolkit allows you to present data as XML really easily.easily.

– PL/SQL is mature, not legacy.PL/SQL is mature, not legacy.

Page 22: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

XML DB Native Web ServicesXML DB Native Web Services

• Oracle XML DB Native Web Services present your PL/SQL as Oracle XML DB Native Web Services present your PL/SQL as Simple Object Access Protocol (SOAP) web services.Simple Object Access Protocol (SOAP) web services.

• Requires a single configuration step on the database.Requires a single configuration step on the database.• Enabled for “schema” by granting:Enabled for “schema” by granting:

– GRANT XDB_WEBSERVICES TO user;GRANT XDB_WEBSERVICES TO user;

• Optionally:Optionally:– GRANT XDB_WEBSERVICES_OVER_HTTP TO user;GRANT XDB_WEBSERVICES_OVER_HTTP TO user;

– GRANT XDB_WEBSERVICES_WITH_PUBLIC TO user; --?GRANT XDB_WEBSERVICES_WITH_PUBLIC TO user; --?

• web_services_setup.sql• XML DB auto-generates a XML DB auto-generates a WSDL file.• We send a We send a SOAP Request and get a SOAP Response returned.• call_ws.pl (run it)• Physical organization of schema keeps things neat.Physical organization of schema keeps things neat.

Page 23: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

Physical organization of objects Physical organization of objects

http://www.oracle-base.com

Schema Owner

WS APIOwner PL/SQL APIsPL/SQL APIs

TablesTables

.NET.NETJavaJava APEXAPEX

PL/SQL APIsPL/SQL APIsAPI Owner

Login User

Page 24: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

XML over HTTPXML over HTTP

• We can also present XML directly from the database.We can also present XML directly from the database.• First we define a Database Access Descriptor (DAD) to give a First we define a Database Access Descriptor (DAD) to give a

“schema” access to the web toolkit functionality.“schema” access to the web toolkit functionality.xml_api_setup.sql

• Next we define a package to generate our XML.Next we define a package to generate our XML.xml_api.sql (run it)

• This is an incredibly simple way to get XML out of the This is an incredibly simple way to get XML out of the database.database.

• Once again, physical organization is important.Once again, physical organization is important.

Page 25: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

XML over HTTP (Semi-Static Data)XML over HTTP (Semi-Static Data)

• The previous method regenerates the XML each time the URL The previous method regenerates the XML each time the URL is called.is called.

• For semi-static data this is a waste of resources.For semi-static data this is a waste of resources.• Solution? Generate once and re-present.Solution? Generate once and re-present.• How? Place it in the XML DB file system.How? Place it in the XML DB file system.

xml_db_setup.sql• Next we define a procedure to generate our XML.Next we define a procedure to generate our XML.

semi_static.sqlsemi_static.sql• How do we access this file?How do we access this file?

HTTP, FTP access, WebDAVHTTP, FTP access, WebDAV• This can reduce resource usages.This can reduce resource usages.

Page 26: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

REST web servicesREST web services

• The previous method is similar to Representational State The previous method is similar to Representational State Transfer (REST) web services, with the exception of the URL.Transfer (REST) web services, with the exception of the URL.

• We can mimic REST web services using the EPG parameters:We can mimic REST web services using the EPG parameters:– PATH-ALIASPATH-ALIAS

– PATH-ALIAS-PROCEDUREPATH-ALIAS-PROCEDURE

• rest_api_setup.sqlrest_api_setup.sql• rest_api.sqlrest_api.sql• REST DemoREST Demo• We can easily code REST web services directly from PL/SQL.We can easily code REST web services directly from PL/SQL.

Page 27: Http:// PL/SQL: It’s all in the presentation! Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OCP DBA (7, 8, 8i, 9i, 10g, 11g)

http://www.oracle-base.com

SummarySummary

• Physical organization of objectsPhysical organization of objects– Important for us to present business driven functionality.Important for us to present business driven functionality.

• Ref cursorsRef cursors– Allows us to hide complexity from client applications.Allows us to hide complexity from client applications.

• Table functionsTable functions– Allow us to return complex data as if it were a simple query.Allow us to return complex data as if it were a simple query.

• Presenting data as web services and XML is easy using PL/SQLPresenting data as web services and XML is easy using PL/SQL– Presenting existing PL/SQL code as SOAP web services.Presenting existing PL/SQL code as SOAP web services.

– Producing REST web services from PL/SQL.Producing REST web services from PL/SQL.

• Which all keeps us relevant in the new world order.Which all keeps us relevant in the new world order.

• Demo Code: http://www.oracle-base.com/workshopsDemo Code: http://www.oracle-base.com/workshops