SlideShare a Scribd company logo
Advanced
Oracle PL/SQL
A CASE STUDY
BY ZIEMOWIT JANKOWSKI, DATABASE ARCHITECT
Purpose of this presentation
To show the power of PL/SQL as general programming
system embedded into a database:
● Calling built-in java in Oracle database from PL/SQL
● Performing tasks outside of database with PL/SQL
● Consolidation of different data sources into one single
SQL query
The original task
An Oracle-based system receives data from different
external systems. Task: for management and error
tracking purposes present a SQL query that will query
many different systems for a specific item. That query can
be mapped into a web page or used directly from TOAD.
Some other pre-conditions exist:
● Two of the external systems are accessible via web
pages only.
● A few of the external systems can be offline at times.
● The usage of the query should be logged and logging
should be hard to bypass.
Limitations and assumptions
● Architectural limitation:
● Do not introduce new inter-database dependencies if no
absolutely necessary
● Accessibility limitation:
● The external systems that expose HTTP interface cannot
be accessed in any other way
● The external systems that expose database access may
be online or offline. (Timeout issues)
● Assumptions:
● All involved systems run some flavour of Unix/Linux
● The delivered amount of data is small enough to reside
in memory
Solution outline
● Only well-known technologies used
● “Everything” implemented in PL/SQL
● Database queries
● HTTP queries
● Queries to other databases
● Using embedded java to execute O/S commands
● Running O/S commands to access remote databases
via SQL Plus
● Using pipelined functions to build a dataset for all data
Pipelined functions
● Gather data from data sources into an internal dataset
in PL/SQL package
● Table of record
● Traverse table and for every row execute PIPE ROW
● Caller needs to execute statement:
SELECT <…>
from TABLE(<pipelined-function>(<params>))
<where clause, order by clause etc>
Pros and cons
● Pros:
● Full control over returned data
● Data acquisition totally obfuscated for caller
● Data can be returned from several disparate sources
with one SQL statement
● Cons:
● Some (small) overhead
● TABLE (…) is not very common construct
Example
Code:
Function SelectAll (
itemID IN varchar2
)
return ItemSearchListTab pipelined as
<…some code…>
retidx := returnTab.first;
while retidx is not null loop
pipe row(returnTab(retidx));
retidx := returnTab.next(retidx);
end loop;
SELECT statement:
Select * from table(packageName.SelectAll(’someID’)) order by 1;
Using embedded java
● Execute O/S commands:
● Runs as user Oracle in O/S
● Harvest the results into varchar2 variables in code
● Can be used to:
● List contents of directories
● Move and remove files
● Send strings to applications or other systems and
harvest results, in particular:
● Send HTTP queries over network
● Run SQL*Plus and execute SQL scripts on external systems
Pros and Cons
● Pros:
● The impossible becomes possible
● Vast opportunities of extending SQL functionality
● Cons:
● Can be misused if left unprotected
Example
HTTP Queries
● Use standard Oracle package UTL_HTTP
● Harvest result as string
● Linefeed as line delimiter
● Limitations of result to varchar2 size
● Analyze HTTP output string and fill a PL/SQL table with
results
● Return resulting PL/SQL table as function value
Example
HTMLArray := UTL_HTTP.REQUEST_PIECES (searchURL, 20000);
if HTMLArray.count > 0 then
for HTMLLine in HTMLArray.first .. HTMLArray.last loop
tempString := tempString || HTMLArray(HTMLLine);
end loop;
<…some code…>
return returnTab;
Queries to other databases
● No inter-database dependencies allowed:
● No database links
● Use external Unix/Linux command to run SQL*Plus with
script
● No changes in local TNSNAMES.ORA
● Use full connection strings
● Harvest output strings into PL/SQL tables
● Unknown connectivity of remote database at given
time:
● Risk for hanging SQL queries
● Use native Unix/Linux function to probe remote node
Example – check
connectivity
-- status 0 means port OK, status 1 means port not
available
out_text := os_cmd.run_cmd('nc -w 1
'||connections(cidx).IPAddress||' '||connections(cidx).port||' ;
echo $?');
Example – run SQL
command
● Connect string:
'(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=<IP>)(PORT=<P>)))(CON
NECT_DATA=(SERVICE_NAME=<SVC>)))‘
● Executed code:
connectString := Replace(connectString,'<IP>',connections(cidx).IPAddress);
connectString := Replace(connectString,'<P>',connections(cidx).port);
connectString := Replace(connectString,'<SVC>',connections(cidx).serviceName);
<…some code…>
out_text := os_cmd.run_cmd('$ORACLE_HOME/bin/sqlplus -S -L '||
connections(cidx).userNme||'/'||connections(cidx).passwrd||
'@"'||connections(cidx).connectString||
'“ @'||gblTempDir||gblFilePrefix||fileNamePart2||'.sql');
Log queries
● Use Oracle autonomous transactions
● Does not interfere with ”ordinary” operations
Wrapping it up
● SQL results, remote dataase queries (not necessarily
Oracle), HTTP calls – all combined into one SQL
statement
● All searches logged
● Not to mention additional functionality:
● Passwords, entered in plain text, stored coded in
dataase
● Passwords retrieved in plain text or verifyed, but only
through code with logging enabled
Demonstration

