Product Documentation

c-treeDB API for C

Previous Topic

Next Topic

Manage

Manage() provides data management functionality for your application and/or process.

Below is the code for Manage():

/*

* Manage()

*

* This function performs record adds and updates using locking

*/

#ifdef PROTOTYPE

VOID Manage(VOID)

#else

VOID Manage()

#endif

{

printf("MANAGE\n");

/* allocate a record handle */

if ((hRecord = ctdbAllocRecord(hTable)) == NULL)

Handle_Error("Manage(): ctdbAllocRecord()");

/* delete any existing records */

Delete_Records(hRecord);

/* populate the table with data */

Add_CustomerMaster_Records();

/* display contents of table */

Display_Records();

/* update a record under locking control */

Update_CustomerMaster_Record();

/* display again after update and effects of lock */

Display_Records();

}

/*

* Delete_Records()

*

* This function deletes all the records in the table

*/

#ifdef PROTOTYPE

VOID Delete_Records(CTHANDLE hRecord)

#else

VOID Delete_Records(hRecord)

CTHANDLE hRecord;

#endif

{

CTDBRET retval;

CTBOOL empty;

printf("\tDelete records...\n");

/* enable session-wide lock flag */

if (ctdbLock(hSession, CTLOCK_WRITE_BLOCK))

Handle_Error("Delete_Records(): ctdbLock()");

empty = NO;

retval = ctdbFirstRecord(hRecord);

if (retval != CTDBRET_OK)

{

if (retval == END_OF_FILE)

empty = YES;

else

Handle_Error("Delete_Records(): ctdbFirstRecord()");

}

while (empty == NO) /* while table is not empty */

{

/* delete record */

if (ctdbDeleteRecord(hRecord))

Handle_Error("Delete_Records(): ctdbDeleteRecord()");

/* read next record */

retval = ctdbNextRecord(hRecord);

if (retval != CTDBRET_OK)

{

if (retval == END_OF_FILE)

empty = YES;

else

Handle_Error("Delete_Records(): ctdbNextRecord()");

}

}

if (ctdbUnlock(hSession))

Handle_Error("Delete_Records(): ctdbUnlock()");

}

/*

* Add_CustomerMaster_Records()

*

* This function adds records to table CustomerMaster from an

* array of strings

*/

typedef struct {

CTSTRING number, zipcode, state, rating, name, address, city;

} CUSTOMER_DATA;

CUSTOMER_DATA data[] = {

"1000", "92867", "CA", "1", "Bryan Williams", "2999 Regency", "Orange",

"1001", "61434", "CT", "1", "Michael Jordan", "13 Main", "Harford",

"1002", "73677", "GA", "1", "Joshua Brown", "4356 Cambridge", "Atlanta",

"1003", "10034", "MO", "1", "Keyon Dooling", "19771 Park Avenue", "Columbia"

};

#ifdef PROTOTYPE

VOID Add_CustomerMaster_Records(VOID)

#else

VOID Add_CustomerMaster_Records()

#endif

{

CTDBRET retval;

CTSIGNED i;

CTSIGNED nRecords = sizeof(data) / sizeof(CUSTOMER_DATA);

printf("\tAdd records...\n");

/* add data to table */

for (i = 0; i < nRecords; i++)

{

/* clear record buffer */

ctdbClearRecord(hRecord);

retval = 0;

/* populate record buffer with data */

retval |= ctdbSetFieldAsString(hRecord, 0, data[i].number);

retval |= ctdbSetFieldAsString(hRecord, 1, data[i].zipcode);

retval |= ctdbSetFieldAsString(hRecord, 2, data[i].state);

retval |= ctdbSetFieldAsString(hRecord, 3, data[i].rating);

retval |= ctdbSetFieldAsString(hRecord, 4, data[i].name);

retval |= ctdbSetFieldAsString(hRecord, 5, data[i].address);

retval |= ctdbSetFieldAsString(hRecord, 6, data[i].city);

if (retval)

Handle_Error("Add_CustomerMaster_Records(): ctdbSetFieldAsString()");

/* add record */

if (ctdbWriteRecord(hRecord))

Handle_Error("Add_CustomerMaster_Records(): ctdbWriteRecord()");

}

}

/*

* Display_Records()

*

* This function displays the contents of a table. ctdbFirstRecord() and

* ctdbNextRecord() fetch the record. Then each field is parsed and displayed

*/

#ifdef PROTOTYPE

VOID Display_Records(VOID)

#else

VOID Display_Records()

#endif

{

CTDBRET retval;

TEXT custnumb[4+1];

TEXT custname[47+1];

printf("\tDisplay records...");

/* read first record */

retval = ctdbFirstRecord(hRecord);

if (retval == END_OF_FILE)

return;

while (retval == CTDBRET_OK)

{

retval = 0;

retval |= ctdbGetFieldAsString(hRecord, 0, custnumb, sizeof(custnumb));

retval |= ctdbGetFieldAsString(hRecord, 4, custname, sizeof(custname));

if (retval)

Handle_Error("Display_Records(): ctdbGetFieldAsString()");

printf("\n\t\t%-8s%10s\n",custnumb, custname);

/* read next record */

retval = ctdbNextRecord(hRecord);

if (retval == END_OF_FILE)

break; /* reached end of file */

if (retval != CTDBRET_OK)

Handle_Error("Display_Records(): ctdbNextRecord()");

}

}

/*

* Update_CustomerMaster_Records()

*

* Update one record under locking control to demonstrate the effects

* of locking

*/

#ifdef PROTOTYPE

VOID Update_CustomerMaster_Record(VOID)

#else

VOID Update_CustomerMaster_Record()

#endif

{

printf("\tUpdate record...\n");

/* enable session-wide lock flag */

if (ctdbLock(hSession, CTLOCK_WRITE_BLOCK))

Handle_Error("Update_CustomerMaster_Record(): ctdbLock()");

/* find record by customer number */

if (ctdbClearRecord(hRecord))

Handle_Error("Update_CustomerMaster_Record(): ctdbClearRecord()");

if (ctdbSetFieldAsString(hRecord, 0, "1003"))

Handle_Error("Update_CustomerMaster_Record(): ctdbSetFieldAsString()");

if (ctdbFindRecord(hRecord, CTFIND_EQ))

Handle_Error("Update_CustomerMaster_Record(): ctdbFindRecord()");

if (ctdbSetFieldAsString(hRecord, 4, "KEYON DOOLING"))

Handle_Error("Update_CustomerMaster_Record(): ctdbSetFieldAsString()");

/* rewrite record */

if (ctdbWriteRecord(hRecord))

Handle_Error("Update_CustomerMaster_Record(): ctdbWriteRecord()");

else

{

printf("\tPress <ENTER> key to unlock\n");

getchar();

}

if (ctdbUnlock(hSession))

Handle_Error("Update_CustomerMaster_Record(): ctdbUnlock()");

}

TOCIndex