OOFILE | Downloads | Purchasing | Press | Services | Company Information | Soapbox | References | F.A.Q. | HOME |
This sample tests the use of indices that ignore nulls it also shows a trick to use a compound index to get effectively multiple indexes on the same field
#include "oofile.h" // the general oofile library
We start by declaring a class. Note that we have declared a dbCompoundField for more on compound fields, see ooftst12. Note that we have declared the index for F2 as being no nulls and no duplicates.
DECLARE_CLASS(dbTest38) dbChar name; dbUlong f2; dbReal f3; dbCompoundField secondName;
dbTest38() :
name(20, "name", kIndexCompress),
f2("F2", kIndexNoDupsNoNulls),
f3("F3"),
secondName("second name", kIndexCompressNoNulls)
{
This is not really a compound index, just a different kind of index.
secondName << name; }; };
This shows a common technique, using our own class to contain all the database
class test38DB : public dbConnect_ctree
{
public:
void addTestData(unsigned long startNum, unsigned long endNum);
void displaySorted(dbField& sortField);
Here we have our table.
dbTest38 mTable;
};
This procedure add the test data to our database table.
void
test38DB::addTestData(unsigned long startNum, unsigned long endNum)
{
Here we declare an array of strings to use for intialising the name fields of the database. The null entries will not be indexed by secondName.
const char* names[] = {
"Andy",
"Fred",
"Sally",
"Zach",
""
};
const unsigned short kNumNames = sizeof(names)/sizeof(const char*);
for (unsigned long i=startNum; i<endNum; i++) {
mTable.newRecord();
const unsigned short iName = i%kNumNames;
mTable.name = names[iName]; // one of 5 names
mTable.f2 = i;
mTable.f3 = i+0.5;
mTable.saveRecord();
}
}
This procedure is passed a field by reference and displays it. It also demonstrates the function fieldName() that can be used to find the name of a field passed by pointer.
Testing: function fieldName()
void
test38DB::displaySorted(dbField& sortField)
{
mTable.setSortOrder(sortField);
cout
<< "Sorted by "
<< sortField.fieldName()
<< " has " << mTable.count() << " records\n"
<< mTable
<< endl << endl;
}
int main()
{
cout << "OOFILE Validation Suite - Test 38\n"
<< "This tests the effect of adding indices to a database\n\n";
We declare our global variables then open the database.
test38DB theDB;
const char* kDatabaseName = "ooftst38.db";
if (dbConnect::fileExists(kDatabaseName)) {
theDB.openConnection(kDatabaseName);
theDB.deleteAll();
}
else {
cout << "Creating a new database\n";
theDB.newConnection(kDatabaseName);
}
We add 5 records to the database (this should include at least one instance of the null field).
cout << "Adding 5 test records\n"; theDB.addTestData(0, 5);
Now we will display each of the fields in turn, showing which ones have and have not been added.
theDB.displaySorted(aDB.mTable.name); theDB.displaySorted(aDB.mTable.secondName); theDB.displaySorted(aDB.mTable.f2); theDB.displaySorted(aDB.mTable.f3);
cout << endl <<"Test Completed" << endl;
return EXIT_SUCCESS; }
(c) Copyright A.D. Software 1994-2000 (All Rights Reserved).
Last Updated: 9th September 2001