diff --git a/db-service/lib/cqn2sql.js b/db-service/lib/cqn2sql.js index 8c94eb15b..4225eeecb 100644 --- a/db-service/lib/cqn2sql.js +++ b/db-service/lib/cqn2sql.js @@ -8,7 +8,7 @@ const { Readable } = require('stream') const DEBUG = cds.debug('sql|sqlite') const LOG_SQL = cds.log('sql') -const LOG_SQLITE = cds.log('sqlite') +const LOG_SQLITE = cds.log('sqlite') class CQN2SQLRenderer { /** @@ -25,7 +25,7 @@ class CQN2SQLRenderer { if (cds.env.sql.names === 'quoted') { this.class.prototype.name = (name, query) => { const e = name.id || name - return (query?.target || this.model?.definitions[e])?.['@cds.persistence.name'] || e + return (query?.target || this.model?.definitions[e])?.['@cds.persistence.name'] || e } this.class.prototype.quote = (s) => `"${String(s).replace(/"/g, '""')}"` } @@ -86,7 +86,7 @@ class CQN2SQLRenderer { if (vars && Object.keys(vars).length && !this.values?.length) this.values = vars const sanitize_values = process.env.NODE_ENV === 'production' && cds.env.log.sanitize_values !== false - + if (DEBUG && (LOG_SQL._debug || LOG_SQLITE._debug)) { let values = sanitize_values && (this.entries || this.values?.length > 0) ? ['***'] : this.entries || this.values || [] if (values && !Array.isArray(values)) { @@ -95,7 +95,7 @@ class CQN2SQLRenderer { DEBUG(this.sql, ...values) } - + return this } @@ -528,9 +528,6 @@ class CQN2SQLRenderer { async *INSERT_entries_stream(entries, binaryEncoding = 'base64') { const elements = this.cqn.target?.elements || {} - const transformBase64 = binaryEncoding === 'base64' - ? a => a - : a => a != null ? Buffer.from(a, 'base64').toString(binaryEncoding) : a const bufferLimit = 65536 // 1 << 16 let buffer = '[' @@ -561,8 +558,8 @@ class CQN2SQLRenderer { buffer += '"' } else { - if (elements[key]?.type in this.BINARY_TYPES) { - val = transformBase64(val) + if (val != null && elements[key]?.type in this.BINARY_TYPES) { + val = Buffer.from(val, 'base64').toString(binaryEncoding) } buffer += `${keyJSON}${JSON.stringify(val)}` } @@ -1149,11 +1146,6 @@ class CQN2SQLRenderer { } } -// REVISIT: Workaround for JSON.stringify to work with buffers -Buffer.prototype.toJSON = function () { - return this.toString('base64') -} - Readable.prototype[require('node:util').inspect.custom] = Readable.prototype.toJSON = function () { return this._raw || `[object ${this.constructor.name}]` } const ObjectKeys = o => (o && [...ObjectKeys(o.__proto__), ...Object.keys(o)]) || []