DB Interface Module User’s Guide : PART II Using the Basic Features : Chapter 6 The Error Model : More About isValid()
More About isValid()
The example in “Checking Status with Class RWDBStatus” showed how to use the isValid() method of class RWDBStatus to check the status of an object. In this section, we add some details about this important function.
The first thing to understand is that the function isValid() returning true means essentially the same as errorCode() returning RWDBStatus::ok. The major difference is that only Producer/Product classes have a status() function, while many classes, including some Producer/Product classes, have an isValid() function.
Although the DB Interface Module error model is based on RWDBStatus, you are more likely to use isValid() for testing the validity of objects.
Defining a Valid Object
In the DB Interface Module, a valid object is a usable object, so we often apply the terms interchangeably. The function isValid() returns true if the object is valid, as shown in the next example:
 
RWDBConnection aConnection = goodDB.connection(); // 1
if (aConnection.isValid()) { // 2
cout << “A good connection was established.” << endl;
}
In //1, a valid RWDBDatabase produces a valid RWDBConnection. Because the connection is successfully established, the function RWDBConnection::isValid() on line //2 returns true. The connection is valid and usable.
If an object that provides an isValid() member is not in a usable state, its isValid() method returns false, as shown in the following example:
 
RWDBConnection aConnection = goodDB.connection(); // 1
RWDBResult aResult =
aConnection.executeSql(“Some Bad SQL”); // 2
if(aResult.isValid()) { // 3
cout << “RWDBResult object is valid.” << endl;
}
else{
cout << “RWDBResult object is not valid--bad SQL statement?”
<< endl;
}
if(aConnection.isValid()) { // 4
cout << “The connection is still good.” << endl;
}
else{
cout << “The connection is bad. Why?” << endl;
}
On line //1, a connection is established from a valid RWDBDatabase. Since the connection is successfully established, line //2 sends the string “Some Bad SQL” to the database server. Of course, this is an invalid query, so the function RWDBResult::isValid() returns false on //3 because this erroneous query is unusable. On line //4, however, aConnection.Valid() returns true, since the RWDBConnection instance is still valid; the connection is still usable for other queries.
Making Unusable Objects Usable
Sometimes an unusable object can be made usable. For example, objects produced under the Producer/Product paradigm of the DB Interface Module, called product objects, are unusable if they are produced by an invalid object. (See “Producers and Products.”) However, producing a product object again, this time by a valid object, can yield a usable and valid object. This technique is shown in the following example:
 
RWDBConnection aConnection = badDB.connection(); // 1
if(!aConnection.isValid()) {
cout << “aConnection isn’t usable right now.” << endl;
}
aConnection = goodDB.connection(); // 2
if(aConnection.isValid()) {
cout << “aConnection is valid and usable now.” << endl;
}
In //1, an aConnection object is produced from an RWDBDatabase instance that is unusable. The method isValid() on aConnection correctly reports that the connection is unusable. In //2, an aConnection object is produced again, this time from a good RWDBDatabase instance. The invalid aConnection object is discarded, and the new object takes its place. Because the connection is successfully produced, the method isValid() on aConnection reports that the connection is in a usable, valid state.
This example is intended to show how reproducing an unusable object can make it usable, but there is a corollary as well: it is always a good idea to understand the validity requirements for the particular class you are using. In this case, it is important to know that usable product objects must be produced by valid objects. Some qualifications apply in other classes as well.
NOTE >> Always check the SourcePro API Reference Guide for the object validity requirements and the description of isValid() for the class you are using.