More Related Content

PDF
Java features. Java 8, 9, 10, 11
PDF
Core Java Programming Language (JSE) : Chapter XIII - JDBC
PPT
oracle plsql training | oracle online training | oracle plsql demo | oracle p...
PPTX
Sqlmap
PPTX
PPT
SQLMAP Tool Usage - A Heads Up
PPT
Oracle data pump
PPT
Sedna XML Database: Query Parser & Optimizing Rewriter
Java features. Java 8, 9, 10, 11
Core Java Programming Language (JSE) : Chapter XIII - JDBC
oracle plsql training | oracle online training | oracle plsql demo | oracle p...
Sqlmap
SQLMAP Tool Usage - A Heads Up
Oracle data pump
Sedna XML Database: Query Parser & Optimizing Rewriter

What's hot (20)

PPTX
Introduction to PL/SQL
PPTX
PLSQL Advanced
PPTX
Data pump-export-examples
PPT
Architecture of Native XML Database Sedna
PPT
Sedna XML Database System: Internal Representation
DOCX
Udemy talend notes
PPTX
ORACLE PL SQL FOR BEGINNERS
PDF
PPTX
Oracle: Procedures
PPT
Changing platforms of Oracle database
DOC
3963066 pl-sql-notes-only
PPTX
PLSQL Tutorial
PPTX
Oracle: PLSQL Introduction
PDF
Improving the Performance of PL/SQL function calls from SQL
PPT
ORACLE PL SQL
PPT
PPT
XQuery Triggers in Native XML Database Sedna
PPTX
PL/SQL Fundamentals I
PPTX
4. plsql
Introduction to PL/SQL
PLSQL Advanced
Data pump-export-examples
Architecture of Native XML Database Sedna
Sedna XML Database System: Internal Representation
Udemy talend notes
ORACLE PL SQL FOR BEGINNERS
Oracle: Procedures
Changing platforms of Oracle database
3963066 pl-sql-notes-only
PLSQL Tutorial
Oracle: PLSQL Introduction
Improving the Performance of PL/SQL function calls from SQL
ORACLE PL SQL
XQuery Triggers in Native XML Database Sedna
PL/SQL Fundamentals I
4. plsql
Ad

Viewers also liked (19)

