Java Code Examples for java.sql.ResultSet.getArray()

The following are Jave code examples for showing how to use getArray() of the java.sql.ResultSet class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: aceql-http   File: ResultSetWriter.java   View Source Code Vote up 8 votes
/**
    * Format the column as an java.sqlArray
    * 
    * @param resultSet
    * @param columnIndex
    * @return
    * @throws SQLException
    * @throws IOException
    */
   private String formatArrayColumn(ResultSet resultSet, int columnIndex)
    throws SQLException, IOException {
Array array = resultSet.getArray(columnIndex);

Object[] objects = (Object[]) array.getArray();
String arrayStr = "{";
for (int i = 0; i < objects.length; i++) {
    arrayStr += objects[i] + ",";
}

if (arrayStr.contains(",")) {
    arrayStr = StringUtils.substringBeforeLast(arrayStr, ",");
}

arrayStr += "}";
return arrayStr;

/*
 * ArrayHttp arrayHttp = new ArrayHttp(array); ArrayTransporter
 * arrayTransporter = new ArrayTransporter(); String base64 =
 * arrayTransporter.toBase64(arrayHttp); return base64;
 */
   }
 
Example 2
Project: rufus   File: SourceMapper.java   View Source Code Vote up 6 votes
@Override
public Source map(int i, ResultSet resultSet, StatementContext statementContext) throws SQLException {
    String resultSource = resultSet.getString("source");
    if (StringUtils.isEmpty(resultSource)) {
        return null;
    }

    Source source;
    try {
        source = new Source(new URL(resultSource));
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }

    Array tags = resultSet.getArray("tags");
    source.setTags(tags == null
        ? Collections.emptyList()
        : Arrays.asList((Object[]) tags.getArray()).stream().map(Object::toString).collect(Collectors.toList())
    );
    source.setFrontpage(resultSet.getBoolean("frontpage"));
    return source;
}
 
Example 3
Project: SqlSauce   File: ArrayListLongUserType.java   View Source Code Vote up 6 votes
@Override
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
        throws HibernateException, SQLException {
    Array array = rs.getArray(names[0]);
    if (array == null) {
        return null;
    }
    Long[] javaArray = (Long[]) array.getArray();
    ArrayList<Long> result = new ArrayList<>();
    Collections.addAll(result, javaArray); //do not use Arrays.asList(), that method returns a fake ArrayList
    return result;
}
 
Example 4
Project: calcite-avatica   File: RemoteMetaTest.java   View Source Code Vote up 6 votes
@Test public void testArrays() throws SQLException {
  ConnectionSpec.getDatabaseLock().lock();
  try (AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url);
       Statement stmt = conn.createStatement()) {
    ResultSet resultSet =
        stmt.executeQuery("select * from (values ('a', array['b', 'c']));");

    assertTrue(resultSet.next());
    assertEquals("a", resultSet.getString(1));
    Array arr = resultSet.getArray(2);
    assertTrue(arr instanceof ArrayImpl);
    Object[] values = (Object[]) ((ArrayImpl) arr).getArray();
    assertArrayEquals(new String[]{"b", "c"}, values);
  } finally {
    ConnectionSpec.getDatabaseLock().unlock();
  }
}
 
Example 5
Project: graphium   File: RowMapperUtils.java   View Source Code Vote up 5 votes
public static Set<Access> convertAccessTypes(ResultSet rs, String fieldName) throws SQLException {
	Array accessTypeArray = rs.getArray(fieldName);
	if (accessTypeArray != null) {
		Integer[] accessTypeIds = (Integer[]) accessTypeArray.getArray();
		int[] accessTypeIds2 = new int[accessTypeIds.length];
		for (int i=0; i<accessTypeIds.length; i++) {
			accessTypeIds2[i] = accessTypeIds[i];
		}
		return Access.getAccessTypes(accessTypeIds2);
	} else {
		return null;
	}
}
 
Example 6
Project: uroborosql   File: ArrayPropertyMapper.java   View Source Code Vote up 5 votes
@Override
public boolean canAcceptTest(final JavaType type, final ResultSet rs, final int columnIndex,
		final PropertyMapperManager mapperManager) throws SQLException {
	java.sql.Array array = rs.getArray(columnIndex);
	if (array == null) {
		return true;
	}
	Object o = array.getArray();
	return type.getRawType().isInstance(o);
}
 
Example 7
Project: uroborosql   File: ArrayPropertyMapper.java   View Source Code Vote up 5 votes
@Override
public Object getValue(final JavaType type, final ResultSet rs, final int columnIndex,
		final PropertyMapperManager mapperManager) throws SQLException {
	java.sql.Array array = rs.getArray(columnIndex);
	if (array == null) {
		return null;
	}
	return array.getArray();
}
 
Example 8
Project: tangyuan2   File: ArrayTypeHandler.java   View Source Code Vote up 5 votes
@Override
public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
	Array array = rs.getArray(columnName);
	return array == null ? null : array.getArray();
}
 
Example 9
Project: calcite-avatica   File: AvaticaResultSetConversionsTest.java   View Source Code Vote up 5 votes
public void testGetArray(ResultSet resultSet) throws SQLException {
  try {
    resultSet.getArray(ordinal);
    fail("Was expecting to throw SQLDataException");
  } catch (Exception e) {
    assertThat(e, isA((Class) SQLDataException.class)); // success
  }
}
 
