Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get bytes #35

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/main/java/org/duckdb/DuckDBResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,11 @@ public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException
}

public byte[] getBytes(int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getBytes");
if (check_and_null(columnIndex)) {
return null;
}

return current_chunk[columnIndex - 1].getBytes(chunk_idx - 1);
}

public Date getDate(int columnIndex) throws SQLException {
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/duckdb/DuckDBVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,22 @@ Blob getBlob(int idx) throws SQLException {
throw new SQLFeatureNotSupportedException("getBlob");
}

byte[] getBytes(int idx) throws SQLException {
if (check_and_null(idx)) {
return null;
}

if (isType(DuckDBColumnType.BLOB)) {
ByteBuffer bb = (ByteBuffer) varlen_data[idx];
bb.position(0);
byte [] bytes = new byte[bb.remaining()];
bb.get(bytes);
return bytes;
}

throw new SQLFeatureNotSupportedException("getBytes");
}

JsonNode getJsonObject(int idx) {
if (check_and_null(idx)) {
return null;
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/org/duckdb/TestDuckDBJDBC.java
Original file line number Diff line number Diff line change
Expand Up @@ -4344,6 +4344,36 @@ public static void test_get_binary_stream() throws Exception {
}
}

public static void test_get_bytes() throws Exception {
try(Connection connection = DriverManager.getConnection("jdbc:duckdb:");
PreparedStatement s = connection.prepareStatement("select ?")) {

byte[] allTheBytes = new byte[256];
for(int b = -128; b <= 127; b++) {
allTheBytes[b + 128] = (byte)b;
}

// Test both all the possible bytes and with an empty array.
byte[][] arrays = new byte[][] {allTheBytes, {}};

for(byte [] array : arrays ) {
s.setBytes(1, array);

int rowsReturned = 0;
try (ResultSet rs = s.executeQuery()) {
assertTrue(rs instanceof DuckDBResultSet);
while (rs.next()) {
rowsReturned++;
byte[] result = rs.getBytes(1);
assertEquals(array, result, "Bytes were not the same after round trip.");
}
}
assertEquals(1, rowsReturned, "Got unexpected number of rows back.");
}
}

}

public static void test_fractional_time() throws Exception {
try (Connection conn = DriverManager.getConnection(JDBC_URL);
PreparedStatement stmt = conn.prepareStatement("SELECT '01:02:03.123'::TIME");
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/duckdb/test/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.duckdb.test.Thrower;

import java.util.Arrays;
import java.util.Objects;
import java.util.function.Function;

Expand All @@ -23,15 +24,21 @@ public static void assertFalse(boolean val) throws Exception {
public static void assertEquals(Object actual, Object expected) throws Exception {
assertEquals(actual, expected, "");
}

public static void assertEquals(Object actual, Object expected, String label) throws Exception {
Function<Object, String> getClass = (Object a) -> a == null ? "null" : a.getClass().toString();

String message = label.isEmpty() ? "" : label + ": ";
message += String.format("\"%s\" (of %s) should equal \"%s\" (of %s)", actual, getClass.apply(actual),
expected, getClass.apply(expected));
// Note this will fail for arrays, which do not implement .equals and so fall back to reference equality checks.
assertTrue(Objects.equals(actual, expected), message);
}

public static void assertEquals(byte [] actual, byte [] expected, String message) throws Exception {
assertTrue(Arrays.equals(actual, expected), message);
}

public static void assertNotNull(Object a) throws Exception {
assertFalse(a == null);
}
Expand Down
Loading