Java Code Examples for java.sql.Array#getResultSet()

The following examples show how to use java.sql.Array#getResultSet() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArrayFloatResultSetGetter.java    From SimpleFlatMapper with MIT License 7 votes vote down vote up
@Override
public float[] get(ResultSet target) throws Exception {
    Array sqlArray = target.getArray(index);

    if (sqlArray != null) {
        float[] array = INIT;
        int capacity = 0;
        int size = 0;

        ResultSet rs = sqlArray.getResultSet();
        try {
            while (rs.next()) {
                if (size >= capacity) {
                    int newCapacity = Math.max(Math.max(capacity + 1, capacity + (capacity >> 1)), 10);
                    array = Arrays.copyOf(array, newCapacity);
                    capacity = newCapacity;
                }
                array[size++] = rs.getFloat(VALUE_INDEX);
            }
        } finally {
            rs.close();
        }

        return Arrays.copyOf(array, size);
    }

    return null;
}
 
Example 2
Source File: SQLQuery.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * Processes a SQL Array instance and transform it into a ParamValue
 * instance
 *
 * @param dataArray
 *            SQLArray instance
 * @param paramValue
 *            Container into which the SQLArray elements should be populated
 * @return ParamValue instance containing all the elements of the
 *         corresponding SQLArray instance
 * @throws SQLException
 *             When it fails to processes the result set produced by the
 *             SQLArray instance
 */
private ParamValue processSQLArray(Array dataArray, ParamValue paramValue) throws SQLException {
    ResultSet rs = null;
    try {
        rs = dataArray.getResultSet();
        while (rs.next()) {
            Object arrayEl = rs.getObject(2);
            if (arrayEl instanceof Struct) {
                paramValue.getArrayValue().add(new ParamValue((Struct) arrayEl));
            } else if (arrayEl instanceof Array) {
                paramValue.getArrayValue().add(
                        processSQLArray((Array) arrayEl, new ParamValue(
                                ParamValue.PARAM_VALUE_ARRAY)));
            } else {
                paramValue.getArrayValue().add(new ParamValue(String.valueOf(arrayEl)));
            }
        }
        return paramValue;
    } finally {
        this.releaseResources(rs, null);
    }
}
 
Example 3
Source File: DBUtils.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * Processes a particular SQL Array object and interprets its value as a ParamValue object.
 *
 * @param sqlArray   SQL Array element.
 * @param paramValue Parameter value object initialized to contain an array of ParamValues.
 * @return ParamValue object representing the SQL Array.
 * @throws SQLException Throws an SQL Exception if the result set is not accessible.
 */
public static ParamValue processSQLArray(Array sqlArray,
                                         ParamValue paramValue) throws SQLException {
    ResultSet rs = sqlArray.getResultSet();
    while (rs.next()) {
        Object arrayEl = rs.getObject(2);
        if (arrayEl instanceof Struct) {
            paramValue.getArrayValue().add(new ParamValue((Struct) arrayEl));
        } else if (arrayEl instanceof Array) {
            paramValue.getArrayValue().add(processSQLArray(
                    (Array) arrayEl, new ParamValue(ParamValue.PARAM_VALUE_ARRAY)));
        } else {
            paramValue.getArrayValue().add(new ParamValue(String.valueOf(arrayEl)));
        }
    }
    rs.close();
    return paramValue;
}
 
Example 4
Source File: SchemaUtil.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Creates a {@link ResultSetFieldExtractor} for array types. */
private static ResultSetFieldExtractor createArrayExtractor(
    ResultSetFieldExtractor elementExtractor) {
  return (rs, index) -> {
    Array arrayVal = rs.getArray(index);
    if (arrayVal == null) {
      return null;
    }

    List<Object> arrayElements = new ArrayList<>();
    ResultSet arrayRs = arrayVal.getResultSet();
    while (arrayRs.next()) {
      arrayElements.add(elementExtractor.extract(arrayRs, 1));
    }
    return arrayElements;
  };
}
 
Example 5
Source File: DaoOperations.java    From kaif with Apache License 2.0 6 votes vote down vote up
default <T> Stream<T> convertArray(Array array,
    CheckedSqlBiFunction<ResultSet, Integer, T> extract) {
  try {
    Stream.Builder<T> builder = Stream.builder();
    try (ResultSet arrayRs = array.getResultSet()) {
      while (arrayRs.next()) {
        // index 1 is array index number, so extract should use 2
        builder.add(extract.apply(arrayRs, 2));
      }
    }
    return builder.build();
  } catch (SQLException e) {
    // see NamedParameterJdbcTemplate for source code
    throw ((JdbcTemplate) jdbc()).getExceptionTranslator()
        .translate("could not convert array: " + array, null, e);
  }

}
 