Example 10
Project: tangyuan2   File: ArrayTypeHandler.java   View Source Code Vote up 4 votes
@Override
public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
	Array array = rs.getArray(columnIndex);
	return array == null ? null : array.getArray();
}
 
Example 11
Project: calcite-avatica   File: JdbcResultSet.java   View Source Code Vote up 4 votes
/** Creates a frame containing a given number or unlimited number of rows
 * from a result set. */
static Meta.Frame frame(StatementInfo info, ResultSet resultSet, long offset,
    int fetchMaxRowCount, Calendar calendar, Optional<Meta.Signature> sig) throws SQLException {
  final ResultSetMetaData metaData = resultSet.getMetaData();
  final int columnCount = metaData.getColumnCount();
  final int[] types = new int[columnCount];
  Set<Integer> arrayOffsets = new HashSet<>();
  for (int i = 0; i < types.length; i++) {
    types[i] = metaData.getColumnType(i + 1);
    if (Types.ARRAY == types[i]) {
      arrayOffsets.add(i);
    }
  }
  final List<Object> rows = new ArrayList<>();
  // Meta prepare/prepareAndExecute 0 return 0 row and done
  boolean done = fetchMaxRowCount == 0;
  for (int i = 0; fetchMaxRowCount < 0 || i < fetchMaxRowCount; i++) {
    final boolean hasRow;
    if (null != info) {
      hasRow = info.next();
    } else {
      hasRow = resultSet.next();
    }
    if (!hasRow) {
      done = true;
      resultSet.close();
      break;
    }
    Object[] columns = new Object[columnCount];
    for (int j = 0; j < columnCount; j++) {
      columns[j] = getValue(resultSet, types[j], j, calendar);
      if (arrayOffsets.contains(j)) {
        // If we have an Array type, our Signature is lacking precision. We can't extract the
        // component type of an Array from metadata, we have to update it as we're serializing
        // the ResultSet.
        final Array array = resultSet.getArray(j + 1);
        // Only attempt to determine the component type for the array when non-null
        if (null != array && sig.isPresent()) {
          ColumnMetaData columnMetaData = sig.get().columns.get(j);
          ArrayType arrayType = (ArrayType) columnMetaData.type;
          SqlType componentSqlType = SqlType.valueOf(array.getBaseType());

          // Avatica Server will always return non-primitives to ensure nullable is guaranteed.
          ColumnMetaData.Rep rep = ColumnMetaData.Rep.serialRepOf(componentSqlType);
          AvaticaType componentType = ColumnMetaData.scalar(array.getBaseType(),
              array.getBaseTypeName(), rep);
          // Update the ArrayType from the Signature
          arrayType.updateComponentType(componentType);

          // We only need to update the array's type once.
          arrayOffsets.remove(j);
        }
      }
    }
    rows.add(columns);
  }
  return new Meta.Frame(offset, done, rows);
}
 
Example 12
Project: calcite-avatica   File: JdbcResultSet.java   View Source Code Vote up 4 votes
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;
    }
    try {
      // Recursively extracts an Array using its ResultSet-representation
      return extractUsingResultSet(array, calendar);
    } catch (UnsupportedOperationException | SQLFeatureNotSupportedException e) {
      // Not every database might implement Array.getResultSet(). This call
      // assumes a non-nested array (depends on the db if that's a valid assumption)
      return extractUsingArray(array, calendar);
    }
  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 13
Project: calcite-avatica   File: ColumnMetaData.java   View Source Code Vote up 4 votes
/** Returns the value of a column of this type from a result set. */
public Object jdbcGet(ResultSet resultSet, int i) throws SQLException {
  switch (this) {
  case PRIMITIVE_BOOLEAN:
    return resultSet.getBoolean(i);
  case PRIMITIVE_BYTE:
    return resultSet.getByte(i);
  case PRIMITIVE_SHORT:
    return resultSet.getShort(i);
  case PRIMITIVE_INT:
    return resultSet.getInt(i);
  case PRIMITIVE_LONG:
    return resultSet.getLong(i);
  case PRIMITIVE_FLOAT:
    return resultSet.getFloat(i);
  case PRIMITIVE_DOUBLE:
    return resultSet.getDouble(i);
  case BOOLEAN:
    final boolean aBoolean = resultSet.getBoolean(i);
    return resultSet.wasNull() ? null : aBoolean;
  case BYTE:
    final byte aByte = resultSet.getByte(i);
    return resultSet.wasNull() ? null : aByte;
  case SHORT:
    final short aShort = resultSet.getShort(i);
    return resultSet.wasNull() ? null : aShort;
  case INTEGER:
    final int anInt = resultSet.getInt(i);
    return resultSet.wasNull() ? null : anInt;
  case LONG:
    final long aLong = resultSet.getLong(i);
    return resultSet.wasNull() ? null : aLong;
  case FLOAT:
    final float aFloat = resultSet.getFloat(i);
    return resultSet.wasNull() ? null : aFloat;
  case DOUBLE:
    final double aDouble = resultSet.getDouble(i);
    return resultSet.wasNull() ? null : aDouble;
  case JAVA_SQL_DATE:
    return resultSet.getDate(i);
  case JAVA_SQL_TIME:
    return resultSet.getTime(i);
  case JAVA_SQL_TIMESTAMP:
    return resultSet.getTimestamp(i);
  case ARRAY:
    return resultSet.getArray(i);
  case STRUCT:
    return resultSet.getObject(i, Struct.class);
  default:
    return resultSet.getObject(i);
  }
}