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

fix: Binary columns now return as Buffer for HANAService #689

Merged
merged 8 commits into from
Jun 17, 2024
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 @@
? null
: (
row[col].createReadStream?.()
|| Readable.from(echoStream(row[col]), { objectMode: false })
|| row[col]
)
: row[col]
}
Expand Down Expand Up @@ -169,7 +169,7 @@
}
}

function* echoStream(ret) {

Check warning on line 172 in hana/lib/drivers/hdb.js

View workflow job for this annotation

GitHub Actions / Node.js 18

'echoStream' is defined but never used

Check warning on line 172 in hana/lib/drivers/hdb.js

View workflow job for this annotation

GitHub Actions / HANA Node.js 18

'echoStream' is defined but never used
yield ret
}

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