40
Programming Using Pro *C Oracle Day 4

Programming Using Pro C

Embed Size (px)

Citation preview

Page 1: Programming Using Pro C

Programming Using Pro *C Oracle Day 4

Page 2: Programming Using Pro C

2Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Objectives

Introduction to Oracle Pre-compilers

Embedded SQL

Pro *C

Page 3: Programming Using Pro C

3Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Pre-compilers• Pre-compilers are tools that allow you to embed SQL statements in the HLL

(High Level Language) source code.

• Pre-compilers accept SQL statements , translate the SQL statements into runtime calls , generate a source code that can be compiled, linked and executed.

Page 4: Programming Using Pro C

4Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Oracle Pre-compilers• Oracle supports pre-compilers for the following HLL,

• C

• COBOL

• Fortran

• Pascal

• PL/I

• Ada

• Oracle supports all this on different platforms like UNIX,DOS, NETWARE,WINDOWS,VAX,SUN... etc

Page 5: Programming Using Pro C

5Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Advantages of Pre-compilers• Write applications using High level programming language

• Embed SQL in HLL programs.

• Automatically convert datatypes– between those supported by Oracle and the Programming Language.

• Transalate SQL queries into appropriate programming language code automatically

• Handle errors and warnings – using the SQLCA (SQL Communication Area) – Explained further

• Separate pre-compilation of the program modules and then linking them together.

Page 6: Programming Using Pro C

6Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Embedded SQL- Definition

• SQL statements within a programming language called Embedded SQL statements.

• The source code containing the embedded SQL is called Host program.

• Embedded SQL statements in the source code begin with EXEC SQL

• Example:

EXEC SQL INSERT INTO branch (bcode,location)

VALUES (:br_code.:br_location);

Page 7: Programming Using Pro C

7Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Embedded SQL Program Dev – The big picture

Embedded SQL Program

Editor

ORACLE Pre-compiler

Translated Source Program

Compiler

Object Program

Linker

Executable Program

ORACLE Run time Library(SQLLIB)

Resolve callsSQL statements replaced by Library calls

Page 8: Programming Using Pro C

8Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Embedded SQL- Some Basic Concepts

• Embedded SQL statements

– Executable

– Declarative

• Host, Indicator and pointer variables

• Context areas, cursors and active sets

• Transactions

• Errors and Warnings

Page 9: Programming Using Pro C

9Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Executable Statements• Result in calls and return codes to Oracle.

• They are used to connect to the ORACLE database, define, query, manipulate and control access to the Oracle Database.

• Example:

– EXEC SQL insert into branch values(:brcode,:br_location,:br_mgr);

Page 10: Programming Using Pro C

10Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Declarative Statements • All statements that allow you to declare variables(host variables) used in SQL

statements , Communication areas and Oracle objects.

• Following keywords are used for the respective job:

• DECLARE for Oracle objects

• INCLUDE for communication areas

• WHENEVER for error handling

• The Declare section statements are needed when the precompiler MODE=ANSI, if MODE=ORACLE then you may omit the Declare section statements.

Page 11: Programming Using Pro C

11Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Example: Using DECLARE

EXEC SQL BEGIN DECLARE SECTION;

int emp_no;

char emp_name[30];

float emp_sales;

short ind_sales;

EXEC SQL END DECLARE SECTION;

Page 12: Programming Using Pro C

12Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Example: Using INCLUDE

EXEC SQL INCLUDE sqlca;

• SQLCA is the SQL communication area between Oracle and your Pro *C program (Explained in further slides)

• SQLCA is a structure (Structure is given in the appendix slides)

Page 13: Programming Using Pro C

13Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Host variables• Host variables allow communication between Oracle and your program.

• Host variables must be declared using the host language data types and rules.

– Must be prefixed with a colon in SQL statements

– Data types must be supported by host language.

• A host variable must not be

– Used as an array subscript

– be prefixed with colon in host language statements.

Page 14: Programming Using Pro C

14Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Host variables

• Host variables are of two types

– Input

Program assigns values to input host variables and passes data to ORACLE

– Output

ORACLE assigns values to output host variables and passes data to program

Page 15: Programming Using Pro C

15Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Indicator Variables

• Every host variable can be associated with an indicator variable.

• Used for

– assigning null values to input host variables.

– Detect null or truncated values in output host variables.

• They must be

– declared in the declare section.

– Prefixed with a colon(:) in the SQL statement.

• They cannot be

– used in the WHERE clause of an SQL statement

Page 16: Programming Using Pro C

16Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Indicator variables• Indicator variables can be used to monitor host variables as follows:

