(2) Introduction to PL/SQL

Advantages Main features Architecture
Tight Integration with SQL Error handling PL/SQL Engine
High Performance Blocks PL/SQL Units
High Productivity vars and consts Compilation Parameters
Portability subprograms
Scalability packages
Manegeability triggers
Support OO Programming I/O
Suppport Dev Web Apps Data Abstraction
Suppport Dev Server Pages Control Stmts
Conditional compilation
Process query result one row at a time

Advantages
Integration with SQL
- You can use all SQL data manipulation, cursor control, transaction control stmts
- You can use all SQL functions, operators, and pseudocolumns.
- PL/SQL fully supports SQL data types.(i.e. VARCHAR2 <--> VARCHAR2)
- Attributes: %TYPE and %ROWTYPE (define a var/record in PL/SQL with the type of a column/row)
- run a query and process the resultset one row at a time
- supports static SQL and dynamic SQL.

High Performance
- Reduced network traffic: Blocks of SQL stmts can be send at once and individual stmts processed by the SQL Engine in the database.
- SQL statement reuse: SQL stmts can be reused by the DB (less parsing takes place) each time the same code runs. (PL/SQL compiler turns the variables in the WHERE and VALUES clauses into bind arguments)
- Subprograms stored in executable form: Cached and shared among users. Reduced parsing/compiling. One network call might lead to many SQL stmts in the server (less network traffic).
- Optimizer: rearrange code for better performance.

Scalability
- If you can expand server power, concentrate code on the server to reduce network and use shared memory.
- Oracle Connection Manager can multiplex network connections.

Manageability
- One central copy of the program, rather than one in each client.

Support for OO Programing
- Through the use of Abstract data types.

Support for Web Applications
- can generate HTML/XML from the DB with PL/SQL
- Using PL/SQL instead of CGI Perl Scripts you can use DML statements, dynamic SQL, and cursors, and you also eliminate the overhead of forking a new CGI process to handle each HTTP request.



PL/SQL Gateway enables a Web browser to invoke a PL/SQL stored subprogram through an HTTP listener.
mod_plsql is one implementation of the PL/SQL Gateway.
It is a plug-in of Oracle HTTP Server. It enables Web browsers to send HTTP requests in the form of URLs that include parameter values to be passed to a stored subprogram.

mod_plsql mod_plsql (a) translates the URL; (b) invokes the subprogram and (c) returns the output (html) to the client.
Advantages over the embedded PL/SQL gateway (DBMS_EPG):
  • Can run in an Oracle HTTP Server in front of a firewall while the database remains behind the firewall. The embedded gateway (DBMS_EPG) cannot take this configuration. In which situations could this configuration be preferred?
  • The embedded gateway does not support mod_plsql features such as dynamic HTML caching, system monitoring, and logging in the Common Log Format.
Embedded PL/SQL Gateway Runs in the XML DB HTTP Listener in the DB.
Provides core features of mod_plsql.
Configured with the DBMS_EPG package in the PL/SQL Web Toolkit.
Does not require the HTTP Server powered by Apache.

For a browser to have access to a PL/SQL app: (1) a Database Access Descriptor (DAD) must be created and (2) mapped to a virtual path.
DAD: set of configuration values used for database access.
DAD: represented as a servlet in XML DB HTTP Server.
Virtual path mapping: makes the application accessible under a virtual path of the XML DB HTTP Server.

SQL Web Toolkit PL/SQL Web Toolkit is a set of PL/SQL packages that provides a generic interface to use stored subprograms invoked by mod_plsql at run time.

Web Toolkit API enables stored subprograms to perform actions such as:
  • Obtain information about an HTTP request
  • Generate HTTP headers such as content-type and mime-type
  • Set browser cookies
  • Generate HTML pages

Support for the Development of Server Pages

PL/SQL Server Pages (PSP) are server-side scripts that include dynamic content, including the results of SQL queries, inside web pages. You can author the web pages in an HTML authoring tool and insert blocks of PL/SQL code.

Simple Server Page. Example extracted from the Oracle manual.
To compile and load the server page, use the loadpsp utility:


