A few days ago, I happened to know a new term: occi. What about Xiami Dongdong? After looking at the time, I checked it out. It turned out to be a set of C ++ interfaces provided by Oracle to access the Oracle database. A friend who has used Microsoft database will immediately think of something, that is, Dao, yes. The principle of this is the same as Dao. Let's take a look at occi. (Dao is the abbreviation of data access object. It is also a set of database access. There are many acronyms in the IT field, and you may not know what to use. It doesn't matter)
Because I checked it directly from the Oracle website. There are two compressed packages in the address below, which contain the occi DEMO code. Let's analyze them from here:
Http://www.oracle.com/technology/global/cn/sample_code/tech/occi/index.html
We can start with the content of the second compressed package, which is the package:
Occiplsql.tar.tar
After the download, decompress the package, which contains three files, one of which is
Storedproc. cpp
Let's start with her. (In fact, this file is a detailed description in the PL/hosts file. You can use schema. SQL to create a stored procedure in your own database, which is omitted here)
Let's analyze one line of code.
First, it calls the header file required by the file:
The most important thing here is occi. H. This is the header file of the occi library. Generally, we need to import this header file later.
The code is followed by a main () function. Let's take a look at the following code:
//initialize in object mode since we are accessing VARRAY types Environment *env = Environment::createEnvironment(Environment::OBJECT); Connection *conn = env->createConnection("scott","tiger"); Statement *stmt = conn->createStatement();
These three pieces of code are the basis for calling occi. In the initial environment, the connection is established, and a statement is created to operate the database.
Let's take a look at how he executes the SQL statement:
// Create anonymous block for executing stored proc/function <br/> stmt-> setsql ("begin occidemo1.getbookdetails (: 1,: 2,: 3,: 4,: 5 ); end; "); <br/> int id = 1; // book to retrieve </P> <p> // pass in parameters by setxxx (), indicate out parameters <br/> // By registeroutparam <br/> stmt-> setnumber (1, ID ); // 1-in-id <br/> stmt-> registeroutparam (2, occistring, 100); // 2-out-title, max = 100 <br/> stmt-> registeroutparam (3, occidouble); // 3-out-price <br/> stmt-> registeroutparam (4, occidate ); // 4-out-pubdate <br/> stmt-> registeroutparam (5, occivector, 0, <br/> "Scott. keywordslist "); // 5-out-keywords (varray) </P> <p> stmt-> execute (); // execute procedure
It's easy, isn't it? The stored procedure is called and several output functions are registered. These are similar to the stored procedure calls on the. NET platform, especially the database calls in JSP (just like that ). Note that the output function calls the occi variable, which is occistring, occidouble, and so on. It is a variable in the form of occi.
Then let's look down:
// Get the out parameters <br/> String title = stmt-> getstring (2); <br/> double price = stmt-> getdouble (3 ); <br/> date pubdate = stmt-> getdate (4); // occi date class <br/> vector <string> keywords; <br/> // There are overloaded getvector methods for each vector <t> <br/> getvector (stmt, 5, keywords );
The comment here is very clear. To get the output value of the output variable of the stored procedure we just defined, we just need to retrieve the result from the statement.
Then some output functions are executed below to print our results:
cout << "Book Details : " << title << " " << price << " " << pubdate.toText() << endl; cout << "Keywords : "; for (int i = 0 ; i < keywords.size(); i++) cout << keywords[i] << ","; cout << endl; cout << "******Calling GetBookList******" << endl;
Then let's look at the second stored procedure below. This is different from the first one (nonsense). What is the difference? Let's take a look at his notes:
// Stored function 2-shows PL/SQL tables and function Returning Value
Use the function return value to obtain the table
/* To pass/retrieve PL/SQL tables use setdatabufferarray method :-
Void setdatabufferarray (unsigned int paramindex,
Void * array, type, ub4 arraysize, ub4 * arrayelements,
Sb4 elementsize, ub2 * elementlengths, sb2 * ind = NULL, ub2 * rc = NULL)
After the execute, the buffer will contain in out values
*/
First, use the setdatabufferarray function to pass and restore PL/SQL tables.
Let's take a look at our first stored procedure, which is a directly transmitted variable and the result. Let's call it a "Direct Method". The second demonstration method is to use the setdatabufferarray function provided by statement for operations.
Note: The annotations here also tell us the prototype of the setdatabufferarray function we will use. You can take a look at it and use it below.
Next let's take a look at the code of the second stored procedure.
First, various variables are defined:
stmt->setSQL("begin :1 := OCCIDEMO1.GetBookList(:2, :3); end;"); int idlist[10]; char booklist[10][200]; ub4 inelems=3,outelems=0; ub2 outelemlens[10], inelemlens[10]; idlist[0] = 1;idlist[1] = 2;idlist[2] = 3;//book Ids 1,2,3 inelemlens[0] = inelemlens[1] = inelemlens[2] = sizeof(int);
Here, we define the confusing ub4, ub2, and other types of variables used by setdatabufferarray. These are actually used to buffer our results.
Then, setdatabufferarray is officially used for the operation:
stmt->setDataBufferArray(2, idlist, OCCIINT, 10, &inelems, sizeof(int), inelemlens);//2 - IN - TABLE OF NUMBER stmt->setDataBufferArray(3, booklist, OCCI_SQLT_STR, 10, &outelems, 200, outelemlens);//3 - OUT - TABLE OF VARCHAR2 stmt->registerOutParam(1, OCCIINT); //1 - Function return value stmt->execute(); //execute the function
Here we can see that setdatabufferarray is used twice. As I have said before, setdatabufferarray is used to buffer our variables (with values), and the final result is contained in outelemlens. (These are mentioned in the program comments)
Then we can get the returned value, as shown below:
Int ret = stmt-> getint (1); // function return value <br/> cout <"function return:" <RET <Endl; </P> <p> cout <"elements returned:" <outelems <Endl; <br/> for (Int J = 0; j <outelems; j ++) <br/> cout <booklist [J] <"[" <outelemlens [J] <"]" <Endl; </P> <p> cout <"******* calling searchbooks ******" <Endl;
Well, this is the use of setdatabufferarray.
Next let's look at the last usage in the example:
// Stored proc 3-returns a ref cursor for a query
It is clear that a ref cursor (reference cursor) that can be used for operation is returned, so that we can operate on the set data. This is also very simple and we know all the databases, let's take a look:
stmt->setSQL("begin OCCIDEMO1.SearchBooks(:1, :2); end;"); //for IN/OUT, input with setXXX, do not call registerOutParam stmt->setString(1, "Databases"); //1 - IN/OUT - varchar stmt->setMaxParamSize(1, 100);//max param size for string parameter stmt->registerOutParam(2, OCCICURSOR);//2 - OUT - Ref Cursor stmt->execute();//execute stored procedure
First, these are the same. They also specify input and output variables and then execute them.
cout << "Searched using keyword: " << stmt->getString(1) << endl; ResultSet *rs = stmt->getCursor(2);//use ResultSet to fetch rows //we know cursor has 1 varchar column in Select, use getString() while (rs->next()) cout << "Book : " << rs->getString(1) << endl; stmt->closeResultSet(rs);
Then, the statement getcursor is used to assign the result to a resultset (result set), and then we can traverse the returned data.
So far, I have explained the PL/SQL language example of occi operations (as if I didn't talk about anything, don't blame me, because it is really simple ^ _ ^, is what we use every day, and the example is also very simple, but this is good, simple is beauul UL ).
Now, it's over. We have to finish the work, as shown below:
It should be a good habit to release the three variables we use in reverse order.
In fact, exception handling is used in this part. We did not mention it here. You can study it on your own.
Here, the simple database operations on occi are over.
If you have time, let's talk about the content of another compressed package in tutorial 2. Debug all the involved code to see how it works.
conn->terminateStatement(stmt); env->terminateConnection(conn); Environment::terminateEnvironment(env);
#include <iostream>#include <string>#include <vector>#include <occi.h>