Skip to content

Commit

Permalink
fix: enable cesu8 by default for hdb driver and encode entries st…
Browse files Browse the repository at this point in the history
…reams (#868)

fixes:
https://github.com/cap-js/cds-dbs/blob/da629d98192cf1b196cceb172cb01f4a39acd887/test/compliance/resources/db/basic/literals/basic.literals.string.js#L41

The tests run with `hana-client` inside the pipelines. Tested locally
works as expected.

---------

Co-authored-by: Johannes Vogel <[email protected]>
  • Loading branch information
BobdenOs and johannes-vogel authored Oct 25, 2024
1 parent bfba7f1 commit d85d7e6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
4 changes: 3 additions & 1 deletion hana/lib/HANAService.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class HANAService extends SQLService {
const row = rows[i]
const expands = JSON.parse(row._expands_)
const blobs = JSON.parse(row._blobs_)
const data = Object.assign(JSON.parse(row._json_), expands, blobs)
const data = Object.assign(JSON.parse(row._json_ || '{}'), expands, blobs)
Object.keys(blobs).forEach(k => (data[k] = row[k] || data[k]))

// REVISIT: try to unify with handleLevel from base driver used for streaming
Expand Down Expand Up @@ -667,6 +667,8 @@ class HANAService extends SQLService {

const _stream = entries => {
const stream = Readable.from(this.INSERT_entries_stream(entries, 'hex'), { objectMode: false })
stream.setEncoding('utf-8')
stream.type = 'json'
stream._raw = entries
return stream
}
Expand Down
13 changes: 10 additions & 3 deletions hana/lib/drivers/hdb.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Readable, Stream } = require('stream')
const { Readable, Stream, promises: { pipeline } } = require('stream')
const { StringDecoder } = require('string_decoder')
const { text } = require('stream/consumers')

Expand All @@ -22,7 +22,6 @@ class HDBDriver extends driver {
*/
constructor(creds) {
creds = {
useCesu8: false,
fetchSize: 1 << 16, // V8 default memory page size
...creds,
}
Expand Down Expand Up @@ -138,7 +137,7 @@ class HDBDriver extends driver {
_getResultForProcedure(rows, outParameters) {
// on hdb, rows already contains results for scalar params
const isArray = Array.isArray(rows)
const result = isArray ? {...rows[0]} : {...rows}
const result = isArray ? { ...rows[0] } : { ...rows }

// merge table output params into scalar params
const args = isArray ? rows.slice(1) : []
Expand All @@ -158,6 +157,14 @@ class HDBDriver extends driver {
const streams = []
values = values.map((v, i) => {
if (v instanceof Stream) {
if (this._creds.useCesu8 !== false && v.type === 'json') {
const encode = iconv.encodeStream('cesu8')
v.setEncoding('utf-8')
// hdb will react to the stream error no need to handle it twice
pipeline(v, encode).catch(() => { })
return encode
}

streams[i] = v
const iterator = v[Symbol.asyncIterator]()
return Readable.from(iterator, { objectMode: false })
Expand Down

0 comments on commit d85d7e6

Please sign in to comment.