CPackedRecord class

Location

palmlib/database/record.h
palmlib/database/record.cpp

Dependencies

No dependencies.

Tutorial

// This tutorial describes how to setup a custom record and use it.
// Step 1: Record.h and Record.cpp contain all stuff needed for
//         our Custom record.

#include 
#include "Record.h"

// Step 2: We define our CCustomer class which stores the first and last
//         name of our customer and its contribution to something. CCustomer
//         is derived from CPackedRecord.

class CCustomer : public CPackedRecord
{
public:
// Construction
	// Step 3: We initialize our variables which we will define in Step 4.
	inline CCustomer() :
		firstname(0),
		lastname(0),
		contribution(0)
		{ }

// Attributes
	// Step 4: We define firstname, lastname and contribution as normal
	//         variables. firstname and lastname link to the record itself,
	//         after we will unpack the record.
	CharPtr firstname;
	CharPtr lastname;
	ULong contribution;

// Mapping
	// Step 5: In every derived CPackedRecord class we declare the field map.
	DM_DECLAREFIELDS(CCustomer);
};

// Step 6: After we declare the field map, we can implement it. Every attribute
//         has an associated DM_XXXFIELD. DM_FIXEDFIELD describes a field with
//         four bytes (ULong) and its storage, in our case the contribution.
//         DM_STRINGFIELD describes a field with a variable length string
//         terminated by '\0'. Now we designed a scheme for the record.
DM_IMPLEMENTFIELD_BEGIN(CCustomer)
	DM_FIXEDFIELD(ULong, contribution);
	DM_STRINGFIELD(firstname);
	DM_STRINGFIELD(lastname);
DM_IMPLEMENTFIELD_END()

void TestPackMechanism();

void TestPackMechanism()
{
	DmOpenRef db = 0;
	CCustomer customer;

	// Step 7: We create a database
	if ( ::DmCreateDatabase(0, "TestDb", 'test', 'DATA', false) != 0 )
		return;
	db = ::DmOpenDatabaseByTypeCreator('DATA', 'test', dmModeReadWrite);
	if ( db == 0 )
		return;

	// Step 8: We add three customers to the database. We simply assign
	//         extern variables to the exchange variables and AttachToDatabase()
	//         packs them and attach the newly created record to the database.
	//         If we want to link the exchange variables to the created record
	//         instead of the extern variables, we write:
	//         customer.AttachToDatabase(db, true);
	//         instead of
	//		   customer.AttachToDatabase(db);
	customer.firstname = "John";
	customer.lastname  = "Doe";
	customer.contribution = 10;
	customer.AttachToDatabase(db);

	customer.firstname = "Jim";
	customer.lastname  = "Franklyn";
	customer.contribution = 50;
	customer.AttachToDatabase(db);

	customer.firstname  = "Holly";
	customer.lastname   = "Mueller";
	customer.contribution = 30;
	customer.AttachToDatabase(db);

	// Step 9: We iterate through the database and unpack all exchange variables.
	//         Then we add 10 dollars to the contribution. We do this to all people.
	for (UInt i = 0; i < DmNumRecords(db); ++i)
	{
		// Step 9.1: We first attach the database manager record loosly to
		//           our customer record.
		customer.AttachToRecord(db, i);
		// Step 9.2: We unpack the whole database manager record and gain access
		//           to all variables in the database manager record.
		customer.Unpack();
		// Step 9.3: We add 10 dollars to the contribution.
		customer.contribution += 10;
		// Step 9.4: We pack all exchange variables back to the database
		//	     manager record. Commit automatically releases the
		//           database manager record if we
		//           does not unpack it during commit.
		customer.Commit();
	}

	// Step 10: Again we iterate through the database and look whether the contribution
	//          was changed.
	for (UInt i = 0; i < DmNumRecords(db); ++i)
	{
		// Step 10.1: We attach loosly the database record to the Customer record.
		customer.AttachToRecord(db, i);
		// Step 10.2: We unpack the whole record and we can see that the
		//            contribution is changed.
		customer.Unpack();
		// Step 10.3: We can release the record.
		customer.Release();
	}

	// Step 11: Finally we close the database.
	DmCloseDatabase(db);
}

DWord PilotMain( Word cmd, Ptr cmdPBP, Word launchFlags)
{
	// If normal launch
	if (cmd == sysAppLaunchCmdNormalLaunch)
	{
		TestPackMechanism();
	}
	return 0;
}