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;
}
}
}