Memory Tables
So far, we have declared all our tables as instances of the base class
RWDBTable. We did this to emphasize the uniform interface to tabular data provided by
RWDBTable. However, there are alternatives to
RWDBTable. For example, class
RWDBMemTable represents a tabular collection of data stored in program memory.
RWDBDatabase instances can produce memory tables in much the same way that they produce database tables:
RWDBTable purchases = myDbase.memTable("purchase", myConnection); //1
The result of this call is to copy all the rows of data from the purchase table in myDbase into program memory. Since this task requires database interaction, you have the option of supplying a connection. You can also obtain memory tables by copying database tables. The constructor on //3 below has the same effect as //1 above.
RWDBTable dbPurchases = myDbase.table("purchase"); //2
RWDBTable mtPurchases =
RWDBMemTable(dbPurchases, myConnection); //3
Note that as a direct result of the inheritance mechanism of C++, you can read the dbPurchases object and the mtPurchases object in the same way, with a reader:
RWDBReader dbRdr = dbPurchases.reader();
RWDBReader mtRdr = mtPurchases.reader();
RWDBMemTable provides other services as well, including an adjustable capacity feature and unrestricted random access via double indexing. For example:
RWDBMemTable mt20Purchases(dbPurchases, 20); //4
size_t numberOfColumns = mt20Purchases[0].entries(); //5
for (int i = 0; i < mt20Purchases.entries(); i++) { //6
for (int j = 0; j < numberOfColumns; j++) //7
std::cout << mt20Purchases[i][j].asString() << "\t"; //8
std::cout << std::endl;
}
On
//4 of the example above, we define a memory table restricted to a maximum of twenty entries. Thereafter, we output each cell of the memory table as if the memory table were a two-dimensional array. On
//5, we determine how many columns there will be by accessing the first row and asking how many columns it has. The loops on
//6 and
//7 set up the indexing of the two-dimensional table, and
//8 prints a cell of the table. Within the
for loop, the first invocation of
operator[] on the
RWDBMemTable returns an instance of
RWDBRow. The second invocation of
operator[] is applied to the row to return an
RWDBValue. The call to
asString() is applied to the
RWDBValue to ensure properly formatted output.
In addition to class
RWDBMemTable, class
RWDBTPtrMemTable<T,C> provides another alternative to
RWDBTable. This class is a template-based memory table, and is documented in the
SourcePro API Reference Guide.
There are limitations regarding the use of memory tables, as not all functionality of tables within databases is supported. Please consult the
SourcePro API Reference Guide to determine the functionality available to your application.