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 following 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 RWDBMAIN (MAIN_ARGS_DECL) //8 { associateStreams ("t4in.dat", "t4out.txt", "t4err.txt"); //9 RWDBManager::setErrorHandler (outputStatus); //10 RWCString serverType, serverName, userName, password, databaseName, role; //11 initializeDatabaseArguments (MAIN_ARGS, serverType, serverName, userName, password, databaseName, role); //12 RWDBDatabase aDB = RWDBManager::database (serverType, serverName, userName, password, databaseName, role); //13 VVVideoRepository videoStock (aDB, videoTableName); //14 VVPurchaseRepository purchaseTransactions (aDB, purchaseTableName); //15 VVPurchase aPurchase; //16 VVVideo aVideo; //17 while (aPurchase.read (inputStream)) { //18 aVideo.read (inputStream); //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 } } return 0; } //26
//1-12 | These lines are for initialization and multiplatform portability. They are common to all the tutorials and are explained in Section 15.5.1. |
//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 customer 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. When no more new purchase transactions are available on the input stream, the read routine returns a zero value and the loop terminates. |
//19 | After successfully reading in a purchase transaction, a complete video record can be 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 purchaseTransactions 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 along with the output and input streams. |
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; }
//1 | This is the definition of the updateStock() member function of the VVVideoRepository class. It accepts two arguments, the ID of the video and the number of new copies purchased. |
//2 | Here an updater object is fetched from the table associated with this instance of VVVideoRepository . Through this new instance of RWDBUpdater, rows that match a certain criterion can have new values assigned to their columns. |
//3 | The updater object behaves somewhat like an input stream. However, it accepts only RWDBAssignment instances. An RWDBAssignment instance encapsulates an expression to be evaluated by the server and assigned to a column. RWDBAssignment instances are created by calling the assign() member function of RWDBColumn instances. The assign() member function accepts an RWDBExpr instance involving RWDBColumn instances and/or literals in expressions. This process encapsulates the means for creating SQL SET expressions. On this line, the quantity column from the videos table is incremented by the variable quantity. |
//4 | Both the fields represented by the two RWDBColumn instances quantityColumn_ and numOnHandColumn_ need to be incremented. This line creates an RWDBAssignment for incrementing the latter column. |
//5 | On this line, a predicate is formed to limit the update to a specific row. This predicate takes the form of RWDBCriterion instances created when applying the operator==() to an RWDBColumn instance and number. This creates a predicate equivalent to videos.ID = 22, where the number 22 is an arbitrarily chosen value for the variable aVidID. |
//6 | Once the updater has been given its instructions, calling the execute() member function submits the UPDATE to the database. |
©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.