VVVideoRepository::find
The find() member function of the class VVVideoRepository, takes a VVVideo instance as an argument, with only some of its fields filled in. It uses this partial VVVideo as a search criterion. The function returns the first video it finds that matches the field passed in. The source code of this member function follows, taken from the file vidrep.cpp.
 
VVVideo
VVVideoRepository::find(const VVVideo& aVideo) const //1
{
RWDBCriterion searchCriterion("0 = 0", 0, 0); //2
RWCString title = aVideo.title(); //3
if (!title.isNull()) //4
searchCriterion = searchCriterion &&
titleColumn_.like(title); //5
if (aVideo.id()) //6
searchCriterion = searchCriterion &&
idColumn_ == aVideo.id(); //7
if (aVideo.year()) //8
searchCriterion = searchCriterion &&
yearColumn_ == aVideo.year(); //9
RWCString category = aVideo.category(); //10
if (!category.isNull()) //11
searchCriterion = searchCriterion &&
categoryColumn_.like(category); //12
if (aVideo.quantity())
searchCriterion = searchCriterion &&
quantityColumn_ == aVideo.quantity(); //13
if (aVideo.numOnHand()) //14
searchCriterion = searchCriterion &&
numOnHandColumn_ == aVideo.numOnHand(); //15
 
RWCString synopsis = aVideo.synopsis(); //16
if (!synopsis.isNull()) //17
searchCriterion = searchCriterion &&
synopsisColumn_.like(synopsis); //18
 
RWDBSelector aSelector = aDB_.selector(searchCriterion); //19
aSelector << table_; //20
RWDBReader aReader = aSelector.reader(); //21
VVVideo theVideo; //22
if (aReader()) //23
aReader >> theVideo; //24
 
return theVideo; //25
 
//1 This is the definition of the find() member function for the VVVideoRepository class. It accepts a single parameter, an instance of VVVideo. This VVVideo instance is designed as a criterion for selecting a specific video. The fields within the VVVideo are used to select the first video that matches the fields, returning the completed VVVideo.
//2 Throughout the following section of code, a condition is created based on the fields that have values. Building the condition dictates surveying the fields of this VVVideo, and for each nonzero or non-null value found, adding an appropriate clause to the condition. Clauses are added to the condition using a logical and operation &&. Since it is not known which field will be the first to have a value, the search criterion is first set to a simple expression of true. The && operator can be applied to additional conditions as they are found. This line of code creates a predicate in the form of an RWDBCriterion with true as its value.
//3 The RWCString created on this line is a copy of the VVVideo instance’s title field.
//4-5 If the title field is not null, then a value exists in the field that should be used as a search criterion. The existing search criterion is combined with an expression using the && operator that translates to the equivalent of ... AND video.title LIKE 'sometitle'.
NOTE: Do not confuse the RWCString::isNull method with NULL database values. The RWCString::isNull method returns true if the string is empty. However, some database vendors do not consider an empty string to be the same as a NULL value.
//6-7 If the ID field has a nonzero value, it should be added to the search criterion. (Zero is an invalid value for an ID.) The existing search criterion is combined with an expression using the && operator that translates to the equivalent of ... AND video.ID = 69, where 69 is an arbitrarily chosen value that represents the value in aVideo.id().
//8-18 These lines continue in the same vein, adding predicates to the search criterion as the routine finds nonempty fields.
//19 Here, a selector is created by the database object. The search criterion is passed to the constructor where it is used in the WHERE clause of the SELECT statement.
//20 Shifting a table instance into an RWDBSelector instance selects all columns from that table.
//21 Creating a reader here submits the query to the server.
//22 Instantiate a VVVideo instance to hold the results of the query.
//23 Advance the reader to the first row of the result set.
//24 Read the row into the instance of VVVideo.
//25 Return the VVVideo. If the query resulted in multiple matches, the additional rows are discarded when the RWDBReader is destroyed.