home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv7.zip / VACPP / IBMCPP / smarts / DAX / DAXCPP / DAXBASIC.CPP
Text File  |  1995-06-02  |  7KB  |  176 lines

  1.  
  2. %PROLOG%
  3.  
  4. /***************************************************************************/
  5. /*                                                                         */
  6. /* %FILE_NAME% - Skeleton of a basic DAX application.  It can serve as a        */
  7. /*           starting point for a program that uses Data Access            */
  8. /*           services.                                                     */
  9. /*                                                                         */
  10. /***************************************************************************/
  11.  
  12. #include <iostream.h>
  13.  
  14. #include "idsmcon.hpp"
  15. #include "%DAX_FILE%v.hpp"
  16.  
  17. void main()
  18. {
  19.   // Create an instance of a datastore object
  20.   IDatastore DSObject;
  21.  
  22.   // Create a DAX object and DAX manager object
  23.   %DAX_CLASS% DAXObject;
  24.   %DAX_CLASS%Manager DAXMgrObject;
  25.  
  26.   // You need a cursor object to go through each DAX object in the sequence.
  27.   IVSequence<%DAX_CLASS% *>::Cursor cursor(*(DAXMgrObject.items()));
  28.   
  29.   // Establish connection to a datastore
  30.   try {
  31.     DSObject.connect("%DATASTORE_NAME%", "USERID", "PASSWORD");
  32.   } catch (IException &exc) {
  33.     cout << "Failed: Exception occurs" << endl;
  34.     cout << "Error id: " << exc.errorId() << endl;
  35.     for (unsigned long i = 0; i < exc.textCount(); i++)
  36.       cout << "Error Text: " << exc.text(i) << endl;
  37.     cout << "Error Class: " << exc.name() << endl;
  38.   }
  39.  
  40.   // Add a row
  41.   try {
  42.     // Take away the comment and set the values of all the fields for the add.
  43.     // Replace <Attribute> with the attribute name you use in the DAX Class.
  44.     // Please make sure that the value for the key field is unique.
  45.     // DAXObject.set<Attribute>(value);
  46.     DAXObject.add();
  47.   } catch (IException &exc) {
  48.     cout << "Error id: " << exc.errorId() << endl;
  49.     for (unsigned long i = 0; i < exc.textCount(); i++)
  50.       cout << "Error Text: " << exc.text(i) << endl;
  51.     cout << "Error Class: " << exc.name() << endl;
  52.   }
  53.  
  54.   // Update a row
  55.   try{ 
  56.     // Take away the comment and set the valued for all the fields for the update.
  57.     // Replace <Attribute> with the attribute name you use in the DAX Class.
  58.     // You should do a retrieve to refresh the values kept in the object with the
  59.     // row you want to update. 
  60.     // WARNING: Do not change the values of the key fields. If you do, you will
  61.     // update a wrong row.
  62.     // DAXObject.set<Attribute>(value);
  63.     DAXObject.update();
  64.   } catch (IException &exc) {
  65.     cout << "Error id: " << exc.errorId() << endl;
  66.     for (unsigned long i = 0; i < exc.textCount(); i++)
  67.       cout << "Error Text: " << exc.text(i) << endl;
  68.     cout << "Error Class: " << exc.name() << endl;
  69.   }                                                              
  70.  
  71.   // Retrieve a row
  72.   try {
  73.     // Take away the comment and set the value of the key field for the retrieve
  74.     // Replace <Attribute> with the attribute name you use in the DAX Class.
  75.     // If you have more than one key attributes, you need to set them all.
  76.     // DAXObject.set<Attribute>(value);
  77.     DAXObject.retrieve();
  78.     // Get the attribute value by calling the function. 
  79.     // Replace <Attribute> with the attribute name you use in the DAX Class.
  80.     // temp = DAXObject.<Attribute>();
  81.   } catch (IException &exc) {
  82.     cout << "Error id: " << exc.errorId() << endl;
  83.     for (unsigned long i = 0; i < exc.textCount(); i++)
  84.       cout << "Error Text: " << exc.text(i) << endl;
  85.     cout << "Error Class: " << exc.name() << endl;
  86.   }
  87.  
  88.   // Delete a row
  89.   try {
  90.     // Take away the comment and set the value for the key field for the delete.
  91.     // Replace <Attribute> with the attribute name you use in the DAX Class.
  92.     // If you have more than one key attribute, you need to set them all.
  93.     // DAXObject.set<Attribute>(value);
  94.     DAXObject.del();
  95.   } catch (IException &exc) {
  96.     cout << "Error id: " << exc.errorId() << endl;
  97.     for (unsigned long i = 0; i < exc.textCount(); i++)
  98.       cout << "Error Text: " << exc.text(i) << endl;
  99.     cout << "Error Class: " << exc.name() << endl;
  100.   }
  101.  
  102.   // Retrieve all the rows from a table
  103.   try {
  104.     DAXMgrObject.refresh();
  105.  
  106.     // Process all the rows in a for loop
  107.     for (cursor.setToFirst();cursor.isValid();cursor.setToNext()) {
  108.        //Get the item from the sequence
  109.        DAXObject = *(DAXMgrObject.items()->elementAt(cursor));
  110.  
  111.        // Process the DAXObject ...
  112.  
  113.     } /* endfor */
  114.   } catch (IException &exc) {
  115.     cout << "Error id: " << exc.errorId() << endl;
  116.     for (unsigned long i = 0; i < exc.textCount(); i++)
  117.       cout << "Error Text: " << exc.text(i) << endl;
  118.     cout << "Error Class: " << exc.name() << endl;
  119.   }                                                              
  120.  
  121.   // select some rows from a table
  122.   try {
  123.     char selectArg[100];
  124.     // Put the SQL where clause in the selectArg before calling select.
  125.  
  126.     DAXMgrObject.select(selectArg);
  127.  
  128.     // Process all the selected rows in a for loop
  129.     for (cursor.setToFirst();cursor.isValid();cursor.setToNext()) {
  130.       //Get the item from the sequence
  131.       DAXObject = *(DAXMgrObject.items()->elementAt(cursor));
  132.  
  133.      // Process the DAXObject ...
  134.  
  135.     } /* endfor */
  136.   } catch (IException &exc) {
  137.     cout << "Error id: " << exc.errorId() << endl;
  138.     for (unsigned long i = 0; i < exc.textCount(); i++)
  139.       cout << "Error Text: " << exc.text(i) << endl;
  140.     cout << "Error Class: " << exc.name() << endl;
  141.   }                                                              
  142.  
  143.   // Commit the work by calling transact with OCLI_COMMIT
  144.   try{
  145.     DSObject.commit();
  146.   } catch (IException &exc) {
  147.     cout << "Error id: " << exc.errorId() << endl;
  148.     for (unsigned long i = 0; i < exc.textCount(); i++)
  149.       cout << "Error Text: " << exc.text(i) << endl;
  150.     cout << "Error Class: " << exc.name() << endl;
  151.   }                                                              
  152.   
  153.   // Rollback the work by calling transact with OCLI_ROLLBACK
  154.   try{
  155.     DSObject.rollback();
  156.   } catch (IException &exc) {
  157.     cout << "Error id: " << exc.errorId() << endl;
  158.     for (unsigned long i = 0; i < exc.textCount(); i++)
  159.       cout << "Error Text: " << exc.text(i) << endl;
  160.     cout << "Error Class: " << exc.name() << endl;
  161.   }                                                              
  162.  
  163.   // disconnect from a datastore
  164.   try {
  165.     DSObject.disconnect();
  166.   } catch (IException &exc) {
  167.     cout << "Error id: " << exc.errorId() << endl;
  168.     for (unsigned long i = 0; i < exc.textCount(); i++)
  169.       cout << "Error Text: " << exc.text(i) << endl;
  170.     cout << "Error Class: " << exc.name() << endl;
  171.   }                                                              
  172.  
  173. } /* endmain */
  174.  
  175.  
  176.