Embedded SQL Precompiling Embedded SQL Call Level Interface
Total Page:16
File Type:pdf, Size:1020Kb
Embedded SQL Call level interface (CLI) Extract from C program: An application programming interface (API), i.e.itprovides a set of EXEC SQL BEGIN DECLARE SECTION; function calls. short DEPT, DNO; In the SQL standards wor ld, ‘‘CLI’’means ‘‘not Embedded SQL’’ char NAME[26]; EXEC SQL END DECLARE SECTION; (which is also referred to as and API). main() •aclean separation between the application programand SQL. { GetInput(&DEPT); EXEC SQL DECLARE C1 CURSOR FOR •building a programissimpler ; no preprocessor (although calls to SELECT ENAME, DEPTNO FROM EMP WHERE DEPTNO = :DEPT; the DBMS runtime librar y are the same). EXEC SQL OPEN C1 while (SQLCODE == 0) { EXEC SQL FETCH C1 INTO :NAME, :DNO •debugging is simpler;you debug your own code rather than code } generated byaprecompiler. EXEC SQL CLOSE C1 } Precompilers from different vendors will produce different code. Graham Kemp,University of Aberdeen Graham Kemp,University of Aberdeen Precompiling Embedded SQL Programming models for relational systems The precompiler and DBMS interact in severalways: What if wewant to change the DBMS that our application uses? Embedded SQL •The DBMS parser checks the syntax of the SQL statements. Requires compiling application programusing newDBMS vendor’s precompiler.Wemight not have the source code (e.g. ‘‘shr ink- •The DMBS checks the semantics of the SQL statements (table wrapped’’applications). and column names are correct, data types of host language variables are correct, etc.) Vendor-specific API Requires changing the source code to use the newDBMS vendor’s •Ifthe SQL statements are OK, optimise them to produce an API, and recompiling with the newDBMS vendor’slibrar ies.Again, access plan, which is stored in the database. we might not have the source code. ODBC •Relevant sections of the access plan are stored as arguments to Edit the ‘‘.odbc.ini’’file to refer to a newODBC driver.Donot change function calls generated bythe precompiler. the source code.Donot recompile.ODBC driver manager will pick up the newdriveratruntime. Graham Kemp,University of Aberdeen Graham Kemp,University of Aberdeen ODBC architecture ODBC SQL execution models ExecDirect Application Combine all steps in a single function call (SQLExecDirect). Prepare/Execute ODBC Driver Manager Separate steps 1, 2 and 3 from 4 and 5. Good when same SQL statements (possibly with parameters) are executed repeatedly; only ODBC Driver needs to be prepared once,and access plan will be execute immediately each time.Access plan is deleted from DBMS after Network/Communications Software programends. Stored procedures DBMS LikePrepare/Execute,but preparation step can be independent from application program. Stored procedure persist in database beyond the runtime of the application. Application doesn’t need to perfor m preparation phase. Graham Kemp,University of Aberdeen Graham Kemp,University of Aberdeen Executing SQL statements 1. For mulate SQL statement 2. Send SQL statement to DBMS 3. Parse and optimise the SQL statement to produce an access plan. 4. Execute the access plan. Author isation checks done here (although these could be done in step 3). 5. Client and serverinteract to send status infor mation and data to the client as requested bythe client. Graham Kemp,University of Aberdeen.