Inserting Data with a Custom Callback Class
To use a custom callback class with Open SQL to insert data, the onSend() method must be implemented. Also, the getLength() method may need to be overridden depending on whether the database vendor requires the total length of the data to be inserted before the operation is executed. See the relevant access module guide to determine if getLength() is required. An instance of the callback class is associated with an RWDBOSql that is set up to insert data.
Here is an example of inserting data with a custom callback class. It differs slightly from earlier examples in using RWDBCharCallback rather than RWDBBinaryCallback. Note that the datatype of theData has changed.
 
class MyCallback : public RWDBCharCallback
{
public:
MyCallback(size_t rowsetSize) : RWDBCharCallback(rowsetSize)
{ }
 
virtual ~MyCallback() { }
 
bool onSend(size_t rownum, char* theData, size_t& length,
RWDBNullIndicator& ni, bool& lastPiece)
{
// Assume the data is being read from some pre-defined source
if (!getDataFromDataSource(rownum, theData, length, ni, lastPiece))
return false;
return true;
}
// We may or may not need to return the length of the data we're sending.
// This depends on the access module in use.
size_t getLength(size_t rownum) const
{
return getDataLengthFromDataSource(rownum);
}
. . .
};
 
RWDBDatabase aDB = RWDBManager::database(...);
RWDBConnection aConn = aDb.connection();
RWCString smallStringData[10];
populateStrings(smallStringData, 10);
RWDBOSql inserter("INSERT INTO STRINGS VALUES (:ph1, :ph2)"); // 1
RWDBTBuffer<RWCString> dataBuffer(smallStringData, 10); // 2
MyCallback myCB(10); // 3
inserter << dataBuffer << myCB; // 4
inserter.execute(aConn); // 5
//1 Note that the placeholder syntax used here is specific to Oracle. It will obviously differ for other databases.
//2 Creates an RWDBTBuffer for the first placeholder in the inserter.
//3 Creates an instance of the custom callback class, MyCallback.
//4 Binds RWDBTBuffer to the first placeholder, and MyCallback is bound to the second placeholder.
//5 Executes the insert statement, during which the callback class onSend() method is called repeatedly until all the data is sent, or the operation is terminated.