chapter 9 sql in a server environmentli-fang/chapter9.pdf · options 1. sql statements are embedded...
TRANSCRIPT
![Page 1: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/1.jpg)
Chapter 9 SQL in a server
environment
SQL in a Programming Environment
embedded SQL
persistent stored modules
Database-Connection Libraries
Call-level interface (CLI)
JDBC
PHP
![Page 2: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/2.jpg)
SQL in Real Programs
We have seen only how SQL is used at the generic query interface --- an environment where we sit at a terminal and ask queries of a database.
Reality is almost always different: conventional programs interacting with SQL.
![Page 3: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/3.jpg)
Options
1. SQL statements are embedded in a host language (e.g., C).
2. Code in a specialized language is stored in the database itself (e.g., PSM, PL/SQL).
3. Connection tools are used to allow a conventional language to access a database (e.g., CLI, JDBC, PHP/DB).
![Page 4: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/4.jpg)
SQL in a Programming
Environment
Embedded SQL: add to a conventional
programming language (C for example,
we called host language ), certain
statements that represent SQL operation.
Host language+embedded SQL
code?
![Page 5: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/5.jpg)
System Implementation
How to identify SQL statements?
How to move data between SQL and a conventional programming language?
Mismatch problem exists?
Host Language + Embedded SQL
Preprocessing
Host Language + Function calls
Host-language compiler SQL library
Object-code program
![Page 6: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/6.jpg)
How to recognize SQL statements (the
Interface between SQL statements and
programming language)
Each embedded SQL statement introduced with
EXEC SQL
Shared variables : exchange data between SQL
and a host language. When they are referred by a
SQL statement, these shared variables are prefixed
by a colon, but they appear without colon in host-
language statements.
EXEC SQL BEGIN / END DECLARE SECTION
to declare shared variables.
![Page 7: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/7.jpg)
the Interface between SQL statements
and programming language
SQL define an array of characters
SQLSTATE that is set every time the
system is called.
SQLSTATE connects the host-language
program with the SQL execution system.
00000: no error
02000: could not be found
![Page 8: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/8.jpg)
Implementations of SQLSTATE
SQL defines an array of characters SQLSTATE that is set every time the system is called.
Errors are signaled there
Different systems use different way
Oracle provides us with a header file sqlca.h that declares a communication area and defines macros to access it, such as NOT FOUND.
Sybase provides SQLCA with sqlcode
0:success, <0: fail, 100: not found
![Page 9: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/9.jpg)
Example: Find the price for a
given beer at a given bar
Sells (bar, beer, price)
EXEC SQL BEGIN DECLARATION SECTION
CHAR theBar[21], theBeer[21];
Float thePrice;
EXEC SQL END DECLARAE SECTION
EXEC SQL SELECT price INTO :thePrice
FROM sells
WHERE beer = :theBeer AND bar =:theBar;
![Page 10: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/10.jpg)
Queries produce sets of tuples as a result, while none
of the major host languages supports a set data type
directly. So, cursors are used.
A cursor declaration: EXEC SQL DECLARE <cursor> CURSOR FOR <query>
A statement EXEC SQL OPEN<cursor> : the cursor is ready to retrieve the first tuple of the relation over which the cursor ranges.
EXEC SQL FETCH FROM < cursor > INTO <list of variables>
EXEC SQL CLOSE <cursor>: the cursor is no longer ranges over tuples of the relation.
![Page 11: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/11.jpg)
Cursor Example Void worthRanges() {
int i,digits, counts[15];
EXEC SQL BEGIN DECLARE SECTION;
int worth; char SQLSTATE[6];
EXEC SQL END DECLARE SECTION;
EXEC SQL DECLARE execCursor CURSOR FOR
SELECT netWorth FROM MovieExec;
EXEC SQL OPEN execCursor;
while (1) { EXEC SQL FETCH FROM execCursor INTO :worth;
if (NO_MORE_TUPLES) BREAK;
else …..
}
EXEC SQL CLOSE execCursor;
…
}
![Page 12: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/12.jpg)
More about cursor:
The order in which tuples are fetched from
the relation can be specified.
The effect of changes to the relation that the
cursor ranges over can be limited.
The motion of the cursor through the list of
tuples can be varied.
![Page 13: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/13.jpg)
Modification by cursor
With Where clause WHERE CURRENT OF followed by the name of the cursor.
e.g. ….. EXEC SQL OPEN execCursor;
while (1) { EXEC SQL FETCH FROM execCursor INTO :execName,:execAddr,:certNo,:worth;
if (NO_MORE_TUPLES) BREAK;
IF (WORTH < 1000)
EXEC SQL DELETE FROM MovieExec
WHERE CURRENT OF execCursor;
else …..
EXEC SQL CLOSE execCursor;
Define NO_MORE_TUPLES !(strcmp(SQLSTATE,”02000”))
![Page 14: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/14.jpg)
Protecting against concurrent updates
EXEC SQL DECLARE execCursor INSENSITIVE CURSOR FOR
SELECT netWorth FROM MovieExec;
The SQL system will guarantee that changes to
relation MovieExec made between one opening and closing of execCursor will not affect the set of tuples fetched.
Insensitive cursors could be expensive, systems spend a lot of time to manage data access.
![Page 15: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/15.jpg)
Scrolling Cursors
EXEC SQL DECLARE execCursor SCROLL CURSOR FOR MovieExec;
The cursor may be used in a manner other than moving forward in the order of tuples.
Follow FETCH by one of several options that tell where to find the desired tuple. Those options are NEXT, PRIOR, FIRST, LAST and so on.
![Page 16: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/16.jpg)
Need for Dynamic SQL
Most applications use specific queries and modification statements to interact with the database.
The DBMS compiles EXEC SQL … statements into specific procedure calls and produces an ordinary host-language program that uses a library.
Sometimes we don‟t know what it needs to do until it runs?
![Page 17: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/17.jpg)
Dynamic SQL
Preparing a query:
EXEC SQL PREPARE <query-name>
FROM <text of the query>;
Executing a query:
EXEC SQL EXECUTE <query-name>;
“Prepare” = optimize query.
Prepare once, execute many times.
![Page 18: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/18.jpg)
Example: A Generic Interface
EXEC SQL BEGIN DECLARE SECTION;
char query[MAX_LENGTH];
EXEC SQL END DECLARE SECTION;
while(1) {
/* issue SQL> prompt */
/* read user‟s query into array query */
EXEC SQL PREPARE q FROM :query;
EXEC SQL EXECUTE q;
}
q is an SQL variable representing the optimized form of whatever statement is typed into :query
![Page 19: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/19.jpg)
Execute-Immediate
If we are only going to execute the query once, we can combine the PREPARE and EXECUTE steps into one.
Use:
EXEC SQL EXECUTE IMMEDIATE <text>;
![Page 20: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/20.jpg)
Example: Generic Interface Again
EXEC SQL BEGIN DECLARE SECTION;
char query[MAX_LENGTH];
EXEC SQL END DECLARE SECTION;
while(1) {
/* issue SQL> prompt */
/* read user’s query into array
query */
EXEC SQL EXECUTE IMMEDIATE :query;
}
![Page 21: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/21.jpg)
Stored Procedures
PSM, or “persistent stored modules,” allows us to store procedures as database schema elements.
PSM = a mixture of conventional statements (if, while, etc.) and SQL.
Lets us do things we cannot do in SQL alone.
![Page 22: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/22.jpg)
Procedures Stored in the Schema
Aim
Provide a way for the user to store with a database schema some functions or procedures that can be used in SQL queries or other SQL statements.
![Page 23: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/23.jpg)
Creating PSM Functions and Procedures
Procedure Declarations CREATE PROCEDURE
<name>(<arglist>)
local declarations;
procedure body;
Function Declarations CREATE FUNCTION <name> (<parameters>)
RETURNS <type>
local declarations
function body;
![Page 24: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/24.jpg)
Example:
CREATE PROCEDURE move (
IN oldAddr VARCHAR [255],
IN newAddr VARCHAR [255]
UPDATE MOVIEsTAR
SET address = newAddr
WHERE address = oldAddr; )
–The parameters of a procedure are triples of mode-name-type
IN = procedure uses value, does not change value.
OUT = procedure changes, does not use.
INOUT = both.
![Page 25: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/25.jpg)
Function Declaration
–Function parameter may only be of mode IN, the only way to obtain information from a function is through its return-value.
![Page 26: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/26.jpg)
Example: Stored Procedure
Let‟s write a procedure that takes two arguments b and p, and adds a tuple to Sells that has bar = ‟Joe‟‟s Bar‟, beer = b, and price = p.
Used by Joe to add to his menu more easily.
![Page 27: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/27.jpg)
The Procedure
CREATE PROCEDURE JoeMenu (
IN b CHAR(20),
IN p REAL
)
INSERT INTO Sells
VALUES(‟Joe‟‟s Bar‟, b, p);
Parameters are both read-only, not changed
The body --- a single insertion
![Page 28: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/28.jpg)
Invoking Procedures
Use SQL/PSM statement CALL, with the name of the desired procedure and arguments.
Example:
CALL JoeMenu(‟Moosedrool‟, 5.00);
![Page 29: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/29.jpg)
Where to call?
CALL <procedure name> (<argument list>);
• From a host-language program, e.g.
EXEC SQL CALL foo(:x,3);
• As a statement of another PSM function
or procedure
• As an SQL command issued to the
generic SQL interface, e.g. CALL
foo(1,3)
![Page 30: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/30.jpg)
Invoking Functions
It is not permitted to call a function.
Use the function name and suitable arguments as part of an expression.
Functions used in SQL expressions where a value of their return type is appropriate.
![Page 31: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/31.jpg)
Simple statements in PSM
Return statement in a function: RETURN <expression>;
declare local variables : DECLARE <name><type>;
Assignments: SET <variable>=<expression>; SET b = ‟Bud‟;
Groups of statements: BEGIN…END Separate by semicolons.
Branching statements: If then else,
Loops: for-loops, loops,
![Page 32: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/32.jpg)
Example: IF
Let‟s rate bars by how many customers they have, based on Frequents(drinker, bar).
<100 customers: „unpopular‟.
100-199 customers: „average‟.
>= 200 customers: „popular‟.
Function Rate(b) rates bar b.
![Page 33: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/33.jpg)
Example: IF (continued)
CREATE FUNCTION Rate (IN b CHAR(20) )
RETURNS CHAR(10)
DECLARE cust INTEGER;
BEGIN
SET cust = (SELECT COUNT(*) FROM Frequents
WHERE bar = b);
IF cust < 100 THEN RETURN ‟unpopular‟
ELSEIF cust < 200 THEN RETURN ‟average‟
ELSE RETURN ‟popular‟
END IF;
END;
Number of customers of bar b
Nested IF statement
![Page 34: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/34.jpg)
Loops
Basic form:
<loop name>: LOOP <statements> END LOOP;
Exit from a loop by:
LEAVE <loop name>
![Page 35: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/35.jpg)
Example: Exiting a Loop
loop1: LOOP
. . .
LEAVE loop1;
. . .
END LOOP;
If this statement is executed . . .
Control winds up here
![Page 36: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/36.jpg)
Other Loop Forms
WHILE <condition> DO <statements> END WHILE;
REPEAT <statements> UNTIL <condition> END REPEAT;
![Page 37: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/37.jpg)
Queries
General SELECT-FROM-WHERE queries are not permitted in PSM.
There are three ways to get the effect of a query:
1. Queries producing one value can be the expression in an assignment.
2. Single-row SELECT . . . INTO.
3. Cursors.
![Page 38: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/38.jpg)
Example: Assignment/Query
Using local variable p and Sells(bar, beer, price), we can get the price Joe charges for Bud by:
SET p = (SELECT price FROM Sells
WHERE bar = ’Joe’’s Bar’ AND
beer = ’Bud’);
![Page 39: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/39.jpg)
SELECT . . . INTO
Another way to get the value of a query that returns one tuple is by placing INTO <variable> after the SELECT clause.
Example:
SELECT price INTO p FROM Sells
WHERE bar = ’Joe’’s Bar’ AND
beer = ’Bud’;
![Page 40: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/40.jpg)
Cursors
A cursor is essentially a tuple-variable that ranges over all tuples in the result of some query.
Declare a cursor c by:
DECLARE c CURSOR FOR <query>;
![Page 41: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/41.jpg)
Opening and Closing Cursors
To use cursor c, we must issue the command:
OPEN c;
The query of c is evaluated, and c is set to point to the first tuple of the result.
When finished with c, issue command:
CLOSE c;
![Page 42: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/42.jpg)
Fetching Tuples From a Cursor
To get the next tuple from cursor c, issue command:
FETCH FROM c INTO x1, x2,…,xn ;
The x ‟s are a list of variables, one for each component of the tuples referred to by c.
c is moved automatically to the next tuple.
![Page 43: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/43.jpg)
Breaking Cursor Loops – (1)
The usual way to use a cursor is to create a loop with a FETCH statement, and do something with each tuple fetched.
A tricky point is how we get out of the loop when the cursor has no more tuples to deliver.
![Page 44: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/44.jpg)
Breaking Cursor Loops – (2)
Each SQL operation returns a status, which is a 5-digit character string.
For example, 00000 = “Everything OK,” and 02000 = “Failed to find a tuple.”
In PSM, we can get the value of the status in a variable called SQLSTATE.
![Page 45: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/45.jpg)
Breaking Cursor Loops – (3)
We may declare a condition, which is a boolean variable that is true if and only if SQLSTATE has a particular value.
Example: We can declare condition NotFound to represent 02000 by:
DECLARE NotFound CONDITION FOR
SQLSTATE ’02000’;
![Page 46: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/46.jpg)
Breaking Cursor Loops – (4)
The structure of a cursor loop is thus:
cursorLoop: LOOP
…
FETCH c INTO … ;
IF NotFound THEN LEAVE cursorLoop;
END IF;
…
END LOOP;
![Page 47: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/47.jpg)
Exceptions in PSM CREATE FUNCTION GetYear(t VARCHAR[255])
RETURNS INTEGER
DECLARE Not_Found CONDITION FOR SQLSTATE „02000‟;
DECLARE Too_Mamy CONDITION FOR SQLSTATE „21000‟;
BEGIN
DECLARE EXIT HANDLER FOR Not_Found,Too_Many
RETURN NULL;// handler declaration
RETURN (SELECT year FROM Movie WHERE title=t);
END;
Where to go: 1) continue:execute the
statement after the one that raised the exception.
2) Exit:leave the BEGIN…END
block.the statement after the block is executed next.
3) Undo: not executed the statement within the block and exit like 2)
![Page 48: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/48.jpg)
Components of Exception handler in PSM
A list of exception conditions that invoke the handler when raised.
Code to be executed when one of the associated exceptions is raised.
An indication of where to go after the handler has finished its work.
DELARE <where to go> HANDLER FOR <condition list> <statement>
![Page 49: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/49.jpg)
Example: Cursor in PSM
Let‟s write a procedure that examines Sells(bar, beer, price), and raises by $1 the price of all beers at Joe‟s Bar that are under $3.
Yes, we could write this as a simple UPDATE, but the details are instructive anyway.
![Page 50: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/50.jpg)
The Needed Declarations
CREATE PROCEDURE JoeGouge( )
DECLARE theBeer CHAR(20);
DECLARE thePrice REAL;
DECLARE NotFound CONDITION FOR
SQLSTATE ‟02000‟;
DECLARE c CURSOR FOR
(SELECT beer, price FROM Sells
WHERE bar = ‟Joe‟‟s Bar‟);
Used to hold beer-price pairs when fetching through cursor c
Returns Joe‟s menu
![Page 51: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/51.jpg)
The Procedure Body BEGIN
OPEN c;
menuLoop: LOOP
FETCH c INTO theBeer, thePrice;
IF NotFound THEN LEAVE menuLoop END IF;
IF thePrice < 3.00 THEN
UPDATE Sells SET price = thePrice+1.00
WHERE bar = ‟Joe‟‟s Bar‟ AND beer = theBeer;
END IF;
END LOOP;
CLOSE c;
END;
Check if the recent FETCH failed to get a tuple
If Joe charges less than $3 for the beer, raise it‟s price at Joe‟s Bar by $1.
![Page 52: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/52.jpg)
Database connection
The third approach to connecting databases to conventional languages is to use library calls.
1. C + CLI
2. Java + JDBC
3. PHP + PEAR/DB
![Page 53: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/53.jpg)
Three-Tier Architecture
A common environment for using a database has three tiers of processors:
1. Web servers --- talk to the user.
2. Application servers --- execute the business logic.
3. Database servers --- get what the app servers need from the database.
![Page 54: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/54.jpg)
DBMS environment: the framework under
which data may exist and SQL operation on data may be executed.
environment
cluster catalog
catalog
catalog
schema
Schemas: collections of tables, views, assertions, domains and so on. Catalog: collections of schemas, information about all the schemas in the catalog. Clusters: each user has an associated cluster, so in a sense, a cluster is “the database‟ as seen
by a particular user.
![Page 55: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/55.jpg)
Environments, Connections, Queries
The database is, in many DB-access languages, an environment.
Database servers maintain some number of connections, so app servers can ask queries or perform modifications.
The app server issues statements : queries and modifications, usually.
![Page 56: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/56.jpg)
Diagram to Remember
Environment
Connection
Statement
![Page 57: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/57.jpg)
SQL/CLI
Instead of using a preprocessor (as in embedded SQL), we can use a library of functions.
The library for C is called SQL/CLI = “Call-Level Interface.”
Embedded SQL‟s preprocessor will translate the EXEC SQL … statements into CLI or similar calls, anyway.
![Page 58: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/58.jpg)
Data Structures
C connects to the database by structs of the following types:
1. Environments : represent the DBMS installation.
2. Connections : logins to the database.
3. Statements : SQL statements to be passed to a connection.
4. Descriptions : records about tuples from a query, or parameters of a statement.
![Page 59: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/59.jpg)
JDBC
Java Database Connectivity (JDBC) is a library similar to SQL/CLI, but with Java as the host language.
Like CLI, but with a few differences for us to cover.
![Page 60: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/60.jpg)
Making a Connection
import java.sql.*;
Class.forName(com.mysql.jdbc.Driver);
Connection myCon =
DriverManager.getConnection(…);
The JDBC classes
The driver for mySql; others exist
URL of the database, your name, and password go here.
Loaded by forName
![Page 61: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/61.jpg)
Statements
JDBC provides two classes:
1. Statement = an object that can accept a string that is a SQL statement and can execute such a string.
2. PreparedStatement = an object that has an associated SQL statement ready to execute.
![Page 62: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/62.jpg)
Creating Statements
The Connection class has methods to create Statements and PreparedStatements.
Statement stat1 = myCon.createStatement();
PreparedStatement stat2 =
myCon.createStatement(
”SELECT beer, price FROM Sells ” +
”WHERE bar = ‟Joe‟ ‟s Bar‟ ”
); createStatement with no argument returns a Statement; with one argument it returns a PreparedStatement.
![Page 63: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/63.jpg)
Executing SQL Statements
JDBC distinguishes queries from modifications, which it calls “updates.”
Statement and PreparedStatement each have methods executeQuery and executeUpdate.
For Statements: one argument (the query or modification to be executed).
For PreparedStatements: no argument.
![Page 64: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/64.jpg)
Example: Update
stat1 is a Statement.
We can use it to insert a tuple as:
stat1.executeUpdate(
”INSERT INTO Sells ” +
”VALUES(’Brass Rail’,’Bud’,3.00)”
);
![Page 65: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/65.jpg)
Example: Query
stat2 is a PreparedStatement holding the query ”SELECT beer, price FROM Sells WHERE bar = ‟Joe‟‟s Bar‟ ”.
executeQuery returns an object of class ResultSet – we‟ll examine it later.
The query:
ResultSet menu = stat2.executeQuery();
![Page 66: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/66.jpg)
Accessing the ResultSet
An object of type ResultSet is something like a cursor.
Method next() advances the “cursor” to the next tuple.
The first time next() is applied, it gets the first tuple.
If there are no more tuples, next() returns the value false.
![Page 67: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/67.jpg)
Accessing Components of Tuples
When a ResultSet is referring to a tuple, we can get the components of that tuple by applying certain methods to the ResultSet.
Method getX (i ), where X is some type, and i is the component number, returns the value of that component.
The value must have type X.
![Page 68: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/68.jpg)
Example: Accessing Components
Menu = ResultSet for query “SELECT beer, price FROM Sells WHERE bar = ‟Joe‟ ‟s Bar‟ ”.
Access beer and price from each tuple by:
while ( menu.next() ) {
theBeer = Menu.getString(1);
thePrice = Menu.getFloat(2);
/*something with theBeer and
thePrice*/
}
![Page 69: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/69.jpg)
PHP (personal home page)
A scripting language to be used for actions within HTML text.
Indicated by <? PHP code ?>.
DB library exists within PEAR (PHP Extension and Application Repository). Include with include(DB.php).
![Page 70: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/70.jpg)
Variables in PHP
Must begin with $.
OK not to declare a type for a variable.
But you give a variable a value that belongs to a “class,” in which case, methods of that class are available to it.
![Page 71: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/71.jpg)
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 72: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/72.jpg)
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 73: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/73.jpg)
PHP Arrays
Two kinds: numeric and associative.
Numeric arrays are ordinary, indexed 0,1,…
Example: $a = array(”Paul”, ”George”, ”John”, ”Ringo”);
Then $a[0] is ”Paul”, $a[1] is ”George”, and so on.
![Page 74: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/74.jpg)
Associative Arrays
Elements of an associative array $a are pairs x => y, where x is a key string and y is any value.
If x => y is an element of $a, then $a[x] is y.
![Page 75: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/75.jpg)
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 76: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/76.jpg)
Making a Connection
With the DB library imported and the array $myEnv available:
include(db.php);
$myCon = DB::connect($myEnv);
Function connect in the DB library
Class is Connection because it is returned by DB::connect().
<vendor>://<user name>:<password><host name>/<database name>
![Page 77: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/77.jpg)
Executing SQL Statements
Method query applies to a Connection object.
It takes a string argument and returns a result.
Could be an error code or the relation returned by a query.
![Page 78: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/78.jpg)
Example: Executing a Query
Find all the bars that sell a beer given by the variable $beer.
$beer = ’Bud’;
$result = $myCon->query(
”SELECT bar FROM Sells” .
”WHERE beer = $beer ;”);
Concatenation in PHP
Remember this variable is replaced by its value.
Method application
![Page 79: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/79.jpg)
Cursors in PHP
The result of a query is the tuples returned.
Method fetchRow applies to the result and returns the next tuple, or FALSE if there is none.
![Page 80: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/80.jpg)
Example: Cursors
while ($bar =
$result->fetchRow()) {
// do something with $bar
}
![Page 81: Chapter 9 SQL in a server environmentli-fang/chapter9.pdf · Options 1. SQL statements are embedded in a host language (e.g., C). 2. Code in a specialized language is stored in the](https://reader034.vdocuments.net/reader034/viewer/2022051810/601aed726c7aa7686523dee7/html5/thumbnails/81.jpg)
Summary
Embedded SQL (shared variables, EXEC SQL, Cursor), Dynamic SQL
SQL/PSM
Call-level Interface (SQL/CLI)
JDBC
PHP