The Program
This section examines the Tutorial Two program you just ran. Its purpose is to write the mailing labels for all customers who are more than seven days overdue to a file called t2out.txt.
The Main Routine
This code sample shows the main routine for the tutorial. The line numbers correspond to the comments that follow the code.
#include <rw/db/db.h> //1
#include "conrep.h" //2
#include "rentrep.h" //3
#include "tututil.h" //4
int main(int argc, char** argv) //5
{
associateStreams("", "t2out.txt", "t2err.txt");
RWDBManager::setErrorHandler (outputStatus);
RWCString serverType, serverName, userName,
password, databaseName, pstring;
initializeDatabaseArguments(argc, argv, serverType,
serverName, userName, password,
databaseName, pstring); //5
RWDBDatabase aDB = RWDBManager::database
(serverType, serverName, userName, password,
databaseName, pstring); //6
VVContactRepository customerPool (aDB,
customerTableName); //7
VVRentalTransactionRepository rentalTransactions
(aDB, rentalTableName); //8
RWDBColumn customerID = customerPool.idColumn(); //9
RWDBColumn rentalCustomerID =
rentalTransactions.customeridColumn(); //10
RWDBColumn dueDate =
rentalTransactions.dueDateColumn(); //11
RWDateTime aWeekAgo(9,6,2000); //12
aWeekAgo = aWeekAgo - (7 * RWDateTime::millisecsInDay); //13
customerPool.mailingLabels (outStream,
customerID == rentalCustomerID &&
dueDate < aWeekAgo); //14
closeStreams ("", "t2out.txt", "t2err.txt");
return 0;
} //15
Here is a line-by-line description of the program:
VVContactRepository::mailingLabels
The invocation of the mailingLabels() member function on //18 above does most of the work in this tutorial. The source code of this member function, from the file conrep.cpp is shown here:
VVContactRepository&
VVContactRepository::mailingLabels (ostream& o,
const RWDBCriterion& criterion) //1
{
RWDBReader aReader = reader(criterion); //2
VVContact aContact; //3
while (aReader()) { //4
aReader >> aContact; //5
aContact.mailingLabel (o); //6
o << endl << endl; //7
}
return *this; //8
}
VVContactRepository::reader
Line
//2 above invokes the
reader() member function of
VVContactRepository. The definition of the function called on
//2 is shown below. This function actually creates the query and submits it to the database. It then returns an
RWDBReader instance that can be used to get results back from the server.
RWDBReader
VVContactRepository::reader(const RWDBCriterion& criterion) //1
{
RWDBSelector s = aDB_.selector(criterion); //2
s.distinct(); //3
s << table_; //4
s.orderBy(nameColumn_); //5
return s.reader(); //6
}
customer.ID = rentals.customerID AND rentals.dueDate < '6/2/2000'