sql/psm. persistent, stored modules ansi/iso 1996: sql/psm (or psm or psm-96) –also part of...

77
SQL/PSM

Upload: bryce-oliver

Post on 26-Dec-2015

241 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

SQL/PSM

Page 2: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Persistent, Stored Modules

• ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96)– also part of SQL:2003

• Write procedures /functions and store them in the DB.– Part of the DB schema– Can be used in SQL queries or other statements

• Often called stored procedures in commercial DBMS

Page 3: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Creating Functions/Procedures

• Procedure declarationCREATE PRECEDURE name (parameters) local declarations procedure body;

• Function declarationCREATE FUNCTION name (parameters)RETURNS type local declarations function body;

• Module: collection of function/procedure definitions, temporary relation declarations and others.

Page 4: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Parameters

mode name type

• Procedure parameters– mode: IN (default), OUT, INOUT

• Function parameters– mode: IN

Forbid side-effectsOutput information through return-value

Page 5: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Example

CREATE PROCEDURE Move (

IN oldAddr VARCHAR[255],

IN newAddr VARCHAR[255]

)

UPDATE MovieStar

SET address = newAddr

WHERE address = oldAddr;– Note the difference with embedded SQL

Page 6: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Statements in PSM

• Call-statementCALL proc-name (argument list);

• CALL is used:1. From host-language program:

EXEC SQL CALL Foo(:x, 3);

2. As a statement of another PSM procedure/function

3. As a SQL command issued to the generic SQL interface

Page 7: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Statements in PSM (cont.)

• Return-statementRETURN expression;– Only appear in a function.– Set the return-value of the function.– It does NOT terminate the function!

Control continues with the following statementPossible that the return-value will be changed

before the function completes.

Page 8: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Statements in PSM (cont.)

• Local variablesDECLARE name type;– Local: values not preserved after running the

procedure/function– Declared before executable statement

Page 9: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Statements in PSM (cont.)

• Assignment statementSET variable = expression;– NULL is a permissible expression– The expression may be a query, as long as it

returns a single value

Page 10: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Statements in PSM (cont.)

• Statement groupBEGIN

......

END;– Treated as a single statement.– Can appear anywhere a single statement can.

Example: proc/function body is expected to be a single statement.

Page 11: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Statements in PSM (cont.)

• Statement labelslabel:statement– Used in a loop statement, allowing leaving the

loop.

Page 12: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Statements in PSM (cont.)

• Branching statementsIF condition THEN

statement list

ELSEIF condition THEN

statement list

ELSEIF ......

ELSE

statement list

END IF;

• Notes– condition: like that in

WHERE

– statement list: not need a surrounding BEGIN...END

– Final ELSE is optional

Page 13: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Statements in PSM (cont.)

• Queries in PSM

1. Subqueries can be used anywhere a subquery is legal in SQL, e.g., in conditions.

2. Queries that return a single value can be used as the right sides of assignment statements.

3. A single-row SELECT is a legal statement in PSM.– With INTO local variables / parameters

4. Cursors are used as in embedded SQL

Page 14: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Example

CREATE FUNCTION BW(y INT, s CHAR[15]) RETURNS BOOLEAN

IF NOT EXISTS(

SELECT * FROM Movie

WHERE year = y AND studioName = s)

THEN RETURN TRUE;

ELSEIF 1 <= (SELECT COUNT(*) FROM Movie

WHERE year = y AND studioName = s AND NOT inColor)

THEN RETURN TRUE;

ELSE RETURN FALSE;

END IF;

Page 15: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Example

CREATE PROCEDURE SomeProc(IN sName CHAR[10])

DECLARE total INTEGER;

SELECT SUM(grade)

INTO total

FROM Student, SC

WHERE Student.sno = SC.sno AND Student.name = sName;

Or:

SET total = (SELECT SUM(grade) INTO total FROM Student, SC

WHERE Student.sno = SC.sno AND Student.name = sName);

Page 16: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Statements in PSM (cont.)

• LOOP statement

LOOP

statement list

END LOOP;– If LOOP statement has a label, we can break out of the

loop using a statement:

LEAVE loop-label;– Loop usually involves a cursor.

Page 17: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Example

