Download presentation
Presentation is loading. Please wait.
Published byPreston Hodge Modified over 9 years ago
1
http://www.oracle-base.com 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) OCA PL/SQL Developer http://www.oracle-base.com Oracle PL/SQL Tuning (Rampant) Oracle Job Scheduling (Rampant)
2
http://www.oracle-base.com PL/SQL: It’s all in the presentation! Physical organization of objectsPhysical organization of objects Present queries from APIsPresent queries from APIs –Ref cursors –Table functions and pipelining Presenting data as web services and XMLPresenting data as web services and XML –SOAP web services –XML over HTTP –REST web services
3
Physical organization of objects http://www.oracle-base.com Schema Owner API Owner Login User PL/SQL APIs Views? Tables APEXPHP.NETJava
4
Physical organization of objects http://www.oracle-base.com Schema Owner API Owner Login User PL/SQL APIs Tables APEXPHP.NETJava API Owner Login User
5
http://www.oracle-base.com Why break things up like this? All data access via presentation (API) layer.All data access via presentation (API) layer. –Better security. –Hides processing complexity from clients. –Presentation layer is sharable between applications. –Easier to tune and trace. –Hides schema changes from client applications. Table or Transactional APIs?Table or Transactional APIs? –Transactional APIs are important to me. APIs that perform business functions and and are understandable by the business. –I feel table APIs are unnecessary, but if you like them use them. –Don’t present table APIs to the outside world.
6
http://www.oracle-base.com 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. –Ref cursors and pipelined table functions for data presentation. schema_setup.sqlschema_setup.sqlschema_setup.sql
7
http://www.oracle-base.com Without using APIs basic_query.php
8
http://www.oracle-base.com 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
9
http://www.oracle-base.com Using a view view_query.php
10
http://www.oracle-base.com Cursor variables (REF CURSOR) What are they?What are they? –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. –Can be passed as parameters. Why is that useful to us?Why is that useful to us? –Allows 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… 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.
11
http://www.oracle-base.com Returning a cursor from a function web_rc_api.sql
12
http://www.oracle-base.com Using ref cursors rc_query.php
13
http://www.oracle-base.com 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. How can that help me?How can that help me? –In combination with the TABLE function, they can be used in the FROM clause of a query like a regular table. tf_test.sql tf_test.sql For regular table functions, the collection must be defined using database OBJECT types.For regular table functions, the collection must be defined 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.
14
http://www.oracle-base.com Pipelining table functions A table function builds the entire collection before returning any data, while a pipelined table function “pipes” each row out as soon as it is created.A table function builds the entire collection before returning any data, while a pipelined table function “pipes” each row out as soon as it is created. How does that help me?How does that help me? –It reduces first row lag… ptf_schema.sql, ptf_package.sql, ptf_query.sql ptf_schema.sqlptf_package.sqlptf_query.sql ptf_schema.sqlptf_package.sqlptf_query.sql –As the collection is never fully resident in memory, pipelining can produce a considerable memory saving. memory_usage.sql memory_usage.sql –Since 9i R2, the types used to define the pipelined table function can be defined in a package, but this method can produce management problems, so I prefer the explicit method. implicit_types.sql 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.
15
http://www.oracle-base.com Defining a Pipelined table Function web_ptf_api.sql
16
http://www.oracle-base.com Using a pipelined table function ptf_query.php
17
http://www.oracle-base.com Can we improve on this? We can combine Pipelined Table Functions and Ref Cursors. web_rc_ptf_api.sqlWe can combine Pipelined Table Functions and Ref Cursors. web_rc_ptf_api.sql web_rc_ptf_api.sql How will that affect the client code?How will that affect the client code?
18
http://www.oracle-base.com RC & PTF together (rc_ptf_query.php) rc_ptf_query.php
19
http://www.oracle-base.com What have we shown? We can use APIs to hide complexity from client application developers.We can use APIs to hide complexity from client application developers. How do we present queries from our APIs?How do we present queries from our APIs? –Ref cursors –Pipelined table functions –Views? Remember: The technology dictates will and won’t want to do with APIs, not just our ideals.Remember: The technology dictates will and won’t want to do 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…
20
http://www.oracle-base.com Web Services and XML eGov AU: Craig Thomler's personal eGovernment and Gov 2.0 thoughts and speculations from an Australian perspectiveeGov AU: Craig Thomler's personal eGovernment and Gov 2.0 thoughts and speculations from an Australian perspective Overcoming public sector hurdles to Gov 2.0 http://egovau.blogspot.com/2009/10/overcoming-public-sector-hurdles-to-gov.htmlOvercoming public sector hurdles to Gov 2.0 http://egovau.blogspot.com/2009/10/overcoming-public-sector-hurdles-to-gov.html http://egovau.blogspot.com/2009/10/overcoming-public-sector-hurdles-to-gov.html Choice quotes:Choice quotes: –"...people are generally most comfortable with the technologies they grew up with...” –"...government systems are struggling in some areas to keep up with the rate of change...” –"If our systems can't support Gov 2.0 initiatives then it is unlikely that our senior management will.”
21
http://www.oracle-base.com Web 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 curve of Java or.NET. –Oracle allows you to present existing PL/SQL code as web services with zero effort. –Oracle's web toolkit allows you to present data as XML really easily. –PL/SQL is mature, not legacy.
22
http://www.oracle-base.com XML DB Native Web Services Oracle XML DB Native Web Services present your PL/SQL as Simple Object Access Protocol (SOAP) web services.Oracle XML DB Native Web Services present your PL/SQL as 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; Optionally:Optionally: –GRANT XDB_WEBSERVICES_OVER_HTTP TO user; –GRANT XDB_WEBSERVICES_WITH_PUBLIC TO user; --? web_services_setup.sqlweb_services_setup.sqlweb_services_setup.sql XML DB auto-generates a WSDL file.XML DB auto-generates a WSDL file.WSDL We send a SOAP Request and get a SOAP Response returned.We send a SOAP Request and get a SOAP Response returned.SOAP RequestSOAP ResponseSOAP RequestSOAP Response call_ws.pl (run it)call_ws.pl (run it)call_ws.plrun itcall_ws.plrun it Physical organization of schema keeps things neat.Physical organization of schema keeps things neat.
23
Physical organization of objects http://www.oracle-base.com Schema Owner WS API Owner PL/SQL APIs Tables.NETJavaAPEX API Owner Login User
24
http://www.oracle-base.com XML 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 “schema” access to the web toolkit functionality. xml_api_setup.sqlFirst we define a Database Access Descriptor (DAD) to give a “schema” access to the web toolkit functionality. xml_api_setup.sql xml_api_setup.sql Next we define a package to generate our XML. xml_api.sql (run it)Next we define a package to generate our XML. xml_api.sql (run it) xml_api.sqlrun it xml_api.sqlrun it This is an incredibly simple way to get XML out of the database.This is an incredibly simple way to get XML out of the database. Once again, physical organization is important.Once again, physical organization is important.
25
http://www.oracle-base.com XML over HTTP (Semi-Static Data) The previous method regenerates the XML each time the URL is called.The previous method regenerates the XML each time the URL 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. xml_db_setup.sqlHow? Place it in the XML DB file system. xml_db_setup.sql xml_db_setup.sql Next we define a procedure to generate our XML. semi_static.sqlNext we define a procedure to generate our XML. semi_static.sql semi_static.sql How do we access this file? HTTP, FTP access, WebDAVHow do we access this file? HTTP, FTP access, WebDAV HTTP This can reduce resource usages.This can reduce resource usages.
26
http://www.oracle-base.com REST web services The previous method is similar to Representational State Transfer (REST) web services, with the exception of the URL.The previous method is similar to Representational State 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-ALIAS –PATH-ALIAS-PROCEDURE rest_api_setup.sqlrest_api_setup.sqlrest_api_setup.sql rest_api.sqlrest_api.sqlrest_api.sql REST DemoREST DemoREST DemoREST Demo We can easily code REST web services directly from PL/SQL.We can easily code REST web services directly from PL/SQL.
27
http://www.oracle-base.com Summary Physical organization of objectsPhysical organization of objects –Important for us to present business driven functionality. Ref cursorsRef cursors –Allows us to hide complexity from client applications. Table functionsTable functions –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. –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/workshopshttp://www.oracle-base.com/workshops
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.