Retrieving the column datatype and processing values

In the body of printResultSet(), the _d() method (the discriminator method) is used to retrieve the datatype of the column and switch/case processing is used to process the column value in the current row. values is a reference to a Data object that represents the column value. _d() returns the datatype of the referenced value to the switch statement and the body of the case statement that matches the datatype is executed. In each case, the current row’s column value that corresponds to the case’s datatype is printed.

For the Date, Time, Timestamp datatypes, some conversion is required to print a value in a standard format (such as “January 5, 1998”).

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