Overview

Concepts covered in this lecture: Application Development ™ SQL in application code ™ Embedded SQL ™ Cursors Chapter 6 ™ Dynamic SQL ™ JDBC ™ SQLJ ™ Stored procedures

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2

SQL in Application Code SQL in Application Code (Contd.)

™ SQL commands can be called from within a host language (e.g., C++ or Java) program. Impedance mismatch: ƒ SQL statements can refer to host variables ™ SQL relations are (multi-) sets of records, with (including special variables used to return status). no a priori bound on the number of records. ƒ Must include a statement to connect to the right No such data structure exist traditionally in database. procedural programming languages such as ™ Two main integration approaches: C++. (Though now: STL) ƒ Embed SQL in the host language (Embedded SQL, ƒ SQL supports a mechanism called a cursor to SQLJ) handle this. ƒ Create special API to call SQL commands (JDBC)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4

Embedded SQL Embedded SQL: Variables ™ Approach: Embed SQL in the host language. ƒ A converts the SQL statements into special API calls. EXEC SQL BEGIN DECLARE SECTION ƒ Then a regular is used to compile the char c_sname[20]; code. long c_sid; short c_rating; float c_age; ™ Language constructs: EXEC SQL END DECLARE SECTION ƒ Connecting to a database: EXEC SQL CONNECT ƒ Declaring variables: ™ Two special “error” variables: EXEC SQL BEGIN (END) DECLARE SECTION ƒ SQLCODE (long, is negative if an error has occurred) ƒ Statements: ƒ SQLSTATE (char[6], predefined codes for common errors) EXEC SQL Statement;

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6 Cursors Cursor that gets names of sailors who’ve reserved a red boat, in alphabetical order ™ Can declare a cursor on a relation or query statement (which generates a relation). EXEC SQL DECLARE sinfo CURSOR FOR SELECT S.sname ™ Can open a cursor, and repeatedly fetch a tuple then FROM Sailors S, Boats B, Reserves R move the cursor, until all tuples have been retrieved. WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ ORDER BY S.sname ƒ Can use a special clause, called ORDER BY, in queries that are accessed through a cursor, to control the order in ™ Note that it is illegal to replace S.sname by, say, which tuples are returned. ORDER BY • Fields in ORDER BY clause must also appear in SELECT clause. S.sid in the clause! (Why?) ƒ The ORDER BY clause, which orders answer tuples, is only ™ Can we add S.sid to the SELECT clause and allowed in the context of a cursor. replace S.sname by S.sid in the ORDER BY clause? ™ Can also modify/delete tuple pointed to by a cursor.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8

Embedding SQL in C: An Example Dynamic SQL char SQLSTATE[6]; EXEC SQL BEGIN DECLARE SECTION char c_sname[20]; short c_minrating; float c_age; ™ SQL query strings are now always known at compile EXEC SQL END DECLARE SECTION time (e.g., spreadsheet, graphical DBMS frontend): c_minrating = random(); Allow construction of SQL statements on-the-fly EXEC SQL DECLARE sinfo CURSOR FOR SELECT S.sname, S.age FROM Sailors S WHERE S.rating > :c_minrating ™ Example: ORDER BY S.sname; char c_sqlstring[]= do { {“DELETE FROM Sailors WHERE raiting>5”}; EXEC SQL FETCH sinfo INTO :c_sname, :c_age; printf(“%s is %d years old\n”, c_sname, c_age); EXEC SQL PREPARE readytogo FROM :c_sqlstring; } while (SQLSTATE != ‘02000’); EXEC SQL EXECUTE readytogo; EXEC SQL CLOSE sinfo;

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 9 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10

Database APIs: Alternative to JDBC: Architecture embedding

Rather than modify compiler, add with database ™ calls (API) Four architectural components: ƒ Application (initiates and terminates connections, ™ Special standardized interface: procedures/objects submits SQL statements) ™ Pass SQL strings from language, presents result sets ƒ in a language-friendly way Driver manager (load JDBC driver) ƒ Driver (connects to data source, transmits requests ™ Sun’s JDBC: Java API and returns/translates results and error codes) ™ Supposedly DBMS-neutral ƒ Data source (processes SQL statements) ƒ a “driver” traps the calls and translates them into DBMS- specific code ƒ database can be across a network

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 11 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12 JDBC Architecture (Contd.) JDBC Classes and Interfaces

Four types of drivers: Bridge: Steps to submit a database query: ƒ Translates SQL commands into non-native API. Example: JDBC-ODBC bridge. Code for ODBC and JDBC 1. Load the JDBC driver driver needs to be available on each client. Direct translation to native API, non-Java driver: 2. Connect to the data source ƒ Translates SQL commands to native API of data source. 3. Execute SQL statements Need OS-specific binary on each client. Network bridge: ƒ Send commands over the network to a middleware server that talks to the data source. Needs only small JDBC driver at each client. Direction translation to native API via Java driver: ƒ Converts JDBC calls directly to network protocol used by DBMS. Needs DBMS-specific Java driver at each client. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14

