Manage() provides data management functionality for your application and/or process.
Below is the code for Manage():
/*
* Manage()
*
* Populates table and perform a simple query
*
*/
#ifdef PROTOTYPE
VOID Manage(VOID)
#else
VOID Manage()
#endif
{
printf("MANAGE\n");
if ((hRecordCustMast = ctdbAllocRecord(hTableCustMast)) == NULL)
Handle_Error("Add_Customer_Records(): ctdbAllocRecord()");
if ((hRecordCustOrdr = ctdbAllocRecord(hTableCustOrdr)) == NULL)
Handle_Error("Add_Customer_Records(): ctdbAllocRecord()");
if ((hRecordOrdrItem = ctdbAllocRecord(hTableOrdrItem)) == NULL)
Handle_Error("Add_Customer_Records(): ctdbAllocRecord()");
if ((hRecordItemMast = ctdbAllocRecord(hTableItemMast)) == NULL)
Handle_Error("Add_Customer_Records(): ctdbAllocRecord()");
/* write lock required for transaction updates */
if (ctdbLock(hSession, CTLOCK_WRITE))
Handle_Error("Manage(): ctdbLock()");
/* start a transaction */
if (ctdbBegin(hSession))
Handle_Error("Manage(): ctdbBegin()");
/* populate the tables with data */
Add_CustomerMaster_Records();
Add_ItemMaster_Records();
/* commit transaction */
if (ctdbCommit(hSession))
Handle_Error("Manage(): ctdbCommit()");
/* free locks */
if (ctdbUnlock(hSession))
Handle_Error("Manage(): ctdbUnlock()");
Add_Transactions();
/* display the orders and their items */
Display_CustomerOrders();
Display_OrderItems();
}
/*
* 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 data4[] = {
"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(data4) / sizeof(CUSTOMER_DATA);
Delete_Records(hRecordCustMast);
printf("\tAdd records in table CustomerMaster...\n");
/* add records to table */
for (i = 0; i < nRecords; i++)
{
/* clear record buffer */
ctdbClearRecord(hRecordCustMast);
retval = 0;
/* populate record buffer with data */
retval |= ctdbSetFieldAsString(hRecordCustMast, 0, data4[i].number);
retval |= ctdbSetFieldAsString(hRecordCustMast, 1, data4[i].zipcode);
retval |= ctdbSetFieldAsString(hRecordCustMast, 2, data4[i].state);
retval |= ctdbSetFieldAsString(hRecordCustMast, 3, data4[i].rating);
retval |= ctdbSetFieldAsString(hRecordCustMast, 4, data4[i].name);
retval |= ctdbSetFieldAsString(hRecordCustMast, 5, data4[i].address);
retval |= ctdbSetFieldAsString(hRecordCustMast, 6, data4[i].city);
if(retval)
Handle_Error("Add_Customer_Records(): ctdbSetFieldAsString()");
/* add record */
if (ctdbWriteRecord(hRecordCustMast))
Handle_Error("Add_Customer_Records(): ctdbWriteRecord()");
}
}
/*
* Add_ItemMaster_Records()
*
* This function adds records to table ItemMaster from an
* array of strings
*/
typedef struct {
CTSIGNED weight;
CTMONEY price;
CTSTRING itemnum, description;
} ITEM_DATA;
ITEM_DATA data3[] = {
{10, 1995, "1", "Hammer"},
{3, 999, "2", "Wrench"},
{4, 1659, "3", "Saw"},
{1, 398, "4", "Pliers"}
};
#ifdef PROTOTYPE
VOID Add_ItemMaster_Records(VOID)
#else
VOID Add_ItemMaster_Records()
#endif
{
CTDBRET retval;
CTSIGNED i;
CTSIGNED nRecords = sizeof(data3) / sizeof(ITEM_DATA);
Delete_Records(hRecordItemMast);
printf("\tAdd records in table ItemMaster...\n");
/* add records to table */
for (i = 0; i < nRecords; i++)
{
/* clear record buffer */
ctdbClearRecord(hRecordItemMast);
retval = 0;
/* populate record buffer with data */
retval |= ctdbSetFieldAsSigned(hRecordItemMast, 0, data3[i].weight);
retval |= ctdbSetFieldAsMoney(hRecordItemMast, 1, data3[i].price);
retval |= ctdbSetFieldAsString(hRecordItemMast, 2, data3[i].itemnum);
retval |= ctdbSetFieldAsString(hRecordItemMast, 3, data3[i].description);
if(retval)
Handle_Error("Add_ItemMaster_Records(): ctdbSetFieldAsString()|ctdbSetFieldAsSigned()");
/* add record */
if (ctdbWriteRecord(hRecordItemMast))
Handle_Error("Add_ItemMaster_Records(): ctdbWriteRecord()");
}
}
/*
* Delete_Records()
*
* This function deletes all the records in the table based
* on the input parameter
*/
#ifdef PROTOTYPE
VOID Delete_Records(CTHANDLE hRecord)
#else
VOID Delete_Records(hRecord)
CTHANDLE hRecord;
#endif
{
CTDBRET retval;
CTBOOL empty;
printf("\tDelete records...\n");
if (ctdbClearRecord(hRecord))
Handle_Error("Delete_Records(): ctdbClearRecord()");
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()");
}
}
}
/*
* Add_Transactions()
*
* Add an Order and associated Items "as a transaction" to their
* respective tables. A transaction is committed or aborted if the
* customer number on the order is confirmed valid. Likewise each
* item in the order is verified to be a valid item. SavePoints are
* established as an order is processed, allowing a transaction to
* rollback to the previously verified item
*/
typedef struct {
CTSTRING orderdate, promdate, ordernum, custnum;
} ORDER_DATA;
typedef struct {
CTSTRING ordernum;
CTSIGNED sequnumb;
CTSIGNED quantity;
CTSTRING itemnumb;
} ORDERITEM_DATA;
ORDER_DATA orders[] = {
{"09/01/2002", "09/05/2002", "1", "1001"},
{"09/02/2002", "09/06/2002", "2", "9999"}, /* bad customer number */
{"09/22/2002", "09/26/2002", "3", "1003"}
};
ORDERITEM_DATA items[] = {
{"1", 1, 2, "1"},
{"1", 2, 1, "2"},
{"2", 1, 1, "3"},
{"2", 2, 3, "4"},
{"3", 1, 2, "3"},
{"3", 2, 2, "99"} /* bad item number */
};
#ifdef PROTOTYPE
VOID Add_Transactions(VOID)
#else
VOID Add_Transactions()
#endif
{
CTDBRET retval;
NINT savepoint;
CTDATE orderdate;
CTDATE promdate;
CTSIGNED i, j = 0;
CTSIGNED nOrders = sizeof(orders) / sizeof(ORDER_DATA);
CTSIGNED nItems = sizeof(items) / sizeof(ORDERITEM_DATA);
/* write lock required for transaction updates */
if (ctdbLock(hSession, CTLOCK_WRITE))
Handle_Error("Add_Transactions(): ctdbLock()");
/* start a transaction */
if (ctdbBegin(hSession))
Handle_Error("Add_Transactions(): ctdbBegin()");
Delete_Records(hRecordCustOrdr);
Delete_Records(hRecordOrdrItem);
/* commit transaction */
if (ctdbCommit(hSession))
Handle_Error("Add_Transactions(): ctdbCommit()");
/* free locks */
if (ctdbUnlock(hSession))
Handle_Error("Add_Transactions(): ctdbUnlock()");
printf("\tAdd transaction records... \n");
/* process orders */
for(i = 0; i < nOrders; i++)
{
/* start a transaction */
if (ctdbBegin(hSession))
Handle_Error("Add_Transactions(): ctdbBegin()");
ctdbClearRecord(hRecordCustOrdr);
retval = 0;
/* populate record buffer with order data */
retval |= ctdbStringToDate(orders[i].orderdate, CTDATE_MDCY, &orderdate);
retval |= ctdbStringToDate(orders[i].promdate, CTDATE_MDCY, &promdate);
retval |= ctdbSetFieldAsDate(hRecordCustOrdr, 0, orderdate);
retval |= ctdbSetFieldAsDate(hRecordCustOrdr, 1, promdate);
retval |= ctdbSetFieldAsString(hRecordCustOrdr, 2, orders[i].ordernum);
retval |= ctdbSetFieldAsString(hRecordCustOrdr, 3, orders[i].custnum);
if(retval)
Handle_Error("Add_Transactions(): ctdbSetFieldAsString()");
/* add order record */
if (ctdbWriteRecord(hRecordCustOrdr))
Handle_Error("Add_Transactions(): ctdbWriteRecord()");
/* set transaction savepoint */
savepoint = ctdbSetSavePoint(hSession);
/* process order items */
while (!(strcmp(items[j].ordernum, orders[i].ordernum)))
{
ctdbClearRecord(hRecordOrdrItem);
retval = 0;
/* populate record buffer with order item data */
retval |= ctdbSetFieldAsSigned(hRecordOrdrItem, 0, items[j].sequnumb);
retval |= ctdbSetFieldAsSigned(hRecordOrdrItem, 1, items[j].quantity);
retval |= ctdbSetFieldAsString(hRecordOrdrItem, 2, items[j].ordernum);
retval |= ctdbSetFieldAsString(hRecordOrdrItem, 3, items[j].itemnumb);
if (retval)
Handle_Error("Add_Transactions(): ctdbSetFieldAsString()|ctdbSetFieldAsSigned()");
/* add order item record */
if (ctdbWriteRecord(hRecordOrdrItem))
Handle_Error("Add_Transactions(): ctdbWriteRecord()");
/* check that item exists in ItemMaster table */
if (ctdbClearRecord(hRecordItemMast))
Handle_Error("Add_Transactions(): ctdbClearRecord()");
if (ctdbSetFieldAsString(hRecordItemMast, 2, items[j].itemnumb))
Handle_Error("Add_Transactions(): ctdbSetFieldAsString()");
if (ctdbFindRecord(hRecordItemMast, CTFIND_EQ))
/* if not found, restore back to previous savepoint */
ctdbRestoreSavePoint(hSession, savepoint);
else
/* set transaction savepoint */
savepoint = ctdbSetSavePoint(hSession);
/* bump to next item */
j++;
/* exit the while loop on last item */
if (j >= nItems)
break;
}
/* check that customer exists in CustomerMaster table */
if ((retval = ctdbClearRecord(hRecordCustMast)) != CTDBRET_OK)
Handle_Error("Add_Transactions(): ctdbClearRecord()");
if (ctdbSetFieldAsString(hRecordCustMast, 0, orders[i].custnum))
Handle_Error("Add_Transactions(): ctdbSetFieldAsString()");
/* commit or abort the transaction */
if (ctdbFindRecord(hRecordCustMast, CTFIND_EQ))
{
if (ctdbAbort(hSession))
Handle_Error("Add_Transactions(): ctdbAbort()");
}
else
{
if (ctdbCommit(hSession))
Handle_Error("Add_Transactions(): ctdbCommit()");
}
}
}
/*
* Display_CustomerOrders()
*
* 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_CustomerOrders(VOID)
#else
VOID Display_CustomerOrders()
#endif
{
CTDBRET retval;
TEXT custnumb[4+1];
TEXT ordrnumb[6+1];
printf("\n\tCustomerOrder table...\n");
/* read first record */
retval = ctdbFirstRecord(hRecordCustOrdr);
if (retval != CTDBRET_OK)
Handle_Error("Display_CustomerOrders(): ctdbFirstRecord()");
while (retval != END_OF_FILE)
{
retval = 0;
retval |= ctdbGetFieldAsString(hRecordCustOrdr, 2, ordrnumb, sizeof(ordrnumb));
retval |= ctdbGetFieldAsString(hRecordCustOrdr, 3, custnumb, sizeof(custnumb));
if (retval)
Handle_Error("Display_CustomerOrders(): ctdbGetFieldAsString()");
printf("\t %s %s\n", ordrnumb, custnumb);
/* read next record */
retval = ctdbNextRecord(hRecordCustOrdr);
if (retval == END_OF_FILE)
break; /* reached end of file */
if (retval != CTDBRET_OK)
Handle_Error("Display_CustomerOrders(): ctdbNextRecord()");
}
}
/*
* Display_OrderItems()
*
* 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_OrderItems(VOID)
#else
VOID Display_OrderItems()
#endif
{
CTDBRET retval;
TEXT itemnumb[5+1];
TEXT ordrnumb[6+1];
printf("\n\tOrderItems Table...\n");
/* read first record */
retval = ctdbFirstRecord(hRecordOrdrItem);
if (retval != CTDBRET_OK)
Handle_Error("Display_OrderItems(): ctdbFirstRecord()");
while (retval != END_OF_FILE)
{
retval = 0;
retval |= ctdbGetFieldAsString(hRecordOrdrItem, 2, ordrnumb, sizeof(ordrnumb));
retval |= ctdbGetFieldAsString(hRecordOrdrItem, 3, itemnumb, sizeof(itemnumb));
if (retval)
Handle_Error("Display_OrderItems(): ctdbGetFieldAsString()");
printf("\t %s %s\n", ordrnumb, itemnumb);
/* read next record */
retval = ctdbNextRecord(hRecordOrdrItem);
if (retval == END_OF_FILE)
break; /* reached end of file */
if (retval != CTDBRET_OK)
Handle_Error("Display_OrderItems(): ctdbNextRecord()");
}
}