Example 6
Source File: ArrayResultSetGetter.java    From SimpleFlatMapper with MIT License 6 votes vote down vote up
@Override
public T[] get(ResultSet target) throws Exception {
    Array sqlArray = target.getArray(index);

    if (sqlArray != null) {
        List<T> list = new ArrayList<T>();
        ResultSet rs = sqlArray.getResultSet();
        try {
            while (rs.next()) {
                list.add(getter.get(rs));
            }
        } finally {
            rs.close();
        }
        return list.toArray(emptyArray);
    }
    return null;
}
 
Example 7
Source File: ArrayBooleanResultSetGetter.java    From SimpleFlatMapper with MIT License 5 votes vote down vote up
@Override
public boolean[] get(ResultSet target) throws Exception {
    Array sqlArray = target.getArray(index);

    if (sqlArray != null) {
        boolean[] array = INIT;
        int capacity = 0;
        int size = 0;

        ResultSet rs = sqlArray.getResultSet();
        try {
            while (rs.next()) {
                if (size >= capacity) {
                    int newCapacity = Math.max(Math.max(capacity + 1, capacity + (capacity >> 1)), 10);
                    array = Arrays.copyOf(array, newCapacity);
                    capacity = newCapacity;
                }
                array[size++] = rs.getBoolean(VALUE_INDEX);
            }
        } finally {
            rs.close();
        }

        return Arrays.copyOf(array, size);
    }

    return null;
}
 
Example 8
Source File: ArrayLongResultSetGetter.java    From SimpleFlatMapper with MIT License 5 votes vote down vote up
@Override
public long[] get(ResultSet target) throws Exception {
    Array sqlArray = target.getArray(index);

    if (sqlArray != null) {
        long[] array = INIT;
        int capacity = 0;
        int size = 0;

        ResultSet rs = sqlArray.getResultSet();
        try {
            while (rs.next()) {
                if (size >= capacity) {
                    int newCapacity = Math.max(Math.max(capacity + 1, capacity + (capacity >> 1)), 10);
                    array = Arrays.copyOf(array, newCapacity);
                    capacity = newCapacity;
                }
                array[size++] = rs.getLong(VALUE_INDEX);
            }
        } finally {
            rs.close();
        }

        return Arrays.copyOf(array, size);
    }

    return null;
}
 
Example 9
Source File: ArrayDoubleResultSetGetter.java    From SimpleFlatMapper with MIT License 5 votes vote down vote up
@Override
public double[] get(ResultSet target) throws Exception {
    Array sqlArray = target.getArray(index);

    if (sqlArray != null) {
        double[] array = INIT;
        int capacity = 0;
        int size = 0;

        ResultSet rs = sqlArray.getResultSet();
        try {
            while (rs.next()) {
                if (size >= capacity) {
                    int newCapacity = Math.max(Math.max(capacity + 1, capacity + (capacity >> 1)), 10);

                    array = Arrays.copyOf(array, newCapacity);
                    capacity = newCapacity;
                }
                array[size++] = rs.getDouble(VALUE_INDEX);
            }
        } finally {
            rs.close();
        }

        return Arrays.copyOf(array, size);
    }

    return null;
}
 
Example 10
Source File: ArrayShortResultSetGetter.java    From SimpleFlatMapper with MIT License 5 votes vote down vote up
@Override
public short[] get(ResultSet target) throws Exception {
    Array sqlArray = target.getArray(index);

    if (sqlArray != null) {
        short[] array = INIT;
        int capacity = 0;
        int size = 0;

        ResultSet rs = sqlArray.getResultSet();
        try  {
            while(rs.next()) {
                if (size >= capacity) {
                    int newCapacity = Math.max(Math.max(capacity+ 1, capacity + (capacity >> 1)), 10);
                    array = Arrays.copyOf(array, newCapacity);
                    capacity = newCapacity;
                }
                array[size++] = rs.getShort(VALUE_INDEX);
            }
        } finally {
            rs.close();
        }

        return Arrays.copyOf(array, size);
    }

    return null;
}
 
