KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: 4D records can be stored in blobs
PRODUCT: 4D | VERSION: | PLATFORM: Mac & Win
Published On: February 14, 2002

One of the interesting things about blobs is that they can store any type of information, including documents. This makes it possible to store 4D records in blobs.


In the following pseudo code, the project method SendRecordToBlob stores the contents of a record from Table1 to an external document. Then, that document is read into the blob field of a new record in Table2.

Then, the pseudo code of the project method RestoreRecordFromBlob writes the contents of the BLOB field from the record in Table 2 (containing the record from Table1) to a document. The record in that document is then imported into Table3.


Note that:

1. Table3 must be identical in structure to Table1 in order to receive the

record from Table 1.

2. SET CHANNEL must be used with SEND RECORD/RECEIVE RECORD. (Please refer

to the 4D Language Reference for the SET CHANNEL operation codes.)

3. This code is only intended to demonstrate a concept; it is not a full

implementation (explained below).




PROJECT METHOD: SendRecordToBlob



ALL RECORDS([Table1]) `Make sure you have a current record

SET CHANNEL(10;"TempRecord") `Create a document on disk to write to.

SEND RECORD([Table1]) `Write the current record to the document

SET CHANNEL(11) `Close the document.

CREATE RECORD([Table2]) `Create a record to receive the blob

DOCUMENT TO BLOB(Document;[Table2]BlobField)

`Import the document into the blob

SAVE RECORD([Table2])

DELETE DOCUMENT(Document) `Delete the temporary file




PROJECT METHOD: RestoreRecordFromBlob


LOAD RECORD([Table2]) `You must have a record in memory, or BLOB TO

DOCUMENT fails.

BLOB TO DOCUMENT("TempRecord";[Table2]BlobField) `Write the blob field to a

document.

SET CHANNEL(10;"TempRecord") `Access the document.

RECEIVE RECORD([Table3])

SAVE RECORD([Table3])

SET CHANNEL(11) `Close the document.

DELETE DOCUMENT(Document)


As mentioned before, the preceding code is not a full implementation. There are many issues that have not been dealt with here, in order to keep the code focused on the topic of this Tech Tip. There is a BLOBIt demonstration database, which handles many of those issues. Some of those issues are:


  • How to structure the tables of such an archiving system.
  • Dealing with Mac and Windows document files.
  • Dealing with storing the two forks of a MAC document.
  • Program control based upon success or failure of the execution of various 4D commands throughout the code.
  • Error handling at various points throughout the code.


The BLOBIt demonstration database can be found and downloaded at

http://www.4d.com/downloads/beginner_examples.html.


An alternate implementation might loop over all the fields of a record, or just a few fields that you wanted to be replicated in another database, and that data would be put into a BLOB. From there, the data could be extracted on a field-by-field basis. A blob containing such data could be sent over the Internet using the 4D Internet Commands to set up a replicated database. But that is a topic for another time!