Step 3: Accessing Data Directly

This step shows how to use external binding, as well as the advantage of using it. The external binding feature (as opposed to internal binding) is first presented.

Binding is the process of sending and receiving RDBMS data directly to and from the application memory.

In previous steps, the DB Link default mode was used. This mode is called internal binding. With internal binding, DB Link automatically allocates the memory where data is stored. Then, the data must be copied from the application memory to the memory allocated by DB Link. For example, in Step 1, the setParamValue method is used to copy the value needed to the buffer allocated by DB Link.

This is the easiest method since the application does not need to do an explicit binding— DB Link does it by itself. However, this method is less efficient since the value has to be copied from the application memory area to the area allocated by DB Link.

With external binding, the application allocates the memory and tells DB Link to use this area directly. This is done using one of the binding methods, bindCol and bindParam. The bindCol method is used when retrieving column data from the RDBMS. The bindParam method is used to bind application memory to the array of value arguments for a query with parameters.

To see how this works from the input side (writing data to the RDBMS), the memory to be used is first allocated. An array of parameters is used, as described in the previous step, to run only one execute call. This is done as follows:

static const IldUShort nbParam = 5 ;

// strBuf will be used to store an array of 5 string values.

char strBuf[nbParam][strLen + 1] ;

IldInt intBuf[nbParam] ;

short strNulls[nbParam], intNulls[nbParam] ;

This declares an array of five strBuf strings where string parameter values are stored.

Then, the intBuf array records the integer parameter values.

The strBuf array is used with strNulls, and the intBuf array is used with intNulls. These arrays record the NULL indicators. They are initialized with zeros to specify that the parameters are not NULL.

A null indicator is required to specify that the value is NULL. In the context of an RDBMS, a null value means that there is no value at all. For instance, for an integer column, a null value is NULL, not zero.

Note

The null indicators are not required when you do not have to handle a null value.

Then, the parameter array size is set as described in the previous step, and the query is parsed.

The intBuf and strBuf buffers are given as arguments to the bindParam method. The use of external binding requires nothing more.

if (!request->bindParam((IldUShort)0,

IldIntegerType,

sizeof(IldInt),

intBuf,

intNulls)) {

IldDisplayError("Bind first parameter failed : ", request) ;

Ending(dbms) ;

exit(1) ;

}

if (!request->bindParam((IldUShort)1,

IldStringType,

strLen + 1,

strBuf,

strNulls)) {

IldDisplayError("Bind second parameter failed : ", request) ;

Ending(dbms) ;

exit(1) ;

}

Now you work directly with the buffers. They are used to send data to the RDBMS.

IldUShort i, j ;

IldInt nbRows ;

for (i = 0 ; i < nbParam ; i++) {

intBuf[i] = i ;

// Build a new string value for this set of parameters.

for (j = 0 ; j < strLen ; ++j)

strBuf[i][j] = 'a' + i ;

}

if (!request->execute(&nbRows, nbParam)) {

IldDisplayError("Insertion failed : ", request) ;

Ending(dbms) ;

exit(1) ;

}

else cout << "\t" << nbRows << " row inserted." << endl ;

Conclusion

This step demonstrates how to bind DB Link directly to application memory. This is an efficient way of exchanging data with the RDBMS.

See source code.