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

OOFTST 36 - duplicate record handling

This sample tests the database backend handling of duplicate records, duplicate record handling by automatic skipping, duplicate value tests and exceptions.


#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 a couple of extra classes. This first contains a string wheras the second contains text.

	class dbNoBlob : public dbTable {
		OOFILE_METHODS(dbNoBlob)
		dbChar	Title;		
		dbNoBlob() :
			Title(80, "Title", kIndexNoDups)
		{};
	};

The text field, here, is a blob as it inherits from the bdBlob field type.
 

	class dbHasBlob : public dbNoBlob {
		OOFILE_METHODS(dbHasBlob)
		dbText		Notes;			
	};  

These are the global variables that define the database using the ooftst02 classes.

	TEST_CONNECT    theDB;
	dbNoBlob     	noBlob;
	dbHasBlob		hasBlob;
	  
int main()
{
	cout << "OOFILE Validation Suite - Test 36\n"
		 << "Test ways to cope with duplicate records" << endl << endl;  

We assign a filename to the database and then open or create it, making sure the tables are empty.

	#ifdef TESTING_DBASE
		#ifdef _Macintosh
			const char* kExistsName =  ":ooftst36:noBlob.dbf";
			const char* kDatabaseName = ":ooftst36:";
		#else
			const char* kExistsName =   "noBlob.dbf"
			const char* kDatabaseName = "";
		#endif	  
	#else
		const char* kDatabaseName = "ooftst36.db";
		const char* kExistsName = kDatabaseName;
	#endif
	  
	if (dbConnect::fileExists(kExistsName)) {
		theDB.openConnection(kDatabaseName);
		noBlob.deleteAll();
		hasBlob.deleteAll();
	}
	else {
		theDB.newConnection(kDatabaseName);
	}  

Now we add some data.

	noBlob.newRecord();
	noBlob.Title = "Andy";
	noBlob.saveRecord();  

Then we ignore the duplicate records on the noBlob class by calling ignorDuplicateRecords(). We then try adding a duplicate record (to see if we can). It will not be added.

Testing: function ignoreDuplicateRecords()
Testing: adding duplicate record when ignoreDuplucateRecords() is on

	noBlob.ignoreDuplicateRecords();
	noBlob.newRecord();
	noBlob.Title = "Andy";
	noBlob.saveRecord();

We again allow duplicate records by calling the function noticeDuplicateRecords().

Testing: function noticeDuplicateRecords()

	noBlob.noticeDuplicateRecords();  

Now we try another duplicate record, but use stIgnoreDuplicateRecords which will use the stack. The record will still not be added.

	{
		stIgnoreDuplicateRecords blobDups(noBlob);	
		noBlob.newRecord();
		noBlob.Title = "Andy";
		noBlob.saveRecord();	// will not be added
	}

Now we try it with blobs. 

	hasBlob.newRecord();
	hasBlob.Title = "Dent";
	hasBlob.Notes = "This is the first record with a blob";
	hasBlob.saveRecord();  

Now we'll try a duplicate. It will again not be added.

	hasBlob.ignoreDuplicateRecords();
	hasBlob.newRecord();
	hasBlob.Title = "Dent";
	hasBlob.Notes = "Try to save a duplicate record with a blob";
	hasBlob.saveRecord();
	hasBlob.noticeDuplicateRecords();  

Now we print the database to show that it hasn't added all the dupliacte records.

	cout << theDB;

Now we'll try some duplicate detection. We have a conditional statement below that attempts to determine whether the record has been duplicated. We test for this by using the valueIsDuplicate() function on the title field.

Testing: function valueIsDuplicate()

	hasBlob.newRecord();
	hasBlob.Title = "Dent";
	if (hasBlob.Title.valueIsDuplicate())
		cout << "It works! - 'Dent' is a duplicate";
	else
		cout << "Duplicate checking doesn't work without indexes";
	cout << endl << 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