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;

}

 

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