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

OOFTST 25 - tests RAM backend

This sample tests the database RAM backend by creating a single table similarly to a ctree table.


#include "oofile.h"	// the general oofile library
#include "ooftst01.h"	// the declarations for the database classes we will be using in this test.
  

First we declare our table class. We only want it to be small, so will just include a couple of little fields.

	DECLARE_CLASS(littleDB)
		dbChar	f1;
		dbLong	f2;
		littleDB() :
			f1(80, "f1"),
			f2("f2")
		{};
	};
  
int main()
{
	cout << "OOFILE Validation Suite - Test 25\n"
		 << "Simple test to store some data and retrieve it\n"
		 << "in a single-table database, and then use a RAM-based equivalent\n";
	  

These are the global variables for our database and table.

	TEST_CONNECT    theDB;
	dbPeople     People;  

Here, we determine what platform we are running under and assign a filename accordingly.

#ifdef TESTING_CTREE
	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
		#ifdef _Macintosh
			const char* kDatabaseName = ":ooftst01:ooftst01.db";
		#else
			const char* kDatabaseName = "ooftst01.db";
		#endif	
		const char* kExistsName = kDatabaseName;
	#endif
#endif  

Now we open or create our database, making sure it is empty and then filling it with new test data.

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

We initially have it sorted by lastName.

	People.setSortOrder(People.LastName);
	cout << "Listing records\n" << People << endl;
	  

Up to here, this sample has been the same as ooftst01.

Opening a simple RAM table

Now we We start by declaring a ram connection. This creates our table in RAM, the connection itself is temporary and can be easily thrown away when we've finished defining the tables of the database.

Testing: declaring connection for RAMtable

	dbConnect_ram	tempDB;

We then declare our little database (from the class above), calling it simpleRAMtable, and then create the connection for it using newConnection().

Testing: creating new connection for RAMtable

	littleDB simpleRAMtable;
	tempDB.newConnection();

Now we'll add some test data to our table. 

Testing: creating record for RAMtable
Testing: adding data to RAMtable
Testing: saving data to RAMtable

	simpleRAMtable.newRecord();
	simpleRAMtable.f1 = "Andy Dent";
	simpleRAMtable.f2 = 99;
	simpleRAMtable.saveRecord();
	simpleRAMtable.newRecord();
	simpleRAMtable.f1 = "Trissa Dent";
	simpleRAMtable.f2 = 100;
	simpleRAMtable.saveRecord();

Then see if it prints the data out for us.

	cout << "Listing simple table\n" << simpleRAMtable << endl;  

Cloning a RAM table from an existing table

	cout << "Defining a table in RAM from an existing ctree table\n";

First, we must declare our table. We initialise it by cloning from the table we declared and opened earlier. The function we use is cloneEmptyInRAM() this copies the other table's structure into our RAM table, but does not copy the data in (see below for that). It is empty.

Testing: function cloneEmptyInRAM()

	dbPeople* PR = People.cloneEmptyInRAM();

Now we'll print out the data to see if it cloned properly.

	cout << "Listing empty table in RAM\n" << PR << endl;

Now we try adding data to our table. We can just use the same method as for the c-tree table, but must reference the table slightly differently (as shown below).

	PR->AddTestData();

Now we print it out again to see the difference.

	cout << "Listing populated table in RAM\n" << PR << endl;
	delete PR;  

We'll now try to create and populate a table in one hit. As you can see, we use the function cloneInRAM() instead of cloneEmptyInRAM() (used above). This means all the data will be copied into the database as well.

Testing: function cloneInRAM()

	dbPeople* PR2 = People.cloneInRAM();

Now we print it out to see if it has all the data that the other one had.

	cout << "Listing populated table in RAM\n" << PR2 << endl;
	delete PR2;  
	cout << "Test Completed" << endl;
		  
	return EXIT_SUCCESS;
}  

 

Feature index

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