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

The following examples show how to use java.sql.ResultSet#getArray() . 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: RegexpSplitFunctionIT.java    From phoenix with Apache License 2.0 10 votes vote down vote up
@Test
public void testSplit_Upsert() throws SQLException {
    Connection conn = DriverManager.getConnection(getUrl());
    initTable(conn, "ONE,TWO,THREE");

    conn.createStatement().executeUpdate(
        "UPSERT INTO " + tableName + " (ID, ARR) SELECT ID, " + "REGEXP_SPLIT(VAL, ',') FROM "
            + tableName);
    conn.commit();

    ResultSet rs = conn.createStatement().executeQuery("SELECT ARR FROM " + tableName);
    assertTrue(rs.next());
    Array array = rs.getArray(1);
    String[] values = (String[]) array.getArray();
    assertArrayEquals(new String[]{ "ONE", "TWO", "THREE" }, values);
}
 
Example 2
Source File: ArrayTest.java    From phoenix with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testSelectArrayUsingUpsertLikeSyntax() throws Exception {
	long ts = nextTimestamp();
	String tenantId = getOrganizationId();
	createTableWithArray(BaseConnectedQueryTest.getUrl(),
			getDefaultSplits(tenantId), null, ts - 2);
	initTablesWithArrays(tenantId, null, ts, false);
	String query = "SELECT a_double_array FROM TABLE_WITH_ARRAY WHERE a_double_array = ARRAY [ 25.343d, 36.763d, 37.56d,386.63d]";
	Properties props = new Properties(TEST_PROPERTIES);
	props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB,
			Long.toString(ts + 2)); // Execute at timestamp 2
	Connection conn = DriverManager.getConnection(PHOENIX_JDBC_URL, props);
	try {
		PreparedStatement statement = conn.prepareStatement(query);
		ResultSet rs = statement.executeQuery();
		assertTrue(rs.next());
		Double[] doubleArr =  new Double[4];
           doubleArr[0] = 25.343;
           doubleArr[1] = 36.763;
           doubleArr[2] = 37.56;
           doubleArr[3] = 386.63;
		Array array = conn.createArrayOf("DOUBLE", doubleArr);
		PhoenixArray resultArray = (PhoenixArray) rs.getArray(1);
		assertEquals(resultArray, array);
		assertFalse(rs.next());
	} finally {
		conn.close();
	}
}
 
Example 3
Source File: ArrayIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testArrayWithCast() throws Exception {
    Connection conn;
    PreparedStatement stmt;
    ResultSet rs;
    long ts = nextTimestamp();
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 10));
    conn = DriverManager.getConnection(getUrl(), props);
    conn.createStatement().execute("CREATE TABLE t ( k VARCHAR PRIMARY KEY, a bigint ARRAY[])");
    conn.close();

    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 30));
    conn = DriverManager.getConnection(getUrl(), props);
    stmt = conn.prepareStatement("UPSERT INTO t VALUES(?,?)");
    stmt.setString(1, "a");
    Long[] s = new Long[] { 1l, 2l };
    Array array = conn.createArrayOf("BIGINT", s);
    stmt.setArray(2, array);
    stmt.execute();
    conn.commit();
    conn.close();
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 40));
    conn = DriverManager.getConnection(getUrl(), props);
    rs = conn.createStatement().executeQuery("SELECT CAST(a AS DOUBLE []) FROM t");
    assertTrue(rs.next());
    Double[] d = new Double[] { 1.0, 2.0 };
    array = conn.createArrayOf("DOUBLE", d);
    PhoenixArray arr = (PhoenixArray)rs.getArray(1);
    assertEquals(array, arr);
    conn.close();
}
 
Example 4
Source File: ResultSetUtils.java    From elucidate-server with MIT License 5 votes vote down vote up
public static <T> Set<T> getArrayAsSet(ResultSet rs, String columnName) throws SQLException {
    Array array = rs.getArray(columnName);

    if (array == null) {
        return Collections.emptySet();
    }

    return new HashSet<>(Arrays.asList((T[]) array.getArray()));
}
 
Example 5
Source File: IntegerArrayTypeHandler.java    From mmpt with MIT License 5 votes vote down vote up
@Override
public Integer[] getNullableResult(ResultSet rs, String columnName)
        throws SQLException {
    Array outputArray = rs.getArray(columnName);
    if (outputArray == null) {
        return null;
    }
    return (Integer[])outputArray.getArray();
}
 
