VVInvoice::addRental
The invocation of the addRental() function inserts new data into the rentals table and updates the videos table. The source code of this member function can be found in the source file invoice.cpp.
 
void
VVInvoice::addRental(unsigned long aVideoID) //1
{
RWDBConnection dbSession = aDB_.connection(); //2
dbSession.beginTransaction(); //3
RWDateTime rentalDate(9,6,2000); //4
// due date is three(3) days from default
RWDateTime dueDate(rentalDate); //5
dueDate = dueDate + (3 * RWDateTime::millisecsInDay); //6
// returnDate is not assigned now, so make it invalid
RWDateTime returnDate; //7
 
// add a rental transaction
VVRentalTransactionRepository rentals(aDB_, rentalTableName); //8
VVRentalTransaction aRental(aCustomerID_, aVideoID,
aInvoiceID_, rentalDate,
dueDate, returnDate); //9
rentals.insert(aRental, dbSession); //10
 
// update video inventory
VVVideoRepository videos(aDB_, videoTableName); //11
VVVideo aVideo("", aVideoID, 0, "", 0, 0, ""); //12
aVideo = videos.find(aVideo); //13
if (aVideo.numOnHand() > 0) //14
aVideo.numOnHand(aVideo.numOnHand() - 1); //15
videos.update(aVideo, aVideo, dbSession); //16
// commit the transaction
dbSession.commitTransaction(); //17
}
//1 This line defines the addRental() member function. It accepts one parameter, a video ID. The invoice ID and customer ID were saved as local data members of the VVInvoice when it was constructed. A database was also retained to make access to the database simpler for all member functions.
//2 Because two tables are updated by this routine, a single connection is used to control the table manipulations as a single transaction. This is only possible through a common connection.
//3 The first step in grouping several data manipulations into a single transaction is to turn off the default auto-commit behavior of the DB Interface Module. By calling beginTransaction(), the connection is reset to not make data manipulations permanent until commitTransaction() is invoked. See //17.
//4-7 The RWDateTime instances created on these lines are used to initialize a new VVRentalTransaction instance on line 9.
//8 An instance of the class VVRentalTransactionRepository is created on this line to represent the rentals table. The first argument, aDB_, identifies the database where the instance’s data resides, which is the same database where invoice’s data resides. The second argument identifies the specific table name that holds the rental transaction information.
//9 A new VVRentalTransaction instance is created on this line to represent a rental of the video identified by aCustomerID_, aVideoID and aInvoiceID_. Rental dates and times are also provided.
//10 The new rental is inserted into the rental table.
//11 Now the video table must be updated to reflect the stock going down by one video. On this line, an instance of the class VVVideoRepository is created to represent the videos table. The first argument, aDB_, identifies the database where the instance’s data resides, which is the same database where the invoice’s data resides. The second argument identifies the specific table name that holds the video information.
//12 An incomplete VVVideo instance is created to pass to the find() member function of VVVideoRepository. Only the video ID is provided.
//13 The find() member function of VVVideoRepository takes an instance of VVVideo that has only partial information and finds a matching record in the videos table. It then returns an instance of VVVideo containing the complete record. On this line, a VVVideo instance containing only a VideoID is given to the find() function. Once the routine returns, the instance of VVVideo contains complete information about the video with the correct video ID. The find() member function of VVVideoRepository is explained in VVVideoRepository::find.
//14-15 The following section of code updates the quantity on hand for the video being rented. It does this by using the video found in the previous line. It decrements the numOnHand field by one.
//16 On this line, an update of the video record takes place. The first parameter is used only for a key to the video that is to be updated. The second parameter is the new value for the video record. Since these are the same, the update is to the same video. This routine is explained in more detail below. The third parameter is the common connection. This groups this update with the previous addition of a video rental.
//17 Once both tables have been updated, this line commits all data manipulations as a single transaction. In this case, the addition of a new video rental on //10 and the update of the number of videos on hand on //16 are committed together.