Skip to content

Commit

Permalink
feat(action-impl): sql action can read blob type
Browse files Browse the repository at this point in the history
  • Loading branch information
rbenyoussef committed Nov 15, 2024
1 parent d3db2ec commit edf5032
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

import com.chutneytesting.tools.NotEnoughMemoryException;
import com.zaxxer.hikari.HikariDataSource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
Expand Down Expand Up @@ -136,6 +140,9 @@ private static Object boxed(ResultSet rs, int i) throws SQLException {
if (isPrimitiveOrWrapper(type) || isJDBCNumericType(type) || isJDBCDateType(type)) {
return o;
}
if (o instanceof Blob) {
return new String(readBlob((Blob) o));
}

return Optional.ofNullable(rs.getString(i)).orElse("null");
}
Expand All @@ -160,5 +167,18 @@ private static boolean isJDBCDateType(Class<?> type) {
type.equals(Duration.class); // INTERVAL
}

private static byte[] readBlob(Blob blob) throws SQLException {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); InputStream inputStream = blob.getBinaryStream()) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
return outputStream.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

}
}

0 comments on commit edf5032

Please sign in to comment.