Skip to content

Commit dedf7b7

Browse files
authored
Merge pull request #35 from jghoman/addGetBytes
Add get bytes
2 parents ee09550 + 5dabc0e commit dedf7b7

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

src/main/java/org/duckdb/DuckDBResultSet.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,11 @@ public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException
359359
}
360360

361361
public byte[] getBytes(int columnIndex) throws SQLException {
362-
throw new SQLFeatureNotSupportedException("getBytes");
362+
if (check_and_null(columnIndex)) {
363+
return null;
364+
}
365+
366+
return current_chunk[columnIndex - 1].getBytes(chunk_idx - 1);
363367
}
364368

365369
public Date getDate(int columnIndex) throws SQLException {

src/main/java/org/duckdb/DuckDBVector.java

+16
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,22 @@ Blob getBlob(int idx) throws SQLException {
284284
throw new SQLFeatureNotSupportedException("getBlob");
285285
}
286286

287+
byte[] getBytes(int idx) throws SQLException {
288+
if (check_and_null(idx)) {
289+
return null;
290+
}
291+
292+
if (isType(DuckDBColumnType.BLOB)) {
293+
ByteBuffer bb = (ByteBuffer) varlen_data[idx];
294+
bb.position(0);
295+
byte [] bytes = new byte[bb.remaining()];
296+
bb.get(bytes);
297+
return bytes;
298+
}
299+
300+
throw new SQLFeatureNotSupportedException("getBytes");
301+
}
302+
287303
JsonNode getJsonObject(int idx) {
288304
if (check_and_null(idx)) {
289305
return null;

src/test/java/org/duckdb/TestDuckDBJDBC.java

+30
Original file line numberDiff line numberDiff line change
@@ -4344,6 +4344,36 @@ public static void test_get_binary_stream() throws Exception {
43444344
}
43454345
}
43464346

4347+
public static void test_get_bytes() throws Exception {
4348+
try(Connection connection = DriverManager.getConnection("jdbc:duckdb:");
4349+
PreparedStatement s = connection.prepareStatement("select ?")) {
4350+
4351+
byte[] allTheBytes = new byte[256];
4352+
for(int b = -128; b <= 127; b++) {
4353+
allTheBytes[b + 128] = (byte)b;
4354+
}
4355+
4356+
// Test both all the possible bytes and with an empty array.
4357+
byte[][] arrays = new byte[][] {allTheBytes, {}};
4358+
4359+
for(byte [] array : arrays ) {
4360+
s.setBytes(1, array);
4361+
4362+
int rowsReturned = 0;
4363+
try (ResultSet rs = s.executeQuery()) {
4364+
assertTrue(rs instanceof DuckDBResultSet);
4365+
while (rs.next()) {
4366+
rowsReturned++;
4367+
byte[] result = rs.getBytes(1);
4368+
assertEquals(array, result, "Bytes were not the same after round trip.");
4369+
}
4370+
}
4371+
assertEquals(1, rowsReturned, "Got unexpected number of rows back.");
4372+
}
4373+
}
4374+
4375+
}
4376+
43474377
public static void test_fractional_time() throws Exception {
43484378
try (Connection conn = DriverManager.getConnection(JDBC_URL);
43494379
PreparedStatement stmt = conn.prepareStatement("SELECT '01:02:03.123'::TIME");

src/test/java/org/duckdb/test/Assertions.java

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.duckdb.test.Thrower;
44

5+
import java.util.Arrays;
56
import java.util.Objects;
67
import java.util.function.Function;
78

@@ -23,15 +24,21 @@ public static void assertFalse(boolean val) throws Exception {
2324
public static void assertEquals(Object actual, Object expected) throws Exception {
2425
assertEquals(actual, expected, "");
2526
}
27+
2628
public static void assertEquals(Object actual, Object expected, String label) throws Exception {
2729
Function<Object, String> getClass = (Object a) -> a == null ? "null" : a.getClass().toString();
2830

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

38+
public static void assertEquals(byte [] actual, byte [] expected, String message) throws Exception {
39+
assertTrue(Arrays.equals(actual, expected), message);
40+
}
41+
3542
public static void assertNotNull(Object a) throws Exception {
3643
assertFalse(a == null);
3744
}

0 commit comments

Comments
 (0)