Date post: | 26-Dec-2015 |
Category: |
Documents |
Upload: | bryce-oliver |
View: | 227 times |
Download: | 6 times |
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 DB.– Part of the DB schema– Can be used in SQL queries or other statements
• Often called stored procedures in commercial DBMS
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.
Parameters
mode name type
• Procedure parameters– mode: IN (default), OUT, INOUT
• Function parameters– mode: IN
Forbid side-effectsOutput information through return-value
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
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
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.
Statements in PSM (cont.)
• Local variablesDECLARE name type;– Local: values not preserved after running the
procedure/function– Declared before executable statement
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
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.
Statements in PSM (cont.)
• Statement labelslabel:statement– Used in a loop statement, allowing leaving the
loop.
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
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
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;
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);
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.
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;
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;
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.
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;
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?
Statements in PSM (cont.)
• WHILE-Loops WHILE condition DO
statement list
END WHILE;
• REPEAT-Loops REPEAT
statement list
UNTIL condition
END REPEAT;
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
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.
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;
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’);
SQL/Host-Language Interface
SQL/CLI
JDBC
PHP
28
Review
Host language + Embedded SQL
DBMS Preprocessors
Host language + Function calls
Host language compiler
Object code
SQL Library
API
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, …
SQL/CLI
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.
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.
33
Hierarchy
Environment1 Environment2
Connection1
Connection2Connection2
Connection1
Statement1
Statement2
34
Environments
• Created in preparation for one or more connections to the DB server.SQLAllocHandle(SQL_HANDLE_ENV,
SQL_NULL_HANDLE,
&myEnv);
35
Connections
• Created to connect to the DB server, within some environment.SQLAllocHandle(SQL_HANDLE_DBC,
myEnv,
&myCon);
36
Connections (cont.)
• Connect to the DB serverSQLConnect(myCon,
sqlserver, SQL_NTS,
userid, SQL_NTS,
pword, SQL_NTS);
37
Statements
• Created to pass SQL statements to the DB server, within some connection.SQLAllocHandle(SQL_HANDLE_STMT,
myCon,
&myStmt);
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);
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);
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
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
42
Fetching Result Data (cont.)
2. Fetch dataSQLFetch(sh)– sh:statement handle
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);
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]++;
}
}
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.
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
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);
JDBC
49
Introduction
• JDBC: Java Database Connectivityimport java.sql.*;
• Allowing Java programs to access SQL databases.
• OO flavor
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”);
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
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
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.
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( );
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( );
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.
57
Example
Statement myStmt = myCon.createStatement( );
ResultSet rs = myStmt.executeQuery(
“SELECT name,age FROM Student”);
while(rs.next( )){
sname = getString(1);
sage = getInt(2);
......
}
58
Parameter Passing
• Prepared statements
• Question-mark for parameters
• Bind values to parameters– setString(i, v), setInt(i, v)
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( );
60
Summary
• Approaches to SQL Programming– Embedded SQL– PSM– CLI
PHP
62
PHP
• A scripting language to be used for actions within HTML text.
• Indicated by <?PHP code ?>.
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()
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.
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’
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.
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”);
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”);
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(…);
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
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.
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.
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.
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.
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 …
}
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);
End