SQL in a Programming Environment CIS 4301 Lecture Notes Lecture /11/2006
Lecture 22© CIS Spring SQL in a Program You're starting by using "direct" (or "interactive") SQL In reality SQL is rarely used in this way, instead it's specified within programs Host language (e.g., ADA, C, Cobol, Fortran, M, Pascal) Some of the steps are actually SQL statements Entire program sent to preprocessor, changes the embedded SQL into something that makes sense in host language Pre-processed program is then compiled in usual manner DBMS vendor normally provides library that supplies necessary function definitions Call-level interface (CLI)
Lecture 22© CIS Spring Processing Programs with SQL Statements Host Language + Embedded SQL Preprocessor Host Language + Function Calls Host-language compiler Object-code program SQL library
Lecture 22© CIS Spring Impedance Mismatch Why not use a single language SQL? Conventional language? Many things SQL cannot do… Example? Also, conventional progr. language not suitable for letting humans write database operations that can be executed efficiently… Why not? Need both, but must deal with disconnect between the data models used Rel. model uses sets, not naturally supported by programming languages Programming lang. support pointers, loops, branches, not available in SQL Passing data back and forth between SQL and other prog. languages is not straightforward
Lecture 22© CIS Spring SQL/Host Language Interface How are values passed from the program into SQL commands? How are results of SQL commands returned into program variables? How do we deal with set-valued (i.e., relation-valued) results? Shared variables Variables of the host languages DECLARE Section Identify in program by preceding variable with colon Special variable SQLSTATE connects the host language with the SQL execution system ‘00000’ no error ‘02000’ no tuple found
Lecture 22© CIS Spring The Declare Section EXEC SQL BEGIN DECLARE SECTION; char studioName[50], studioAddr[256]; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION;
Lecture 22© CIS Spring Embedded SQL SQL statements that do not return a result can be embedded directly into a host language using EXEC SQL … syntax EXEC SQL INSERT INTO Studio (name, address) VALUES (:studioName, :studioAddress);
Lecture 22© CIS Spring Sample C Function void getStudio() { EXEC SQL BEGIN DECLARE SECTION; char studioName[50], studioAddr[256]; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* code for handling user input… */ EXEC SQL INSERT INTO STUDIO(name, address) VALUES (:studioName, :studioAddr); }
Lecture 22© CIS Spring Embedded SQL So far, statements return no results No impedance mismatch – EASY SFW queries must use one of two mechanisms Single-row select using shared variables to hold components of single tuple being returned Queries producing more than one tuple must declare cursor
Lecture 22© CIS Spring Single-Row Select Statement Void printNetWorth() { EXEC SQL BEGIN DECLARE SECTION; char studioName[50]; int presNetWorth; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* print request that studio name be entered, read response into studioName */ EXEC SQL SELECT netWorth INTO :presNetWorth FROM Studio, MovieExec WHERE presC# = cert# AND Studio.name = :studioName; /* print value of presNetWorth */ } Declaration of shared vars
Lecture 22© CIS Spring Cursors For set-valued results: associate handle (cursor) with query, get tuples in result one at a time through handle Example: Print names and GPAs of all students who applied to Santa Cruz Step 1: declare cursor EXEC BEGIN DECLARE SECTION; char n[30]; float g; EXEC END DECLARE SECTION; EXEC DECLARE Scinfo CURSOR FOR (SELECT DISTINCT name, GPA FROM Student, Apply WHERE Student.ID = Apply.ID AND Apply.location = "Santa Cruz");
Lecture 22© CIS Spring General Cursor Declaration EXEC SQL DECLARE CURSOR FOR
Lecture 22© CIS Spring Using a Cursor Step 2: run query using iteration EXEC SQL OPEN SCinfo; initializes cursor EXEC SQL FETCH FROM SCinfo INTO :n, :g; WHILE (MORE_TUPLES) Print(:n, :g); EXEC SQL FETCH SCinfo INTO :n,:g; ENDWHILE; EXEC SQL CLOSE SCinfo; closes cursor #define MORE_TUPLES !(strcmp(SQLSTATE,”02000”))
Lecture 22© CIS Spring General Fetch Statement EXEC SQL FETCH FROM INTO
Lecture 22© CIS Spring Other Uses of Cursor Cursor can be used to modify or delete tuples See example in FCDBS
Lecture 22© CIS Spring Scrolling Cursors Default movement through cursor is forward, one tuple at a time Option to move in different directions When declaring cursor, declare cursor as scrollable Use FETCH in conjunction with direction keyword NEXT or PRIOR FIRST or LAST RELATIVE ABSOLUTE
Lecture 22© CIS Spring Dynamic SQL So far, model was to embed specific SQL queries and commands within host-language program Alternatively, can have host language compute and assemble SQL statements Since not known at compile time, cannot be handled by SQL preprocessor or host-language compiler Dynamic SQL Example? Host-language program must instruct SQL system to take char string, turn it into executable SQL statement, and finally to execute that statement
Lecture 22© CIS Spring A very simple sqlplus Void vssqlplus () { EXEC SQL BEGIN DECLARE SECTION; char *query; EXEC SQL END DECLARE SECTION; /* prompt user for a query, allocate space and make shared variable :query point to first char of query */ EXEC SQL PREPARE SQLquery FROM :query; EXEC SQL EXECUTE SQLquery; /* code for displaying result to user */ } EXEC SQL EXECUTE IMMEDIATE :query;