The Program
This section examines the t1 program you just ran. Its purpose is to output the mailing labels for all the customers to a file called t1out.txt.
The Main Routine
This code sample shows how to use the classes VVContact and VVContactRepository. The line numbers correspond to the comments that follow the code.
#include <rw/db/db.h> //1
#include "conrep.h" //2
#include "tututil.h" //3
int main(int argc, char** argv) //4
{
associateStreams("", "t1out.txt", "t1err.txt"); //5
RWDBManager::setErrorHandler(outputStatus); //6
RWCString serverType, serverName, userName,
password, databaseName, pstring; //7
initializeDatabaseArguments(argc, argv, serverType,
serverName, userName, password,
databaseName, pstring); //8
RWDBDatabase aDB = RWDBManager::database
(serverType, serverName, userName,
password, databaseName, pstring); //9
VVContactRepository customerPool(aDB, customerTableName); //10
customerPool.mailingLabels(outStream); //11
closeStreams("", "t1out.txt", "t1err.txt");
return 0;
} //12
Here is a line-by-line description of the program:
This program is really quite short, considering all that it accomplishes. Lines //10 and //11 do most of the work. A detailed examination of the code behind these lines is important for understanding how the DB Interface Module is best used.
VVContactRepository Constructor
Let’s look closely at the constructor for VVContactRepository found in the file conrep.cpp. This constructor sets the stage for manipulating the pool of customers.
VVContactRepository::VVContactRepository
(const RWDBDatabase& theDB,
const RWCString& theTableName) //1
: aDB_(theDB) //2
, table_(aDB_.table(theTableName)) //3
, nameColumn_(table_["name"]) //4
, idColumn_(table_["ID"])
, addressColumn_(table_["address"])
, cityColumn_(table_["city"])
, stateColumn_(table_["state"])
, zipColumn_(table_["zip"])
, phoneColumn_(table_["phone"])
{
; //5
}
VVContactRepository::mailingLabels
Now that the construction of the VVContactRepository instance is complete, the next step in the main routine program was to call the mailingLabels() member function. Here is the source code for VVContactRepository::mailingLabels() from the file conrep.cpp and the description of the code:
VVContactRepository&
VVContactRepository::mailingLabels(ostream& o) { //1
RWDBSelector select = aDB_.selector(); //2
select << table_; //3
select.orderBy(nameColumn_); //4
RWDBReader aReader = select.reader(); //5
VVContact aContact; //6
while (aReader()) { //7
aReader >> aContact; //8
aContact.mailingLabel(o); //9
o << endl << endl; //10
}
return *this; //11
}
operator>>() for VVContact
In
//8 of the previous code section, a
VVContact instance was initialized by shifting out from an
RWDBReader instance. This is an important technique and warrants closer examination. Line
//8 above actually invokes a global overloading of the
operator>>() function involving an
RWDBReader on the left and a
VVCustomer on the right. The code for this function, found in
conrep.cpp, follows:
RWDBReader&
operator>>(RWDBReader& aReader, VVContact& aContact) {
RWCString name, address, city, state, zip, phone;
unsigned long id;
aReader >> name >> id >> address
>> city >> state >> zip >> phone;
aContact = VVContact(name, id, address,
city, state, zip, phone);
return aReader;
}