下面列出了java.sql.ResultSet#getArray ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
@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();
}
@Override
public UUID[] getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
Array outputArray = rs.getArray(columnIndex);
if (outputArray == null) {
return null;
}
return (UUID[])outputArray.getArray();
}
@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;
}
@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();
}
}
@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);
}
@Override
public Integer[] getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
Array outputArray = rs.getArray(columnIndex);
if (outputArray == null) {
return null;
}
return (Integer[])outputArray.getArray();
}
@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;
}
@Override
public Long[] getNullableResult(ResultSet rs, String columnName)
throws SQLException {
Array outputArray = rs.getArray(columnName);
if (outputArray == null) {
return null;
}
return (Long[])outputArray.getArray();
}
@Override
public Object getObject(ResultSet rs) throws SQLException {
var array = rs.getArray(name);
if(array != null) {
return Arrays.asList((Object[]) array.getArray());
}
return null;
}
@Override
public Integer[] getNullableResult(ResultSet rs, String columnName)
throws SQLException {
Array outputArray = rs.getArray(columnName);
if (outputArray == null) {
return null;
}
return (Integer[])outputArray.getArray();
}
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()));
}
/**
* 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));
}
}
@Override
public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Array array = rs.getArray(columnIndex);
return array == null ? null : array.getArray();
}
@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();
}
}
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);
}
}
}
@Override
public java.sql.Array getValue(ResultSet rs, int columnIndex) throws SQLException {
return rs.getArray(columnIndex);
}
@Override
public Object getDbValue(ResultSet resultSet) throws SQLException {
Array fieldVal = resultSet.getArray(fieldSQL);
return resultSet.wasNull() ? null : fieldVal;
}
/**
* 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));
}
}
/**
* 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 );
}