Why use PSP?
  • Easier way to create web pages that include DB-generated content. Use your preferred tool to develop web pages and embed PL/SQL.
  • Writing out HTML content line by line: PSP is more convenient than HTP and HTF packages.
  • Client does not need to process any special script tags besides plain HTML. All processing is done on the DB server. Supports all browsers.
  • Less network traffic. Less round-trips to the DB.
  • Follow a rapid, iterative development process. Central control of the software. Client needs only a browser.


Main Features

Blocks, Subprograms and other Constructs
Oracle supports two types of PL/SQL Blocks: named and anonymous.
named
  • Can be stored in the DB (procedures, funtions, packages)
  • May be dynamically created, allocated a name and executed, but not stored in the DB.
anonymous
  • Can be dynamically created and executed, but cannot be stored in the database.
  • Are treated as single SQL statements
  • Are compiled each time they are loaded into memory.
  • Compilation stages: Syntax checking (syntax check, parse tree gen) => Semantic check (Type checking; parse tree refined) => Code generation

(a) Categories of constructs that can be created at the server side:
  • Anonymous blocks – PL/SQL blocks without a name. Not stored in the DB. Run and gone.
  • Stored Procedures and Functions – Named PL/SQL blocks also known as subprograms
  • Stored Packages – PL/SQL constructs that are used to group related objects
  • Database Triggers – PL/SQL blocks associated with a table, a view, a schema, or the database
  • Object Types - (Abstract Data Types - ADTs) – User-Defined composite data types

<< label >> (optional)
DECLARE -- Declarative part (optional)
-- Declare local types, vars, & subprograms

BEGIN -- Executable part (required)
-- At least one statement
-- You may use a NULL stmt: (BEGIN NULL; END;)

[EXCEPTION -- Exception-handling part (optional)
-- handlers for exceptions raised in exec part]
END;
A block is defined by the keywords DECLARE, BEGIN, EXCEPTION, and END. It can have a label.

(b) In the client side (Forms, Developer, etc), you can create constructs:
  • Anonymous blocks
  • Application Procedures and Functions
  • Application Packages
  • Application Triggers
  • Object Types (Abstract Data Types - ADTs)


named block
CREATE OR REPLACE <block type> <block name>
IS | AS -- Mandatory
. . .
-- Declarative section
-- (Type, Name[P|F], Parameters, RETURN)
BEGIN
. . .
-- Executable section
EXCEPTION
. . . –- Exception-handling section (optional)
END;
-- Marks the end of the block

<block type> can be:
Procedure, Function, Package, Trigger

Why use Subprograms? (Benefits)
Performance

Integrity/Security

Maintenance

Clarity

(a) Improved Performance
  • Multiple statements sent to the Server in a group.=> Reduced network traffic.
  • Subprgrams (named) are compiled once (when created).: Avoid re-parsing at execution time.
  • Compiled code placed into the shared pool, thus shared by multiple sessions.

(b) Data Security and Integrity
  • Inherit the privileges of the owner by default. (Control indirect access to objects)
  • Atomicity: Either all statements complete, or none will.

(c) Easy to Maintain
  • Subprograms are easily shared by multiple apps.
  • change in the DB Objects require change on the subprogram only.

(d) Increased code clarity
  • More readability: each section of the subprogram block is defined separately.
    (i.e., all exceptions are handled in the exception-handling section).


Input/Output
Besides the use of SQL Statements, PL/SQL has also a number of I/O-processing packages:

DBMS_OUTPUT Lets PL/SQL blocks, subprograms, packages, and triggers display output.
Especially useful for displaying PL/SQL debugging information
HTF Has hypertext functions that generate HTML tags (i.e., HTF.ANCHOR => anchor tag ).
HTP Has hypertext procedures that generate HTML tags.
DBMS_PIPE Lets two or more sessions in the same instance communicate.
UTL_FILE Lets PL/SQL programs read and write OS files.
UTL_HTTP Lets PL/SQL programs make HTTP callouts, and access data on the Internet over HTTP.
UTL_SMTP Sends electronic mails (emails) over Simple Mail Transfer Protocol (SMTP) as specified by RFC821.

