This section examines the program you just ran, which writes a list of the customer IDs that were successfully deleted from the database. The file called t5out.txt contains a log of the customer IDs that were processed.
The following is the main routine for the tutorial. The line numbers correspond to the comments that follow the code.
#include <rw/db/db.h> //1 #include "tututil.h" //2 #include "conrep.h" //3 RWDBMAIN (MAIN_ARGS_DECL) //4 { associateStreams ("t5in.dat", "t5out.txt", "t5err.txt"); //5 RWDBManager::setErrorHandler (outputStatus); //6 RWCString serverType, serverName, userName, password, databaseName, role; //7 initializeDatabaseArguments (MAIN_ARGS, serverType, serverName, userName, password, databaseName, role); //8 istream& inputStream = inStream; //9 RWDBDatabase aDB = RWDBManager::database (serverType, serverName, userName, password, databaseName, role); //10 VVContactRepository customerRep (aDB, customerTable); //11 while (!inputStream.eof() && !inputStream.bad()) { //12 unsigned long ID; //13 inputStream >> ID; //14 if (inputStream.eof()) //15 break; //16 customerRep.remove( ID ); //17 outStream << "Removed customer with ID = " << ID << " from the database." << endl; //18 } return 0; } //19
//1-9 | These lines are for initialization and multiplatform portability. They are common to all the tutorials and are explained in Section 15.5.1. |
//10 | Here a connection to a database server is established. The variable aDB serves as a handle to the database defined by arguments to the RWDBManager::database function. |
//11 | An instance of the class VVContactRepository representing the customer table 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 name that holds the customer information. |
//12 | The while loop here cycles through the input file. The loop should terminate at the end-of-file or when something untoward happens. |
//13 | Customer ID numbers are fetched one at a time from the input stream. This variable holds the ID number read from the stream. |
//14 | Fetches an ID number from the stream. |
//15-16 | If an attempt to read past the end-of-file is made, breaks out of the while loop. |
//17 | After successfully reading in the customer ID, invoke the remove() member function of the VVContactRepository class. This function deletes the customer record from the customer table. The remove() routine is explained in Section 19.5.2. |
//18 | Once a customer has been removed from the database, a log message is created about the deletion. |
//19 | Destructors for all the objects are called here. The database closes automatically along with the output and input streams. |
The remove() member function of VVContactRepository takes a customer ID and removes the associated customer from the customer list.
VVContactRepository& VVContactRepository::remove(unsigned long ID) //1 { RWDBDeleter aDeleter = table_.deleter(); //2 aDeleter.where(idColumn_ == ID); //3 aDeleter.execute(); //4 return *this; }
//1 | This is the definition of the remove() member function of the VVContactRepository class. It accepts one argument, the ID of the customer to be terminated. |
//2 | Here a deleter object is fetched from the table associated with this instance of VVContactRepository. Through this new instance of RWDBDeleter, rows that match a certain criterion can be removed from the table. An RWDBDeleter object encapsulates the SQL DELETE statement. |
//3 | On this line, a predicate is formed to limit the deletion to a specific row. This predicate takes the form of an RWDBCriterion instance created when applying the operator== to an RWDBColumn instance and a number. For example, this might create a predicate equivalent to customer.ID = 69, if customer number 69 were to be deleted. |
//4 | Once the deleter is given its predicate, calling the execute() member function submits the DELETE statement to the database. |
©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.