OOFILE | Downloads | Purchasing | Press | Services | Company Information | Soapbox | References | F.A.Q. | HOME |
This sample tests the dBase backend's ability to open a schema with different field sizes and adjust the current schema to match the dBase file.
#include "oofile.h" // the general oofile library #include "ooftst02.h" // the declarations for the database classes we will be using in this test. #include "ooftest.h"
These are the global variables that define the database using the ooftst02.h classes
dbConnect_dbase theDB; dbPatients Patients; dbVisits Visits; dbRelationship PatientVisits(Patients.Visits, Visits.Patient);
Now we'll declare a variant of Patients (from ooftst02.h) that has a few fields at different sizes
class dbPatients2 : public dbTable {
OOFILE_METHODS(dbPatients2)
dbChar LastName, Othernames;
dbLong PatientNo;
dbPatients2() : dbTable("Patients"),
LastName(50, "Last Name", kIndexed), // was 40
Othernames(20, "Other names", kIndexed),// was 80
PatientNo("PatientNo", kIndexNoDups)
{};
};
Now we'll declare a subclass of Patients that has a few extra fields.
class dbPatients3 : public dbPatients {
OOFILE_METHODS(dbPatients3)
dbChar FirstExtra;
dbLong SecondExtra;
dbPatients3() :
FirstExtra(40, "FirstExtra"),
SecondExtra("SecondExtra")
{};
};
int main()
{
cout << "OOFILE Validation Suite - Test 41\n"
<< "tests the dBase backend's ability to open a schema with\n"
<< "different field sizes and adjust the current schema to\n"
<< "match the dBase file." << endl << endl;
We assign a filename to the database, opening the file we created in ooftst02.
const char* kExistsName = "Patients.dbf"; const char* kDatabaseName = "";
if (dbConnect::fileExists(kExistsName)) {
theDB.openConnection(kDatabaseName);
}
else {
theDB.newConnection(kDatabaseName);
Patients.AddTestData();
}
We start by setting a sort order and printing out the database as it stands. This is the database as the original Patients table.
Patients.setSortOrder(Patients.LastName); cout << theDB; theDB.close();
Now, we will open the same database with the slightly altered table (defined above). Remember that the schema is different. We must start by defining new global variables for this table.
dbConnect_dbase theDB2; dbPatients2 Patients2;
Then we will describe the schema so that we can see that it is different.
cout << "Modified Patients schema (different widths & fewer fields) before opening\n"; Patients2.describe(cout);
Now we open the connection from the same file as before, but into the new table.
theDB2.openConnection(kDatabaseName);
Now we see how the schema has changed and print the database, showing that the fields are intact.
cout << "Modified Patients schema after opening\n"; Patients2.describe(cout); cout << endl << endl << Patients2 << endl << endl; theDB2.close();
Now we will open the 3rd database (defined above) with MORE fields than the original. Opening a new table in this way is somewhat akin to when you wish to add new fields into an old database. We start by declaring new global variables for this new table.
dbConnect_dbase theDB3; dbPatients3 Patients3;
Then we describe the schema to see the difference between it and the original.
cout << "Modified Patients schema (MORE fields) before opening\n"; Patients3.describe(cout);
Now we will open the database connection and once again describe the table to see if the schema has changed.
theDB3.openConnection(kDatabaseName); cout << "Modified Patients schema after opening\n"; Patients3.describe(cout); cout << endl << endl << Patients3 << endl << endl;
Now we test writing into the new fields of the database, and print to see if they changed.
cout << "Now attempting to write to extra fields:\n"; Patients3.start(); Patients3.FirstExtra = "First Extra test"; Patients3.SecondExtra = 999; Patients3.saveRecord(); cout << endl <<endl << Patients3 << endl << endl;
theDB3.close();
Now we will open a totally unpopulated table, reading the entire schema in from the dbf file. We first create the new class and don't give it anyt fields of its own.
class dbPatients4 : public dbTable {
OOFILE_METHODS(dbPatients4)
};
We declare global variables for it.
dbConnect_dbase theDB4; dbPatients4 Patients4;
Then give it a name - the default table namewould be dbPatients4. This demonstrates the use of the function setName().
Testing: function setName() on a table
Patients4.setName("Patients");
Now we open the connection for it, the same as previously.
theDB4.openConnection(kDatabaseName);
Then describe the schema to see if the fields have been added.
cout << "Patients schema after opening with no fields defined\n"; Patients4.describe(cout); cout << endl <<endl << Patients4 << endl << endl;
theDB4.close();
cout << "Test Completed" << endl;
return EXIT_SUCCESS; }
(c) Copyright A.D. Software 1994-2000 (All Rights Reserved).
Last Updated: 9th September 2001