JDBC Driver Management Connections in JDBC

™ All drivers are managed by the We interact with a data source through sessions. Each DriverManager class connection identifies a logical session. ™ JDBC URL: ™ Loading a JDBC driver: jdbc:: ƒ In the Java code: Class.forName(“oracle/jdbc.driver.Oracledriver”); Example: ƒ When starting the Java application: String url=“jdbc:oracle:www.bookstore.com:3083”; -Djdbc.drivers=oracle/jdbc.driver Connection con; try{ con = DriverManager.getConnection(url,usedId,password); } catch SQLException excpt { …}

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 15 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 16

Connection Class Interface Executing SQL Statements

™ public int getTransactionIsolation() and void setTransactionIsolation(int level) ™ Three different ways of executing SQL Sets isolation level for the current connection. statements: ™ public boolean getReadOnly() and ƒ Statement (both static and dynamic SQL void setReadOnly(boolean b) statements) Specifies whether transactions in this connection are read- ƒ PreparedStatement (semi-static SQL statements) only ƒ CallableStatment (stored procedures) ™ public boolean getAutoCommit() and void setAutoCommit(boolean b) If autocommit is set, then each SQL statement is ™ PreparedStatement class: considered its own transaction. Otherwise, a transaction is Precompiled, parametrized SQL statements: committed using commit(), or aborted using rollback(). ƒ Structure is fixed ™ public boolean isClosed() ƒ Values of parameters are determined at run-time Checks whether connection is still open.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 17 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 18 Executing SQL Statements (Contd.) ResultSets

String =“INSERT INTO Sailors VALUES(?,?,?,?)”; PreparedStatment pstmt=con.prepareStatement(sql); ™ PreparedStatement.executeUpdate only returns the pstmt.clearParameters(); number of affected records pstmt.setInt(1,sid); ™ PreparedStatement.executeQuery returns data, pstmt.setString(2,sname); encapsulated in a ResultSet object (a cursor) pstmt.setInt(3, rating); pstmt.setFloat(4,age); ResultSet rs=pstmt.executeQuery(sql); // rs is now a cursor // we know that no rows are returned, thus we use While (rs.next()) { executeUpdate() // process the data int numRows = pstmt.executeUpdate(); }

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 19 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 20

ResultSets (Contd.) Matching Java and SQL Data Types

SQL Type Java class ResultSet get method A ResultSet is a very powerful cursor: BIT Boolean getBoolean() CHAR String getString() ™ previous(): moves one row back VARCHAR String getString() ™ absolute(int num): moves to the row with the specified number DOUBLE Double getDouble() FLOAT Double getDouble() ™ relative (int num): moves forward or INTEGER Integer getInt() backward REAL Double getFloat() ™ first() and last() DATE java.sql.Date getDate() TIME java.sql.Time getTime() TIMESTAMP java.sql.TimeStamp getTimestamp() Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 21 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 22