CREATE PROCEDURE MeanVar(

IN s CHAR[15], OUT mean REAL, OUT variance REAL

)

DECLARE Not_Found CONDITION FOR SQLSTATE ‘02000’;

DECLARE MovieCursor CURSOR FOR

SELECT length FROM Movie WHERE studioName = s;

DECLARE newLength INTEGER;

DECLARE movieCount INTEGER;

BEGIN

SET mean = 0.0;

SET variance = 0.0;

Page 18: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Example (cont.)

SET movieCount = 0;

OPEN MovieCursor;

movieLoop: LOOP

FETCH MovieCursor INTO newLength;

IF Not_Found THEN LEAVE movieLoop END IF;

SET movieCount = movieCount + 1;

SET mean = mean + newLength;

SET variance = variance + newLength * newLength;

END LOOP;

SET mean = mean / movieCount;

SET variance = variance / movieCount - mean * mean;

CLOSE MovieCursor;

END;

Page 19: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Statements in PSM (cont.)

• FOR-Loops FOR loop-name AS csr-name CURSOR FOR

query

DO

statement list

END FOR;– Used only to iterate over a cursor.

– FOR-loop handles all details of cursor usage.

– Attributes are also treated as local variables.

Page 20: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Example

CREATE PROCEDURE MeanVar(

IN s CHAR[15], OUT mean REAL, OUT variance REAL

)

DECLARE movieCount INTEGER;

BEGIN

SET mean = 0.0;

SET variance = 0.0;

SET movieCount = 0;

Page 21: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Example (cont.)

FOR MovieLoop AS MovieCursor CURSOR FOR

SELECT length FROM Movie WHERE studioName = s;

DO

SET movieCount = movieCount + 1;

SET mesn = mean + length;

SET variance = variance + length * length;

END FOR;

SET mean = mean / movieCount;

SET variance = variance / movieCount - mean * mean;

END;– MovieLoop and MovieCursor are not used, why invent them?

Page 22: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Statements in PSM (cont.)

• WHILE-Loops WHILE condition DO

statement list

END WHILE;

• REPEAT-Loops REPEAT

statement list

UNTIL condition

END REPEAT;

Page 23: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Exceptions in PSM

• Exception handler: invoked whenever error codes appears in SQLSTATE during the execution of a statement.

• DeclarationDECLARE where-to-go-next

HANDLER FOR condition-list

statement

Page 24: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Exceptions in PSM (cont.)

• where-to-go-next choices:1. CONTINUE:execute the statement after the one that

raised the exception.

2. EXIT: leave the BEGIN...END block in which the handler is declared and execute the statement after this block.

3. UNDO: Same as EXIT, but undo any changes to DB or local variables made in this block.

Page 25: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Example

CREATE FUNCTION GetYear (t VARCHAR[255]) RETURNS INTEGER

DECLARE Not_Found CONDITION FOR SQLSTATE ‘02000’;

DECLARE Too_Many CONDITION FOR SQLSTATE ‘02100’;

BEGIN

DECLARE EXIT HANDLER FOR Not_Found, Too_Many

RETURN NULL;

RETURN (SELECT year FROM Movie WHERE title = t);

END;

Page 26: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

Using Functions/Procedures

• embedded SQL, PSM, generic interface• Example: call the function GetYear( ) from the

generic interface.INSERT INTO StarsIn(movieTitle,movieYear,starName)

VALUES(‘Remember the Titans’,

GetYear(‘Remember the Titans’),

‘Denzel Washington’);

Page 27: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

SQL/Host-Language Interface

SQL/CLI

JDBC

PHP

Page 28: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

28

Review

Host language + Embedded SQL

DBMS Preprocessors

Host language + Function calls

Host language compiler

Object code

SQL Library

API

Page 29: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

29

API

• Using API, we can write host-language code to access DB.– SQL statements as string parameters of functions in a

standard library.

– We gain system independence, compared with the preprocessor of embedded SQL.

• Examples of API– C + SQL/CLI (adaptation of ODBC)

– Java + JDBC

– PHP + PEAR/DB

– Others: OLE-DB, ADO, ADO.NET, …

Page 30: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

SQL/CLI

