1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Oracle Unleashed- P18 ppt

50 200 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Oracle Precompilers
Trường học University
Chuyên ngành Database Systems / Oracle
Thể loại Document
Định dạng
Số trang 50
Dung lượng 108,77 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

This notifies the precompiler that embedded SQL is included and the program must be translated precompiled into the host language format.. Executable Statements Executable statements ar

Trang 1

■ Reducing Oracle Communication Overhead

■ Reducing Processing Overhead

■ New Features in Version 1.4

Oracle Precompilers

The precompiler is a mechanism that allows SQL statements to be embedded within procedural languages This tool provides the programmer with unlimited capabilities To fully understand all the benefits of this tool, this chapter will focus on why we precompile, general features of the precompiler, what languages source code can be written in, how standardized are the precompilers, what options do they have, how to precompile your source code, and what is

conditional precompiling

Why Precompile Source Code?

Embedded SQL statements are not something a high-level language understands The precompiler must therefore take the SQL statements and translate them into something the high-level language will understand Libraries contain data structures that help guide the translation process for consistency This general concept will help in understanding the precompiler features

General Precompiler Features

One of the primary reasons to use a precompiler is to have the capability of utilizing SQL statements in a high-level language Oracle precompilers offer many benefits and features that can help in this development These features include the following:

● Source code can be written in six different high-level languages, which all have ANSI/ISO precompilers

● Using a precomplier enables a user to take advantage of dynamic SQL, which allows for a more highly

Trang 2

● Precompilers provide variables to handle warning and error conditions, which are included in the ORACLE Communication Area (ORACA)

Each of these features will be discussed in further detail later in the chapter But you should know how many

precompilers Oracle has before too much more detail is given

ANSI/IO Standards

Because SQL has become the standard language for relational databases, standardization has become an important issue Oracle precompilers have taken exceptional efforts to meet standards set by the following organizations:

● American National Standards Institute (ANSI)

● International Standards Organization (ISO)

● U.S National Institute of Standards and Technology (NIST)

Compliance for these requirements is measured by NIST, which uses over 300 test programs Oracle precompilers have conformed to these standards by checking the SQL statements that are being embedded for standard syntax and

semantics But there is much more to precompiler function than syntax checking

Precompiler Options

To increase flexibility among applications, Oracle precompilers have options that can be manipulated by the programmer

to allow for certain conditions For example, if a line of the source program exceeds 80 characters, an error occurs while precompiling There is a precompiler option that allows the programmer to extend that line to 132 characters These controls, along with others, are outlined in Table 50.1 The * character next to an item indicates that it can be entered inline

Table 50.1 Precompiler options.

Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

Trang 3

Syntax Default Specifies

COMMON_NAME=block_name Name of FORTRAN common blocks

DEFINE=symbol Symbol used in conditional precompilation

ERRORS=YES|No* YES Whether errors are sent to the terminal

FORMAT=ANSI|TERMINAL ANSI Format of COBOL or FORTRAN input line

HOLD_CURSOR=YES|NO* NO How a cursor cache handles SQL statements

INAME=path and filename Name of input file

INCLUDE=path * Directory path for the INCLUDEd files

IRECLEN=integer 80 Record length of input file

LINES=YES|NO NO Whether C #line directives are generated

LITDELIM=APOST

QUOTE* QUOTE Delimiter for COBOL strings

LNAME=path and filename Name of listing file

Trang 4

LRECLEN=integer 132 Record length of listing file

LTYPE=LONG

SHORT

MAXLITERAL=integer* Maximum length of string

MAXOPENCURSORS=integer* 10 Maximum number of cursors cached

ISO14 ORACLE Compliance with ANSI/ISO standard

ONAME=path and filename Name of output file

ORACA=YES|NO NO Whether the ORACA is used

ORECLEN=integer 80 Record length of output file

PAGELEN=integer 66 Lines per page in listing

RELEASE_CURSOR=YES|NO* NO How cursor cache handles SQL statements

SELECT_ERROR=YES|NO* YES How SELECT errors are handled

SQLCHECK=SEMANTICS

Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

Trang 5

SYNTAX

LIMITED

NONE * SYNTAX Extent of syntax and/or semantic checking

USERID=username/password Valid Oracle username and password

XREF=YES|NO* YES Cross-reference section in listing

A precompiler command line might look something like the following:

proc iname=example.pc include=ora_pcc: include=clib: ireclen=132

There is only one required argument, INAME This argument tells the precompiler what the input filename is So the minimum requirements for any precompiler command line could look like the following:

proc iname=example.pc

