Database Tables
The most straightforward way to obtain an RWDBTable is to request one by name from RWDBDatabase:
 
RWDBTable purchases = myDbase.table("purchase");
The purchases object allocated by this code fragment represents the tabular collection of data known as the purchase table in myDbase. Allocating it is relatively inexpensive, owing to one of the design axioms of the DB Interface Module:
NOTE: All database access is delayed until absolutely necessary, and unnecessary database interaction is avoided entirely.
This axiom exists because hitting the database is the most expensive thing that database applications do. In the example above, it's unnecessary to consult the database in order to create the table object; database interaction is deferred until database services are requested. Here’s an illustration:
 
RWDBTable purchases = myDbase.table("purchase"); //1
if (!purchases.exists(conn1)) { //2
std::cout << "purchase table does not exist!" << std::endl;
}
else {
RWDBSchema purchasesSchema = purchases.describe(RWDBTable::ColumnList); //3
std::cout << "Columns in the " << purchases.name() //4
<< " table:" << std::endl;
size_t numCols = purchasesSchema.entries(); //5
for (size_t i = 0; i < numCols; i++) { //6
std::cout << "\t" << purchasesSchema[i].name() << std::endl; //7
}
}
 
Here //1 does not require the application to access the database. The exists() call on //2 must access the database to determine whether the purchase table actually exists. The API of the DB Interface Module makes it easy to determine whether a method will access the database or not:
NOTE: Methods that require database access may be called with a connection parameter. Methods without a form that accepts a connection parameter do not access the database.
A table in a relational database is defined by its schema, which is an ordered collection of column definitions. The RWDBSchema object declared on //3 of the example above represents the schema of the purchase table. It is an ordered collection of RWDBColumn instances. To illustrate its use, the code on //5 determines the number of columns in the purchase table, and the code on //6 and //7 print the name of each column.
Finally, a table in a relational database always has an owner, the person who controls access to the table and data within the table. The owner of a table can grant privileges to other users, such as access, insert, update, and so on. For example, an application may be able to determine if a table exists, but not update the data. Be sure to determine the ownership and privileges of a table before working with it.