DB Interface Module User’s Guide : PART III Using Advanced Features : Chapter 8 Bulk Reading and Writing : Examples with Bulk Classes
Examples with Bulk Classes
This section describes examples with bulk classes.
Bulk Read and Writes
Example 13 illustrates the use of RWDBBulkInserter and RWDBBulkReader to insert, read, and print out numeric and date values in the purchase table using RWDBTBuffer<T>.
Example 13 – Bulk reads and writes
void insertPurchaseTable(const RWDBDatabase& db) {
RWDBTable tab = db.table("purchase");
RWDBBulkInserter ins = tab.bulkInserter(db.connection());
const int dataElements = 10;
RWDBTBuffer<int> videoID(dataElements);
RWDBTBuffer<int> supplierID(dataElements);
RWDBTBuffer<int> purchaseOrderNumber(dataElements);
RWDBTBuffer<double> pricePerUnit(dataElements);
RWDBTBuffer<int> quantity(dataElements);
RWDBTBuffer<RWDateTime> dateBuffer(dataElements);
 
// Bind the arrays to the inserter before execution.
ins << videoID << supplierID << purchaseOrderNumber
<< pricePerUnit << quantity << dateBuffer;
 
// Initialize data:
RWDateTime now(RWDateTime::setCurrentTime);
for (int i = 0; i < dataElements; i++) {
videoID[i] = supplierID[i] = purchaseOrderNumber[i] =
pricePerUnit[i] = quantity[i] = i;
dateBuffer[i] = now;
}
 
ins.execute();
return;
}
 
 
void readPurchaseTable(const RWDBDatabase& db) {
RWDBTable tab = db.table("purchase");
RWDBBulkReader rdr = tab.bulkReader(db.connection());
 
const int dataElements = 10;
RWDBTBuffer<int> videoID(dataElements);
RWDBTBuffer<int> supplierID(dataElements);
RWDBTBuffer<int> purchaseOrderNumber(dataElements);
RWDBTBuffer<double> pricePerUnit(dataElements);
RWDBTBuffer<int> quantity(dataElements);
RWDBTBuffer<RWDateTime> dateBuffer(dataElements);
 
// Bind the arrays to the reader before execution.
rdr << videoID << supplierID << purchaseOrderNumber
<< pricePerUnit << quantity << dateBuffer;
 
// Fetch up to dataElements rows at a time.
size_t rowsRead= 0;
while (rowsRead = rdr()) {
for (size_t i = 0; i < rowsRead; i++) {
cout << videoID[i] << supplierID[i]
<< purchaseOrderNumber[i]
<< pricePerUnit[i] << quantity[i]
<< dateBuffer[i].asString() << endl;
}
}
}
Bulk Classes and RWDBBlob
Example 14 uses classes RWDBBulkInserter and RWDBBulkReader along with RWDBTBuffer<T> to insert and read binary data. To use this code, the <ver> placeholder in any library names would need to be replaced with the actual digits representing the library version number.
Example 14 – Bulk classes and RWDBBlob
#include <rw/rstream.h>
#include <rw/db/db.h>
#include <rw/db/tbuffer.h>
 
int main()
{
RWDBDatabase db = RWDBManager::database(
"ctl<ver>15d.dll", // Access Module type
"aServer", // server name
"aUser", // user name
"aPassword", // password
"aDB" // database name
);
 
// get a connection
RWDBConnection cn = db.connection();
 
// create an example table.
RWDBTable tbl = db.table("binExample");
if( tbl.exists(cn) ) tbl.drop(cn);
RWDBSchema sch;
sch.appendColumn("cBlob", RWDBValue::Blob, 30);
db.createTable(tbl.name(), sch, cn);
 
// scope this section so that the RWDBBulkInserter
// will release the connection after inserting.
// This is also needed for Sybase to finish bulk insertion.
 
{
// insert some data
RWDBTBuffer<RWDBBlob> binBuffer(5);
RWDBBlob aBlob("hello there\0again", 17);
for (size_t i = 0; i < 5; ++i)
binBuffer[i] = aBlob;
RWDBBulkInserter ins = tbl.bulkInserter(cn);
ins << binBuffer;
ins.execute();
}
 
// scoped so that RWDBBulkReader releases the connection
{
// now read back the data and print it to the screen
RWDBBulkReader rdr = tbl.bulkReader(cn);
RWDBTBuffer<RWDBBlob> binBuffer(5);
rdr << binBuffer;
RWDBBlob aBlob;
size_t rowsRead;
while (rowsRead = rdr()) {
for (size_t i = 0; i < rowsRead; ++i) {
aBlob = binBuffer[i];
std::cout << "the blob's length is: "
<< aBlob.length() << std::endl;
std::cout << "the blob's data is: ";
unsigned char* ptr = aBlob.data();
for (size_t j = 0; j < aBlob.length(); ++j, ++ptr) {
std::cout.put(*ptr);
}
std::cout << std::endl;
}
}
}
 
tbl.drop(cn);
return 0;
}
For more information on bulk reading and writing, see the entries in the SourcePro API Reference Guide on RWDBBulkInserter and RWDBBulkReader, and their related class RWDBTBuffer<T>.