All of these precompiler options can be accessed online Just enter the precompiler option without any argument at your operating system prompt This help feature will display the name, syntax, default value and purpose of each option There are some general guidelines to follow when setting options for precompiling:

● When you precompile the program module that CONNECTs to Oracle, specify a value for

MAXOPENCURSORS that is high enough for any of the program modules that will be linked together

● If you want to use just one SQLCA, you must declare it as a global in one of the program modules and as an external in the other modules In C, for example, this is done by using the external storage class, which tells the precompiler to look for the SQLCA in another program module Unless you declare the SQLCA as external, each program module will use its own local SQLCA

You cannot DECLARE a cursor in one file and reference it in another

All these options can be utilized in several ways when precompiling your program, but what is the best way to

precompile, compile, and link your source program?

How to Precompile a Program

In Version 1.4 of the precompilers, executables exists for each of the six languages previously mentioned So each host language has a different command to run the precompiler Table 50.2 shows which command to use for a specific language

Table 50.2 Precompiler commands.

Trang 6

Host Language Precompiler Command

@PCL TEST TEST100 PCL

Control Filename: @PCL

System Name: TEST

Host Program: TEST100

precompiling, compiling, and linking host programs

$ write sys$output "PCL.COM Version 2.3"

Trang 7

$ assign $disk7:[vaxprod.com.ccom],$disk7:[vaxprod.'P1'.c'P1'] clib

$ if "''P1'" eqs "" then goto USAGE

$ if "''P2'" eqs "" then goto USAGE

$!

$ if "''P3'" eqs "" then goto precompile

$ if "''P3'" eqs "P" then goto precompile

$ if "''P3'" eqs "p" then goto precompile

$ if "''P3'" eqs "PC" then goto precompile

$ if "''P3'" eqs "pc" then goto precompile

$ if "''P3'" eqs "PCL" then goto precompile

$ if "''P3'" eqs "pcl" then goto precompile

$ if "''P3'" eqs "PCLR" then goto precompile

$ if "''P3'" eqs "pclr" then goto precompile

$!

$ if "''P3'" eqs "c" then goto compile

$ if "''P3'" eqs "C" then goto compile

$ if "''P3'" eqs "cl" then goto compile

$ if "''P3'" eqs "CL" then goto compile

$ if "''P3'" eqs "clr" then goto compile

$ if "''P3'" eqs "CLR" then goto compile

$!

$ if "''P3'" eqs "l" then goto link

$ if "''P3'" eqs "L" then goto link

$ if "''P3'" eqs "lr" then goto link

$ if "''P3'" eqs "LR" then goto link

$!

$ if "''P3'" eqs "r" then goto run

$ if "''P3'" eqs "R" then goto run

$ goto USAGE

Trang 8

$ if "''P3'" eqs "p" then goto continue

$ if "''P3'" eqs "P" then goto continue

$ if "''P3'" eqs "pc" then goto continue

$ if "''P3'" eqs "PC" then goto continue

$ if "''P3'" eqs "c" then goto continue

$ if "''P3'" eqs "C" then goto continue

Trang 9

$ @ora_rdbms:loutl 'P2'

'P2''P4',clib:c'P1'.olb/lib,clib:ccom.olb/lib-,ora_util:sqllib.olb/lib,ora_rdbms:oci/lib/include=(ocicee) 'P2' s

$!

$ endlink:

$ if "''P3'" eqs "PCL" then goto continue

$ if "''P3'" eqs "pcl" then goto continue

$ if "''P3'" eqs "cl" then goto continue

$ if "''P3'" eqs "CL" then goto continue

$ if "''P3'" eqs "l" then goto continue

$ if "''P3'" eqs "L" then goto continue

Trang 10

$ write sys$output " "

$ write sys$output " "

$ write sys$output "Usage: @PCL [SYSTEM] [PROGRAM NAME] [options] [d] [libinfo]

$ write sys$output " "

$ write sys$output "Where: APPLICATION - Application system: (TEST, etc)

$ write sys$output " Used to link the system library (ie.LTEST.OLB/LIB)

$ write sys$output " PROGRAM NAME - (TEST100, TEST200, etc)

$ write sys$output " options - Options (PCLR): P = precompile

$ write sys$output " C = compile

$ write sys$output " L = link

$ write sys$output " R = run

$ write sys$output " PCLR = all the above (default)

$ write sys$output " Options entered must appear in above order

$ write sys$output " valid: P PC C CL PCL not valid:

$ write sys$output "Example: @PCL TEST TEST100 PCL

$ write sys$output " (compile TEST100.C, nodebug, links TEST200.OBJ

Trang 11

