OOFILE | Downloads | Purchasing | Press | Services | Company Information | Soapbox | References | F.A.Q. | HOME |
This sample deletes records including related data.
The include files are:
#include "oofile.h" // the general oofile library #include "ooftst02.h" // the declarations for the database classes we will be using in this test.
These are the global variables that define the database using the ooftst02 classes:
TEST_CONNECT theDB; dbPatients Patients; dbVisits Visits; dbRelationship PatientVisits(Patients.Visits, Visits.Patient);
This is the procedure that will test the deletion of the first and last of the records.
void testRecDeletes()
{
First, we go to the start of the list and print out the first record so we can see what it is.
Patients.start(); cout << "Deleting individual record - " << Patients.LastName << ", " << Patients.Othernames << endl << endl;
Now we try deleting the record. To do this, we use the function deleteRecord()
Testing: function deleteRecord()
Testing: deletion of single record
Testing: deletion of first record in list
Patients.deleteRecord();
We check to see if we are now on a different record (ie that the first record is really gone).
cout << "Should be on next record after delete, which is now first record in file - " << Patients.LastName << ", " << Patients.Othernames << endl << endl;
Now, we try going to the end of the list and printing out the last record, so we can see what it is.
Patients.last(); cout << "Goto last record in file - " << Patients.LastName << ", " << Patients.Othernames << endl << endl;
We again delete a single record using deleteRecord()
Testing: deletion of last record in list
Patients.deleteRecord();
And again test to see if the record we are on is now different (due to the other one being deleted).
cout << "Should be on first record after deleting last - " << Patients.LastName << ", " << Patients.Othernames << endl << endl;
We now print out the entire database to see what difference our deletion has made.
Patients.selectAll(); cout << "Patients - should be 2 recs" << endl << Patients << endl << endl;
}
int main()
{
cout << "OOFILE Validation Suite - Test 9\n"
<< "Simple test to demonstrate deleting records" << endl
<< "using the database from ooftst02" << endl;
The following is a bit of complicated filename logic to support different
backends with this one test program.
This code determines what platform we are running under and assigns a name for
the database file accordingly.
#ifdef TESTING_DBASE #ifdef _Macintosh const char* kExistsName = ":ooftst09:Patients.dbf"; const char* kDatabaseName = ":ooftst09:"; #else const char* kExistsName = "Patients.dbf" const char* kDatabaseName = ""; #endif
#else const char* kDatabaseName = "ooftst09.db"; const char* kExistsName = kDatabaseName; #endif
If the file already exists we use openConnection() to open the file and
use deleteAll() to delete everything so that it is empty.
If it does not exist, we create the file using newConnection().
if (dbConnect::fileExists(kExistsName)) {
theDB.openConnection(kDatabaseName);
}
else {
theDB.newConnection(kDatabaseName);
}
We then add records to the database by calling the method AddTestData() defined in ooftst02.h
Patients.AddTestData(); // always add data - file is empty from last run if it exists
Here, we print the full database to see its initial state.
cout << "Entire database before deletions" << endl << theDB << endl;
We then define a dbView to make printing the database much easier.
dbView smithVisits(Patients.Visits); smithVisits << Patients.Visits->VisitDate << Patients.Visits->Why;
We define a search to select a particular type of record we want to delete
Patients.search(Patients.LastName=="Smith"); cout << "Dumping Smith and his visits: " << endl << Patients << endl << smithVisits << endl;
Here, we test the ability to delete a record by using deleteRecord()
Testing: deleting search selection
cout << "Deleting Smith" << endl; Patients.deleteRecord();
We then print the database again to see if it (or they if more than one record was found) is gone
cout << "Entire database without Smith" << endl << theDB << endl;
We now show how to delete all the records in the database. This uses the function deleteAll() and will clear the database of everything.
Testing: function deleteAll()
Testing: deletion of all records
cout << "Deleting all remaining records" << endl; Patients.deleteAll();
We now check to see if the database really is empty
cout << "Entire database - should be empty" << endl << theDB << endl << endl;
We will now use the procedure testRecDeletes() which, as defined above, deletes the first and last members of the list of records given to it. We will pass it various lists, sorted and unsorted complete database and just selections to test what effect these differences have.
We add new test data each time and delete it after each test so that we may have a clean slate for each test.
The first test shows what happens when the entire database is handed unsorted to testRecDeletes().
Testing: deletion of specific records on unsorted database
Patients.AddTestData(); cout << "Testing record position after deletes, with all recs & no sort order"<< endl; testRecDeletes(); Patients.deleteAll();
In the second test, testRecDeletes() is passed the whole database, but this time it has been sorted.
Testing: deletion of specific records on sorted database (indexed)
Patients.AddTestData(); cout << "Test again, with all recs & sort by othername"<< endl << endl; Patients.setSortOrder(Patients.Othernames); testRecDeletes(); Patients.deleteAll();
In the third test, the database is passed to the function sorted in reverse order (without using the indexes).
Testing: deletion of specific records on sorted database (reverse order unindexed)
Patients.AddTestData(); cout << "Test again, with all recs & non-indexed sort by reverse othername"<< endl << endl; Patients.setReverseSortOrder(Patients.Othernames); testRecDeletes(); Patients.deleteAll();
cout << "done" << endl;
return EXIT_SUCCESS; }
(c) Copyright A.D. Software 1994-2000 (All Rights Reserved).
Last Updated: 9th September 2001