The Main Routine
The next code is the main routine for the tutorial. The line numbers correspond to the comments that follow the code.
 
#include <rw/db/db.h> //1
 
#include "rentrep.h" //2
#include "tututil.h" //3
#include "vidrep.h" //4
#include "purchrep.h" //5
#include "video.h" //6
#include "purchase.h" //7
 
int main(int argc, char** argv) //8
{
associateStreams("t4in.dat", "t4out.txt", "t4err.txt"); //9
RWDBManager::setErrorHandler (outputStatus); //10
 
RWCString serverType, serverName, userName,
password, databaseName, pstring; //11
initializeDatabaseArguments(argc, argv, serverType,
serverName, userName, password,
databaseName, pstring); //12
 
RWDBDatabase aDB = RWDBManager::database
(serverType, serverName, userName, password,
databaseName, pstring); //13
 
VVVideoRepository videoStock(aDB, videoTableName); //14
VVPurchaseRepository
purchaseTransactions(aDB, purchaseTableName); //15
VVPurchase aPurchase; //16
VVVideo aVideo; //17
while (aPurchase.read(inStream)) { //18
aVideo.read(inStream); //19
purchaseTransactions.insert(aPurchase); //20
if (videoStock.exists(aVideo.id())) { //21
videoStock.updateStock (aVideo.id(), aVideo.quantity()); //22
outStream << aVideo.quantity()
<< " copies of " << aVideo.title()
<< " added to existing stock." << endl; //23
}
else {
videoStock.insert(aVideo); //24
outStream << aVideo.quantity()
<< " copies of " << aVideo.title()
<< " added." << endl; //25
}
}
 
closeStreams("t4in.dat", "t4out.txt", "t4err.txt");
return 0;
} //26
 
 
//1-12 These lines are for initialization. They are common to all the tutorials and are explained in the comments in The Main Routine.
//13 Here a connection to a database server is established. The variable aDB will serve as a handle to the database defined by arguments to the RWDBManager::database function.
//14 An instance of the class VVVideoRepository representing the inventory of videos is created on this line. The first argument, aDB, identifies the database in which the instance’s data resides. The second argument identifies the specific table that holds the video information.
//15 An instance of the class VVPurchaseRepository representing the purchases of videos is created on this line. The first argument, aDB, identifies the database in which the instance’s data resides. The second argument identifies the specific table that holds the purchase information.
//16 An instance of VVPurchase is instantiated here to hold the information about the purchasing transaction that has taken place.
//17 An instance of VVVideo is instantiated here to hold the information about the new video that has just been purchased.
//18 In this while loop, new purchase transactions are read one at a time from the input stream. The input stream is tied to the file t4in.dat using the associateStreams() utility method call on //9. When no more new purchase transactions are available on the input stream, the read() routine returns false and the loop terminates.
//19 After successfully reading in a purchase transaction, a complete video record is read. The line loads the new video information into an instance of VVVideo from the input stream.
//20 This line invokes the insert() member function of the class VVPurchaseRepository. This function takes the purchase transaction provided as an argument and inserts it into the purchase table.
//21 The video must next be inserted or updated in the video inventory. To determine which should be done, this line calls a test for the existence of the video ID number in the current inventory.
//22 At this point, it is determined that the video inventory already includes a video of this ID number, so this purchase represents stocking additional copies. The updateStock() member function of VVVideoRepository, increases the quantity in stock columns of the videos table as identified by the first parameter. The second parameter represents the number of new copies of the video to be added to the existing stock. The function updateStock() is discussed in detail below.
//23 When the update occurs, it is logged to the output stream.
//24 At this point, it is determined that the video that was just purchased does not exist in the repository. Therefore, it is inserted as a new video.
//25 The insertion of a new video is logged to the output stream.
//26 Destructors for all the objects are called here. The database closes automatically when its destructor is invoked.