public T getObject()

in src/main/java/com/amazon/redshift/jdbc/RedshiftResultSet.java [3704:3972]


  public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
    if (type == null) {
      throw new SQLException("type is null");
    }
    int sqlType = getSQLType(columnIndex);
    if (type == BigDecimal.class) {
      if (sqlType == Types.NUMERIC || sqlType == Types.DECIMAL) {
        return type.cast(getBigDecimal(columnIndex));
      } else {
        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
                RedshiftState.INVALID_PARAMETER_VALUE);
      }
    } else if (type == String.class) {
      if (sqlType == Types.CHAR || sqlType == Types.VARCHAR) {
        return type.cast(getString(columnIndex));
      } else {
        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
                RedshiftState.INVALID_PARAMETER_VALUE);
      }
    } else if (type == Boolean.class) {
      if (sqlType == Types.BOOLEAN || sqlType == Types.BIT) {
        boolean booleanValue = getBoolean(columnIndex);
        if (wasNull()) {
          return null;
        }
        return type.cast(booleanValue);
      }
      if (sqlType == Types.DATE 
      		|| sqlType == Types.TIME
      		|| sqlType == Types.TIMESTAMP
      		|| sqlType == Types.TIMESTAMP_WITH_TIMEZONE
      		|| sqlType == Types.BINARY) {
        if (wasNull()) {
          return null;
        }
      	
	      throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
	      RedshiftState.INVALID_PARAMETER_VALUE);
      }
      else {
      	String booleanStrValue = getString(columnIndex);
        if (wasNull()) {
          return null;
        }
      	return type.cast(BooleanTypeUtil.castToBoolean(booleanStrValue));      	
//        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
//                RedshiftState.INVALID_PARAMETER_VALUE);
      }
    } else if (type == Short.class) {
      if (sqlType == Types.SMALLINT) {
        short shortValue = getShort(columnIndex);
        if (wasNull()) {
          return null;
        }
        return type.cast(shortValue);
      } else {
        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
                RedshiftState.INVALID_PARAMETER_VALUE);
      }
    } else if (type == Integer.class) {
      if (sqlType == Types.INTEGER || sqlType == Types.SMALLINT) {
        int intValue = getInt(columnIndex);
        if (wasNull()) {
          return null;
        }
        return type.cast(intValue);
      } else {
        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
                RedshiftState.INVALID_PARAMETER_VALUE);
      }
    } else if (type == Long.class) {
      if (sqlType == Types.BIGINT) {
        long longValue = getLong(columnIndex);
        if (wasNull()) {
          return null;
        }
        return type.cast(longValue);
      } else {
        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
                RedshiftState.INVALID_PARAMETER_VALUE);
      }
    } else if (type == BigInteger.class) {
      if (sqlType == Types.BIGINT) {
        long longValue = getLong(columnIndex);
        if (wasNull()) {
          return null;
        }
        return type.cast(BigInteger.valueOf(longValue));
      } else {
        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
                RedshiftState.INVALID_PARAMETER_VALUE);
      }
    } else if (type == Float.class) {
      if (sqlType == Types.REAL) {
        float floatValue = getFloat(columnIndex);
        if (wasNull()) {
          return null;
        }
        return type.cast(floatValue);
      } else {
        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
                RedshiftState.INVALID_PARAMETER_VALUE);
      }
    } else if (type == Double.class) {
      if (sqlType == Types.FLOAT || sqlType == Types.DOUBLE) {
        double doubleValue = getDouble(columnIndex);
        if (wasNull()) {
          return null;
        }
        return type.cast(doubleValue);
      } else {
        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
                RedshiftState.INVALID_PARAMETER_VALUE);
      }
    } else if (type == Date.class) {
      if (sqlType == Types.DATE) {
        return type.cast(getDate(columnIndex));
      } else {
        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
                RedshiftState.INVALID_PARAMETER_VALUE);
      }
    } else if (type == Time.class) {
      if (sqlType == Types.TIME) {
        return type.cast(getTime(columnIndex));
      } else {
        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
                RedshiftState.INVALID_PARAMETER_VALUE);
      }
    } else if (type == Timestamp.class) {
      if (sqlType == Types.TIMESTAMP
              //JCP! if mvn.project.property.redshift.jdbc.spec >= "JDBC4.2"
              || sqlType == Types.TIMESTAMP_WITH_TIMEZONE
      //JCP! endif
      ) {
        return type.cast(getTimestamp(columnIndex));
      } else {
        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
                RedshiftState.INVALID_PARAMETER_VALUE);
      }
    } else if (type == Calendar.class) {
      if (sqlType == Types.TIMESTAMP
              //JCP! if mvn.project.property.redshift.jdbc.spec >= "JDBC4.2"
              || sqlType == Types.TIMESTAMP_WITH_TIMEZONE
      //JCP! endif
      ) {
        Timestamp timestampValue = getTimestamp(columnIndex);
        if (wasNull()) {
          return null;
        }
        Calendar calendar = Calendar.getInstance(getDefaultCalendar().getTimeZone());
        calendar.setTimeInMillis(timestampValue.getTime());
        return type.cast(calendar);
      } else {
        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
                RedshiftState.INVALID_PARAMETER_VALUE);
      }
    } else if (type == Blob.class) {
      if (sqlType == Types.BLOB || sqlType == Types.BINARY || sqlType == Types.BIGINT) {
        return type.cast(getBlob(columnIndex));
      } else {
        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
                RedshiftState.INVALID_PARAMETER_VALUE);
      }
    } else if (type == Clob.class) {
      if (sqlType == Types.CLOB || sqlType == Types.BIGINT) {
        return type.cast(getClob(columnIndex));
      } else {
        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
                RedshiftState.INVALID_PARAMETER_VALUE);
      }
    } else if (type == java.util.Date.class) {
      if (sqlType == Types.TIMESTAMP) {
        Timestamp timestamp = getTimestamp(columnIndex);
        if (wasNull()) {
          return null;
        }
        return type.cast(new java.util.Date(timestamp.getTime()));
      } else {
        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
                RedshiftState.INVALID_PARAMETER_VALUE);
      }
    } else if (type == Array.class) {
      if (sqlType == Types.ARRAY) {
        return type.cast(getArray(columnIndex));
      } else {
        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
                RedshiftState.INVALID_PARAMETER_VALUE);
      }
    } else if (type == SQLXML.class) {
      if (sqlType == Types.SQLXML) {
        return type.cast(getSQLXML(columnIndex));
      } else {
        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
                RedshiftState.INVALID_PARAMETER_VALUE);
      }
    } else if (type == UUID.class) {
      return type.cast(getObject(columnIndex));
    } else if (type == InetAddress.class) {
      String inetText = getString(columnIndex);
      if (inetText == null) {
        return null;
      }
      int slash = inetText.indexOf("/");
      try {
        return type.cast(InetAddress.getByName(slash < 0 ? inetText : inetText.substring(0, slash)));
      } catch (UnknownHostException ex) {
        throw new RedshiftException(GT.tr("Invalid Inet data."), RedshiftState.INVALID_PARAMETER_VALUE, ex);
      }
      // JSR-310 support
      //JCP! if mvn.project.property.redshift.jdbc.spec >= "JDBC4.2"
    } else if (type == LocalDate.class) {
      if (sqlType == Types.DATE) {
        Date dateValue = getDate(columnIndex);
        if (wasNull()) {
          return null;
        }
        long time = dateValue.getTime();
        if (time == RedshiftStatement.DATE_POSITIVE_INFINITY) {
          return type.cast(LocalDate.MAX);
        }
        if (time == RedshiftStatement.DATE_NEGATIVE_INFINITY) {
          return type.cast(LocalDate.MIN);
        }
        return type.cast(dateValue.toLocalDate());
      } else if (sqlType == Types.TIMESTAMP) {
        LocalDateTime localDateTimeValue = getLocalDateTime(columnIndex);
        if (wasNull()) {
          return null;
        }
        return type.cast(localDateTimeValue.toLocalDate());
      } else {
        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
                RedshiftState.INVALID_PARAMETER_VALUE);
      }
    } else if (type == LocalTime.class) {
      if (sqlType == Types.TIME) {
        return type.cast(getLocalTime(columnIndex));
      } else {
        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
                RedshiftState.INVALID_PARAMETER_VALUE);
      }
    } else if (type == LocalDateTime.class) {
      if (sqlType == Types.TIMESTAMP) {
        return type.cast(getLocalDateTime(columnIndex));
      } else {
        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
                RedshiftState.INVALID_PARAMETER_VALUE);
      }
    } else if (type == OffsetDateTime.class) {
      if (sqlType == Types.TIMESTAMP_WITH_TIMEZONE || sqlType == Types.TIMESTAMP) {
        OffsetDateTime offsetDateTime = getOffsetDateTime(columnIndex);
        return type.cast(offsetDateTime);
      } else {
        throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
                RedshiftState.INVALID_PARAMETER_VALUE);
      }
      //JCP! endif
    } else if (RedshiftObject.class.isAssignableFrom(type)) {
      Object object;
      if (isBinary(columnIndex)) {
        object = connection.getObject(getRSType(columnIndex), null, thisRow.get(columnIndex - 1));
      } else {
        object = connection.getObject(getRSType(columnIndex), getString(columnIndex), null);
      }
      return type.cast(object);
    }
    throw new RedshiftException(GT.tr("conversion to {0} from {1} not supported", type, getRSType(columnIndex)),
            RedshiftState.INVALID_PARAMETER_VALUE);
  }