SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

18
SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006

Transcript of SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

Page 1: SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

SQL in a Programming Environment

CIS 4301

Lecture Notes

Lecture 22 - 4/11/2006

Page 2: SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

Lecture 22© CIS 4301 - Spring 2006 2

SQL in a Program

You're starting by using "direct" (or "interactive") SQL

In reality SQL is rarely used in this way, instead it's specified within programs

Host language (e.g., ADA, C, Cobol, Fortran, M, Pascal)

Some of the steps are actually SQL statements Entire program sent to preprocessor, changes the embedded SQL into something that makes sense in host language

Pre-processed program is then compiled in usual manner

DBMS vendor normally provides library that supplies necessary function definitions

Call-level interface (CLI)

Page 3: SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

Lecture 22© CIS 4301 - Spring 2006 3

Processing Programs with SQL Statements

Host Language+

Embedded SQL

Preprocessor

Host Language+

Function Calls

Host-languagecompiler

Object-codeprogram

SQL library

Page 4: SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

Lecture 22© CIS 4301 - Spring 2006 4

Impedance Mismatch Why not use a single language

SQL? Conventional language?

Many things SQL cannot do… Example? Also, conventional progr. language not suitable

for letting humans write database operations that can be executed efficiently… Why not?

Need both, but must deal with disconnect between the data models used

Rel. model uses sets, not naturally supported by programming languages

Programming lang. support pointers, loops, branches, not available in SQL

Passing data back and forth between SQL and other prog. languages is not straightforward

Page 5: SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

Lecture 22© CIS 4301 - Spring 2006 5

SQL/Host Language Interface

How are values passed from the program into SQL commands?

How are results of SQL commands returned into program variables?

How do we deal with set-valued (i.e., relation-valued) results?

Shared variables Variables of the host languages DECLARE Section Identify in program by preceding variable with colon

Special variable SQLSTATE connects the host language with the SQL execution system

‘00000’ no error ‘02000’ no tuple found

Page 6: SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

Lecture 22© CIS 4301 - Spring 2006 6

The Declare Section

EXEC SQL BEGIN DECLARE SECTION;char studioName[50], studioAddr[256];

char SQLSTATE[6];

EXEC SQL END DECLARE SECTION;

Page 7: SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

Lecture 22© CIS 4301 - Spring 2006 7

Embedded SQL

SQL statements that do not return a result can be embedded directly into a host language using EXEC SQL … syntaxEXEC SQL INSERT INTO Studio (name, address)

VALUES (:studioName, :studioAddress);

Page 8: SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

Lecture 22© CIS 4301 - Spring 2006 8

Sample C Function

void getStudio() {EXEC SQL BEGIN DECLARE SECTION;

char studioName[50], studioAddr[256];char SQLSTATE[6];

EXEC SQL END DECLARE SECTION;

/* code for handling user input… */

EXEC SQL INSERT INTO STUDIO(name, address)VALUES (:studioName, :studioAddr);

}

Page 9: SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

Lecture 22© CIS 4301 - Spring 2006 9

Embedded SQL

So far, statements return no results No impedance mismatch – EASY

SFW queries must use one of two mechanisms Single-row select using shared variables to hold components of single tuple being returned

Queries producing more than one tuple must declare cursor

Page 10: SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

Lecture 22© CIS 4301 - Spring 2006 10

Single-Row Select Statement

Void printNetWorth() {EXEC SQL BEGIN DECLARE SECTION;

char studioName[50];int presNetWorth;char SQLSTATE[6];

EXEC SQL END DECLARE SECTION;

/* print request that studio name be entered, read response into studioName */

EXEC SQL SELECT netWorthINTO :presNetWorthFROM Studio, MovieExecWHERE presC# = cert#

AND Studio.name = :studioName;

/* print value of presNetWorth */}

Declaration of shared vars

Page 11: SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

Lecture 22© CIS 4301 - Spring 2006 11

Cursors For set-valued results: associate handle (cursor) with query, get tuples in result one at a time through handle

Example: Print names and GPAs of all students who applied to Santa Cruz

Step 1: declare cursor

EXEC BEGIN DECLARE SECTION;char n[30];float g;

EXEC END DECLARE SECTION;EXEC DECLARE Scinfo CURSOR FOR (SELECT DISTINCT name, GPA FROM Student, Apply WHERE Student.ID = Apply.ID AND Apply.location = "Santa Cruz");

Page 12: SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

Lecture 22© CIS 4301 - Spring 2006 12

General Cursor Declaration

EXEC SQL DECLARE <cursor> CURSOR FOR <query>

Page 13: SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

Lecture 22© CIS 4301 - Spring 2006 13

Using a Cursor

Step 2: run query using iterationEXEC SQL OPEN SCinfo; initializes cursor

EXEC SQL FETCH FROM SCinfo INTO :n, :g;WHILE (MORE_TUPLES) Print(:n, :g); EXEC SQL FETCH SCinfo INTO :n,:g;ENDWHILE;EXEC SQL CLOSE SCinfo; closes cursor

#define MORE_TUPLES !(strcmp(SQLSTATE,”02000”))

Page 14: SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

Lecture 22© CIS 4301 - Spring 2006 14

General Fetch Statement

EXEC SQL FETCH FROM <cursor> INTO <list of variables>

Page 15: SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

Lecture 22© CIS 4301 - Spring 2006 15

Other Uses of Cursor

Cursor can be used to modify or delete tuples See example in FCDBS

Page 16: SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

Lecture 22© CIS 4301 - Spring 2006 16

Scrolling Cursors Default movement through cursor is forward, one tuple at a time

Option to move in different directions

When declaring cursor, declare cursor as scrollable

Use FETCH in conjunction with direction keyword NEXT or PRIOR FIRST or LAST RELATIVE ABSOLUTE

Page 17: SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

Lecture 22© CIS 4301 - Spring 2006 17

Dynamic SQL So far, model was to embed specific SQL queries and commands within host-language program

Alternatively, can have host language compute and assemble SQL statements Since not known at compile time, cannot be handled by SQL preprocessor or host-language compiler

Dynamic SQL Example?

Host-language program must instruct SQL system to take char string, turn it into executable SQL statement, and finally to execute that statement

Page 18: SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

Lecture 22© CIS 4301 - Spring 2006 18

A very simple sqlplus

Void vssqlplus () {EXEC SQL BEGIN DECLARE SECTION;char *query;

EXEC SQL END DECLARE SECTION;

/* prompt user for a query, allocate space and make shared variable :query point to first char of query */

EXEC SQL PREPARE SQLquery FROM :query;EXEC SQL EXECUTE SQLquery;/* code for displaying result to user */}

EXEC SQL EXECUTE IMMEDIATE :query;