The Program
This program writes a list of the most often rented videos between January 1, 1994 and January 31, 1994 inclusive. The file called t6out.txt contains the list. This section looks at the code for t6 in detail.
The Main Routine
The following example is the main routine for the tutorial. Remember that you cannot run this tutorial unless your compiler supports templates. Not all of the source code is listed here, as the TopTenVideo class is also defined within this file. The line numbers correspond to the comments that follow the code.
#include <rw/db/db.h> //1
#include "tututil.h" //2
#include "rentrep.h" //3
#include "vidrep.h" //4
#include <rw/tpordvec.h> //5
#include <rw/db/tpmemtab.h> //6
int main(int argc, char** argv) //7
{
RWDBManager::setErrorHandler(outputStatus); //8
RWCString serverType, serverName, userName,
password, databaseName, pstring; //9
initializeDatabaseArguments(argc, argv, serverType,
serverName, userName, password,
databaseName, pstring); //10
associateStreams("", "t6out.txt", "t6err.txt") //11
RWDBDatabase aDB = RWDBManager::database
(serverType, serverName, userName, password,
databaseName, pstring); //12
VVVideoRepository videos(aDB, videoTableName); //13
VVRentalTransactionRepository rentals(aDB, rentalTableName); //14
RWDateTime beginDate(1, 1, 1994); //15
RWDateTime endDate(31, 1, 1994); //16
RWDBSelector aSelector = aDB.selector(); //17
aSelector << videos.titleColumn() << rwdbCount()
<< rentals.videoIDColumn() << videos.idColumn(); //18
aSelector.where
(rentals.dueDateColumn().between
(beginDate, endDate)); //19
aSelector.groupBy(rentals.videoIDColumn()).groupBy(
videos.idColumn()).groupBy(videos.titleColumn()); //20
aSelector.having
(rentals.videoIDColumn() == videos.idColumn()); //21
aSelector.orderByDescending(2); //22
aSelector.orderBy(1); //23
RWDBTPtrMemTable<TopTenVideo,
RWTPtrOrderedVector<TopTenVideo> >
theTopTenRenters(aSelector, 10); //24
outStream << "TOP TEN Videos for "
<< beginDate.monthName() << endl << endl; //25
for (size_t i = 0; i < theTopTenRenters.entries(); i++) { //26
outStream << "#" << i+1 << ‘\t’
<< theTopTenRenters[i]->name() << endl; //27
}
theTopTenRenters.clearAndDestroy(); //28
closeStreams("", "t6out.txt", "t6err.txt");
return 0;
} //29
SELECT videos.title, COUNT(*), rentals.videoID, videos.ID
FROM videos, rentals
WHERE rentals.dueDate BETWEEN '1994/1/1' AND '1994/1/31'
GROUP BY rentals.videoID, videos.ID, videos.title
HAVING rentals.videoID = videos.ID
ORDER BY 2 DESC, 1 ASC