예제 #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;
    }
  }
예제 #2
0
파일: ArrayTable.java 프로젝트: joe776/jOOQ
  @SuppressWarnings({"unchecked"})
  ArrayTable(Field<?> array, String alias) {
    super(alias);

    Class<?> arrayType;

    // TODO [#523] Solve this in a more object-oriented way...
    if (array.getDataType().getType().isArray()) {
      arrayType = array.getDataType().getType().getComponentType();
    }

    // [#1110] Keep track of element type information of Oracle VARRAY / TABLE types
    else if (array instanceof ArrayConstant) {
      arrayType = array.getDataType().getType();
    }

    // [#1111] Keep track of element type information of Oracle
    // VARRAY / TABLE types returned from functions
    else if (ArrayRecord.class.isAssignableFrom(array.getDataType().getType())) {
      // TODO [#523] This information should be available in ARRAY meta-data
      ArrayRecord<?> dummy =
          Utils.newArrayRecord(
              (Class<ArrayRecord<?>>) array.getDataType().getType(), new DefaultConfiguration());
      arrayType = dummy.getDataType().getType();
    }

    // Is this case possible?
    else {
      arrayType = Object.class;
    }

    this.array = array;
    this.alias = alias;
    this.field = init(alias, arrayType);

    init(alias, arrayType);
  }
예제 #3
0
  private static <T> void writeToSQLOutput(
      SQLOutput stream, Class<? extends T> type, DataType<T> dataType, T value)
      throws SQLException {
    if (value == null) {
      stream.writeObject(null);
    } else if (type == Blob.class) {
      stream.writeBlob((Blob) value);
    } else if (type == Boolean.class) {
      stream.writeBoolean((Boolean) value);
    } else if (type == BigInteger.class) {
      stream.writeBigDecimal(new BigDecimal((BigInteger) value));
    } else if (type == BigDecimal.class) {
      stream.writeBigDecimal((BigDecimal) value);
    } else if (type == Byte.class) {
      stream.writeByte((Byte) value);
    } else if (type == byte[].class) {

      // [#1327] Oracle cannot serialise BLOBs as byte[] to SQLOutput
      // Use reflection to avoid dependency on OJDBC
      if (dataType.isLob()) {
        Blob blob = null;

        try {
          blob =
              on("oracle.sql.BLOB")
                  .call(
                      "createTemporary",
                      on(stream).call("getSTRUCT").call("getJavaSqlConnection").get(),
                      false,
                      on("oracle.sql.BLOB").get("DURATION_SESSION"))
                  .get();

          blob.setBytes(1, (byte[]) value);
          stream.writeBlob(blob);
        } finally {
          DefaultExecuteContext.register(blob);
        }
      } else {
        stream.writeBytes((byte[]) value);
      }
    } else if (type == Clob.class) {
      stream.writeClob((Clob) value);
    } else if (type == Date.class) {
      stream.writeDate((Date) value);
    } else if (type == Double.class) {
      stream.writeDouble((Double) value);
    } else if (type == Float.class) {
      stream.writeFloat((Float) value);
    } else if (type == Integer.class) {
      stream.writeInt((Integer) value);
    } else if (type == Long.class) {
      stream.writeLong((Long) value);
    } else if (type == Short.class) {
      stream.writeShort((Short) value);
    } else if (type == String.class) {

      // [#1327] Oracle cannot serialise CLOBs as String to SQLOutput
      // Use reflection to avoid dependency on OJDBC
      if (dataType.isLob()) {
        Clob clob = null;

        try {
          clob =
              on("oracle.sql.CLOB")
                  .call(
                      "createTemporary",
                      on(stream).call("getSTRUCT").call("getJavaSqlConnection").get(),
                      false,
                      on("oracle.sql.CLOB").get("DURATION_SESSION"))
                  .get();

          clob.setString(1, (String) value);
          stream.writeClob(clob);
        } finally {
          DefaultExecuteContext.register(clob);
        }
      } else {
        stream.writeString((String) value);
      }
    } else if (type == Time.class) {
      stream.writeTime((Time) value);
    } else if (type == Timestamp.class) {
      stream.writeTimestamp((Timestamp) value);
    } else if (type == YearToMonth.class) {
      stream.writeString(value.toString());
    } else if (type == DayToSecond.class) {
      stream.writeString(value.toString());
    }
    //        else if (type.isArray()) {
    //            stream.writeArray(value);
    //        }
    else if (UNumber.class.isAssignableFrom(type)) {
      stream.writeString(value.toString());
    } else if (ArrayRecord.class.isAssignableFrom(type)) {

      // [#1544] We can safely assume that localConfiguration has been
      // set on DefaultBindContext, prior to serialising arrays to SQLOut
      Connection connection = getDriverConnection(DefaultBindContext.LOCAL_CONFIGURATION.get());
      ArrayRecord<?> arrayRecord = (ArrayRecord<?>) value;
      stream.writeArray(
          on(connection)
              .call("createARRAY", arrayRecord.getName(), arrayRecord.get())
              .<Array>get());
    } else if (EnumType.class.isAssignableFrom(type)) {
      stream.writeString(((EnumType) value).getLiteral());
    } else if (MasterDataType.class.isAssignableFrom(type)) {
      Object key = ((MasterDataType<?>) value).getPrimaryKey();
      writeToSQLOutput(stream, key.getClass(), key);
    } else if (UDTRecord.class.isAssignableFrom(type)) {
      stream.writeObject((UDTRecord<?>) value);
    } else {
      throw new UnsupportedOperationException("Type " + type + " is not supported");
    }
  }