Data Abstraction
Allows you to access the essential property of data without getting too involved with details.
Abstraction mechanisms include:
Cursors, Composite Variables, %ROWTYPE attribute, %TYPE attribute, Abstract Data Types



Cursors
Pointer to a private SQL Area that keeps information about a specific SQL stmt or PL/SQL SELECT INTO statement.

Composite Variables
Have internal components, which can be access individually. Can be passed as parameters.
Two kinds of composite variables: collections and records.
Collections
  • The internal components are always of the same data type, and are called elements. You access each element by its unique index.
  • Lists and arrays are classic examples of collections.
Records
  • Here the internal components can be of different data types, and are called fields.
  • You access each field by its name.
  • A record variable can hold a table row, or some columns from a table row.
  • Struct (in C); RECORD (in Pascal)


%ROWTYPE attribute and %TYPE attribute
The %ROWTYPE attribute is used to declare a record that represents a FULL row of a database table.


The %ROWTYPE can also be used to represent a PARTIAL row (using a Cursor).

The record fields do not inherit the constraints or initial values of the corresponding columns


With the %TYPE attribute you can declare a data item of the same data type as a previously declared variable or column (without knowing what that type is).

If the declaration of the referenced item changes, then the declaration of the referencing item changes accordingly.

referencing item inherits from the referenced item: (a) datatype and size; (b) constraints (unless the referenced item is a column)

Referencing item does not inherits from the referenced item: (a) the initial value of the referenced item.

Abstract Data Types (ADTs) - Creating OO Classes
  • Composed of data structure (attributes) + subprograms that manipulate the data (methods).
  • Stored in the database. Instances of ADTs can be stored in tables and used as PL/SQL variables.
  • Reduce complexity: separate a large system into logical, reusable, components.
Check the static dictionary views:

DBA_OBJECTS:
OBJECT_TYPE='TYPE'
DBA_TYPES:
TYPECODE='OBJECT'

Control Statements
  • Conditional selection: IF THEN [ELSE || ELSIF]; CASE
  • Loop: LOOP, FOR, Cursor FOR, WHILE [EXIT || EXIT WHEN; CONTINUE || CONTINUE WHEN]
  • Sequential control: GOTO, NULL


Conditional Compilation
Customize functionality of a PL/SQL application. Compiler recognizes db versions and enable/disable features properly.
Recognize development/production environment: activate debugging/tracing properly.

Processing a Query Result Set One Row at a Time
You can issue a SQL query and process the rows of the result set one at a time.

You can use a basic loop, or you can control the process precisely by using a cursor to run the query, retrieve the results, and finish processing.


Architecture
PL/SQL Engine
Compilation and run-time system that is present in the Server and/or in an app dev tool.

PL/SQL Units

  • Anonymous Block
  • Function
  • Procedure
  • Trigger
  • Library
  • Package
  • Package Body
  • Type
  • Type Body

PL/SQL compilation parameters
Category of DB Init parameters.
The compile-time values of the parameters are stored with the metadata of each stored PL/SQL unit, which means that you can reuse those values when you explicitly recompile the unit.
To explicitly recompile a stored PL/SQL unit and reuse its parameter values, you must use an ALTER statement with both the COMPILE clause and the REUSE SETTINGS clause

PLSCOPE_SETTINGS Controls compile-time collection, cross-reference, and storage of PL/SQL source text identifier data.
PLSCOPE_SETTINGS = IDENTIFIERS:{ NONE | ALL }
PLSQL_CCFLAGS Enables you to control conditional compilation of each PL/SQL unit independently.
PLQL_CODE_TYPE Defines compilation mode for PL/SQL libary units.
PLSQL_CODE_TYPE = { INTERPRETED | NATIVE }
PLSQL_OPTIMIZE_LEVEL The higher the setting of this parameter, the more effort the compiler makes to optimize PL/SQL library units.
Range: {0,3}
PLSQL_WARNINGS enables or disables the reporting of warning messages by the PL/SQL compiler, and specifies which warning messages to show as errors.
NLS_LENGTH_SEMANTICS