Example 11
Source File: ArrayByteResultSetGetter.java    From SimpleFlatMapper with MIT License 5 votes vote down vote up
@Override
public byte[] get(ResultSet target) throws Exception {
    Array sqlArray = target.getArray(index);

    if (sqlArray != null) {
        byte[] array = INIT;
        int capacity = 0;
        int size = 0;

        ResultSet rs = sqlArray.getResultSet();
        try {
            while (rs.next()) {
                if (size >= capacity) {
                    int newCapacity = Math.max(Math.max(capacity + 1, capacity + (capacity >> 1)), 10);
                    array = Arrays.copyOf(array, newCapacity);
                    capacity = newCapacity;
                }
                array[size++] = rs.getByte(VALUE_INDEX);
            }
        } finally {
            rs.close();
        }

        return Arrays.copyOf(array, size);
    }

    return null;
}
 
Example 12
Source File: ArrayIntegerResultSetGetter.java    From SimpleFlatMapper with MIT License 5 votes vote down vote up
@Override
public int[] get(ResultSet target) throws Exception {
    Array sqlArray = target.getArray(index);

    if (sqlArray != null) {
        int[] array = INIT;
        int capacity = 0;
        int size = 0;

        ResultSet rs = sqlArray.getResultSet();
        try {
            while (rs.next()) {
                if (size >= capacity) {
                    int newCapacity = Math.max(Math.max(capacity + 1, capacity + (capacity >> 1)), 10);
                    array = Arrays.copyOf(array, newCapacity);
                    capacity = newCapacity;
                }
                array[size++] = rs.getInt(VALUE_INDEX);
            }
        } finally {
            rs.close();
        }

        return Arrays.copyOf(array, size);
    }

    return null;
}
 
Example 13
Source File: SqlArrayToListConverter.java    From SimpleFlatMapper with MIT License 5 votes vote down vote up
@Override
public List<T> convert(Array in, Context context) throws Exception {
    if (in == null) return null;
    List<T> list = new ArrayList<T>();

    ResultSet rs = in.getResultSet();
    try {
        while(rs.next()) {
            list.add(getter.get(rs));
        }
    } finally {
        rs.close();
    }
    return list;
}
 
Example 14
Source File: SqlArrayConversion.java    From dalesbred with MIT License 5 votes vote down vote up
private @NotNull List<?> readArray(@NotNull Array array) {
    try {
        boolean allowNulls = !TypeUtils.isPrimitive(elementType);
        ResultSet resultSet = array.getResultSet();
        try {
            NamedTypeList types = NamedTypeList.builder(1).add("value", ResultSetUtils.getColumnType(resultSet.getMetaData(), 2)).build();
            Instantiator<?> ctor = instantiatorRegistry.findInstantiator(elementType, types);
            ArrayList<Object> result = new ArrayList<>();

            // For performance reasons we reuse the same arguments-array and InstantiatorArguments-object for all rows.
            // This should be fine as long as the instantiators don't hang on to their arguments for too long.
            Object[] arguments = new Object[1];
            InstantiatorArguments instantiatorArguments = new InstantiatorArguments(types, arguments);

            while (resultSet.next()) {
                arguments[0] = resultSet.getObject(2);

                Object value = ctor.instantiate(instantiatorArguments);
                if (value != null || allowNulls)
                    result.add(value);
                else
                    throw new UnexpectedResultException("Expected " + elementType + ", but got null");
            }

            return result;

        } finally {
            try {
                resultSet.close();
            } finally {
                SqlUtils.freeArray(array);
            }
        }

    } catch (SQLException e) {
        throw new DatabaseSQLException(e);
    }
}
 
Example 15
Source File: ArrayCharacterResultSetGetter.java    From SimpleFlatMapper with MIT License 5 votes vote down vote up
@Override
public char[] get(ResultSet target) throws Exception {
    Array sqlArray = target.getArray(index);

    if (sqlArray != null) {
        char[] array = INIT;
        int capacity = 0;
        int size = 0;

        ResultSet rs = sqlArray.getResultSet();
        try {
            while (rs.next()) {
                if (size >= capacity) {
                    int newCapacity = Math.max(Math.max(capacity + 1, capacity + (capacity >> 1)), 10);
                    array = Arrays.copyOf(array, newCapacity);
                    capacity = newCapacity;
                }
                array[size++] = (char) rs.getInt(VALUE_INDEX);
            }
        } finally {
            rs.close();
        }

        return Arrays.copyOf(array, size);
    }

    return null;
}
 
