Type and Error Checking in RWDBStoredProc
RWDBStoredProc does not check actual parameters for type; instead, it allows the database to do type conversion where possible. If there is a type incompatibility, the DB Interface Module passes along whatever the database reports.
The DB Interface Module produces an RWDBStatus::invalidPosition error if too many arguments are inserted into an RWDBStoredProc. No check is made for too few arguments, since the underlying database may supply defaults. If it does not, the DB Interface Module passes along any errors reported by the database.
In the next example, suppose there is a database procedure called squareRoot that expects two parameters: an integer x, and a floating point number y. The procedure is supposed to compute the square root of x. If no error occurs, it places the result in y and returns 0; otherwise it returns 1 and leaves y unchanged.
 
RWDBStoredProc squareRoot = myDbase.storedProc("squareRoot"); //1
int x;
double y;
squareRoot << x << &y; //2
squareRoot.execute(myConnection); //3
squareRoot.fetchReturnParams(); //4
RWDBValue retValue = squareRoot.returnValue(); //5
if (retValue.asInt() == 0) {
cout << "Sqrt " << x << " = " << y << endl;
}
else {
cout << "An error occurred" << endl;
}
 
On //1, an RWDBStoredProc instance is obtained from the database. This involves using a connection supplied by myDbase in order to query the database for information about the procedure's parameters.
The first argument to squareRoot is an IN parameter, and the second is an OUT parameter used for obtaining the data. The parameters are passed on //2. First the IN parameter is inserted, followed by the address of the OUT parameter.
The call to execute() on //3 causes the stored procedure to be executed by the database server. As usual, the execute() method returns an RWDBResult, which we have elected to ignore in this case. On //4, a call to fetchReturnParams() ensures the OUT and IN/OUT parameters are fetched. Starting on //5, we check the procedure's return value. If the return value is zero, the result of the computation is in the variable y.