Example of processing result sets

This example retrieves a single result set. The following code shows the C++ client in its entirety. For detailed explanations, see the sections that explain each result-set processing step.

All of the required header files are included. The IDL module namespaces are specified with the C++ using statement. The printResultSet() method contains the logic for processing a result set. main() contains the logic to initialize and connect to the EAServer ORB, instantiate the stub, call the component method to retrieve the result set object, and call printResultSet() to process the result set.

After the result set has been processed, execution of printResultSet() ends and control is returned to main(). In main(), the screen is kept open with the fprintf statement. Once you press Return, execution ends.

#include <stdio.h>
#include <time.h>
#include <iostream.h>
#include <SessionManager.hpp>
#include <TabularResults.hpp>
#include <Test.hpp> 
using namespace CORBA;
using namespace SessionManager;
using namespace TabularResults;
using namespace Test; 
void printResultSet(const ResultSet& rs)
{ 
  ULong nc = rs.columns.length(); 
  cout << rs.rows << " rows, " << nc << " columns" << endl; 
  for (ULong row = 0; row < rs.rows; row++) 
  { 
    cout << "row " << row << ": "; 
    for (ULong column = 0; column < nc; column++) 
    { 
      if (column > 0) 
      { 
        cout << ", "; 
      } 
      BooleanSeq& nulls = ((ColumnSeq&)rs.columns)[column].nulls; 
      if (row + 1 <= nulls.length() && nulls[row]) 
      { 
        cout << "null"; 
        continue; 
      } 
      Data& values = ((ColumnSeq&)rs.columns)[column].values; 
      switch (values._d()) 
      { 
         case TYPE_BIT: 
         { 
            BooleanSeq& booleanValues = values.booleanValues(); 
            cout << (booleanValues[row] ? "true" : "false"); 
            break; 
         } 
         case TYPE_TINYINT: 
         { 
            OctetSeq octetValues = values.octetValues(); 
            cout << octetValues[row]; 
            break; 
         } 
         case TYPE_SMALLINT: 
         { 
            ShortSeq& shortValues = values.shortValues(); 
            cout << shortValues[row]; 
            break; 
         } 
         case TYPE_INTEGER: 
             { 
            LongSeq& longValues = values.longValues(); 
            cout << longValues[row]; 
            break; 
         } 
         case TYPE_REAL: 
         { 
            FloatSeq& floatValues = values.floatValues(); 
            cout << floatValues[row]; 
            break; 
         } 
         case TYPE_DOUBLE: 
         case TYPE_FLOAT: 
         { 
            DoubleSeq& doubleValues = values.doubleValues(); 
            cout << doubleValues[row]; 
            break; 
         } 
         case TYPE_CHAR: 
         case TYPE_LONGVARCHAR: 
         case TYPE_VARCHAR: 
         { 
            StringSeq& stringValues = values.stringValues(); 
            cout << stringValues[row]; 
            break; 
         } 
         case TYPE_BINARY: 
         case TYPE_LONGVARBINARY: 
         case TYPE_VARBINARY: 
         { 
            BinarySeq& binaryValues = values.binaryValues(); 
            cout << "(binary)"; 
            break; 
         } 
         case TYPE_BIGINT: 
         case TYPE_DECIMAL: 
         case TYPE_NUMERIC: 
         { 
            DecimalSeq& decimalValues = values.decimalValues(); 
            cout << "(decimal)"; 
            break; 
         } 
         case TYPE_DATE: 
         { 
            DateSeq& dateValues = values.dateValues(); 
            // Assumption: time_t is seconds from Jan 1, 1970 
            time_t t = (time_t)((dateValues[row].dateValue - 40222.0) *
               86400); 
            cout << ctime(&t); 
            break; 
         } 
         case TYPE_TIME: 
         { 
            TimeSeq& timeValues = values.timeValues(); 
            cout << "time: " << timeValues[row].timeValue; 
            break; 
         } 
         case TYPE_TIMESTAMP: 
         { 
            TimestampSeq& timestampValues = values.timestampValues(); 
            time_t t = (time_t)((timestampValues[row].dateValue + 
            timestampValues[row].timeValue - 40222.0) * 86400); 
            cout << ctime(&t); 
            break; 
         } 
       } 
     } 
     cout << endl; 
   }
} 
int main(int argc, char** argv) 
{ 
   ORB_var orb = ORB_init(argc, argv, ""); 
   Manager_var manager = Manager::
       _narrow(Object_var(orb->string_to_object("iiop://myhost:9000"))); 
   Session_var session = manager->createSession("jagadmin", ""); 
   Ping_var p = Ping::_narrow(Object_var(session->create("Test/Java"))); 
   ResultSet_var rs = p->results(); 
   printResultSet(rs.in()); 
   { 
      char c; 
      fprintf(stderr, "Press Return to continue..."); 
      c = getchar(); 
   } 
   return 0;
}