Page 31: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

31

Introduction

• Instead of using a preprocessor (as in embedded SQL), we can use a library of functions.– The library for C is called SQL/CLI

#include sqlcli.h

– Embedded SQL’s preprocessor will translate the EXEC SQL … statements into CLI or similar calls, anyway.

Page 32: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

32

Data Structures

• Create four types of handles to deal with:– Environments: SQLHENV

– Connections: SQLHDBC

– Statements: SQLHSTMT

– Descriptions: SQLHDESC

• Use SQLAllocHandle(T,I,O)– T = type.

– I = input handle = next higher levelstatement < connection < environment

– O = (address of) output handle.

Page 33: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

33

Hierarchy

Environment1 Environment2

Connection1

Connection2Connection2

Connection1

Statement1

Statement2

Page 34: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

34

Environments

• Created in preparation for one or more connections to the DB server.SQLAllocHandle(SQL_HANDLE_ENV,

SQL_NULL_HANDLE,

&myEnv);

Page 35: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

35

Connections

• Created to connect to the DB server, within some environment.SQLAllocHandle(SQL_HANDLE_DBC,

myEnv,

&myCon);

Page 36: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

36

Connections (cont.)

• Connect to the DB serverSQLConnect(myCon,

sqlserver, SQL_NTS,

userid, SQL_NTS,

pword, SQL_NTS);

Page 37: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

37

Statements

• Created to pass SQL statements to the DB server, within some connection.SQLAllocHandle(SQL_HANDLE_STMT,

myCon,

&myStmt);

Page 38: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

38

Example

#include sqlcli.h

SQLHENV myEnv;

SQLHDBC myCon;

SQLHSTMT myStmt;

SQLRETURN errCode1, errCode2, errCode3;

errCode1 = SQLAllocHandle(SQL_HANDLE_ENV,

SQL_NULL_HANDLE, &myEnv);

if(!errCode1)

errCode2 = SQLAllocHandle(SQL_HANDLE_DBC,myEnv,&myCon);

if(!errCode2)

errCode3 = SQLAllocHandle(SQL_HANDLE_STMT,myCon,&myStmt);

Page 39: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

39

Processing Statements

• Preparing an SQL statement

SQLPrepare(sh, st, sl);– sh: statement handle– st: a pointer to an SQL statement– sl: length for the string pointed by st. Use

SQL_NTS to indicate “null-terminating string”• Executing the SQL statement

SQLExecute(sh);

Page 40: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

40

Example

SQLPrepare(myStmt,

”SELECT age FROM Student”, SQL_NTS);

SQLExecute(myStmt);

Or:

SQLExecDirect(myStmt, ”SELECT age FROM Student”, SQL_NTS);

– Without generating query plans

Page 41: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

41

Fetching Result Data

1. Bind variables to tuple componentsSQLBindCol(sh, colNo, colType, pVar, varSize, varInfo)– sh:statement handle– colNo: number of the component within the tuple– colType: type of the variable storing the component,

e.g., SQL_CHAR, SQL_INTEGER– pVar: pointer to the variable storing the value– varSize: length in bytes of the value of pVar– varInfo: pointer to an integer used by SQLBindCol to

provide additional infomation about the value

Page 42: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

42

Fetching Result Data (cont.)

2. Fetch dataSQLFetch(sh)– sh:statement handle

Page 43: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

43

Example

#include sqlcli.hvoid countage( ){int count[2];SQLHENV myEnv;SQLHDBC myCon;SQLHSTMT myStmt;SQLCHAR v1;SQLINTEGER v2, v1Info, v2Info;SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &myEnv);SQLAllocHandle(SQL_HANDLE_DBC, myEnv, &myCon);SQLAllocHandle(SQL_HANDLE_STMT,myCon,&myStmt);

Page 44: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

44

Example (cont.)

SQLPrepare(myStmt,

“SELECT name, age FROM Student”, SQL_NTS);

SQLExecute(myStmt);

SQLBindCol(myStmt,1,SQL_CHAR,&v1,size(v1),&v1Info);

SQLBindCol(myStmt,2,SQL_REAL,&v2,size(v2),&v2Info);