PDF
book escape room nashville
PDF
Regent Knowledge Centre
PPTX
How Email as a Service Can Remove Your Operation Pain
PPTX
mengapa peran guru tidak tergantikan oleh teknologi
DOCX
new world
PPTX
Herman Henselmann
PDF
Generalidades de tic_y_la_formacion_docente
PPT
DN11_U3_A5_PYNR
PDF
Telehealth Accreditation: Adding Value to Your Organization Through Independe...
PPTX
Digital Researcher at Higher Education
PPTX
theories of communication
PDF
Las TIC y la Formación Docente
PPTX
Bla Bla Car
PPT
TSRT Crashes
PDF
JADE Intrapreneurship Conference, Romania
PPS
Rebirth of the Eagle - Story of Eagle Photo Presentation
PPT
Core java concepts
PPT
Core java slides
PDF
Generalidades acerca de las tics y la formacion docente
book escape room nashville
Regent Knowledge Centre
How Email as a Service Can Remove Your Operation Pain
mengapa peran guru tidak tergantikan oleh teknologi
new world
Herman Henselmann
Generalidades de tic_y_la_formacion_docente
DN11_U3_A5_PYNR
Telehealth Accreditation: Adding Value to Your Organization Through Independe...
Digital Researcher at Higher Education
theories of communication
Las TIC y la Formación Docente
Bla Bla Car
TSRT Crashes
JADE Intrapreneurship Conference, Romania
Rebirth of the Eagle - Story of Eagle Photo Presentation
Core java concepts
Core java slides
Generalidades acerca de las tics y la formacion docente
Ad

Similar to Case_Study_-_Advanced_Oracle_PLSQL (20)

PPTX
Is SQLcl the Next Generation of SQL*Plus?
PPT
Store programs
PPTX
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
PDF
Properly Use Parallel DML for ETL
PPTX
Optimizing your Database Import!
PDF
Apache airflow
ODP
Polling Techniques, Ajax, protocol Switching from Http to Websocket standard ...
PDF
SQLcl the next generation of SQLPlus?
PDF
Perl Stored Procedures for MySQL (2009)
PPTX
Introduction to Structured Query Language
PPTX
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
PPTX
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
PPT
Introduction to PLSQL.PPT
PPTX
Oracle Database 12c - New Features for Developers and DBAs
PPTX
Oracle Database 12c - New Features for Developers and DBAs
PPTX
PostgreSQL Database Slides
PPTX
Java 8 streams
PDF
[Altibase] 9 replication part2 (methods and controls)
PPT
Introduction to Threading in .Net
PPS
Procedures/functions of rdbms
Is SQLcl the Next Generation of SQL*Plus?
Store programs
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Properly Use Parallel DML for ETL
Optimizing your Database Import!
Apache airflow
Polling Techniques, Ajax, protocol Switching from Http to Websocket standard ...
SQLcl the next generation of SQLPlus?
Perl Stored Procedures for MySQL (2009)
Introduction to Structured Query Language
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
Introduction to PLSQL.PPT
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAs
PostgreSQL Database Slides
Java 8 streams
[Altibase] 9 replication part2 (methods and controls)
Introduction to Threading in .Net
Procedures/functions of rdbms