Example 16
Source File: ArrayImpl.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
/** Returns whether two arrays have the same contents.
 *
 * <p>Arrays must have the same size, and elements in the same order.
 * Elements are compared using {@link Object#equals(Object)}, and null
 * values are equal to each other. */
public static boolean equalContents(Array left, Array right) throws SQLException {
  ResultSet leftResultSet = left.getResultSet();
  ResultSet rightResultSet = right.getResultSet();
  while (leftResultSet.next() && rightResultSet.next()) {
    if (!Objects.equals(leftResultSet.getObject(1), rightResultSet.getObject(1))) {
      return false;
    }
  }
  return !leftResultSet.next() && !rightResultSet.next();
}
 
Example 17
Source File: JdbcResultSet.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
/**
 * Converts an Array into a List using {@link Array#getResultSet()}. This implementation is
 * recursive and can parse multi-dimensional arrays.
 */
static List<?> extractUsingResultSet(Array array, Calendar calendar) throws SQLException {
  ResultSet arrayValues = array.getResultSet();
  TreeMap<Integer, Object> map = new TreeMap<>();
  while (arrayValues.next()) {
    // column 1 is the index in the array, column 2 is the value.
    // Recurse on `getValue` to unwrap nested types correctly.
    // `j` is zero-indexed and incremented for us, thus we have `1` being used twice.
    map.put(arrayValues.getInt(1), getValue(arrayValues, array.getBaseType(), 1, calendar));
  }
  // If the result set is not in the same order as the actual Array, TreeMap fixes that.
  // Need to make a concrete list to ensure Jackson serialization.
  return new ArrayList<>(map.values());
}
 
Example 18
Source File: QuicksqlServerResultSet.java    From Quicksql with MIT License 5 votes vote down vote up
/**
 * Converts an Array into a List using {@link Array#getResultSet()}. This implementation is
 * recursive and can parse multi-dimensional arrays.
 */
static List<?> extractUsingResultSet(Array array, Calendar calendar) throws SQLException {
  ResultSet arrayValues = array.getResultSet();
  TreeMap<Integer, Object> map = new TreeMap<>();
  while (arrayValues.next()) {
    // column 1 is the index in the array, column 2 is the value.
    // Recurse on `getValue` to unwrap nested types correctly.
    // `j` is zero-indexed and incremented for us, thus we have `1` being used twice.
    map.put(arrayValues.getInt(1), getValue(arrayValues, array.getBaseType(), 1, calendar));
  }
  // If the result set is not in the same order as the actual Array, TreeMap fixes that.
  // Need to make a concrete list to ensure Jackson serialization.
  return new ArrayList<>(map.values());
}
 