while(SQLFetch(myStmt) != SQL_NO_DATA){

printf(“%s\n”,v1);

if(v2<20) count[0]++;

else count[1]++;

}

}

Page 45: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

45

Passing Parameters to Queries

1. Use SQLPrepare to prepare a statement in which some portions, called parameters, are replaced by a question-mark (?).

2. Use SQLBindParameter to bind values to these places.

3. Use SQLExecute to execute the query with these bindings.

Page 46: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

46

SQLBindParameter

SQLBindParameter(

myStmt, stmt. handle

1, position of para

SQL_PARAMETER_MODE_IN, IN/OUT/INOUT

SQL_INT, type of value provided

SQL_INT, type of host var

4, column

0, digits to the right of dec.pnt

&v, host var

4, length of host var

&ind_v) indicator

Page 47: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

47

Example

/* get values for sNo and sName */

SQLPrepare(myStmt,

“INSERT INTO Student(sno,name) VALUES(?,?)”,

SQL_NTS);

SQLBindParameter(myStmt,1,...,sNo,...);

SQLBindParameter(myStmt,2,...,sName,...);

SQLExecute(myStmt);

Page 48: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

JDBC

Page 49: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

49

Introduction

• JDBC: Java Database Connectivityimport java.sql.*;

• Allowing Java programs to access SQL databases.

• OO flavor

Page 50: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

50

Driver

• Load a “driver” for the DBMS: creating a class called DriverManager.– Similar to the environment in SQL/CLI

• ExampleClass.forName(“com.mysql.jdbc.Driver”);

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

Page 51: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

51

Connection

• Establish a connection to DBConnection myCon =

DriverManager.getConnection(url,uid,pwd);– url: URL for the DB (data source)– uid and pwd: user name and password

Page 52: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

52

Statements

• Creating SQL statements for a connection:1. Statement myStmt =

myCon.createStatement();similar to SQLAllocHandle

2. PreparedStatement myPStmt =

myCon.createStatement(Q);similar to SQLAllocHandle and SQLPrepare

Page 53: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

53

Execution of Statements

1. ResultSet rs = myStmt.executeQuery(Q);

2. ResultSet rs = myPStmt.executeQuery( );

3. myStmt.executeUpdate(U);

4. myPStmt.executeUpdate( );– U may be any modification statements and

schema-related statements.

Page 54: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

54

Example

Statement myStmt = myCon.createStatement( );

ResultSet rs = myStmt.executeQuery(

“SELECT name,age FROM Student”);

Or:

PreparedStatement myPStmt = myCon.createStatement(

“SELECT name,age FROM Student” );

ResultSet rs = myPStmt.executeQuery( );

Page 55: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

55

Example

Statement myStmt = myCon.createStatement( );

myStmt.executeUpdate(

“INSERT INTO Student” +

“VALUES(‘S08’,’Zhangsan’,18,’CS’)”);

Or:

PreparedStatement myPStmt = myCon.createStatement(

“INSERT INTO Student” +

“VALUES(‘S08’,’Zhangsan’,18,’CS’)”);

myPStmt.executeQuery( );

Page 56: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

56

Cursor Operations in JDBC

• ResultSet class providesnext( ): implicit cursor move to the next tuple– Return FALSE if no next tuple

getString(i), getInt(i), getFloat(i): return the ith component of the tuple currently indicated by the cursor.

Page 57: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

57

Example

Statement myStmt = myCon.createStatement( );

ResultSet rs = myStmt.executeQuery(

“SELECT name,age FROM Student”);

while(rs.next( )){

sname = getString(1);

sage = getInt(2);

......

}

Page 58: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

58

Parameter Passing

• Prepared statements

• Question-mark for parameters

• Bind values to parameters– setString(i, v), setInt(i, v)

Page 59: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

59

Example

PreparedStatement myPStmt = myCon.createStatement(

“INSERT INTO Student(sno,name)” +

“VALUES(?, ?)”);

/* get values for variables v1 and v2 from the user */

myPStmt.setString(1, v1);

myPStmt.setString(2, v2);

myPStmt.executeUpdate( );

Page 60: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

60

Summary

• Approaches to SQL Programming– Embedded SQL– PSM– CLI

Page 61: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

