OOFILE | Downloads | Purchasing | Press | Services | Company Information | Soapbox | References | F.A.Q. | HOME

OOFTST 39 - database backend behaviour

This sample tests the database backend behaviour, see also ooftst01 and ooftst01.h. It's a simple test to store some data and retrieve it in a single-table database, with no relations. We open the database and add the test data then use deleteAll() to delete the data. We then add three of the four records that were in there previously and test if the fourth one stays deleted.


#include "oofile.h"	// the general oofile library
#include "ooftst01.h"	// the declarations for the database classes we will be using in this test.
#include "ooftest.h"
#include "oofios.h"	// an easy way to get your stream libraries, regardless of platform
  
int main()
{
	cout << "OOFILE Validation Suite - Test 1\n"
		 << "Simple test to store some data and retrieve it\n"
		 << "in a single-table database, with no relations\n" << endl;  
	TEST_CONNECT    theDB;
	dbPeople     People;  

This is a bit of complicated filename logic to support different backends with this one test program 

#ifdef TESTING_CTREE

We can use separate files, or a single container file and this sample uses separate files.

	theDB.useSeparateFiles();  
	#ifdef _Macintosh
		const char* kExistsName =  ":ooftst01:People.dat";
		const char* kDatabaseName = ":ooftst01:";
	#else
		const char* kExistsName =   "People.dat";
		const char* kDatabaseName = "";
	#endif	  
#else
	#ifdef TESTING_DBASE
		#ifdef _Macintosh
			const char* kExistsName =  ":ooftst01:People.dbf";
			const char* kDatabaseName = ":ooftst01:";
		#else
			const char* kExistsName =   "People.dbf";
			const char* kDatabaseName = "";
		#endif	  
	#else

This is if we chose persistent RAM, with no option of single file

		#ifdef _Macintosh
			const char* kDatabaseName = ":ooftst01:ooftst01.db";
		#else
			const char* kDatabaseName = "ooftst01.db";
		#endif	
		const char* kExistsName = kDatabaseName;
	#endif
#endif  

Open or create the database and delete all the records by using deleteAll().

	if (dbConnect::fileExists(kExistsName)) {
		theDB.openConnection(kDatabaseName);
		People.deleteAll();
	}
	else {
		theDB.newConnection(kDatabaseName);
		People.AddTestData();
		People.deleteAll();
	}

Always want to add records after deleting some, for this test but leave at least one record deleted, from the original 4 in ooftst01.h. Note that the first is created specifically to test zero searches.

	People.Add("Smith", "John", 0);   
	People.Add("Dent", "Trissa", 5000);
	People.Add("Dent", "Andy", 25000);  

We start by declaring and initialising a dbView.

	dbView justNames(People, false);
	justNames << People.LastName << People.OtherNames;

Now we print the first and second records as they are, by passing two fields into cout.

	People.gotoRecord(0);
	cout << "Record 0 is: " << People.LastName << ", " << People.OtherNames << endl;
	People.gotoRecord(1);
	cout << "Record 1 is: " << People.LastName << ", " << People.OtherNames << endl << endl;  

Then we set the sort order and repeat the above, then use the dbView to show the same fields.

	People.setSortOrder(People.OtherNames);
	People.gotoRecord(0);
	cout << "Record 0 is: " << People.LastName << ", " << People.OtherNames << endl;
	People.gotoRecord(1);
	cout << "Record 1 is: " << People.LastName << ", " << People.OtherNames << endl << endl;  
	cout << "Listing records in OtherNames order\n" << justNames << endl;  
	
	cout << "Test Completed" << endl;
	  
	return EXIT_SUCCESS;
}  

 

Feature index

(c) Copyright A.D. Software 1994-2000 (All Rights Reserved).
Last Updated: 9th September 2001