JDBC: Exceptions and Warnings Warning and Exceptions (Contd.) try { stmt=con.createStatement(); ™ Most of java.sql can throw and SQLException warning=con.getWarnings(); if an error occurs. while(warning != null) { // handle SQLWarnings; ™ SQLWarning is a subclass of EQLException; warning = warning.getNextWarning(): not as severe (they are not thrown and their } existence has to be explicitly tested) con.clearWarnings(); stmt.executeUpdate(queryString); warning = con.getWarnings(); … } //end try catch( SQLException SQLe) { // handle the exception } Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 23 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 24 Examining Database Metadata Database Metadata (Contd.)

DatabaseMetaData md=con.getMetaData(); DatabaseMetaData object gives information ResultSet trs=md.getTables(null,null,null,null); about the database system and the catalog. String tableName; While(trs.next()) { tableName = trs.getString(“TABLE_NAME”); DatabaseMetaData md = con.getMetaData(); System.out.println(“Table: “ + tableName); //print all attributes // print information about the driver: ResultSet crs = md.getColumns(null,null,tableName, null); System.out.println( while (crs.next()) { “Name:” + md.getDriverName() + System.out.println(crs.getString(“COLUMN_NAME” + “, “); “version: ” + md.getDriverVersion()); } }

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 25 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 26

A (Semi-)Complete Example SQLJ Connection con = // connect DriverManager.getConnection(url, ”login", ”pass"); Complements JDBC with a (semi-)static query model: Statement stmt = con.createStatement(); // set up stmt Compiler can perform syntax checks, strong type String query = "SELECT name, rating FROM Sailors"; checks, consistency of the query with the schema ResultSet rs = stmt.executeQuery(query); ƒ All arguments always bound to the same variable: try { // handle exceptions #sql = { // loop through result tuples SELECT name, rating INTO :name, :rating while (rs.next()) { FROM Books WHERE sid = :sid; String s = rs.getString(“name"); Int n = rs.getFloat(“rating"); ƒ Compare to JDBC: System.out.println(s + " " + n); sid=rs.getInt(1); } if (sid==1) {sname=rs.getString(2);} } catch(SQLException ex) { else { sname2=rs.getString(2);} System.out.println(ex.getMessage () ™ SQLJ (part of the SQL standard) versus embedded + ex.getSQLState () + ex.getErrorCode ()); SQL (vendor-specific) }

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 27 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 28

SQLJ Code SQLJ Iterators Int sid; String name; Int rating; Two types of iterators (“cursors”): // named iterator ™ Named iterator #sql iterator Sailors(Int sid, String name, Int rating); ƒ Need both variable type and name, and then allows retrieval Sailors sailors; of columns by name. // assume that the application sets rating ƒ See example on previous slide. #sailors = { ™ Positional iterator SELECT sid, sname INTO :sid, :name ƒ Need only variable type, and then uses FETCH .. INTO FROM Sailors WHERE rating = :rating construct: }; #sql iterator Sailors(Int, String, Int); Sailors sailors; // retrieve results #sailors = … while (sailors.next()) { while (true) { System.out.println(sailors.sid + “ “ + sailors.sname)); #sql {FETCH :sailors INTO :sid, :name} ; } if (sailors.endFetch()) { break; } sailors.close(); // process the sailor }

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 29 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 30 Stored Procedures Stored Procedures: Examples CREATE PROCEDURE ShowNumReservations SELECT S.sid, S.sname, COUNT(*) ™ What is a stored procedure: FROM Sailors S, Reserves R WHERE S.sid = R.sid ƒ Program executed through a single SQL statement GROUP BY S.sid, S.sname ƒ Executed in the process space of the server ™ Advantages: Stored procedures can have parameters: ƒ Can encapsulate application logic while staying ™ Three different modes: IN, OUT, INOUT “close” to the data ƒ Reuse of application logic by different users CREATE PROCEDURE IncreaseRating( ƒ Avoid tuple-at-a-time return of records through IN sailor_sid INTEGER, IN increase INTEGER) cursors UPDATE Sailors SET rating = rating + increase WHERE sid = sailor_sid Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 31 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 32

Stored Procedures: Examples Calling Stored Procedures (Contd.)

Stored procedure do not have to be written in EXEC SQL BEGIN DECLARE SECTION SQL: Int sid; Int rating; CREATE PROCEDURE TopSailors( IN num INTEGER) EXEC SQL END DECLARE SECTION LANGUAGE JAVA EXTERNAL NAME “file:///c:/storedProcs/rank.jar” // now increase the rating of this sailor EXEC CALL IncreaseRating(:sid,:rating);

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 33 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 34

Calling Stored Procedures (Contd.) SQL/PSM

Most DBMSs allow users to write stored procedures in a simple, general-purpose language (close to SQL) Æ JDBC: SQLJ: SQL/PSM standard is a representative CallableStatement cstmt= #sql iterator con.prepareCall(“{call ShowSailors(…); Declare a stored procedure: ShowSailors}); ShowSailors showsailors; CREATE PROCEDURE name(p1, p2, …, pn) ResultSet rs = #sql showsailors={CALL local variable declarations cstmt.executeQuery(); ShowSailors}; procedure code; while (rs.next()) { while (showsailors.next()) { Declare a function: … … CREATE FUNCTION name (p1, …, pn) RETURNS } } sqlDataType local variable declarations function code; Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 35 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 36 Main SQL/PSM Constructs Main SQL/PSM Constructs (Contd.) CREATE FUNCTION rate Sailor (IN sailorId INTEGER) RETURNS INTEGER ™ Local variables (DECLARE) DECLARE rating INTEGER ™ RETURN values for FUNCTION DECLARE numRes INTEGER ™ Assign variables with SET SET numRes = (SELECT COUNT(*) ™ Branches and loops: FROM Reserves R ƒ IF (condition) THEN statements; WHERE R.sid = sailorId) ELSEIF (condition) statements; IF (numRes > 10) THEN rating =1; … ELSE statements; END IF; ƒ LOOP statements; END LOOP ELSE rating = 0; END IF; ™ Queries can be parts of expressions RETURN rating; ™ Can use cursors naturally without “EXEC SQL”

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 37 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 38

Summary Summary (Contd.) ™ Embedded SQL allows execution of parametrized static queries within a host ™ SQLJ: Static model, queries checked a language compile-time. ™ Dynamic SQL allows execution of completely ad- hoc queries within a host language ™ Stored procedures execute application logic directly at the server ™ Cursor mechanism allows retrieval of one record at a time and bridges impedance mismatch ™ SQL/PSM standard for writing stored between host language and SQL procedures ™ APIs such as JDBC introduce a layer of abstraction between application and DBMS

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 39 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 40