Example 19
Source File: QuarkMetaResultSet.java    From quark with Apache License 2.0 4 votes vote down vote up
private static Object getValue(ResultSet resultSet, int type, int j,
                               Calendar calendar) throws SQLException {
  switch (type) {
    case Types.BIGINT:
      final long aLong = resultSet.getLong(j + 1);
      return aLong == 0 && resultSet.wasNull() ? null : aLong;
    case Types.INTEGER:
      final int anInt = resultSet.getInt(j + 1);
      return anInt == 0 && resultSet.wasNull() ? null : anInt;
    case Types.SMALLINT:
      final short aShort = resultSet.getShort(j + 1);
      return aShort == 0 && resultSet.wasNull() ? null : aShort;
    case Types.TINYINT:
      final byte aByte = resultSet.getByte(j + 1);
      return aByte == 0 && resultSet.wasNull() ? null : aByte;
    case Types.DOUBLE:
    case Types.FLOAT:
      final double aDouble = resultSet.getDouble(j + 1);
      return aDouble == 0D && resultSet.wasNull() ? null : aDouble;
    case Types.REAL:
      final float aFloat = resultSet.getFloat(j + 1);
      return aFloat == 0D && resultSet.wasNull() ? null : aFloat;
    case Types.DATE:
      final Date aDate = resultSet.getDate(j + 1, calendar);
      return aDate == null
          ? null
          : (int) (aDate.getTime() / DateTimeUtils.MILLIS_PER_DAY);
    case Types.TIME:
      final Time aTime = resultSet.getTime(j + 1, calendar);
      return aTime == null
          ? null
          : (int) (aTime.getTime() % DateTimeUtils.MILLIS_PER_DAY);
    case Types.TIMESTAMP:
      final Timestamp aTimestamp = resultSet.getTimestamp(j + 1, calendar);
      return aTimestamp == null ? null : aTimestamp.getTime();
    case Types.ARRAY:
      final Array array = resultSet.getArray(j + 1);
      if (null == array) {
        return null;
      }
      ResultSet arrayValues = array.getResultSet();
      TreeMap<Integer, Object> map = new TreeMap<>();
      while (arrayValues.next()) {
        // column 1 is the index in the array, column 2 is the value.
        // Recurse on `getValue` to unwrap nested types correctly.
        // `j` is zero-indexed and incremented for us, thus we have `1` being used twice.
        map.put(arrayValues.getInt(1), getValue(arrayValues, array.getBaseType(), 1, calendar));
      }
      // If the result set is not in the same order as the actual Array, TreeMap fixes that.
      // Need to make a concrete list to ensure Jackson serialization.
      //return new ListLike<Object>(new ArrayList<>(map.values()), ListLikeType.ARRAY);
      return new ArrayList<>(map.values());
    case Types.STRUCT:
      Struct struct = resultSet.getObject(j + 1, Struct.class);
      Object[] attrs = struct.getAttributes();
      List<Object> list = new ArrayList<>(attrs.length);
      for (Object o : attrs) {
        list.add(o);
      }
      return list;
    default:
      return resultSet.getObject(j + 1);
  }
}
 
Example 20
Source File: QuarkMetaResultSet.java    From quark with Apache License 2.0 4 votes vote down vote up
private static Object getValue(Object obj, int type, int j,
                               Calendar calendar) throws SQLException {
  if (obj == null) {
    return null;
  }
  // Hack for QuboleDB
  // returns every null value as String of value null
  if (obj instanceof String
      && ((String) obj).equalsIgnoreCase("null")) {
    return null;
  }

  switch (type) {
    case Types.BIGINT:
      if (obj instanceof String) {
        return Long.valueOf((String) obj);
      }
      return obj;
    case Types.INTEGER:
      if (obj instanceof String) {
        return Integer.valueOf((String) obj);
      }
      return obj;
    case Types.SMALLINT:
      if (obj instanceof String) {
        return Short.valueOf((String) obj);
      }
      return obj;
    case Types.TINYINT:
      if (obj instanceof String) {
        return Byte.valueOf((String) obj);
      }
      return obj;
    case Types.DOUBLE:
    case Types.FLOAT:
      if (obj instanceof String) {
        return Double.valueOf((String) obj);
      }
      return obj;
    case Types.REAL:
      if (obj instanceof String) {
        return Float.valueOf((String) obj);
      }
      return obj;
    case Types.DATE:
      if (obj instanceof String) {
        return Date.valueOf((String) obj);
      }
      return obj;
    case Types.TIME:
      if (obj instanceof String) {
        return Time.valueOf((String) obj);
      }
      return obj;
    case Types.TIMESTAMP:
      if (obj instanceof String) {
        return Timestamp.valueOf((String) obj);
      }
      return obj;
    case Types.ARRAY:
      final Array array = (Array) obj;
      if (null == array) {
        return null;
      }
      ResultSet arrayValues = array.getResultSet();
      TreeMap<Integer, Object> map = new TreeMap<>();
      while (arrayValues.next()) {
        // column 1 is the index in the array, column 2 is the value.
        // Recurse on `getValue` to unwrap nested types correctly.
        // `j` is zero-indexed and incremented for us, thus we have `1` being used twice.
        map.put(arrayValues.getInt(1), getValue(arrayValues, array.getBaseType(), 1, calendar));
      }
      // If the result set is not in the same order as the actual Array, TreeMap fixes that.
      // Need to make a concrete list to ensure Jackson serialization.
      //return new ListLike<Object>(new ArrayList<>(map.values()), ListLikeType.ARRAY);
      return new ArrayList<>(map.values());
    case Types.STRUCT:
      Struct struct = (Struct) obj;
      Object[] attrs = struct.getAttributes();
      List<Object> list = new ArrayList<>(attrs.length);
      for (Object o : attrs) {
        list.add(o);
      }
      return list;
    default:
      return obj;
  }
}