DB Interface Module User’s Guide : PART V The Tutorials : Chapter 21 Deleting Rows from a Table : The Program
The Program
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 Main Routine
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
 
int main(int argc, char** argv) //4
{
associateStreams("t5in.dat", "t5out.txt", "t5err.txt"); //5
RWDBManager::setErrorHandler(outputStatus); //6
 
RWCString serverType, serverName, userName,
password, databaseName, pstring; //7
initializeDatabaseArguments(argc, argv, serverType,
serverName, userName, password,
databaseName, pstring); //8
 
RWDBDatabase aDB = RWDBManager::database
(serverType, serverName, userName, password,
databaseName, pstring); //9
 
VVContactRepository customerRep(aDB, customerTable); //10
while (!inStream.eof()
&& !inStream.bad() && !inStream.fail()) { //11
unsigned long id; //12
inStream >> id; //13
if (inStream.eof()) //14
break; //15
customerRep.remove( id ); //16
outStream << "Removed customer with ID = "
<< id << " from the database." << endl; //17
}
 
closeStreams("t5in.dat", "t5out.txt", "t5err.txt");
return 0;
} //18
 
 
//1-8 These lines are for initialization. They are common to all the tutorials and are explained in the comments in “The Main Routine.”
//9 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.
//10 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.
//11 The while loop cycles through the input file. The loop terminates at the end-of-file or when something untoward happens.
//12 Customer ID numbers are fetched one at a time from the input stream. This variable holds the ID number read from the stream.
//13 Fetches an ID number from the stream.
//14-15 If an attempt to read past the end-of-file is made, breaks out of the while loop.
//16 After successfully reading in the customer ID, the remove() member function of the VVContactRepository class is invoked. This function deletes the customer record from the customer table. The remove() routine is explained in “VVContactRepository::remove.”
//17 Once a customer has been removed from the database, a log message is created about the deletion.
//18 Destructors for all the objects are called here. The database closes automatically when its destructor is called.
VVContactRepository::remove
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 produced from the table associated with this instance of VVContactRepository. Through this 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 condition is formed to limit the deletion to a specific row. This condition takes the form of an RWDBCriterion instance created when applying the operator== to an RWDBColumn instance and a number. For example, this would create a condition equivalent to customer.ID = 69, if customer number 69 were to be deleted.
//4 Once the deleter is given its condition, calling the execute() member function submits the DELETE statement to the database for execution.