Ejemplo n.º 1
0
  private static ArrayRecord<?> getArrayRecord(
      Configuration configuration, Array array, Class<? extends ArrayRecord<?>> type)
      throws SQLException {

    if (array == null) {
      return null;
    } else {
      // TODO: [#523] Use array record meta data instead
      ArrayRecord<?> record = Util.newArrayRecord(type, configuration);
      record.set(array);
      return record;
    }
  }
Ejemplo n.º 2
0
  /**
   * Create a UDT record from a PGobject
   *
   * <p>Unfortunately, this feature is very poorly documented and true UDT support by the PostGreSQL
   * JDBC driver has been postponed for a long time.
   *
   * @param object An object of type PGobject. The actual argument type cannot be expressed in the
   *     method signature, as no explicit dependency to postgres logic is desired
   * @return The converted {@link UDTRecord}
   */
  private static UDTRecord<?> pgNewUDTRecord(Class<?> type, Object object) throws SQLException {
    if (object == null) {
      return null;
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    UDTRecord<?> record = (UDTRecord<?>) Util.newRecord((Class) type);
    List<String> values = PostgresUtils.toPGObject(object.toString());

    List<Field<?>> fields = record.getFields();
    for (int i = 0; i < fields.size(); i++) {
      pgSetValue(record, fields.get(i), values.get(i));
    }

    return record;
  }
Ejemplo n.º 3
0
  @SuppressWarnings("unchecked")
  public static <T> T getFromSQLInput(Configuration configuration, SQLInput stream, Field<T> field)
      throws SQLException {
    Class<? extends T> type = field.getType();
    DataType<T> dataType = field.getDataType();

    if (type == Blob.class) {
      return (T) stream.readBlob();
    } else if (type == Boolean.class) {
      return (T) checkWasNull(stream, Boolean.valueOf(stream.readBoolean()));
    } else if (type == BigInteger.class) {
      BigDecimal result = stream.readBigDecimal();
      return (T) (result == null ? null : result.toBigInteger());
    } else if (type == BigDecimal.class) {
      return (T) stream.readBigDecimal();
    } else if (type == Byte.class) {
      return (T) checkWasNull(stream, Byte.valueOf(stream.readByte()));
    } else if (type == byte[].class) {

      // [#1327] Oracle cannot deserialise BLOBs as byte[] from SQLInput
      if (dataType.isLob()) {
        Blob blob = null;
        try {
          blob = stream.readBlob();
          return (T) (blob == null ? null : blob.getBytes(1, (int) blob.length()));
        } finally {
          Util.safeFree(blob);
        }
      } else {
        return (T) stream.readBytes();
      }
    } else if (type == Clob.class) {
      return (T) stream.readClob();
    } else if (type == Date.class) {
      return (T) stream.readDate();
    } else if (type == Double.class) {
      return (T) checkWasNull(stream, Double.valueOf(stream.readDouble()));
    } else if (type == Float.class) {
      return (T) checkWasNull(stream, Float.valueOf(stream.readFloat()));
    } else if (type == Integer.class) {
      return (T) checkWasNull(stream, Integer.valueOf(stream.readInt()));
    } else if (type == Long.class) {
      return (T) checkWasNull(stream, Long.valueOf(stream.readLong()));
    } else if (type == Short.class) {
      return (T) checkWasNull(stream, Short.valueOf(stream.readShort()));
    } else if (type == String.class) {
      return (T) stream.readString();
    } else if (type == Time.class) {
      return (T) stream.readTime();
    } else if (type == Timestamp.class) {
      return (T) stream.readTimestamp();
    } else if (type == YearToMonth.class) {
      String string = stream.readString();
      return (T) (string == null ? null : YearToMonth.valueOf(string));
    } else if (type == DayToSecond.class) {
      String string = stream.readString();
      return (T) (string == null ? null : DayToSecond.valueOf(string));
    } else if (type == UByte.class) {
      String string = stream.readString();
      return (T) (string == null ? null : UByte.valueOf(string));
    } else if (type == UShort.class) {
      String string = stream.readString();
      return (T) (string == null ? null : UShort.valueOf(string));
    } else if (type == UInteger.class) {
      String string = stream.readString();
      return (T) (string == null ? null : UInteger.valueOf(string));
    } else if (type == ULong.class) {
      String string = stream.readString();
      return (T) (string == null ? null : ULong.valueOf(string));
    }

    // The type byte[] is handled earlier. byte[][] can be handled here
    else if (type.isArray()) {
      Array result = stream.readArray();
      return (T) (result == null ? null : result.getArray());
    } else if (ArrayRecord.class.isAssignableFrom(type)) {
      return (T)
          getArrayRecord(configuration, stream.readArray(), (Class<? extends ArrayRecord<?>>) type);
    } else if (EnumType.class.isAssignableFrom(type)) {
      return getEnumType(type, stream.readString());
    } else if (MasterDataType.class.isAssignableFrom(type)) {
      return (T) getMasterDataType(type, stream.readObject());
    } else if (UDTRecord.class.isAssignableFrom(type)) {
      return (T) stream.readObject();
    } else {
      return (T) stream.readObject();
    }
  }