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

OOFTST 31 - BLOB operations

BLOBs are stored in the database and updated, with various operations changing their size and partial contents.


#include "oofile.h"	// the general oofile library

We start by declaring a class that emulates the requirements of an image viewer. It will take in a BLOB as a representation of the actual image.

DECLARE_CLASS(dbImageViewer)
        dbChar          FileName;
        dbDate	        FileModDate;
        dbChar          Description;

Note that this is the BLOB field. We give it a name, below, but no size and it is non-indexed.

        dbBLOB          Thumbnail;  
        dbImageViewer() : dbTable("ImagView") ,
                                FileName(50, "File Name"),
                                FileModDate("Mod Date"),
                                Description(100, "Description"),
                                Thumbnail("Thumbnail")
        {
                FileName.index(kIndexCompress);
                FileModDate.index(kIndexCompress);
                Description.index(kIndexCompress);
                setSortOrder(FileName);
        };
          
        void addTestData();
};  

This procedure adds some test data.

void
dbImageViewer::addTestData()
{

We declare the first buffer as an array, and initialise it with a mix of different types of information: decimal as well as hexadecimal.

	long testBuf[] = {0, 2, 4, 8, 16, 65535, 0xADADADAD};

We also need the size of the buffer, so that we can copy it later.

	long bufLen = sizeof(testBuf);

Here we declare the other buffer as being a pointer to long and initialise its size as being the number of longs in the previous buffer -> we find this number by dividing the previous buffer's total size by the sizeof() a long.

	long* copiedBuf = new long[bufLen/sizeof(long)];

Now we copy the buffer into the new buffer by using memcpy(). Note that we pass in the buffer to be copied to, then the buffer to be copied from, then the size of memory to be copied.

	memcpy(copiedBuf, testBuf, bufLen);

Here we just add two records.

	newRecord();
	FileName = "first blob";
	FileModDate = dbDate::currentDate();
	Description = "0, 2, 4, 8, 16, 65535, 0xADADADAD";

Note here, that we actually transfer ownership of this memory, it's not just a copy operation!

Testing: adding data to BLOB by transfer of ownership using adoptBody()

	Thumbnail.adoptBody(copiedBuf, bufLen);
	saveRecord();
	  
	newRecord();
	FileName = "second blob";
	FileModDate = dbDate::currentDate()+1;
	Description = "0, 2, 4, 8, 16, 65535, 0xADADADAD repeated";
	Thumbnail.setBytes(testBuf, bufLen);

This statement would cause assertion failure:     Thumbnail += Thumbnail;
But we plan to eventually make it available to use. Instead we use append(), below.

Testing: appending data to BLOB using append()

	Thumbnail.append(testBuf, bufLen);
	saveRecord();
}
  
int 
main()
{
	cout << "OOFILE Validation Suite - Test 31\n"
		 << "Store blobs in database"
		 << endl << endl;  

Now we declare our global variables, the database and the Imagers. Note that they aren't really images, just arbitrary blobs for now! 

	TEST_CONNECT    theDB;
	dbImageViewer     Images;

We assign our filename according to platform.

	#ifdef TESTING_DBASE
		#ifdef _Macintosh
			const char* kExistsName =  ":ooftst31:ImagView.dbf";
			const char* kDatabaseName = ":ooftst31:";
		#else
			const char* kExistsName =   "ImagView.dbf"
			const char* kDatabaseName = "";
		#endif	  
	#else
		const char* kDatabaseName = "ooftst31.db";
		const char* kExistsName = kDatabaseName;
	#endif

Then either open and clean our database file or create a new one.

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

Then we add our new test data and print its initial condition

	Images.addTestData(); 
	cout << "Dump of entire database\n" << Images << endl;

We move to the first record, using start(), then modify part of the BLOB to 'ABC' using the function setBytes(). We save our changes and reprint the database to see what has changed.

Testing: function  setBytes()
Testing: modifying BLOB data using setBytes()

	Images.start();
	Images.Thumbnail.setBytes("ABC", 3);
	Images.saveRecord();
	cout << "Modify BLOB in first record to 'ABC' (hex 41, 42, 43)\n" << Images << endl;
	  
	cout << endl << "done" << endl;
	return EXIT_SUCCESS;
}        

 

Feature index

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