Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
DB Interface Module User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

20.5 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.

20.5.1 The Main Routine

The next code is the main routine for the tutorial. The line numbers correspond to the comments that follow the code.

//1-12

These lines are for initialization. They are common to all the tutorials and are explained in the comments in Section 17.5.1, "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.

20.5.2 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.

//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 produced from the table associated with this instance of VVVideoRepository. Through this 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 condition is formed to limit the update to a specific row. This condition takes the form of an RWDBCriterion instance created when applying the operator==() to an RWDBColumn instance and number. This creates a condition 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.



Previous fileTop of DocumentContentsNo linkNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.