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()

//

// Populates table and perform a simple query

//

VOID Manage(VOID)

{

CTSIGNED quantity;

CTFLOAT price, total;

TEXT itemnumb[5+1], custnumb[4+1], ordrnumb[6+1], custname[47+1];

CTBOOL isOrderFound, isItemFound;

printf("MANAGE\n");

// populate the tables with data

Add_CustomerMaster_Records();

Add_CustomerOrders_Records();

Add_OrderItems_Records();

Add_ItemMaster_Records();

// perform a query:

// list customer name and total amount per order

// name total

// @@@@@@@@@@@@@ $xx.xx

// for each order in the CustomerOrders table

// fetch order number

// fetch customer number

// fetch name from CustomerMaster table based on customer number

// for each order item in OrderItems table

// fetch item quantity

// fetch item number

// fetch item price from ItemMaster table based on item number

// next

// next

printf("\n\tQuery Results\n");

try

{

// get the first order

isOrderFound = recordCustOrdr.First();

while (isOrderFound) // for each order in the CustomerOrders table

{

// fetch order number

strcpy(ordrnumb, recordCustOrdr.GetFieldAsString(2).c_str());

// fetch customer number

strcpy(custnumb, recordCustOrdr.GetFieldAsString(3).c_str());

// fetch name from CustomerMaster table based on customer number

recordCustMast.Clear();

recordCustMast.SetFieldAsString(0, custnumb);

if (!recordCustMast.Find(CTFIND_EQ))

continue; // not possible in our canned example

strcpy(custname, recordCustMast.GetFieldAsString(4).c_str());

// fetch item price from OrderItems table

recordOrdrItem.Clear();

recordOrdrItem.SetFieldAsString(2, ordrnumb);

// define a recordset to scan only items applicable to this order

recordOrdrItem.RecordSetOn(6);

isItemFound = recordOrdrItem.First();

total = 0;

while (isItemFound) // for each order item in OrderItems table

{

// fetch item quantity

quantity = recordOrdrItem.GetFieldAsSigned(1);

// fetch item number

strcpy(itemnumb, recordOrdrItem.GetFieldAsString(3).c_str());

// fetch item price from ItemMaster table based on item number

recordItemMast.Clear();

recordItemMast.SetFieldAsString(2, itemnumb);

recordItemMast.Find(CTFIND_EQ);

price = recordItemMast.GetFieldAsFloat(1);

// calculate order total

total += (price * quantity);

isItemFound = recordOrdrItem.Next();

}

recordOrdrItem.RecordSetOff();

// output data to stdout

printf("\t\t%-20s %.2f\n", custname, total);

// read next order

if (!recordCustOrdr.Next())

isOrderFound = 0;

}

}

catch(CTException E)

{

Handle_Exception(E);

}

}

//

// Add_CustomerMaster_Records()

//

// This function adds records to table CustomerMaster from an

// array of strings

//

VOID Add_CustomerMaster_Records(VOID)

{

typedef struct {

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

} DATA_RECORD;

DATA_RECORD 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"

};

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

Delete_Records(recordCustMast);

printf("\tAdd records in table CustomerMaster...\n");

try

{

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

{

recordCustMast.Clear();

// populate record buffer with data

recordCustMast.SetFieldAsString(0, data[i].number);

recordCustMast.SetFieldAsString(1, data[i].zipcode);

recordCustMast.SetFieldAsString(2, data[i].state);

recordCustMast.SetFieldAsString(3, data[i].rating);

recordCustMast.SetFieldAsString(4, data[i].name);

recordCustMast.SetFieldAsString(5, data[i].address);

recordCustMast.SetFieldAsString(6, data[i].city);

// add record

recordCustMast.Write();

}

}

catch(CTException E)

{

Handle_Exception(E);

}

}

//

// Add_CustomerOrders_Records()

//

// This function adds records to table CustomerOrders from an

// array of strings

//

VOID Add_CustomerOrders_Records(VOID)

{

CTDate orderdate;

CTDate promisedate;

typedef struct {

cpTEXT orderdate, promisedate, ordernum, customernum;

} DATA_RECORD;

DATA_RECORD data[] = {

{"09/01/2002", "09/05/2002", "1", "1001"},

{"09/02/2002", "09/06/2002", "2", "1002"}

};

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

Delete_Records(recordCustOrdr);

printf("\tAdd records in table CustomerOrders...\n");

try

{

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

{

recordCustOrdr.Clear();

orderdate.StringToDate(data[i].orderdate, CTDATE_MDCY);

promisedate.StringToDate(data[i].promisedate, CTDATE_MDCY);

// populate record buffer with data

recordCustOrdr.SetFieldAsDate(0, orderdate);

recordCustOrdr.SetFieldAsDate(1, promisedate);

recordCustOrdr.SetFieldAsString(2, data[i].ordernum);

recordCustOrdr.SetFieldAsString(3, data[i].customernum);

// add record

recordCustOrdr.Write();

}

}

catch(CTException E)

{

Handle_Exception(E);

}

}

//

// Add_OrderItems_Records()

//

// This function adds records to table OrderItems from an

// array of strings

//

VOID Add_OrderItems_Records(VOID)

{

typedef struct {

CTSIGNED sequencenum, quantity;

cpTEXT ordernum, itemnum;

} DATA_RECORD;

DATA_RECORD data[] = {

{1, 2, "1", "1"},

{2, 1, "1", "2"},

{3, 1, "1", "3"},

{1, 3, "2", "3"}

};

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

Delete_Records(recordOrdrItem);

printf("\tAdd records in table OrderItems...\n");

try

{

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

{

recordOrdrItem.Clear();

// populate record buffer with data

recordOrdrItem.SetFieldAsSigned(0, data[i].sequencenum);

recordOrdrItem.SetFieldAsSigned(1, data[i].quantity);

recordOrdrItem.SetFieldAsString(2, data[i].ordernum);

recordOrdrItem.SetFieldAsString(3, data[i].itemnum);

// add record

recordOrdrItem.Write();

}

}

catch(CTException E)

{

Handle_Exception(E);

}

}

//

// Add_ItemMaster_Records()

//

// This function adds records to table ItemMaster from an

// array of strings

//

VOID Add_ItemMaster_Records(VOID)

{

typedef struct {

CTSIGNED weight;

CTMONEY price;

cpTEXT itemnum, description;

} DATA_RECORD;

DATA_RECORD data[] = {

{10, 1995, "1", "Hammer"},

{3, 999, "2", "Wrench"},

{4, 1659, "3", "Saw"},

{1, 398, "4", "Pliers"}

};

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

Delete_Records(recordItemMast);

printf("\tAdd records in table ItemMaster...\n");

try

{

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

{

recordItemMast.Clear();

// populate record buffer with data

recordItemMast.SetFieldAsSigned(0, data[i].weight);

recordItemMast.SetFieldAsMoney(1, data[i].price);

recordItemMast.SetFieldAsString(2, data[i].itemnum);

recordItemMast.SetFieldAsString(3, data[i].description);

// add record

recordItemMast.Write();

}

}

catch(CTException E)

{

Handle_Exception(E);

}

}

//

// Delete_Records()

//

// This function deletes all the records in the table

//

VOID Delete_Records(CTRecord& record)

{

CTBOOL found;

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

try

{

// read first record

found = record.First();

while (found) // while records are found

{

// delete record

record.Delete();

// read next record

found = record.Next();

}

}

catch(CTException E)

{

Handle_Exception(E);

}

}

TOCIndex