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

feat: SELECT returns binaries as Buffers #416

Merged
merged 7 commits into from
Jan 18, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions sqlite/lib/SQLiteService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ const $session = Symbol('dbc.session')
const convStrm = require('stream/consumers')
const { Readable } = require('stream')

const BINARY_TYPES = {
'cds.Binary': 1,
'cds.hana.BINARY': 1
}

class SQLiteService extends SQLService {
init() {
return super.init(...arguments)
Expand Down Expand Up @@ -137,6 +142,38 @@ class SQLiteService extends SQLService {
return (await ps.run(vals)).changes
}

// change each binary to Buffer except of large binaries
_changeToBuffers(columns, rows, one, compat) {
if (!rows || !columns) return
if (!Array.isArray(rows)) rows = [rows]
if (!rows.length || !Object.keys(rows[0]).length) return
if (compat) return

for (let col of columns) {
const name = col.as || col.ref?.[col.ref.length - 1] || (typeof col === 'string' && col)
if (col.element?.isAssociation) {
if (one) this._changeToBuffers(col.SELECT.columns, rows[0][name], false, compat)
else
rows.forEach(row => {
this._changeToBuffers(col.SELECT.columns, row[name], false, compat)
})
} else if (col.element?.type in BINARY_TYPES) {
if (one) rows[0][name] = Buffer.from(rows[0][name], 'base64')
else
rows.forEach(row => {
row[name] = Buffer.from(row[name], 'base64')
})
}
}
}

async onSELECT(req) {
const rows = await super.onSELECT(req)
this._changeToBuffers(req.query.SELECT.columns, rows, req.query.SELECT.one, cds.env.features.stream_compat)

return rows
}

onPlainSQL({ query, data }, next) {
if (typeof query === 'string') {
// REVISIT: this is a hack the target of $now might not be a timestamp or date time
Expand Down
Loading