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.

Slides:



Advertisements
Similar presentations
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
Advertisements

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
1 Database-Connection Libraries Call-Level Interface Java Database Connectivity PHP.
Stored Procedure Language Stored Procedure Overview Stored Procedure is a function in a shared library accessible to the database server can also write.
Chapter 9 SQL in a Server Environment Call-Level Interface Java Database Connectivity PHP.
CSC343 – Introduction to databases – A. Vaisman1 Database Application Development.
1 Combining SQL and Conventional Programming Languages Source: slides by Jeffrey Ullman.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Fall 2001Arthur Keller – CS 18011–1 Schedule Oct. 30 (T) Embedded SQL. u Read Section 8.1. u Assignment 5 due. Not accepted late. u Project Part 4 due.
Murali Mani SQL with other Programming Languages.
EmbeddedSQL: 1 Impedance Mismatch Problem Problem : How to connect SQL statements with conventional programming languages Different models of language.
Winter 2002Arthur Keller – CS 18011–1 Schedule Today: Feb. 7 (TH) u PL/SQL, Embedded SQL, CLI, JDBC. u Read Sections 8.1, Feb. 12 (T) Advising.
Dec 11, 2003Murali Mani SQL with other Programming Languages B term 2004: lecture 16.
1 SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity Persistent Stored Modules.
1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
CS411 Database Systems Kazuhiro Minami 07: SQL System Aspects.
Announcements Read JDBC Project Step 5, due Monday.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 9- 1 DATADABASE PROGRAMMING 2Chapter 13 from our text.
1 Real SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity.
Chapter 8 Using SQL in an Application. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 CSC 4480 outline Intro (3-9) Static SQL (10-11)
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Overview of JDBC and Pro*C 1 CSE 5330 – Database Systems.
SQL in a Programming Environment - Part II CIS 4301 Lecture Notes Lecture /4/2006.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
Advanced SQL: Cursors & Stored Procedures
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Stored Procedure James Wang.
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
Winter 2006 Keller, Ullman, Cushing 11–1 Embedded SQL Add to a conventional programming language (C in our examples) certain statements that represent.
SQL in a Programming Environment CIS 4301 Lecture Notes Lecture /11/2006.
1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
SQL Programming SQL in Application Programs
©Silberschatz, Korth and Sudarshan5.1Database System Concepts - 6 th Edition Procedural Constructs in SQL Chapter 5.
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Triggers, Procedures, PL/SQL. u Read Sections , 8.1, 8.5. Next u Transaction concepts, security.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Chapter 7: Constraints and Triggers Foreign Keys Local and Global Constraints Triggers 1.
1 JDBC – Java Database Connectivity CS , Spring 2010.
1 Database Design: DBS CB, 2 nd Edition SQL in a Server Environment: CLI & JDBC & Security Ch Ch. 9.6 – Ch 10.1.
CS422 Principles of Database Systems JDBC and Embedded SQL Chengyu Sun California State University, Los Angeles.
1 Database Design: DBS CB, 2 nd Edition SQL in a Server Environment: Stored Procedure & Embedded SQL Ch. 9.3, 9.4.
1 Introduction to Database Systems, CS420 SQL Persistent Stored Modules (PSM) – Stored Procedure.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
SQL in the real world 1. The Three-Tier Architecture of Database Applications browser network HTTP Web server Application server Database server database.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
10/3/2017.
Web Database Programming Using PHP
Chap 8. SQL in a Server Environment-PartIII
1st Semester, 2017 Sanghyun Park
SQL Environment.
Web Database Programming Using PHP
SQL/PSM.
Chapter 5: Advanced SQL Database System concepts,6th Ed.
CPSC-310 Database Systems
CPSC-310 Database Systems
Database Application Development
Web DB Programming: PHP
Introduction to SQL Programming Techniques
Persistent Stored Modules (PSM) PL/SQL Embedded SQL
Database-Connection Libraries
Stored Procedure Language
Chapter 9: sql in a server environment
Embedded SQL Chapter 8.
Presentation transcript:

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 declaration CREATE PRECEDURE name (parameters) local declarations procedure body; Function declaration CREATE 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-effects Output 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-statement CALL 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-statement RETURN expression; –Only appear in a function. –Set the return-value of the function. –It does NOT terminate the function! Control continues with the following statement Possible that the return-value will be changed before the function completes.

Statements in PSM (cont.) Local variables DECLARE name type; –Local: values not preserved after running the procedure/function –Declared before executable statement

Statements in PSM (cont.) Assignment statement SET 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 group BEGIN 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 labels label:statement –Used in a loop statement, allowing leaving the loop.

Statements in PSM (cont.) Branching statements IF 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. Declaration DECLARE 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 level statement < connection < environment –O = (address of) output handle.

33 Hierarchy Environment1Environment2 Connection1 Connection2 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 server SQLConnect(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 components SQLBindCol(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 data SQLFetch(sh) –sh:statement handle

43 Example #include sqlcli.h void 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 Connectivity import 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 Example Class.forName(“com.mysql.jdbc.Driver”); Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

51 Connection Establish a connection to DB Connection 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 provides next( ): 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.

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” => ” ”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