home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / smarts / dax / daxbasic.cpp next >
Encoding:
Text File  |  1996-02-21  |  5.3 KB  |  190 lines

  1. --------------------------------------------------------------------------------
  2. --
  3. -- COPYRIGHT:
  4. --   IBM WorkFrame - Project Smarts
  5. --   (C) Copyright International Business Machines Corporation 1996
  6. --   Licensed Material - Program-Property of IBM - All Rights Reserved.
  7. --   US Government Users Restricted Rights - Use, duplication, or disclosure
  8. --   restricted by GSA ADP Schedule Contract with IBM Corp.
  9. --
  10. --------------------------------------------------------------------------------
  11. <include prologcp.tde>
  12.  
  13. <if ($EXECUTABLE$==EXEandDLL)>
  14. #pragma library( "$SOURCE_FILE:tolower$.lib" )
  15. </if>
  16.  
  17. <if ($LANGUAGE$ == SOM)>
  18.  
  19. // $FILE_NAME:tolower$.CPP - main() for Data Access Builder application
  20.  
  21. #include <iostream.h>
  22. #include "$SOURCE_FILE:tolower$.xh"
  23.  
  24. #define USERIDANDPASSWORD 3
  25. #define USERID            1
  26. #define PASSWORD          2
  27.  
  28. static int exceptionCheck(void);
  29.  
  30. Environment          *ev                = somGetGlobalEnvironment();
  31. $CLASS_NAME$Factory           *DAXMgrObject      = $CLASS_NAME$NewClass(0,0);
  32.  
  33. // This program connects to a database, prints all its records,
  34. // and then disconnects.  It will work for any database.
  35.  
  36. void main(int argc, char *argv[])
  37. {
  38.   cout << endl;
  39.  
  40.   $CLASS_NAME$Datastore *DSObject  = DAXMgrObject->create_datastore(ev);
  41.   $CLASS_NAME$          *DAXObject = DAXMgrObject->create_object(ev);
  42.  
  43.   _IDL_SEQUENCE_PersistentObject DAXObjectList;
  44.  
  45.   int rc;
  46.  
  47.   // Establish connection to a datastore
  48.   // If there are any arguments, assume that the connection is remote.
  49.   // The arguments, if any, should be the userid and password.
  50.   if (argc == USERIDANDPASSWORD)
  51.      DSObject->connect_user(ev, argv[USERID], argv[PASSWORD]);
  52.   else
  53.      DSObject->connect_defaults(ev);
  54.  
  55.   rc = exceptionCheck();
  56.  
  57.   // Check if connection was successful.  If not, just exit.
  58.   if (rc != 0) {
  59.      cout << " Connection failed" << endl;
  60.      exit(1);
  61.   }
  62.  
  63.   // Retrieve all the rows from a table and store in the object list.
  64.   DAXObjectList = DAXMgrObject->retrieveAll(ev);
  65.   rc = exceptionCheck();
  66.   if (rc == 0)
  67.   {
  68.     // Print all the rows
  69.     for (int j=0; j < sequenceLength(DAXObjectList); j++)
  70.     {
  71.       DAXObject = ($CLASS_NAME$ *) sequenceElement(DAXObjectList,j);                       // Get the item from the sequence
  72.  
  73.       // The asString method prints all the table columns as strings.
  74.       // Here, we just print the current row.
  75.       cout <<  "\"" << DAXObject->asString(ev,"\", \"") << "\"" << endl;             // Output the item from the sequence
  76.     }
  77.   } /* endif */
  78.   else
  79.      cout << " Retrieval of all records failed" << endl;
  80.  
  81.   // disconnect from a datastore
  82.   DSObject->commit(ev);
  83.   DSObject->disconnect(ev);
  84.  
  85. }
  86.  
  87.  
  88. int exceptionCheck(void )
  89. {
  90.    int rc = 0;
  91.    char *exId;
  92.    DA_DatastoreAccessError * er;
  93.  
  94.    switch (ev->_major)
  95.    {
  96.      case SYSTEM_EXCEPTION:
  97.         cout << "system exception" << endl;
  98.         rc ++;
  99.         break;
  100.      case USER_EXCEPTION:
  101.         exId = somExceptionId(ev);
  102.         cout << "Exception ID: " << somExceptionId(ev) << endl;
  103.         rc ++;
  104.         somExceptionFree(ev);
  105.         break;
  106.      case NO_EXCEPTION:
  107.         break;
  108.    }
  109.    return rc;
  110. }
  111.  
  112. <else>
  113. <if ($LANGUAGE$ == C++)>
  114.  
  115. // $FILE_NAME$ - main() for C++ Data Access Builder application
  116.  
  117. #include "$SOURCE_FILE$.hpp"
  118. #include <iostream.h>
  119. #include <istring.hpp>
  120.  
  121.  
  122. #define USERIDANDPASSWORD     3
  123. #define USERID                1
  124. #define PASSWORD              2
  125.  
  126.  
  127. // This program connects to a database, prints all its records,
  128. // and then disconnects.  It will work for any database.
  129.  
  130. main (int argc, char * argv[])
  131. {
  132.   try
  133.      {
  134.           $CLASS_NAME$Datastore aDatastore;
  135.  
  136.           cout << "Connecting..." << endl;
  137.  
  138.           // Establish connection to a datastore.
  139.           // If there are any arguments, assume that the connection is remote.
  140.           // The arguments, if any, should be the userid and password.
  141.           if (argc==USERIDANDPASSWORD)
  142.              aDatastore.connect(argv[USERID], argv[PASSWORD]);
  143.           else
  144.             aDatastore.connect();
  145.  
  146.           cout << "Connected." << endl;
  147.  
  148.           // Create the manager object
  149.           $CLASS_NAME$Manager aManager;
  150.  
  151.           // Retrieve all the records from the database and
  152.           // store the data in the aManager object.
  153.           cout << "Retrieving database records..." << endl;
  154.           aManager.refresh();
  155.           cout << "Refresh complete." << endl << endl;
  156.  
  157.           // Create a cursor to the database items (rows).
  158.           $CLASS_NAME$Manager::sequenceType::Cursor cursor(*aManager.items());
  159.  
  160.           // The forDisplay method concatenates the table columns
  161.           // displayed as strings.  We use a cursor to iterate through
  162.           // all the rows in the table to print them out.
  163.           forCursor(cursor)
  164.           {
  165.             cout <<  cursor.element()->forDisplay(",") << endl;
  166.           }
  167.  
  168.           cout << endl <<  "Disconnecting..." << endl;
  169.           aDatastore.commit();
  170.           aDatastore.disconnect();
  171.           cout << "Disconnected." << endl;
  172.  
  173.      }
  174.  
  175.   catch ( IException exc )
  176.      {
  177.        cout << "IException thrown!" << endl;
  178.        exit(1);
  179.      }
  180.   catch (...)
  181.      {
  182.        cout << "Unknown exception thrown!" << endl;
  183.        exit(1);
  184.      }
  185.  
  186. }
  187.  
  188. </if>
  189. </if>
  190.