Skip to content

Commit

Permalink
blob support for db and dao apis
Browse files Browse the repository at this point in the history
  • Loading branch information
delchev committed Feb 2, 2025
1 parent 979d7df commit bc2f6b6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -776,4 +779,57 @@ public static SqlFactory getNative(Connection connection) throws SQLException {
return SqlFactory.getNative(connection);
}

/**
* Read blob value.
*
* @param resultSet the result set
* @param index the index
* @return the byte[]
*/
public static byte[] readBlobValue(ResultSet resultSet, Integer index) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream input;
try {
input = resultSet.getBinaryStream(index);
readByteStream(baos, input);
} catch (Exception e) {
logger.error("Failed to retreive a BLOB value of [{}].", index, e);
}
return baos.toByteArray();
}

/**
* Read blob value.
*
* @param resultSet the result set
* @param column the column
* @return the byte[]
*/
public static byte[] readBlobValue(ResultSet resultSet, String column) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream input;
try {
input = resultSet.getBinaryStream(column);
readByteStream(baos, input);
} catch (Exception e) {
logger.error("Failed to retreive a BLOB value of [{}].", column, e);
}
return baos.toByteArray();
}

/**
* Read byte stream.
*
* @param baos the baos
* @param input the input
* @throws IOException Signals that an I/O exception has occurred.
*/
public static void readByteStream(ByteArrayOutputStream baos, InputStream input) throws IOException {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buffer)) > 0) {
baos.write(buffer, 0, bytesRead);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1127,12 +1127,12 @@ export class CallableStatement {
}

public getBlob(parameterIndex: number): any /*: sql.Blob*/ {
const data = readBlobValue(this.native.getBlob(parameterIndex));
const data = DatabaseFacade.readBlobValue(this.native, parameterIndex);
return Bytes.toJavaScriptBytes(data);
}

public getBlobNative(parameterIndex: number): any /*: sql.Blob*/ {
return readBlobValue(this.native.getBlob(parameterIndex));
return DatabaseFacade.readBlobValue(this.native, parameterIndex);
}

public getClob(parameterIndex: number): any /*: sql.Clob*/ {
Expand Down Expand Up @@ -1425,12 +1425,12 @@ export class ResultSet {
};

public getBlob(identifier: number | string): any /*: sql.Blob*/ {
const data = readBlobValue(this.native.getBlob(identifier));
const data = DatabaseFacade.readBlobValue(this.native, identifier);
return Bytes.toJavaScriptBytes(data);
}

public getBlobNative(identifier: number | string): any /*: sql.Blob*/ {
return readBlobValue(this.native.getBlob(identifier));
return DatabaseFacade.readBlobValue(this.native, identifier);
}

public getClob(identifier: number | string): any /*: sql.Clob*/ {
Expand Down Expand Up @@ -1522,10 +1522,6 @@ function isHanaDatabase(connection) {
return isHanaDatabase;
}

function readBlobValue(value) {
return value ? value.getBytes(1, value.length()) : value;
}

function createBlobValue(native, value) {
try {
let connection = native.getConnection(); // intentionally not closed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
Expand Down Expand Up @@ -147,6 +148,10 @@ public void write(ResultSet resultSet, OutputStream output) throws Exception {
jsonGenerator.writeNumber((Short) value);
} else if (value instanceof Boolean) {
jsonGenerator.writeBoolean((Boolean) value);
} else if (value instanceof Blob) {
Blob blob = (Blob) value;
int[] intArray = readBlob(blob);
jsonGenerator.writeArray(intArray, 0, intArray.length - 1);
} else {
jsonGenerator.writeString(value == null ? null : value.toString());
}
Expand All @@ -163,4 +168,20 @@ public void write(ResultSet resultSet, OutputStream output) throws Exception {
jsonGenerator.flush();
}

/**
* Read blob.
*
* @param blob the blob
* @return the int[]
* @throws SQLException the SQL exception
*/
private int[] readBlob(Blob blob) throws SQLException {
int blobLength = (int) blob.length();
byte[] blobAsBytes = blob.getBytes(1, blobLength);
blob.free();
int[] intArray = new int[blobLength];
for (int j = 0; j < blobAsBytes.length; intArray[j] = blobAsBytes[j++]);
return intArray;
}

}

0 comments on commit bc2f6b6

Please sign in to comment.