Case_Study_-_Advanced_Oracle_PLSQL

  • 1. Advanced Oracle PL/SQL A CASE STUDY BY ZIEMOWIT JANKOWSKI, DATABASE ARCHITECT
  • 2. Purpose of this presentation To show the power of PL/SQL as general programming system embedded into a database: ● Calling built-in java in Oracle database from PL/SQL ● Performing tasks outside of database with PL/SQL ● Consolidation of different data sources into one single SQL query
  • 3. The original task An Oracle-based system receives data from different external systems. Task: for management and error tracking purposes present a SQL query that will query many different systems for a specific item. That query can be mapped into a web page or used directly from TOAD. Some other pre-conditions exist: ● Two of the external systems are accessible via web pages only. ● A few of the external systems can be offline at times. ● The usage of the query should be logged and logging should be hard to bypass.
  • 4. Limitations and assumptions ● Architectural limitation: ● Do not introduce new inter-database dependencies if no absolutely necessary ● Accessibility limitation: ● The external systems that expose HTTP interface cannot be accessed in any other way ● The external systems that expose database access may be online or offline. (Timeout issues) ● Assumptions: ● All involved systems run some flavour of Unix/Linux ● The delivered amount of data is small enough to reside in memory
  • 5. Solution outline ● Only well-known technologies used ● “Everything” implemented in PL/SQL ● Database queries ● HTTP queries ● Queries to other databases ● Using embedded java to execute O/S commands ● Running O/S commands to access remote databases via SQL Plus ● Using pipelined functions to build a dataset for all data
  • 6. Pipelined functions ● Gather data from data sources into an internal dataset in PL/SQL package ● Table of record ● Traverse table and for every row execute PIPE ROW ● Caller needs to execute statement: SELECT <…> from TABLE(<pipelined-function>(<params>)) <where clause, order by clause etc>
  • 7. Pros and cons ● Pros: ● Full control over returned data ● Data acquisition totally obfuscated for caller ● Data can be returned from several disparate sources with one SQL statement ● Cons: ● Some (small) overhead ● TABLE (…) is not very common construct
  • 8. Example Code: Function SelectAll ( itemID IN varchar2 ) return ItemSearchListTab pipelined as <…some code…> retidx := returnTab.first; while retidx is not null loop pipe row(returnTab(retidx)); retidx := returnTab.next(retidx); end loop; SELECT statement: Select * from table(packageName.SelectAll(’someID’)) order by 1;
  • 9. Using embedded java ● Execute O/S commands: ● Runs as user Oracle in O/S ● Harvest the results into varchar2 variables in code ● Can be used to: ● List contents of directories ● Move and remove files ● Send strings to applications or other systems and harvest results, in particular: ● Send HTTP queries over network ● Run SQL*Plus and execute SQL scripts on external systems
  • 10. Pros and Cons ● Pros: ● The impossible becomes possible ● Vast opportunities of extending SQL functionality ● Cons: ● Can be misused if left unprotected
  • 12. HTTP Queries ● Use standard Oracle package UTL_HTTP ● Harvest result as string ● Linefeed as line delimiter ● Limitations of result to varchar2 size ● Analyze HTTP output string and fill a PL/SQL table with results ● Return resulting PL/SQL table as function value
  • 13. Example HTMLArray := UTL_HTTP.REQUEST_PIECES (searchURL, 20000); if HTMLArray.count > 0 then for HTMLLine in HTMLArray.first .. HTMLArray.last loop tempString := tempString || HTMLArray(HTMLLine); end loop; <…some code…> return returnTab;
  • 14. Queries to other databases ● No inter-database dependencies allowed: ● No database links ● Use external Unix/Linux command to run SQL*Plus with script ● No changes in local TNSNAMES.ORA ● Use full connection strings ● Harvest output strings into PL/SQL tables ● Unknown connectivity of remote database at given time: ● Risk for hanging SQL queries ● Use native Unix/Linux function to probe remote node
  • 15. Example – check connectivity -- status 0 means port OK, status 1 means port not available out_text := os_cmd.run_cmd('nc -w 1 '||connections(cidx).IPAddress||' '||connections(cidx).port||' ; echo $?');
  • 16. Example – run SQL command ● Connect string: '(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=<IP>)(PORT=<P>)))(CON NECT_DATA=(SERVICE_NAME=<SVC>)))‘ ● Executed code: connectString := Replace(connectString,'<IP>',connections(cidx).IPAddress); connectString := Replace(connectString,'<P>',connections(cidx).port); connectString := Replace(connectString,'<SVC>',connections(cidx).serviceName); <…some code…> out_text := os_cmd.run_cmd('$ORACLE_HOME/bin/sqlplus -S -L '|| connections(cidx).userNme||'/'||connections(cidx).passwrd|| '@"'||connections(cidx).connectString|| '“ @'||gblTempDir||gblFilePrefix||fileNamePart2||'.sql');
  • 17. Log queries ● Use Oracle autonomous transactions ● Does not interfere with ”ordinary” operations
  • 18. Wrapping it up ● SQL results, remote dataase queries (not necessarily Oracle), HTTP calls – all combined into one SQL statement ● All searches logged ● Not to mention additional functionality: ● Passwords, entered in plain text, stored coded in dataase ● Passwords retrieved in plain text or verifyed, but only through code with logging enabled