The Program
This section examines the t4 program you just ran. It wrote a list of the new videos that were successfully added to or updated in the database. The file called t4out.txt contains a log of the video titles that were processed.
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
VVVideoRepository::updateStock
The updateStock() member function of VVVideoRepository takes a video ID as its first argument and the number of videos to add to the stock as its second argument. It is used when new copies of a video are added to existing stock in the library.
VVVideoRepository&
VVVideoRepository::updateStock(unsigned long aVidID,
unsigned int quantity) //1
{
RWDBUpdater updater = table_.updater(); //2
updater << quantityColumn_.assign
(quantityColumn_ + quantity); //3
updater << numOnHandColumn_.assign
(numOnHandColumn_ + quantity); //4
updater.where(idColumn_ == aVidID); //5
updater.execute(); //6
return *this;
}