• For input host variables, indicator variables with a value-

-1 Oracle assigns NULL value to the column.

>=0 Oracle assigns value of host variables

• For output host variables, Oracle assigns the indicator variables with a value -

-1 column value is NULL

0 Oracle assigns column value to host

>0 Oracle assigns truncated value to host

Page 17: Programming Using Pro C

17Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Example: Using Host and Indicator variables

main(){

printf(“Enter the employee number”);

scanf(“%d”,&emp_no);

// usage of host and indicator variables

EXEC SQL select name,ytdsales into :emp_name , :emp_sales:ind_sales from salesrep where empno = :empno;

if (ind_sales == -1)

printf(“No sales achieved by %s”,emp_name);

else

printf(“sales achieved ny %s is %d”,emp_name,emp_sales);

}

Demo Host & Indicator variables

Page 18: Programming Using Pro C

18Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

• WHENEVER statement is used to do automatic checking and error handling

• Syntax:

EXEC SQL WHENEVER <condition> <action>;

<condition>

{ [SQLWARNING] |

[SQLERROR] |

[NOT FOUND]

}

<action>

{ [CONTINUE] |

[DO function_call() | break ]

[goto statement_label] |

[STOP]

}

• The scope of the WHENEVER is positional, not logical.

WHENEVER...

Page 19: Programming Using Pro C

19Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

WHENEVER - Actions• CONTINUE

– Continue with next statement if possible

• DO {<function>|break}

– Control transferred to function, at end of routine - control returns to statement following the failed SQL statement

– break : Will break from the loop in which the failed SQL statement is present and transfer control to statement following loop

• GOTO <label>

– Control transferred to labeled statement

• STOP

– Execution of Program stops, uncommitted work rolled back

Page 20: Programming Using Pro C

20Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

WHENEVER - Examplemain()

{

//Some piece of code

EXEC SQL DECLARE emp_cursor FOR SELECT empno,ename FROM emp;

EXEC SQL OPEN emp_cursor;

EXEC SQL WHENEVER NOT FOUND DO break;

WHILE(1)

{

EXEC SQL FETCH EMP_CURSOR INTO :EMP_NUMBER,:EMP_NAME;

...

}

EXEC SQL CLOSE EMP_CURSOR;

}

Page 21: Programming Using Pro C

21Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Summary

• The Pro *C pre-compiler

• SQLCA

• Host and Indicator variables

• Using embedded SQL statements

• Handling errors

• Using embedded PL/SQL

Page 22: Programming Using Pro C

Appendix A – More about Pro C

Page 23: Programming Using Pro C

23Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Pointer variables

• C supports pointers, which point to other variables. One can define pointers as host variables in the declare section of your program.

EXEC SQL BEGIN DECLARE SECTION;

int *int_ptr;

char *char_ptr;

EXEC SQL END DECLARE SECTION;

• When using in SQL statements you must prefix the pointer variable with a colon(:).

EXEC SQL SELECT intcol INTO :int_ptr FROM......

Page 24: Programming Using Pro C

24Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Host arrays

•When a collection of related items is declared as a host variable it is called a host array.

EXEC BEGIN DECLARE SECTION;

char emp_name[20][20];

int salary[20];

EXEC SQL END DECLARE SECTION;

•Likewise a collection of indicator variables is an indicator array.

Page 25: Programming Using Pro C

25Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Host arrays

• Host array of pointers are not allowed.

• Multidimensional host arrays are also not allowed with the exception of char data type.

• When used in SQL statements host arrays must not be subscripted.

Demo Host Arrays

Page 26: Programming Using Pro C

26Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

VARCHAR Variables• VARCHAR is a pre-declared structure.

• Example:

EXEC SQL BEGIN DECLARE SECTION;

varchar username[20];

EXEC SQL END DECLARE SECTION;

translates to:

struct{

unsigned short len;

unsigned char arr[20];

} username;

Page 27: Programming Using Pro C

27Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

VARCHAR variables (contd.)

• Before passing an input VARCHAR variable to ORACLE, always assign the length member to a valid length.

• Example:

varchar userName[20];

strcpy((char *)userName.arr, “dummy”);

userName.len = strlen((char *)userName.arr);

Page 28: Programming Using Pro C

28Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

• After obtaining a value into an Output host variable of type VARCHAR, always terminate the variable value with string terminator(‘\0’)

• Example:

exec sql

select Name,Salary,Comm into :empName:empName_ind,

:salary:salary_ind,:commission:comm_ind

from employee where EmpNo =:empNo;

empName.arr[empName.len]='\0';

VARCHAR variables (contd.)

