home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / database / stockcpp / store.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  13.4 KB  |  314 lines

  1. //******************************************************************************
  2. //                                                                             *
  3. //COPYRIGHT: Copyright (C) International Business Machines Corp., 1992,1994.   *
  4. //                                                                             *
  5. //DISCLAIMER OF WARRANTIES:                                                    *
  6. //  The following [enclosed] code is sample code created by IBM                *
  7. //  Corporation.  This sample code is not part of any standard IBM product     *
  8. //  and is provided to you solely for the purpose of assisting you in the      *
  9. //  development of your applications.  The code is provided "AS IS",           *
  10. //  without warranty of any kind.  IBM shall not be liable for any damages     *
  11. //  arising out of your use of the sample code, even if they have been         *
  12. //  advised of the possibility of such damages.                                *
  13. //                                                                             *
  14. //******************************************************************************
  15.  
  16. #include "store.hpp"
  17. #include <iomanip.h>
  18. //******************************************************************************
  19. // File Name: store.cpp                                                        *
  20. //                                                                             *
  21. // Note: This file illustrates how you can design your own object class        *
  22. //       based on the table object class generated by the DAX Data Access      *
  23. //       tool. This file keeps all the member functions of your own object     *
  24. //       class. The database connection is established in the constructor      *
  25. //       and is disconnected in the destructor.                                *
  26. //                                                                             *
  27. //******************************************************************************
  28. //******************************************************************************
  29. // Constructor: Establish the connection to the data base.                     *
  30. //******************************************************************************
  31. Store::Store() {
  32.   try {
  33.  
  34.        ISESQL = getenv("ISESQL");
  35.  
  36.        if (ISESQL == "TRUE") {
  37.           dsm3.connect("DAXSAMP");                                       // Embedded SQL Connection -  Only requires single connection
  38.        }
  39.        else {
  40.           dsm.connect("DAXSAMP");                                        // CLI or ODBC Connection
  41.           dsm1.connect("DAXSAMP");
  42.           dsm2.connect("DAXSAMP");
  43.        }
  44.  
  45.   } catch (IException &exc) {
  46.     cout << "Failed: Connection" << endl;
  47.     cout << "Error id: " << exc.errorId() << endl;
  48.     cout << "Error Text: " << exc.text() << endl;
  49.     cout << "Error Class: " << exc.name() << endl;
  50.   } catch(...) {
  51.     cout << "Unknown exception in connect" << endl;
  52.   }
  53. }
  54.  
  55. //******************************************************************************
  56. // Constructor: Establish the connection to the data base.                     *
  57. //******************************************************************************
  58. Store::Store(IString usrid, IString pwd): userid(usrid), password(pwd) {
  59.   try {
  60.  
  61.        ISESQL = getenv("ISESQL");
  62.  
  63.        if (ISESQL == "TRUE") {
  64.           dsm3.connect("DAXSAMP", userid, password);                     // Embedded SQL Connection -  Only requires single connection
  65.        }
  66.        else {
  67.           dsm.connect(userid, password);                                 // CLI or ODBC Connection
  68.           dsm1.connect(userid, password);
  69.           dsm2.connect(userid, password);
  70.        }
  71.  
  72.   } catch (IException &exc) {
  73.     cout << "Failed: Connection" << endl;
  74.     cout << "Error id: " << exc.errorId() << endl;
  75.     cout << "Error Text: " << exc.text() << endl;
  76.     cout << "Error Class: " << exc.name() << endl;
  77.   } catch(...) {
  78.     cout << "Unknown exception in connect" << endl;
  79.   }
  80. }
  81.  
  82. //******************************************************************************
  83. // Destructor: Disconnect the connection.                                      *
  84. //******************************************************************************
  85. Store::~Store() {
  86.   try {
  87.     // disconnect the connection
  88.     if (ISESQL == "TRUE") {
  89.        dsm3.disconnect();                           // Only single disconnection for Embedded SQL
  90.     }
  91.     else {
  92.        dsm.disconnect();                            // CLI or ODBC case
  93.        dsm1.disconnect();
  94.        dsm2.disconnect();
  95.     }
  96.   } catch (IException &exc) {
  97.     cout << "Failed: Disconnection" << endl;
  98.     cout << "Error id: " << exc.errorId() << endl;
  99.     cout << "Error Text: " << exc.text() << endl;
  100.     cout << "Error Class: " << exc.name() << endl;
  101.   } catch(...) {
  102.     cout << "Unknown exception in disconnect" << endl;
  103.   }
  104. }
  105.  
  106. //******************************************************************************
  107. // Function Name: generatePriceList
  108. // Note : This member function generates a price list for the store.
  109. //        The data printed in the list is a subset of the inventory
  110. //        database.
  111. //******************************************************************************
  112. void Store::generatePriceList() {
  113.   try {
  114.     // call refresh to get a new copy of the price list.
  115.     priceListMgr.refresh();
  116.  
  117.     // set up the cursor for going through each item.
  118.     PrclistManager::sequenceType::Cursor cursor(*priceListMgr.items());
  119.  
  120.     // loop until the end of the list.
  121.     cout << "Prod. ID   Description            Price " << endl;
  122.     cout << "========   =====================  ===== " << endl;
  123.     forCursor (cursor) {
  124.       cout << cursor.element()->pnumber() << ' ';
  125.       cout << cursor.element()->pdes() << "   $";
  126.  
  127.       cout << setiosflags(ios::fixed) << setprecision(2) << cursor.element()->lstprice() << endl;
  128.     }
  129.   } catch (IDSAccessError &exc) {
  130.     cout << "Failed: generatePriceList " << endl;
  131.     cout << "Error Text:" << exc.text() << endl;
  132.     cout << "Error id:"  << exc.errorId() << endl;
  133.     cout << "Error Class: " << exc.name() << endl;
  134.     if (exc.errorId() == DAX_REF_SQLERR) {
  135.       cout << "SQLCODE: " << exc.getSqlca().sqlcode << endl;
  136.     }
  137.   } catch(...) {
  138.     cout << "Unknown exception in generatePriceList" << endl;
  139.   }
  140. }
  141.  
  142. //******************************************************************************
  143. // Function Name: postingInventory                                             *
  144. // Note: This member function updates the inventory table with the received    *
  145. //       received order table.                                                 *
  146. //******************************************************************************
  147. void Store::postingInventory() {
  148.   double newAveCost;                      // new average cost after update
  149.   double newListingPrice;                 // new listing price after update
  150.   long   newQuantity;                     // new quantity after update
  151.   char   selectConditionArg[] = "pFlag = 0";  // selection condition
  152.  
  153.   try {
  154.     // get a copy of all the rows that meet the select condition.
  155.     receivedOrderMgr.select(selectConditionArg);
  156.  
  157.     // set up the cursor to go through each item.
  158.     ReceiveManager::sequenceType::Cursor cursor(*receivedOrderMgr.items());
  159.  
  160.     // loop until the end of the list.
  161.     forCursor (cursor) {
  162.  
  163.  
  164.       // Use the product number as the key to locate the row of the item
  165.       // in the inventory table.
  166.       inventoryObj.setPnumber(cursor.element()->pnum());
  167.       inventoryObj.retrieve();
  168.  
  169.       // calculate the new quantity and the new average cost
  170.       newQuantity = inventoryObj.onhquan() +
  171.                     cursor.element()->rquan();
  172.       newAveCost  = (inventoryObj.avgcost() * inventoryObj.onhquan() +
  173.                     cursor.element()->tcost()) / newQuantity;
  174.       newListingPrice = newAveCost * 1.50;     // 50% mark up of the new cost
  175.  
  176.       // update the row with the new data
  177.       inventoryObj.setOnhquan(newQuantity);
  178.       inventoryObj.setAvgcost(newAveCost);
  179.       inventoryObj.setLstprice(newListingPrice);
  180.       inventoryObj.update();
  181.  
  182.       // Commit changes to the Inventory connection if CLI or ODBC
  183.       if (ISESQL != "TRUE") {
  184.          dsm1.commit();
  185.       }
  186.  
  187.       // reset the posted flag so that it will not be used again.
  188.       cursor.element()->setPflag(POSTED);
  189.       cursor.element()->update();
  190.     }
  191.   } catch (IDSAccessError &exc) {
  192.     cout << "Failed: postingInventory " << endl;
  193.     cout << "Error id:"  << exc.errorId() << endl;
  194.     cout << "Error Text:" << exc.text() << endl;
  195.     cout << "Error Class: " << exc.name() << endl;
  196.     if ((exc.errorId() == DAX_SEL_SQLERR) ||
  197.         (exc.errorId() == DAX_RET_SQLERR) ||
  198.         (exc.errorId() == DAX_UPD_SQLERR)) {
  199.       cout << "SQLCODE: " << exc.getSqlca().sqlcode << endl;
  200.     }
  201.   } catch (IException &exc) {
  202.     cout << "Failed: postingInventory" << endl;
  203.     cout << "Error id: " << exc.errorId() << endl;
  204.     cout << "Error Text: " << exc.text() << endl;
  205.     cout << "Error Class: " << exc.name() << endl;
  206.   } catch(...) {
  207.     cout << "Unknown exception in generatePriceList" << endl;
  208.   }
  209. }
  210.  
  211. //******************************************************************************
  212. // Function: addReceivedOrder                                                  *
  213. // Note: Whenever an order is received, a new entry is added to the received   *
  214. //       order table                                                           *
  215. //******************************************************************************
  216. void Store::addReceivedOrder(
  217.        short   receivedNumberArg,     //received order number
  218.        IString prodNumberArg,         // product number
  219.        long    receivedQuantityArg,   // received order quantity
  220.        double  totalCostArg ) {          // total cost of this order
  221.   try {
  222.     receivedOrderObj.setPflag           (NOT_POSTED);
  223.     receivedOrderObj.setPnum            (prodNumberArg);
  224.     receivedOrderObj.setRnumber         (receivedNumberArg);
  225.     receivedOrderObj.setRquan           (receivedQuantityArg);
  226.     receivedOrderObj.setTcost           (totalCostArg);
  227.     receivedOrderObj.add();
  228.   } catch (IDSAccessError &exc) {
  229.     cout << "Failed: addReceivedOrder " << endl;
  230.     cout << "Error id:"  << exc.errorId() << endl;
  231.     cout << "Error Text:" << exc.text() << endl;
  232.     cout << "Error Class: " << exc.name() << endl;
  233.     if (exc.errorId() == DAX_ADD_SQLERR) {
  234.       cout << "SQLCODE: " << exc.getSqlca().sqlcode << endl;
  235.     }
  236.   } catch(...) {
  237.     cout << "Unknown exception in addReceiveOrder" << endl;
  238.   }
  239. }
  240.  
  241. //******************************************************************************
  242. // Function: transaction
  243. // Note: This function commits or rollbacks the activities between transactions
  244. //******************************************************************************
  245. void Store::transaction(commitflag flagArg) {  // commit or rollback
  246.   try {
  247.     if (flagArg == COMMIT) {
  248.        if (ISESQL == "TRUE") {
  249.           dsm3.commit();                           // Commit for Embedded SQL
  250.        }
  251.        else {
  252.          dsm.commit();                             // Commit for ODBC or CLI
  253.          dsm1.commit();
  254.          dsm2.commit();
  255.        }
  256.     } else {
  257.          if (ISESQL == "TRUE") {
  258.             dsm3.rollback();                       // Rollback for Embedded SQL
  259.          }
  260.          else {
  261.             dsm.rollback();                        // Rollback for CLI or ODBC
  262.             dsm1.rollback();
  263.             dsm2.rollback();
  264.          }
  265.     }
  266.   } catch (IException &exc) {
  267.     cout << "Failed: transaction " << endl;
  268.     cout << "Error id:"  << exc.errorId() << endl;
  269.     cout << "Error Text:" << exc.text() << endl;
  270.     cout << "Error Class: " << exc.name() << endl;
  271.   } catch(...) {
  272.     cout << "Unknown exception in transaction" << endl;
  273.   }
  274. }
  275.  
  276. //******************************************************************************
  277. // Function: addNewItem
  278. // Note: This function add a new row to the inventory table
  279. //******************************************************************************
  280. void Store::addNewItem(
  281.        IString prodNumberArg,       // product number
  282.        IString prodDescriptionArg,  // product description
  283.        long    onHandQuantityArg,   // on hand quantity
  284.        double  averageCostArg,      // average cost per unit
  285.        double  listingPriceArg) {   // listing price per unit
  286.   try {
  287.     inventoryObj.setPnumber         (prodNumberArg);
  288.     inventoryObj.setPdes            (prodDescriptionArg);
  289.     inventoryObj.setOnhquan         (onHandQuantityArg);
  290.     inventoryObj.setAvgcost         (averageCostArg);
  291.     inventoryObj.setLstprice        (listingPriceArg);
  292.     inventoryObj.add();
  293.   } catch (IDSAccessError &exc) {
  294.     cout << "Failed: addNewItem " << endl;
  295.     cout << "Error id:"  << exc.errorId() << endl;
  296.     cout << "Error Text:" << exc.text() << endl;
  297.     cout << "Error Class: " << exc.name() << endl;
  298.   } catch(...) {
  299.     cout << "Unknown exception in addNewItem" << endl;
  300.   }
  301. }
  302.  
  303. //******************************************************************************
  304. // Function: displayItem
  305. // Note: This function displays a new row in the inventory table
  306. //******************************************************************************
  307. void Store::displayItem() {
  308.       cout << inventoryObj.pnumber() << ' ';
  309.       cout << inventoryObj.pdes() << ' ';
  310.       cout << inventoryObj.lstprice() << endl;
  311. }
  312.  
  313.  
  314.