670 likes | 862 Views
No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.edu Available Oct 2 only until 2 pm. CS 457 - Embedded SQL. To Embed SQL in C/C++. Why do we want to embed SQL in C/C++? 1) Read in data from file, insert into relation
E N D
No class Thurs. Oct. 2 E-day • Exam 1 Thurs. Oct. 9 • E-mail HW3 Oct. 2 to vrbsky@cs.ua.edu • Available Oct 2 only until 2 pm
To Embed SQL in C/C++ Why do we want to embed SQL in C/C++? 1) Read in data from file, insert into relation 2) Compute results based on result from query e.g. generate a report 3) Provide a user interface for SQL if the current one is lacking
Must have: to do 1) must read in values into C variables then use those values to insert using SQL still need SQL statement to insert, select tuples to do 2) must be able to manipulate results from SQL query, but mismatch between C and SQL sets versus one record at a time to do 3) need to accept queries - create SQL queries
Embedded SQL Select dnumber From department Where mgrssn = 987654321 What is needed? • Variables in which to place result (Host variables) • distinguish SQL statements from C statements (EXEC SQL) • Processing of result table (cursors) • Data structure for communicating with DBS in case of errors (SQLCA) • What if we want to process any query typed in? (Dynamic SQL)
To read in data from a file Want to: Loop until the EOF (Need to test this) read values from file – C/C++ code manipulate values with C/C++ code insert into relation values (SQL) End loop
Static SQL • Embedded Select statement EXEC SQL Select [distinct] expr {, expr} into host_var {, host_var} From table_name [alias] {, table_name [alias]} [Where search_cond] [Group by col {, col}] [Having search_cond] EXEC SQL select lname, salary into :lname, :sal From employee Where ssn=123456789;
Static SQL • For what type of query is static SQL appropriate? • Retrieves a single row • Union permitted in SQL-92, as long as returns 1 row • As we will see later, can retrieve multiple rows if use arrays in Oracle
Host variables (host_var) • Referenced by SQL and C/C++ • prefix with : in SQL statements • transmit data between DB manager and application • must declare in a Begin Declare Section
Begin Declare Section EXEC SQL Begin Declare Section; short age = 26; long dept; double salary; char ch; char name[9]; EXEC SQL End Declare Section;
Host variables • Host variables are also used in the search_cond as numerical or char strings in an expression Where ssn = :hv1 Where salary > :hv2 • CANNOT be col_names, tables or logical conditions in expression which require compiler attention at runtime after the value of the host variable value is set
ORACLE host variables • See earlier slide (SQL) on Oracle and C/C++ types • some conversions are possible • Previously, in embedded SQL no arrays allowed except char • Oracle 9i allows host variables to be arrays
Array interface to Oracle • Oracle (Pro C/C++) lets you define array host variables (called host arrays) and arrays of structures and operate on them with a single SQL statement. Using the array SELECT, FETCH, DELETE, INSERT, and UPDATE statements, you can query and manipulate large volumes of data with ease. You can also use host arrays inside a host variable struct • The maximum number of array elements in an SQL statement that is accessible in one fetch is 32K
Array interface to Oracle char emp_name[50][20]; int emp_number[50]; float salary[50]; EXEC SQL SELECT ENAME, EMPNO, SAL INTO :emp_name, :emp_number, :salary FROM EMP WHERE SAL > 1000;
Array interface to Oracle • If there are < 50 rows, will work. • If there are > 50 rows, it will not work. If you reissue the select statement, it will retrieve the first 50 rows again. • You must use a cursor in this case • Therefore, if you do not know the maximum number of rows a SELECT will return, you can declare and open a cursor, then fetch from it in "batches."
Cursors • How to retrieve multiple rows for this query with embedded select without using arrays? Select essn, hours From WORKS_ON Where pno = :proj_id; • Use a cursor • A cursor points to 1 row • A cursor is used instead of placing all of results in an array (may be too large)
Using cursors • 3 steps involved: • 1) declare cursor - just a definition of the select • 2) open cursor - executes select, builds result table • 3) fetch results - to navigate through the results
EXEC SQL using cursors EXEC SQL declare cursor_name cursor for subselect; • When this statement is issued, select is executed: EXEC SQL open cursor_name; • Must place this statement in a loop to get each row in result: EXEC SQL fetch cursor_name into :hv1 {, :hv2} • Can close and open the cursor again with a different values for host vars: EXEC SQL close cursor_name;
Cursors EXEC SQL declare c1 cursor for Select essn, hours From WORKS_ON Where pno = :proj_id; EXEC SQL open c1; EXEC SQL fetch c1 into :essn_id, :hrs; while (sqlca.sqlcode == 0) // checks for EOT { cout << essn_id << “ “ << hrs << endl; EXEC SQL fetch c1 into :essn_id, :hrs; };
Cursor • Cursor points to prior row, when it is opened, it points to position just before the first row • As the cursor is incremented, the values of rows are retrieved into host vars • Cursor is scrollable forward and backward
How does Embedded SQL work? • Since it is a C/C++ program, must be able to compile • Hence, must precompile to identify SQL statements • SQL statements are replaced with calls to SQLLIB routines (API calls to data manager that are recognized by C/C++)
Precompiler for Oracle • start with a source file: fn.pc • Precompiler generates file: fn.c/fn.cpp • (internal representation of SQL statements that were replaced by calls to SQLLIB routines- orasql9.lib) • if examine fn.c/fn.cpp can see all the SQLLib calls • Then you compile your program using C/C++
Pro*C • We will use Pro C/C++ to precompile our .pc file which is C/C++ program containing embedded SQL • Must precompile .pc file using Pro C/C++, or can access it through .NET • Oracle Pro*C info
What is needed in .NET to use Oracle? • Must set up the environment • Add path for oracle executable files • C:\Program Files\Oracle\Ora90\bin • Add path for Oracle include files • C:\Program Files\Oracle\Ora90\precomp\public • Add path for Oracle library files • C:\Program Files\Oracle\Ora90\precomp\lib\msvc • Add orasql9.lib for Linker
Connect • must connect to DBMS • Include the following in C/C++ program EXEC SQL connect :user_name identified by :user_pwd using :host_string; EXEC SQL disconnect; Sample program How to set up .NET in our lab to use Pro C
C program and DBMS • How does the loop terminate? • With a test of the SQLCA structure field to terminate loop will discuss this later
Test Oct. 9 • bring 1 sheet of paper with notes (use both sides)
Industrial Colloquium Series speaker • Jim Azar, UA graduate in '83 • Topic: "What does it take to be successful in the software development industry" • Tuesday night at 5pm • HO119
Embedded SQL Delete EXEC SQL delete from table_name [alias] [Where search_cond]; • If no where clause, all rows are deleted • If there is a where, a search-delete is performed
Positioned Delete using cursor EXEC SQL delete from table_name [alias] Where current of cursor_name; • Deletes row cursor pointing to, then cursor moves to just before next row • Useful if want to print information before it is deleted • If an empty table, not found occurs with next fetch • Need to have the fetch statement at the beginning of a loop, before the delete
Delete using cursor • Must indicate will make changes to the table when cursor is declared. The following precedes a delete statement: EXEC SQL declare cursor_name cursor for subselect {Union subselect} [Order by | For update of ]; • Only 1 table can be specified in the cursor subselect for delete (or update)
Positioned delete example EXEC SQL DELETE FROM emp WHERE deptno = :deptno AND job = :job; EXEC SQL DECLARE c3 CURSOR FOR SELECT empno, comm FROM emp FOR UPDATE OF emp; // This is optional in Oracle EXEC SQL OPEN c3; EXEC SQL FETCH c3 INTO :emp_number, :commission; cout << emp_number << commission << endl; EXEC SQL DELETE FROM emp WHERE CURRENT OF c3;
Delete • Deletions are made to the base table • The for update of indicates will make changes to base table • Standard requires specified if delete (or update) – optional in Oracle • Can choose both update of and order by
Update Updates to base table: • search-update EXEC SQL update table_name [alias] set col = expr {, col = expr} [Where search_cond];
Positioned-update using cursor • Again must indicate will make changes to table, and can only have 1 table EXEC SQL declare cursor_name cursor for subselect {Union subselect} [Order by | For update of col {, col}]; • positioned-update EXEC SQL update table_name [alias] set col = expr {, col = expr] Where current of cursor_name; (cursor must be pointing to a valid row)
Insert • No need for cursor, can't specify position of new row EXEC SQL insert into table_name [ (col {, col})] values (:hv1 {, :hv2}); • Also available: EXEC SQL Create table Drop table
SQLCA • SQL communication area - a structure • used for communication between DBS monitor and C++ program EXEC SQL include SQLCA • allocates program space for errors and starts communication by DBS monitor
SQLCA • after each SQL statement executed, a new value is placed in SQLCA • indicates if successful, EOF, etc. • error or warning conditions
SQLCA • sqlca.sqlcode - testing code part of structure • sqlcode = 0 successful sql call • < 0 error • > 0 warning - call successful but some condition existed e.g. EOF is 100 (DB2,Ingres, but not ORACLE) • use sqlwarn for further info • sqlerrd[2] - indicates number of row affected by insert, update or delete (used for referential integrity)
Whenever • Can test sqlcode directly or use whenever EXEC SQL whenever condition action; • condition can be: • sqlerror - tests of sqlcode < 0 • not found - tests when no rows affected - good for EORows in ORACLE • sqlwarning - tests if sqlcode > 0, other than not found
Whenever • action can be: • do fnCall - do procedure call • stop - cannot be specified for not found • goto label - must be in the scope of subsequent EXEC SQL statements, preprocessor substitutes condition with the goto label • continue - no action taken unless fatal error, such as "fails to connect"
Whenever Example: EXEC SQL whenever sqlerror stop; • precompiler implements whenever by inserting tests after every subsequent run-time DB system call • does not follow the flow of control - just physical location of whenever statement • Must be careful not to set up infinite loops • If no whenever - default is to continue processing, even if error, etc.
Error messages • printing error messages in ORACLE - can extract the error message • The following can be defined by the user: int report_error () { cout << "error occurred" << endl; sqlca.sqlerrm.sqlerrmc[sqlca.sqlerrm.sqlerrml]= '\0'; cout << sqlca.sqlerrm.sqlerrmc << endl; return -1; } EXEC SQL whenever sqlerror do report_error();
Example EXEC SQL fetch c1 in :hv1; while (sqlca.sqlcode == 0) { cout << hv1 << endl; EXEC SQL fetch c1 into :hv1; }
Dynamic SQL • Useful when: • Format of SQL statement is not known - can generate during execution • Statement known but objects referenced don't exist at compile time
Dynamic SQL • Different types of dynamic SQL statements: • execute immediate • prepare • execute • cursors
Dynamic SQL • To execute a dynamic SQL statement, must place a copy of an SQL query into a char string variable char stmt1[80] = " "; // in Declare Section strcpy (stmt1, "Delete From employee where ssn = 987654321"); or: cin >> stmt1;
Execute immediate • Execute immediate statement EXEC SQL Execute immediate :stmt; • where stmt contains an SQL statement as a char string EXEC SQL Execute immediate :stmt1; • Prepares and executes an SQL statement that does not use any host variables
Execute immediate • Can NOT be used for any select statements • Can use it for update, drop, create index, create table, create view, etc.
Execute immediate • Execute immediate statement is precompiled • When it is executed at run time: • executable SQL stmt constructed from SQL char string text • executable form of SQL statement is processed • executable form of SQL statement is destroyed • Recompiles a new SQL statement each time execute this statement