statements that define the environment and what actions to take In this section, procedural and embedded SQL

statements can be used to perform platform specific operations

Remember that conditional statements must be included in your source code, not the control file that will compile your code

The following Oracle statements are utilized when creating a conditional section

EXEC ORACLE DEFINE symbol Define a symbol

EXEC ORACLE IFDEF symbol If symbol is defined

EXEC ORACLE IFNDEF symbol If symbol is not defined

EXEC ORACLE ELSE Otherwise

EXEC ORACLE ENDIF

End this control block

Some symbols are port-specific and predefined for you when the Oracle precompilers are installed Predefined system symbols include CMS, MVS, DOS, UNIX and VMS In the following example, conditional precompiling is shown using a predefined symbol

operating-#include <stdio.h>

#include <string.h>

#include <stdlib.h>

Trang 13

Embedded SQL Host Program

Creating a host program that utilizes embedded SQL statements can be very beneficial There are some specific

guidelines and requirements that must be included in an embedded SQL program This section of the chapter focuses on the basics of a precompiled program, program requirements, handling errors, host arrays, dynamic SQL, user exits, and performance tuning As each topic is introduced, code examples will be provided in C for that section which will build into a completed program by chapters end You then can precompile, compile, link, and execute it This section begins with some basic concepts

Basics of a Host Program

Three basic concepts must be discussed to ensure an understanding of the material that will be presented later in this chapter These concepts are naming conventions, embedded SQL, and using PL/SQL blocks

Naming Conventions

All embedded SQL programs must have the appropriate extension for the host language This notifies the precompiler that embedded SQL is included and the program must be translated (precompiled) into the host language format Table 50.3 indicates what extension should be used for the host language

Table 50.3 Host language file extensions.

Host Language Standard File Extension

Trang 14

When you modify your source code, remember to change the original file In C, for example, the PC file should

be modified and not the C file; otherwise your changes will not go into effect

The second basic concept, which is the focus of these programs, is embedded SQL statements

Embedded SQL

Embedded SQL refers to SQL statements that are placed within an application program The program itself is referred to

as the host program All standard SQL commands can be used in the embedded statement There are two types of embedded SQL statements: executable and declarative

Any SQL command can be embedded within a high-level language, but not the extended SQL commands that are included in SQL*PLUS These include report formatting, editing SQL statements, and environment-setting

commands

Executable Statements

Executable statements are used to connect to Oracle; query, manipulate and control access to Oracle data; and to process data These statements result in calls to and return codes from Oracle There are four types of executable embedded SQL statements: data definition, data control, data manipulation and data retrieval Later in the chapter each of these

statements will be discussed in further detail

Declarative Statements

Declarative statements are used to declare Oracle objects, communication areas, and SQL variables These statements do not result in Oracle calls or return codes and do not operate on Oracle data Now that you know there are two sections involved in writing an embedded SQL host program, we should look at exactly what is required

Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

Trang 15

The last concept is utilizing PL/SQL blocks The next section will cover how PL/SQL blocks are included into your source program

Including PL/SQL Blocks

To include PL/SQL in your host program, you need to first DECLARE the host variables that you want to use in the PL/SQL block Next, you need to bracket the SQL statement that will be included in the PL/SQL blocks with the keywords EXEC SQL EXECUTE and END-EXEC The following code example shows how a PL/SQL block is incorporated into your program

Trang 16

printf("\nSuccessfully connected to Oracle.");

EXEC SQL COMMIT WORK RELEASE;

Trang 17

SELECT JOB, HIREDATE, SAL

INTO :job_title, :hire_date, :salary

If you are working with a system that is database intensive, utilizing PL/SQL blocks will improve performance

Host Program Requirements

Being able to pass data between Oracle and your application requires a variety of tasks to be completed successfully There are two primary parts of an embedded SQL program that must be included: the data declaration area (which involves the declarative statements) and the data manipulation area (which involves the executable statements) Before you can execute anything in your program, you must make your program aware of the players

Data Declaration Area

This area is used to define all host variables, include extra files, and establish a connection to the database Sometimes

this area is referred to as the program prologue There are three required sections within the data declaration area: the

DECLARE section, SQL Include section, and the SQL connect area

Declare Section

All host language variables referenced in a SQL statement must be declared to Oracle; otherwise, an error message will

be issued at precompile time These variables are declared in the DECLARE section Most host languages will allow multiple DECLARE sections per precompiled unit, but you must have at least one These sections can be defined locally