Example 6
Source File: ArrayColumnMapper.java    From alf.io with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Object getObject(ResultSet rs) throws SQLException {
    var array = rs.getArray(name);
    if(array != null) {
        return Arrays.asList((Object[]) array.getArray());
    }
    return null;
}
 
Example 7
Source File: BigIntArrayTypeHandler.java    From mmpt with MIT License 5 votes vote down vote up
@Override
public Long[] getNullableResult(ResultSet rs, String columnName)
        throws SQLException {
    Array outputArray = rs.getArray(columnName);
    if (outputArray == null) {
        return null;
    }
    return (Long[])outputArray.getArray();
}
 
Example 8
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 9
Source File: SmallIntArrayTypeHandler.java    From mmpt with MIT License 5 votes vote down vote up
@Override
public Integer[] getNullableResult(ResultSet rs, int columnIndex)
        throws SQLException {
    Array outputArray = rs.getArray(columnIndex);
    if (outputArray == null) {
        return null;
    }
    return (Integer[])outputArray.getArray();
}
 
Example 10
Source File: RegexpSplitFunctionIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testSplit_Upsert() throws SQLException {
    Connection conn = DriverManager.getConnection(getUrl());
    initTable(conn, "ONE,TWO,THREE");

    conn.createStatement().executeUpdate("UPSERT INTO SPLIT_TEST (ID, ARR) SELECT ID, " +
            "REGEXP_SPLIT(VAL, ',') FROM SPLIT_TEST");
    conn.commit();

    ResultSet rs = conn.createStatement().executeQuery("SELECT ARR FROM SPLIT_TEST");
    assertTrue(rs.next());
    Array array = rs.getArray(1);
    String[] values = (String[]) array.getArray();
    assertArrayEquals(new String[]{ "ONE", "TWO", "THREE" }, values);
}
 
Example 11
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 12
Source File: UUIDArrayTypeHandler.java    From mmpt with MIT License 5 votes vote down vote up
@Override
public UUID[] getNullableResult(ResultSet rs, int columnIndex)
        throws SQLException {
    Array outputArray = rs.getArray(columnIndex);
    if (outputArray == null) {
        return null;
    }
    return (UUID[])outputArray.getArray();
}
 
Example 13
Source File: ArrayIT.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Test
public void testScanWithNonFixedWidthArrayInWhereClause() throws Exception {
	long ts = nextTimestamp();
	String tenantId = getOrganizationId();
	createTableWithArray(getUrl(),
			getDefaultSplits(tenantId), null, ts - 2);
	initTablesWithArrays(tenantId, null, ts, false, getUrl());
	String query = "SELECT a_double_array, /* comment ok? */ b_string, a_float FROM table_with_array WHERE ?=organization_id and ?=a_string_array";
	Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
	props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB,
			Long.toString(ts + 2)); // Execute at timestamp 2
	Connection conn = DriverManager.getConnection(getUrl(), props);
	try {
		PreparedStatement statement = conn.prepareStatement(query);
		statement.setString(1, tenantId);
		// Need to support primitive
		String[] strArr = new String[4];
		strArr[0] = "ABC";
		strArr[1] = "CEDF";
		strArr[2] = "XYZWER";
		strArr[3] = "AB";
		Array array = conn.createArrayOf("VARCHAR", strArr);
		statement.setArray(2, array);
		ResultSet rs = statement.executeQuery();
		assertTrue(rs.next());
		// Need to support primitive
		Double[] doubleArr = new Double[4];
		doubleArr[0] = 25.343;
		doubleArr[1] = 36.763;
	    doubleArr[2] = 37.56;
           doubleArr[3] = 386.63;
		array = conn.createArrayOf("DOUBLE", doubleArr);
		Array resultArray = rs.getArray(1);
		assertEquals(resultArray, array);
		assertEquals(rs.getString("B_string"), B_VALUE);
		assertTrue(Floats.compare(rs.getFloat(3), 0.01f) == 0);
		assertFalse(rs.next());
	} finally {
		conn.close();
	}
}
 
Example 14
Source File: ArrayTypeHandler.java    From mybaties with Apache License 2.0 4 votes vote down vote up
@Override
public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
  Array array = rs.getArray(columnIndex);
  return array == null ? null : array.getArray();
}
 
