Skip to content

Commit

Permalink
fix: Binary columns now return as Buffer for HANAService (#689)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobdenOs authored Jun 17, 2024
1 parent 0ed39ed commit 179bd92
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 14 deletions.
8 changes: 2 additions & 6 deletions hana/lib/HANAService.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class HANAService extends SQLService {

// REVISIT: add prepare options when param:true is used
const sqlScript = isLockQuery || isSimple ? sql : this.wrapTemporary(temporary, withclause, blobs)
let rows
let rows
if (values?.length || blobs.length > 0) {
const ps = await this.prepare(sqlScript, blobs.length)
rows = this.ensureDBC() && await ps.all(values || [])
Expand Down Expand Up @@ -233,7 +233,7 @@ class HANAService extends SQLService {
const expands = JSON.parse(row._expands_)
const blobs = JSON.parse(row._blobs_)
const data = Object.assign(JSON.parse(row._json_), expands, blobs)
Object.keys(blobs).forEach(k => (data[k] = this._stream(row[k] || data[k])))
Object.keys(blobs).forEach(k => (data[k] = row[k] || data[k]))

// REVISIT: try to unify with handleLevel from base driver used for streaming
while (levels.length) {
Expand Down Expand Up @@ -1282,10 +1282,6 @@ class HANAService extends SQLService {
const createContainerDatabase = fs.readFileSync(path.resolve(__dirname, 'scripts/container-database.sql'), 'utf-8')
const createContainerTenant = fs.readFileSync(path.resolve(__dirname, 'scripts/container-tenant.sql'), 'utf-8')

Buffer.prototype.toJSON = function () {
return this.toString('hex')
}

function _not_unique(err, code) {
if (err.code === 301)
return Object.assign(err, {
Expand Down
10 changes: 6 additions & 4 deletions hana/lib/drivers/hana-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,20 @@ class HANAClientDriver extends driver {
const result = []
// Fetch the next row
while (await next()) {
const cols = stmt.getColumnInfo().map(b => b.columnName)
const cols = stmt.getColumnInfo()
// column 0-3 are metadata columns
const values = await Promise.all([getValue(0), getValue(1), getValue(2), getValue(3)])

const row = {}
for (let i = 0; i < cols.length; i++) {
const col = cols[i]
// column >3 are all blob columns
row[col] = i > 3 ?
row[col.columnName] = i > 3 ?
rs.isNull(i)
? null
: Readable.from(streamBlob(rsStreams, rs._rowPosition, i), { objectMode: false })
: col.nativeType === 13 // return binary type as simple buffer
? await getValue(i)
: Readable.from(streamBlob(rsStreams, rs._rowPosition, i), { objectMode: false })
: values[i]
}

Expand Down Expand Up @@ -366,7 +368,7 @@ async function* streamBlob(rs, rowIndex = -1, columnIndex, binaryBuffer = Buffer
const read = await getData(columnIndex, blobPosition, binaryBuffer, 0, binaryBuffer.byteLength)
blobPosition += read
if (read < binaryBuffer.byteLength) {
yield binaryBuffer.slice(0, read)
yield binaryBuffer.subarray(0, read)
break
}
yield binaryBuffer
Expand Down
2 changes: 1 addition & 1 deletion hana/lib/drivers/hdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class HDBDriver extends driver {
? null
: (
row[col].createReadStream?.()
|| Readable.from(echoStream(row[col]), { objectMode: false })
|| row[col]
)
: row[col]
}
Expand Down
4 changes: 1 addition & 3 deletions test/compliance/CREATE.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,8 @@ describe('CREATE', () => {

await Promise.all(Object.keys(expect).map(async k => {
const msg = `Ensure that the Database echos correct data back, property ${k} does not match expected result.`
if (result[k] instanceof Readable) {
if (result[k] instanceof Readable && expect[k] instanceof Readable) {
result[k] = await buffer(result[k])
}
if (expect[k] instanceof Readable) {
expect[k] = await buffer(expect[k])
}
if (result[k] instanceof Buffer && expect[k] instanceof Buffer) {
Expand Down

0 comments on commit 179bd92

Please sign in to comment.