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

6.6 More About isValid()

The example in Section 6.2 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.

6.6.1 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:

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:

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::isValid() returns true, since the RWDBConnection instance is still valid; the connection is still usable for other queries.

6.6.2 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 Section 2.3, "Producers and Products.") However, producing an unusable product object again, this time by a valid object, can yield a usable and valid object. This technique is shown in the following example:

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 product objects must be produced by valid objects. Some qualifications apply in other classes as well.


Always check the SourcePro C++ API Reference Guide for the object validity requirements and the description of isValid() for the class you are using.



Previous fileTop of DocumentContentsIndex pageNext 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.
Contact Rogue Wave about documentation or support issues.