Page 29: Programming Using Pro C

29Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Referencing VARCHAR variables• In SQL statements: struct name preceded by colon

• In C statements: struct name.member name

• Example:

select name into :empName FROM employee where empNo = 1001;

empName.arr[empName.len] = ‘\0’;

printf(“\nName : %s “,empName.arr);

Demo varchar

Page 30: Programming Using Pro C

30Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Context Areas,Cursors & Active sets

• Work area where SQL statements are processed are called context areas/Private SQL Area

• Cursors are defined to process multiple rows.

– Implicit Cursor: Implicitly declared by ORACLE for all DDL and DML statements

– Explicit Cursor: Declared by programmer

• The returned set of rows is called the active set.

Page 31: Programming Using Pro C

31Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Example-Using Cursors

• The cursor control statements DECLARE, OPEN, FETCH and CLOSE allow a user to manipulate the contents of the cursor.

Demo Cursors

Page 32: Programming Using Pro C

32Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Transactions

• Transactions are a set of logically related SQL statements.

• All statements executed between a COMMIT and ROLLBACK comprise a transaction

• Complete execution of a DDL statement is also treated as one complete transaction.

• Example:

EXEC SQL COMMIT WORK RELEASE;

EXEC SQL ROLLBACK WORK RELEASE;

• The keyword WORK RELEASE releases the ORACLE connection

Page 33: Programming Using Pro C

33Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Errors and Warnings

• Generated whenever an SQL statement fails.

• Oracle pre-compilers provide a method to handle and check error statuses using the

• SQLCA (SQL Communication Area)

– Updated after every executable SQL statement

– Contains errors,warning and status information

• WHENEVER (explained further)

– To determine the outcome one can check the variables in the SQLCA explicitly or implicitly with the WHENEVER statement.

• ORACA (Oracle Communication Area)

Page 34: Programming Using Pro C

34Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

PL/SQL in Pro*C

• The Pro*C pre-compiler treats a PL/SQL block as though it were single embedded SQL statement.

• One can place a PL/SQL block anywhere in the host language where one can place a SQL statement.

Page 35: Programming Using Pro C

35Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

PL/SQL in Pro*C

• To embed a PL/SQL block include the PL/SQL block in the keywords EXEC SQL EXECUTE and END-EXEC

• Example:

EXEC SQL EXECUTE

BEGIN

SELECT ename,salary INTO :emp_name,:emp_sal

FROM emp WHERE deptno = :emp.deptno;

-- Processing logic

EXCEPTION

--handlers

END;

END-EXEC;

Page 36: Programming Using Pro C

Appendix B – SQLCA structure

Page 37: Programming Using Pro C

37Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

SQLCA Structure

struct sqlca {

char sqlcaid[8];

long sqlcabc;

long sqlcode;

struct {

unsigned short sqlerrml;

char sqlerrmc[70];

} sqlerrm;

char sqlerrp[8];

long sqlerrd[6];

char sqlwarn[8];

char sqlext[8];

};

Page 38: Programming Using Pro C

38Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

• sqlcaid - is a character string initialized to ‘SQLCA’ when allocated

• sqlcabc - is a long integer set to the length in bytes of the SQLCA structure itself.

• sqlcode - is a long integer which summarizes the result of a SQL statement. =0 - succesful execution

>0 - successful execution with a status code ( eg . 1403 for ‘no row found)

<0 - error in program or system failure

• sqlerrm.sqlerrml - length of SQL error message.

• sqlerrm.sqlerrmc - text of error found

• sqlerrd - array of long to describe the internal state of ORACLE RDBMS. The third element is used to indicate the number of rows fetched by the DML operations such as INSERT or UPDATE.

SQLCA Structure explanation-1/2

Page 39: Programming Using Pro C

39Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

• sqlwarn - consists of eight single character elements to indicate various warns.

[0] - is set to ‘W’ if warnings are set for the particular statement else

set to blank.

[1] - is set to ‘W’ if one or more character fields are set truncated.

[2] - is set to ‘W’ if one or more null values are ignored in comput-

ation of a function.

[3] - is set to ‘W’ if number of columns selected does not equal to

host variables specified in the INTO clause.

[4] - is set to ‘W’ to signify that UPDATE and DELETE have been

specified without a where clause.

[5] - CREATE failed because of PL/SQL compilation error

[6] - not used.

[7] - not used

• sqlext - currently unused.

SQLCA Structure explanation-2/2

Page 40: Programming Using Pro C

40Copyright © 2005, Infosys

Technologies LtdER/CORP/CRS/DB25/003

Version No. 2.0

Thank You!