Example 15
Source File: NumericArrayTypesTest.java    From PgBulkInsert with MIT License 4 votes vote down vote up
private <T> void testArrayInternal(String columnLabel, ArrayEntity entity, @Nullable List<T> samples, Function<BigDecimal, T> converter) throws SQLException {
    Objects.requireNonNull(samples, "samples");

    List<ArrayEntity> entities = Collections.singletonList(entity);

    PgBulkInsert<ArrayEntity> pgBulkInsert = new PgBulkInsert<>(new ArrayEntityMapping());

    pgBulkInsert.saveAll(PostgreSqlUtils.getPGConnection(connection), entities.stream());

    ResultSet rs = getAll();

    while (rs.next()) {
        Array z = rs.getArray(columnLabel);

        BigDecimal[] v = (BigDecimal[]) z.getArray();

        for (int i=0; i<samples.size(); i++) {

            T element = converter.apply(v[i]);

            Assert.assertEquals(samples.get(i), element);
        }
    }
}
 
Example 16
Source File: Jdbc41Bridge.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Delegates to {@link ResultSet#getObject(int, Class)} without throwing a {@link AbstractMethodError}.
 * <p>
 * If the JDBC driver does not implement {@link ResultSet#getObject(int, Class)}, then return 0.
 * </p>
 *
 * @param <T>
 *            See {@link ResultSet#getObject(int, Class)}
 * @param resultSet
 *            See {@link ResultSet#getObject(int, Class)}
 * @param columnIndex
 *            See {@link ResultSet#getObject(int, Class)}
 * @param type
 *            See {@link ResultSet#getObject(int, Class)}
 * @return See {@link ResultSet#getObject(int, Class)}
 * @throws SQLException
 *             See {@link ResultSet#getObject(int, Class)}
 * @see ResultSet#getObject(int, Class)
 */
@SuppressWarnings("unchecked")
public static <T> T getObject(final ResultSet resultSet, final int columnIndex, final Class<T> type)
        throws SQLException {
    try {
        return resultSet.getObject(columnIndex, type);
    } catch (final AbstractMethodError e) {
        if (type == String.class) {
            return (T) resultSet.getString(columnIndex);
        }
        // Numbers
        if (type == Integer.class) {
            return (T) Integer.valueOf(resultSet.getInt(columnIndex));
        }
        if (type == Long.class) {
            return (T) Long.valueOf(resultSet.getLong(columnIndex));
        }
        if (type == Double.class) {
            return (T) Double.valueOf(resultSet.getDouble(columnIndex));
        }
        if (type == Float.class) {
            return (T) Float.valueOf(resultSet.getFloat(columnIndex));
        }
        if (type == Short.class) {
            return (T) Short.valueOf(resultSet.getShort(columnIndex));
        }
        if (type == BigDecimal.class) {
            return (T) resultSet.getBigDecimal(columnIndex);
        }
        if (type == Byte.class) {
            return (T) Byte.valueOf(resultSet.getByte(columnIndex));
        }
        // Dates
        if (type == Date.class) {
            return (T) resultSet.getDate(columnIndex);
        }
        if (type == Time.class) {
            return (T) resultSet.getTime(columnIndex);
        }
        if (type == Timestamp.class) {
            return (T) resultSet.getTimestamp(columnIndex);
        }
        // Streams
        if (type == InputStream.class) {
            return (T) resultSet.getBinaryStream(columnIndex);
        }
        if (type == Reader.class) {
            return (T) resultSet.getCharacterStream(columnIndex);
        }
        // Other
        if (type == Object.class) {
            return (T) resultSet.getObject(columnIndex);
        }
        if (type == Boolean.class) {
            return (T) Boolean.valueOf(resultSet.getBoolean(columnIndex));
        }
        if (type == Array.class) {
            return (T) resultSet.getArray(columnIndex);
        }
        if (type == Blob.class) {
            return (T) resultSet.getBlob(columnIndex);
        }
        if (type == Clob.class) {
            return (T) resultSet.getClob(columnIndex);
        }
        if (type == Ref.class) {
            return (T) resultSet.getRef(columnIndex);
        }
        if (type == RowId.class) {
            return (T) resultSet.getRowId(columnIndex);
        }
        if (type == SQLXML.class) {
            return (T) resultSet.getSQLXML(columnIndex);
        }
        if (type == URL.class) {
            return (T) resultSet.getURL(columnIndex);
        }
        throw new SQLFeatureNotSupportedException(
                String.format("resultSet=%s, columnIndex=%,d, type=%s", resultSet, Integer.valueOf(columnIndex), type));
    }
}
 
Example 17
Source File: JdbcValueHandler.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
@Override
public java.sql.Array getValue(ResultSet rs, int columnIndex) throws SQLException {
    return rs.getArray(columnIndex);
}
 
Example 18
Source File: AbstractCopySQLData.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public Object getDbValue(ResultSet resultSet) throws SQLException {
	Array fieldVal = resultSet.getArray(fieldSQL);
	return resultSet.wasNull() ? null : fieldVal;
}
 
Example 19
Source File: Jdbc41Bridge.java    From commons-dbcp with Apache License 2.0 4 votes vote down vote up
/**
 * Delegates to {@link ResultSet#getObject(int, Class)} without throwing an {@link AbstractMethodError}.
 * <p>
 * If the JDBC driver does not implement {@link ResultSet#getObject(int, Class)}, then return 0.
 * </p>
 *
 * @param <T>
 *            See {@link ResultSet#getObject(int, Class)}
 * @param resultSet
 *            See {@link ResultSet#getObject(int, Class)}
 * @param columnIndex
 *            See {@link ResultSet#getObject(int, Class)}
 * @param type
 *            See {@link ResultSet#getObject(int, Class)}
 * @return See {@link ResultSet#getObject(int, Class)}
 * @throws SQLException
 *             See {@link ResultSet#getObject(int, Class)}
 * @see ResultSet#getObject(int, Class)
 */
@SuppressWarnings("unchecked")
public static <T> T getObject(final ResultSet resultSet, final int columnIndex, final Class<T> type)
        throws SQLException {
    try {
        return resultSet.getObject(columnIndex, type);
    } catch (final AbstractMethodError e) {
        if (type == String.class) {
            return (T) resultSet.getString(columnIndex);
        }
        // Numbers
        if (type == Integer.class) {
            return (T) Integer.valueOf(resultSet.getInt(columnIndex));
        }
        if (type == Long.class) {
            return (T) Long.valueOf(resultSet.getLong(columnIndex));
        }
        if (type == Double.class) {
            return (T) Double.valueOf(resultSet.getDouble(columnIndex));
        }
        if (type == Float.class) {
            return (T) Float.valueOf(resultSet.getFloat(columnIndex));
        }
        if (type == Short.class) {
            return (T) Short.valueOf(resultSet.getShort(columnIndex));
        }
        if (type == BigDecimal.class) {
            return (T) resultSet.getBigDecimal(columnIndex);
        }
        if (type == Byte.class) {
            return (T) Byte.valueOf(resultSet.getByte(columnIndex));
        }
        // Dates
        if (type == Date.class) {
            return (T) resultSet.getDate(columnIndex);
        }
        if (type == Time.class) {
            return (T) resultSet.getTime(columnIndex);
        }
        if (type == Timestamp.class) {
            return (T) resultSet.getTimestamp(columnIndex);
        }
        // Streams
        if (type == InputStream.class) {
            return (T) resultSet.getBinaryStream(columnIndex);
        }
        if (type == Reader.class) {
            return (T) resultSet.getCharacterStream(columnIndex);
        }
        // Other
        if (type == Object.class) {
            return (T) resultSet.getObject(columnIndex);
        }
        if (type == Boolean.class) {
            return (T) Boolean.valueOf(resultSet.getBoolean(columnIndex));
        }
        if (type == Array.class) {
            return (T) resultSet.getArray(columnIndex);
        }
        if (type == Blob.class) {
            return (T) resultSet.getBlob(columnIndex);
        }
        if (type == Clob.class) {
            return (T) resultSet.getClob(columnIndex);
        }
        if (type == Ref.class) {
            return (T) resultSet.getRef(columnIndex);
        }
        if (type == RowId.class) {
            return (T) resultSet.getRowId(columnIndex);
        }
        if (type == SQLXML.class) {
            return (T) resultSet.getSQLXML(columnIndex);
        }
        if (type == URL.class) {
            return (T) resultSet.getURL(columnIndex);
        }
        throw new SQLFeatureNotSupportedException(
                String.format("resultSet=%s, columnIndex=%,d, type=%s", resultSet, columnIndex, type));
    }
}
 
Example 20
Source File: SqlUtils.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Returns a string set for the given result set and column. Assumes
 * that the SQL type is an array of text values.
 *
 * @param rs the result set.
 * @param columnLabel the column label.
 * @return a string set.
 */
public static Set<String> getArrayAsSet( ResultSet rs, String columnLabel )
    throws SQLException
{
    Array sqlArray = rs.getArray( columnLabel );
    String[] array = (String[]) sqlArray.getArray();
    return Sets.newHashSet( array );
}