PHP

Page 62: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

62

PHP

• A scripting language to be used for actions within HTML text.

• Indicated by <?PHP code ?>.

Page 63: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

63

Variables in PHP

• Must begin with $.• OK not to declare a type for a variable.

– Direct assignment of values to variables.– Values can be of types boolean, integer, float, string,

array.

• Object variable: created from a class.$obj=new myClass();– Then members of the class (variables and functions) are

available to it, in form of:$obj -> myVar$obj -> myFunc()

Page 64: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

64

String Values

• PHP solves a very important problem for languages that commonly construct strings as values:– How do I tell whether a substring needs to be

interpreted as a variable and replaced by its value?

• PHP solution: Double quotes means replace; single quotes means don’t.

Page 65: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

65

Example: Replace or Not?

$100 = ”one hundred dollars”;

$sue = ’You owe me $100.’;

$joe = ”You owe me $100.”;• Value of $sue is

’You owe me $100’

while the value of $joe is ’You owe me one hundred dollars’

Page 66: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

66

PHP Arrays

• Two kinds: numeric and associative.

• Numeric arrays– indexed 0,1,2,… – Example:

$a = array(”Paul”, ”George”, ”John”, ”Ringo”);Then $a[0] is ”Paul”, $a[1] is ”George”, and so on.

Page 67: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

67

Associative Arrays

• Indexed by strings.

• Elements of an associative array $a are pairs x => y, where x is a key and y is any value.

• If x => y is an element of $a, then $a[x] is y.

• Ordinary array can also be declared as:$a = array(0=>”CPU”, 1=>”Memory”, 2=>”Disk”);

Page 68: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

68

Example: Associative Arrays

• An environment can be expressed as an associative array, e.g.:$myEnv = array(

”phptype” => ”oracle”,

”hostspec” => ”www.stanford.edu”,

”database” => ”cs145db”,

”username” => ”ullman”,

”password” => ”notMyPW”);

Page 69: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

69

PEAR DB Library

• PEAR: PHP Extension and Application Repository– A collection of libraries.

– DB is one of them.

• DB library has generic functions analogous to JDBC methods.– Include with

include(DB.php);– Invoked with

DB::connect(…);

Page 70: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

70

Making a Connection

• Different forms of connection:$myCon = DB::connect( vendor://username:password hostname/databasename);

– E.g. vendor may be mysqli

$myCon = DB::connect($myEnv);– With the array $myEnv available

$myCon = mysqli.connect( “localhost”,”root”,”123”,”myDB”);

– MySQLi functions

Page 71: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

71

Executing SQL Statements

• Function query applies to a Connection object.

• It takes a string argument and returns a result.– Result could be an error code or a cursor to the

tuples of the relation returned by a query.

Page 72: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

72

Example: Executing a Query

• Find all the students who belong to a department given by the variable $dept.$d = ’CS’;

$result = $myCon->query(

”SELECT name,age FROM S” .

”WHERE dept = $d;”);– Note the use of variables $d.

Page 73: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

73

Parameter from Web Page

• User fills out a form on a Web page, and PHP gets the information from an associative array $_POST.$result = $myCon->query(

”INSERT INTO S(sno,name) VALUES(”.

”$_POST[‘num’],$POST[‘name’])”);– $POST[‘num’] is what user inputed for the ‘num’ element in the form, etc.

Page 74: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

74

Cursors in PHP

• The result object of a query is a list of tuples returned.

• Each tuple is a numeric array.

• Function fetchRow applies to the result object and returns the next tuple, or 0(FALSE) if there is none.

Page 75: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

75

Example: Cursors

$d = ’CS’;

$result = $myCon->query(

”SELECT name,age FROM S”.

”WHERE dept = $d;”);

while($tuple = $result->fetchRow()){

$n = $tuple[0];

$a = $tuple[1];

// processing …

}

Page 76: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

76

Dynamic SQL in PHP

• Similar with JDBC

• Example:$pQuery = $myCon->prepare(

“INSERT INTO S(sno,name)”.

“VALUES(?,?)”);

$args = array(‘007’,’J. Bond’);

$res = $myCon->execute($pQuery,$args);

Page 77: SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the

End