Previous Topic

Next Topic

Manage

The manage step 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)
{
   printf("MANAGE\n");

   // populate the tables with data
   Add_CustomerMaster_Records();
   Add_ItemMaster_Records();

   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
//

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
   {
      // start a transaction
      recordCustMast->Begin();

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

      // commit transaction
      recordCustMast->Commit();
   }
   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
   {
      // start a transaction
      recordItemMast->Begin();

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

      // commit transaction
      recordItemMast->Commit();
   }
   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
   {
      // write lock required for transaction updates
      record->Lock(CTLOCK_WRITE);

      // start a transaction
      record->Begin();

      // read first record
      found = record->First();

      while (found)  // while records are found
      {
         // delete record
         record->Delete();
         // read next record
         found = record->Next();
      }

      // commit transaction
      record->Commit();

      // free locks
      record->Unlock();
   }
   catch(CTException E)
   {
      Handle_Exception(E);
   }
}


//
// 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
//

VOID Add_Transactions(VOID)
{
   typedef struct {
      cpTEXT orderdate, promdate, ordernum, custnum;
   } ORDER_DATA;

   typedef struct {
      cpTEXT ordernum;
      CTSIGNED seqnumber;
      CTSIGNED quantity;
      cpTEXT itemnum;
   } ITEM_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"}
   };

   ITEM_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
   };
   CTSIGNED nOrders = sizeof(orders) / sizeof(ORDER_DATA);
   CTSIGNED nItems = sizeof(items) / sizeof(ITEM_DATA);
   NINT  savepoint;
   CTDate  orderdate;
   CTDate  promdate;
   CTSIGNED j = 0;

   Delete_Records(recordCustOrdr);
   Delete_Records(recordOrdrItem);

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

   // process orders
   for (CTSIGNED i = 0; i < nOrders; i++)
   {
      // start a transaction
      MySession->Begin();

      try
      {
         recordCustOrdr->Clear();

         // populate record buffer with order data
         orderdate.StringToDate(orders[i].orderdate, CTDATE_MDCY);
         promdate.StringToDate(orders[i].promdate, CTDATE_MDCY);
         recordCustOrdr->SetFieldAsDate(0, orderdate);
         recordCustOrdr->SetFieldAsDate(1, promdate);
         recordCustOrdr->SetFieldAsString(2, orders[i].ordernum);
         recordCustOrdr->SetFieldAsString(3, orders[i].custnum);

         // add order record
         recordCustOrdr->Write();
      }
      catch (CTException E)
      {
         Handle_Exception(E);
      }

      // set transaction savepoint
      savepoint = recordCustOrdr->SetSavePoint();

      // process order items
      while (!(strcmp(items[j].ordernum, orders[i].ordernum)))
      {
         try
         {
            recordOrdrItem->Clear();

            // populate record buffer with order item data
            recordOrdrItem->SetFieldAsSigned(0, items[j].seqnumber);
            recordOrdrItem->SetFieldAsSigned(1, items[j].quantity);
            recordOrdrItem->SetFieldAsString(2, items[j].ordernum);
            recordOrdrItem->SetFieldAsString(3, items[j].itemnum);

            // add order item record
            recordOrdrItem->Write();

            // check that item exists in ItemMaster table
            recordItemMast->Clear();
            recordItemMast->SetFieldAsString(2, items[j].itemnum);
            if (!recordItemMast->Find(CTFIND_EQ))
               // if not found, restore back to previous savepoint
               recordItemMast->RestoreSavePoint(savepoint);
            else
               // set transaction savepoint
               savepoint = recordItemMast->SetSavePoint();
         }
         catch (CTException E)
         {
            Handle_Exception(E);
         }

         // bump to next item
         j++;

         // exit the while loop on last item
         if (j >= nItems)
            break;
      }

      // check that customer exists in CustomerMaster table
      recordCustMast->Clear();
      recordCustMast->SetFieldAsString(0, orders[i].custnum);

      // commit or abort the transaction
      if (!recordCustMast->Find(CTFIND_EQ))
         recordCustMast->Abort();
      else
         recordCustMast->Commit();
   }
}


//
// Display_CustomerOrders()
//
// This function displays the contents of a table. ctdbFirstRecord() and
// ctdbNextRecord() fetch the record. Then each field is parsed and displayed
//

VOID Display_CustomerOrders(VOID)
{
   CTString custnumb;
   CTString ordrnumb;

   printf("\n\tCustomerOrder table...\n");

   try
   {
      // read first record
      if (recordCustOrdr->First())
      {
         do
         {
            ordrnumb = recordCustOrdr->GetFieldAsString(2);
            custnumb = recordCustOrdr->GetFieldAsString(3);

            // display data
            printf("\t   %s   %s\n", ordrnumb.c_str(), custnumb.c_str());
         }
         // read next record until end of file
         while (recordCustOrdr->Next());
      }
   }
   catch (CTException E)
   {
      Handle_Exception(E);
   }
}


//
// Display_OrderItems()
//
// This function displays the contents of a table. ctdbFirstRecord() and
// ctdbNextRecord() fetch the record. Then each field is parsed and displayed
//

VOID Display_OrderItems(VOID)
{
   CTString itemnumb;
   CTString ordrnumb;

   printf("\n\tOrderItems Table...\n");

   try
   {
      // read first record
      if (recordOrdrItem->First())
      {
         do
         {
            // get field data from record buffer
            ordrnumb = recordOrdrItem->GetFieldAsString(2);
            itemnumb = recordOrdrItem->GetFieldAsString(3);

            // display data
            printf("\t   %s   %s\n", ordrnumb.c_str(), itemnumb.c_str());
         }
         // read next record until end of file
         while (recordOrdrItem->Next());
      }
   }
   catch (CTException E)
   {
      Handle_Exception(E);
   }
}