or globally Host variables within this section can be of any length, but only the first 31 characters are evaluated These variables can consist of letters, digits, and underscores, but they must begin with an alpha character To store a datatype, Oracle must know the format and valid range of values Oracle recognizes only two kinds of datatypes: internal and external Internal datatypes indicate how Oracle will store the data, and external specifies how the data is stored in the host variable Table 50.4 shows the internal datatypes

Table 50.4 Internal datatypes.

Name Code Description

CHAR 1 < 255-byte, fixed-length character string

Trang 18

NUMBER 2 fixed or floating point number

LONG 8 < 65535-byte, fixed-length character string

ROWID 11 fixed-length binary number

DATE 12 7-byte, fixed-length date/time value

RAW 23 < 255-byte, fixed-length binary data

LONGRAW 24

< 65535-byte, fixed-length binary data

Table 50.5 shows the external datatypes

Table 50.5 External datatypes.

Name Code Description

VARCHAR2 1 < 255-byte, fixed-length character string

CHAR 1 < 255-byte, fixed-length character string

NUMBER 2 fixed or floating point number

INTEGER 3 2-byte or 4-byte signed integer

FLOAT 4 4-byte or 8-byte floating-point number

STRING 5 null-terminated character string

VARNUM 6 variable-length binary number

DECIMAL 7 COBOL or PL/I packed decimal

LONG 8 < 65535-byte, fixed-length character string

VARCHAR 9 < 65535-byte, fixed-length character string

Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

Trang 19

ROWID 11 fixed-length binary number

DATE 12 7-byte, fixed-length date/time value

VARRAW 15 < 65533-byte, fixed-length binary data

RAW 23 < 255-byte, fixed-length binary data

LONGRAW 24 < 65535-byte, fixed-length binary data

UNSIGNED 68 2-byte or 4-byte unsigned integer

DISPLAY 91

COBOL numeric-character data

It is important that the host variables (external) within the DECLARE section match the database datatype (internal) For example, if you declare a host variable such as receipt_date a character string and the database has it declared as a DATE type, you will receive an error The following guidelines are recommended when declaring and referencing host

variables A host variable must be

● explicitly declared in the DECLARE section

● referenced with a colon(:) in all SQL statements and PL/SQL blocks

● of a datatype supported by the host language

● of a datatype compatible with that of its source or target database column

A host variable must not be

● subscripted

● prefixed with a colon in the host language statement

● used to identify a column, table, or other Oracle object

● used in data definition statements such as ALTER, CREATE, and DROP

A host variable can be

● used anywhere an expression can be used in a SQL statement

At precompile time, an association is made between variables declared and the database column type If there is a discrepancy, a runtime error will occur; otherwise, the datatype is converted VARCHAR variables are converted into the following structure:

struct {

unsigned short len;

Trang 20

unsigned char arr[20];

} username;

This structure helps eliminate character-counting algorithms You can reference each element of the structure and

manipulate it The following example shows the syntax for the DECLARE section and how the elements of a

VARCHAR can be used

printf("/n What is your User ID: ");

scanf("%s",user_id.arr); /* referencing the character

string element */

user_id.len = strlen(user_id.arr); /* referencing the length element

*/

}

After declaring a variety of host variables communication between the host program and Oracle needs to be established

to monitor successes and failures This communication is made by utilizing the SQL Include area

SQL Include Area

This section of the program enables the user to include copies of files into the host program Any file can be included if

it contains embedded SQL statements The most common include file is the SQL Communication Area file (SQLCA)

When MODE=ORACLE (the default for the precompiler) or MODE=ANSI13, you must declare the SQLCA by hardcoding it, or by copying it into your program with the INCLUDE statement If MODE=ANSI14

declaring the SQLCA is optional, however, you must declare the status variable SQLCODE

The SQLCA is a data structure that handles certain events and provides diagnostic checking between the Oracle RDMBS and the host program SQLCA variables maintain valuable runtime statistics such as: warning codes with text, Oracle error codes and number of rows processed are convenient for handling special conditions within the host program The following examples shows the syntax for including this file in the host program

declare section

Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

Trang 22

printf("\nSuccessfully connected to Oracle.");

EXEC SQL COMMIT WORK RELEASE;

Data Manipulation Area

The data manipulation area is where SQL statements are executed This section is often referred to as the program body

This section of the chapter focuses on the types of SQL statements, the logical unit of work, controlling transactions, locking data, and the EXIT command

Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

Trang 23

Types of Executable SQL Statements

Several different types of SQL statements that can be executed in the data manipulation area: data manipulation

statements, data definition statements and data control statements

Data manipulation (DML) statements are used to change the data The following is a list of commands that are

considered DML statements:

● UPDATE column values in existing rows

