Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

17.5 The Program

This section examines the t3 program you just ran. Its purpose is to write a list of the new customers who were successfully added to the database to a file called t3out.txt.

17.5.1 The Main Routine

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

//1-8These lines are for initialization and multiplatform portability. They are common to all the tutorials, and are explained in the comments in Section 15.5.1.
//9Here 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.
//10An instance of the class VVContactRepository 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 customer information.
//11An instance of VVContact is instantiated here to hold the information about a new customer.
//12In this while loop, new customers are read one at a time from the input stream. The input stream is tied to the file t3in.dat. When no more new customers are available on the input stream, the read routine returns a zero value and the loop terminates.
//13This line invokes the insert() member function of the class VVContactRepository. This function takes the customer record provided as an argument and inserts it into the customer table.
//14Once a customer record is added to the customer table, the name of the customer, along with a message, is printed to the output stream. Why isn't there any error checking here? If an insert fails, the asynchronous error handler in //6 above is called. This routine outputs an error message to the error stream and exits.
//15Destructors for all the objects are called here. The database closes automatically along with the output and input streams.

17.5.2 VVContactRepository::insert

The insert member function of VVContactRepository takes an instance of VVContact and inserts it into the table associated with this instance of VVContactRepository.

//1This is the definition of the insert() member function of the VVContactRepository class. It accepts one argument, a single instance of VVContact.
//2Here an inserter object is fetched from the table associated with this instance of VVContactRepository. Through this new instance of RWDBInserter, new rows are inserted into the table.
//3The inserter object behaves somewhat like an input stream. On this line, an instance of VVContact is shifted into the inserter. The effect is to insert the new customer into the table. This line is actually an invocation of an overloaded operator<<(). It takes an RWDBInserter instance as the left argument and a VVContact instance as the right argument. The code of the overloaded operator is described below.

17.5.3 operator<<() for VVContact

The purpose of this overloaded function is to ease the insertion of a VVContact instance into an RWDBInserter instance. The function combines the shifting of the individual components of a VVContact into a single function.

//1This line defines the overloaded left shift function for RWDBInserter and VVContact.
//2This line shifts the individual components of a VVContact instance into an RWDBInserter instance. Each left shift is an invocation of a member function of RWDBInserter. The inserter class refines operator<<() for each of the basic C++ types along with several of the Rogue Wave types, such as RWCString.
//3Shifting values into an inserter does not automatically insert the data into a table. The execute() member function of RWDBInserter actually sends the new data to the database.

Previous fileTop of DocumentContentsIndexNext file

©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.