
Embedded SQL, Database APIs, and PL/SQL Juliana Freire Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan SQL: Benefits • Declarative languages: program is a prescription for what data is to be retrieved, rather than a procedure describing how to retrieve the data • When we write an SQL select query, we do not make any assumptions about the order of evaluation • Can be automatically optimized! – Decision about order and evaluation plan is left to the optimized – Optimizer has the resources to make sophisticated decisions SQL: Limitations • Not flexible enough for some applications – Some queries cannot be expressed in SQL – Non-declarative actions can’t be done from SQL, e.g., printing a report, interacting with user/GUI – SQL queries may be just one small component of complex applications Trade-off: automatic optimization of queries expressed in powerful languages is hard Solution • SQL commands called from within a powerful host language program • Three main integration approaches: – Embed SQL in the host language (Embedded SQL) – Create special API to call SQL commands (JDBC,ODBC) – Allow ‘external’ code to be executed from within SQL • Query processing performed by the database – Leverage database capabilities 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. Three-Tier Architecture Example: Amazon • Database holds the information about products, customers, etc. • Business logic includes things like “what do I do after someone clicks ‘checkout’?” – Answer: Show the “how will you pay for this?” screen. 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 Client and Servers in SQL Environment CONNECT TO <server name> AS <connection name> AUTHORIZATION <name and password> - A client may maintain several connections to the server, but only one can be active at any point in time - Operations performed while a connection is active form a session SQL Connection SQL Client Session Server Three Kinds of ‘Modules’ • Module = application program • Embedded SQL • Generic SQL Interface • True modules – stored functions and procedures Embedded SQL • Embed SQL in the host language. – A preprocessor converts the SQL statements into special API calls – optimized access plans that are stored in the database – Then a regular compiler is used to compile the code. • SQL statements can refer to host variables (including special variables used to return status). – Input: variable given a value by the host program to be used in a query – Output: variable used to retrieve value from the database • Language constructs: – Connecting to a database: EXEC SQL CONNECT – Declaring variables: EXEC SQL BEGIN (END) DECLARE SECTION – Statements: EXEC SQL Statement; Declaring Variables • All variables used in SQL statements must be declared EXEC SQL BEGIN DECLARE SECTION char c_sname[20]; long c_sid; short c_rating; float c_age; EXEC SQL END DECLARE SECTION be careful with type mismatches: check basic type correspondences, e.g. in C, Varchar(n) = char[n+1] Executing Statements EXEC SQL INSERT INTO SAILORS(sname,sid,rating,age) VALUES (:c_sname, :c_sid, :c_rating, :c_age) EXEC SQL UPDATE SAILORS SET rating = :c_new_rating WHERE sid = :c_sid These queries could be used to populate and update the Sailors table through a Web interface Error/Status Reporting • Each SQL statement executed returns a status code + additional information – SQL communication area (SQLCA) • Two special “error” variables: ‒ SQLCODE: product specific • zero indicates normal execution • negative indicates an error has occurred • positive indicates warnings or special conditions, e.g., data not found ‒ SQLSTATE: defined by ANSI/ISO SQL92 standard • Error class, e.g., syntax error • Error subclass – implementation dependent Impedance Mismatch • Updates are simple to embed: they execute, possibly modify the contents of the database, and return SQLCA structure indicating what happened • Queries return data! • SQL relations are (multi-) sets of records, with no a priori bound on the number of records. No such data structure exist traditionally in procedural programming languages such as C • SQL supports a mechanism called a cursor to handle this. Cursors • Can declare a cursor on a relation or query statement (which generates a relation) – Name associated with a query • Can open a cursor, and repeatedly fetch a tuple then move the cursor, until all tuples have been retrieved • Open causes the query to be evaluated EXEC SQL open c END-EXEC • Fetch causes values of one tuple in the result to be placed on host variables EXEC SQL fetch c into :cn, :cc END-EXEC Repeated calls to fetch get successive tuples in the query result Cursors (cont.) • SQLSTATE gets set to ‘02000’ to indicate no more data is available • The close statement causes the database system to delete the temporary relation that holds the result of the query EXEC SQL close c END-EXEC Note: above details vary with language. • Can use ORDER BY to control the order in which tuples are returned Cursor: Example Define SQL query that gets names of sailors who’ve reserved a boat of color desired_color, in alphabetical order, and declare a cursor for it EXEC SQL DECLARE sinfo CURSOR FOR SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=:desired_color input variable ORDER BY S.sname When are input host variables bound? When open is executed! Embedding SQL in C: An Example char SQLSTATE[6]; EXEC SQL BEGIN DECLARE SECTION char c_sname[20]; short c_minrating; float c_age; char user_name[10]; char user_pwd[10]; EXEC SQL END DECLARE SECTION c_minrating = 5; strcpy(user_name,”juliana”); strcpy(user_pwd,”xxxxx”); EXEC SQL CONNECT :user_name IDENTIFIED BY :user_pwd EXEC SQL DECLARE sinfo CURSOR FOR SELECT S.sname, S.age FROM Sailors S WHERE S.rating > :c_minrating input variable ORDER BY S.sname; EXEC SQL OPEN sinfo do { EXEC SQL FETCH sinfo INTO :c_sname, :c_age; output printf(“%s is %d years old\n”, c_sname, c_age); } while (SQLSTATE != ‘02000’); variables EXEC SQL CLOSE sinfo; FETCH Statement • What happens if updates are applied to table(s) referenced in a cursor after a result is fetched from a cursor? • Important note [Chamberlin, A Complete Guide to DB2]: The result of a query associated with a cursor may be – Completely materialized when the 1st row is fetched, or – Materialized one row at a time • Choice depends on the access plan chose by the optimizer Updates Through Cursors Can update tuples fetched by cursor by declaring that the cursor is for update" declare c cursor for select *# from account where branch-name = $Perryridge%# for update" ! To update tuple at the current location of cursor" update account set balance = balance + 100# where current of c# ! Apart from their method of finding the row to be updated/deleted, positioned updates behave exactly the same as UPDATE and DELETE statements that contain a search condition# Dynamic SQL •! Sometimes, we can’t predict in advance what SQL statements will be needed •! Dynamic SQL allows construction of SQL statements on-the-fly –! Need to PREPARE/EXECUTE –! Example: char c_sqlstring[]= {“DELETE FROM Sailors WHERE raiting>5”}; EXEC SQL PREPARE readytogo FROM :c_sqlstring; EXEC SQL EXECUTE readytogo; •! Run-time overhead – use only when essential –! Incurs the cost of access path selection (optimization) at run time –! Static SQL prepares statements in advance – well-suited for applications that perform repetitive queries/transactions Database APIs: Generic Interface Rather than modify compiler, add library with database calls (API) •! Special standardized interface: procedures/objects •! Pass SQL strings from language, presents result sets in a language-friendly way •! Embedded vs. Generic API: the difference is more a matter of look and feel than of substance •! Sun’s JDBC: Java API •! Supposedly DBMS-neutral –! a “driver” traps the calls and translates them into DBMS- specific code –! database can be across a network Database APIs: Generic Interface See textbook JDBC: Architecture for details! •! Four architectural components: –! Application (initiates and terminates connections, submits SQL statements) –! Driver manager (load JDBC driver) –! Driver (connects to data source, transmits requests and returns/translates results and error codes) –! Data source (processes SQL statements) Java Driver Oracle applet API Java JDBC JDBC application Driver DB2 Driver Manager XSB-Oracle Interface •! Allow calls to Oracle from Prolog •! XSB-specific calls: db_open(oracle(Name, Pass)) write('% Connected to Oracle as '), write(Name), writeln(' ...'),nl, db_sql('drop table DEPT'), db_create_table('DEPT', 'DEPTNO NUMBER(2),DNAME VARCHAR2(14)'), db_import(‘DEPT'(‘DEPTNO',‘DNAME'), deptall), db_insert(dept_put(A1,A2),(deptall(A1,A2))), dept_put(1,’computer science’), dept_put(1,’biology’),… Stored Procedures/Functions •! Issues with accessing DB from remote application: –! JDBC incurs data transfer overheads –! DB resources are tied up, e.g., open cursors •! Advantageous to execute some of the application logic inside the database –! Minimize data transfer
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages59 Page
-
File Size-