● DELETE rows from a table

● COMMIT WORK writes data to the table

● ROLLBACK WORK removes any changes made to the data

● LOCK reserves the row or table exclusively for the user

Data definition (DDL) statements are used to define and maintain database objects Some common uses of the DDL statements are to create tables or views The following is a list of commands that are considered DDL:

● CREATE TABLE

● CREATE VIEW

● ALTER table or view

● DROP table, view, grant, or sequence number

Data control (DCL) statements are used to access tables and the data associated with them There are two types of access that these statements will control The first type is connecting to the database; the CONNECT and GRANT commands enable a user to do this The second type of control is access to the data; the GRANT SELECT and REVOKE DELETE commands are examples of this

Pulling together a combination of all three of these statements creates what is known as a logical unit of work

Logical Unit of Work

A logical unit of work is defined as a group of SQL statements treated as a single transaction to the Oracle kernal This unit of work begins with any valid SQL DML statement and ends with either an implicit or explicit release of work An implicit commit release is performed by the execution of any DDL statement, whereas an implicit rollback release is performed upon abnormal termination of your program A program may explicitly release the logical unit of work, which is discussed in the section "Controlling Transactions."

Controlling Transactions

Because Oracle is transaction oriented and processes information in logical units of work, controlling these statements is essential to data integrity A transaction begins with the first EXEC SQL statement issued in your program When one transaction ends, the next begins You can end a transaction in one of two means: COMMIT or ROLLBACK If you do not subdivide your program with a COMMIT or ROLLBACK statement, Oracle will treat the whole program as one transaction

Trang 24

Remember to commit what you are not willing to recreate Losing one large transaction could be dangerous; smaller transaction losses are easier to recover from

To make changes to the database permanent, use the COMMIT command The COMMIT command does the following:

● Makes permanent all changes to the databases during the current transactions

● Makes these changes visible to other users

● Erases all savepoints

● Releases all row and table locks, but not parse locks

● Closes cursors referenced in a CURRENT OF clause

● Ends the transaction

The COMMIT statements has no effect on the values of host variables or on the flow of control in your program This statement should be placed in the main path through your program The following example shows the syntax of the COMMIT command:

EXEC SQL COMMIT WORK RELEASE;

The RELEASE option on COMMIT or ROLLBACK releases all process resources and provides a clean exit from Oracle After the release, there is no further access to the Oracle database until another connect is issued

An explicit COMMIT or ROLLBACK with RELEASE should always be done at the end of your program If the RELEASE option is not specified, any locks or resources obtained will not be released until Oracle recognizes that the process is no longer active

The keyword WORK provides ANSI compatibility, whereas the optional RELEASE parameter frees all Oracle resources

To undo pending changes, use the ROLLBACK statement This statement lets you return to the starting point, so the database is not left in an inconsistent state The ROLLBACK statement does the following:

● Undoes all changes made to the database during the current transaction

● Erases all savepoints

● Ends the transaction

● Releases all row and table locks, but not parse locks

● Closes cursors referenced in a CURRENT OF clause

Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

Trang 25

The ROLLBACK statement has no effect on the values of host variables or on the flow of control in your program The following example shows the syntax for the ROLLBACK statement

EXEC SQL ROLLBACK WORK RELEASE;

The ROLLBACK statement is useful when you accidentally delete rows from the table Do a ROLLBACK before the COMMIT

Committing and releasing changes that have been made to the database gives the program enormous control over data, but what happens when someone else executes a program and it tries to manipulate the same data? Oracle has provided a mechanism that enables you to reserve the data just for your use

following guidelines should be followed when using locks:

● Unless specified, any DML operation will acquire a table-level lock

● A DML lock can be bypassed by explicitly requesting a row-level lock See the following code example: /* LOCKS ALL ROWS */

EXEC SQL LOCK TABLE STUDENT

IN SHARE UPDATE MODE[NOWAIT];

/* IN THE FOLLOWING SELECT STATE THE [NOWAIT] */

/* PARAMETER TELLS ORACLE NOT TO WAIT FOR THE TABLE IF IT HAS BEEN */

/*LOCKED BY ANOTHER USER */

EXEC SQL SELECT FNMAE

FROM STUDENT

WHERE BIRTHDATE < '01-JAN-60'

FOR UPDATE OF LNAME[NOWAIT];

Exit Command

The last statement in a Pro*C program should be the EXIT command This command has the option of returning a status check—especially useful when used as a SQL*Forms user exit Table 50.6 shows the return code

Ngày đăng: 15/12/2013, 05:15

TỪ KHÓA LIÊN QUAN

w