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

OOFTST 6 - data modifications (inc caching)

This sample modifies related data including demonstrating the caching of related records as would typically be used in a GUI environment.


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. We are declaring the database, the two tables and the relationsihp between them.

	TEST_CONNECT    theDB;
	dbPatients     Patients;
	dbVisits	Visits;
	dbRelationship PatientVisits(Patients.Visits, Visits.Patient);
	
  
int main()
{
	cout << "OOFILE Validation Suite - Test 6\n"
		 << "Simple test to demonstrate updating related fields" << 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 =  ":ooftst06:Patients.dbf";
			const char* kDatabaseName = ":ooftst06:";
		#else
			const char* kExistsName =   "Patients.dbf"
			const char* kDatabaseName = "";
		#endif	  
	#else
		const char* kDatabaseName = "ooftst06.db";
		const char* kExistsName = kDatabaseName;
	#endif 

This segment shows us how to open or create the database whose name was given in the code above.

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);
		Patients.AddTestData();
	}  

Here we declare a dbView and add some fields to it. Basic info on dbViews

	dbView smithVisits(Patients.Visits); 
	smithVisits << Patients.Visits->VisitDate << Patients.Visits->Why;   

Here, we perform a simple search of the character field "LastName" and pass both the full table and just the dbView to cout -> to compare the different types of output we gain from these. We also gain an idea of the initial state of the database for both the full table and the dbView. this is useful as later we will be changing things and come come back to this printout to see what it was like before it was changed.

Testing: difference between database and dbView of database

	Patients.search(Patients.LastName=="Smith");
	cout << "Dumping Smith and his visits: " << endl 
	     << Patients << endl 
		 << smithVisits << endl;  

 

Modifying related data

First, we change a character field of the second table through the join-field of the relationship. Basic info on relationships.

Testing: modification of character field through join-field

	cout << "changing the first reason to 'Computer-Induced Sanity' " << endl;
	Patients.Visits->gotoRecord(0);
	Patients.Visits->Why = "Computer-Induced Sanity";

Next, we change another character field through the join-field. The difference here is in how we access the table for the record. We use the source() command on a record in the dbView. This returns the table that the record belongs to. We can then access its fields as in the previous test.

Testing: navigation via the dbView using source()

	cout << "changing the second reason to 'Funny Views' " << endl;
	smithVisits.source()->gotoRecord(1);  
	Patients.Visits->Why = "Funny Views";

We now save the changes so they aren't just in cache.

Note: OOFILE can run in two modes. There is the automaticSave mode (in which you don't HAVE to specify saveRecord() and the requireExplicit mode (generally used as default as it then catches errors). The requireExplicit mode reuires you to use saveRecord(). If you don't it will return an error message. see also ooftst01.h for the first introduction to this.

	Patients.saveRecord();

Then we print the table and dbView so we can see the changes made.

	cout << "Dumping Smith and changed visits: " << endl 
	     << Patients << endl 
		 << smithVisits << endl;

Now we test the addition of a new record.

Testing: addition of record

	Patients.AddVisit("14/2/1995", "Anxiety Attacks");	  

Now change to another related record (our new one should be cached). We use the same procedure as previously, accessing a record's table using source().

	smithVisits.source()->gotoRecord(1);  
	Patients.Visits->Why = "Changed Again";  

Now, we return to the new record (in the cache) and update it. Note that for this test we directly use record numbers. This is because there are so few records in the database that we know which ones are where. In a large database, we would probably have to search for the record we just added, just to make sure we could find it again.

Testing: modification of record in cache

	smithVisits.source()->gotoRecord(2);  
	Patients.Visits->VisitDate = "15/2/1994";

We now save the changes so they aren't just in cache any more.

	Patients.saveRecord();   

Then we print the table and dbView again so we can see the changes made.

	cout << "Dumping Smith and visits with added visit: " << endl 
	     << Patients << endl 
		 << smithVisits << endl;

Now we print just the "Visits" table to see the changes made to visits.   

	cout << "Now dumping the entire Visits file: " << endl << Visits;  
	cout << "done" << endl;
	  
	return EXIT_SUCCESS;
}        

 

Feature index

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