Obtaining Data from RWDBMultiRow
When using
RWDBMultiRow, you have two options for obtaining data from its
RWDBTBuffers: the
bufferAt() method, and the
operator[]. The
bufferAt() method provides access to a contained
RWDBTBuffer by returning a pointer to an object that is an
RWDBAbstractBuffer, the base class of all
RWDBTBuffer template instantiations. Since
RWDBTBuffer is a template class, however, you may
not know the specific type on which the template is instantiated; not knowing the type makes the method unusable for ad hoc query situations, like the one above. For these situations,
RWDBMultiRow also provides an
operator[], returning an
RWDBRow.
Class
RWDBMultiRow’s
operator[], which takes a single
size_t parameter,
n, creates a row of data by taking a cross-section of all the data in its
RWDBTBuffers at entry
n. It converts the data to
RWDBValues, and returns the
RWDBValues in an
RWDBRow. Using
operator[], you have a convenient row-access method to
RWDBMultiRow. Furthermore, since the data is converted to
RWDBValues, you can easily interrogate the data’s type, convert the data to other types, and format the data as a string for output, using methods on
RWDBValue.
Let’s now use
operator[] to write the
processResults() function we called in the example in
Using RWDBMultiRow for Ad Hoc Queries:
void processResults(RWDBMultiRow& theRows)
{
// We assume there is only one row in the MultiRow.
// Create an RWDBRow for the data in the 0th entries
// of the RWDBTBuffers.
RWDBRow r = theRows[0]; //1
// Print out the values in the row.
for(size_t i = 0; i < r.entries(); i++) { //2
RWDBValue theField = r[i]; //3
cout << theField.asString(); //4
cout << '\t';
}
cout << endl;
}
On
//1, an
RWDBRow is created from the 0th entries in the
RWDBTBuffers contained by the passed-in
RWDBMultiRow. When this occurs, all the 0th entries in the contained
RWDBTBuffers are automatically converted into
RWDBValues and appended to the
RWDBRow. As in
Using RWDBMultiRow for Ad Hoc Queries, please note that this feature adds overhead to an application in both performance and memory usage. While it may be very helpful in some situations, its use should be restricted to situations where the types of data are not known and
RWDBTBuffers cannot be created based on the
RWDBSchema returned by the
RWDBOSql::schema() call.
NOTE: This technique may impede performance.
On
//2, we loop through the
RWDBRow, using its
entries() method. On
//3, inside the loop, we obtain the
RWDBValue for the current entry in the
RWDBRow using
RWDBRow’s
operator[]. On
//4, we use the
asString() method of
RWDBValue, which can convert any data into a string, and we output this string to
cout.