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: Ensure that Iterator and Readable work correctly for INSERT #844

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 16 additions & 14 deletions db-service/lib/cqn2sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,13 +520,14 @@ class CQN2SQLRenderer {
// Include this.values for placeholders
/** @type {unknown[][]} */
this.entries = []
if (INSERT.entries[0] instanceof Readable) {
if (INSERT.entries[0] instanceof Readable && !INSERT.entries[0].readableObjectMode) {
INSERT.entries[0].type = 'json'
this.entries = [[...this.values, INSERT.entries[0]]]
} else {
const stream = Readable.from(this.INSERT_entries_stream(INSERT.entries), { objectMode: false })
const entries = INSERT.entries[0] instanceof Iterator || INSERT.entries[0] instanceof Readable ? INSERT.entries[0] : INSERT.entries
const stream = Readable.from(this.INSERT_entries_stream(entries), { objectMode: false })
stream.type = 'json'
stream._raw = INSERT.entries
stream._raw = entries
this.entries = [[...this.values, stream]]
}

Expand All @@ -536,14 +537,11 @@ 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 = '['

let sep = ''
for (const row of entries) {
for await (const row of entries) {
buffer += `${sep}{`
if (!sep) sep = ','

Expand All @@ -570,9 +568,13 @@ class CQN2SQLRenderer {
buffer += '"'
} else {
if (elements[key]?.type in BINARY_TYPES) {
val = transformBase64(val)
}
buffer += `${keyJSON}${val != null
? `"${Buffer.from(val, 'base64').toString(binaryEncoding)}"`
: 'null'
}`
} else {
buffer += `${keyJSON}${JSON.stringify(val)}`
}
}
}
buffer += '}'
Expand All @@ -588,9 +590,6 @@ class CQN2SQLRenderer {

async *INSERT_rows_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 = '['

Expand Down Expand Up @@ -618,9 +617,12 @@ class CQN2SQLRenderer {
buffer += '"'
} else {
if (elements[this.columns[key]]?.type in BINARY_TYPES) {
val = transformBase64(val)
buffer += val != null
? `"${Buffer.from(val, 'base64').toString(binaryEncoding)}"`
: 'null'
} else {
buffer += `${sepsub}${val == null ? 'null' : JSON.stringify(val)}`
}
buffer += `${sepsub}${val === undefined ? 'null' : JSON.stringify(val)}`
}

if (!sepsub) sepsub = ','
Expand Down
61 changes: 58 additions & 3 deletions test/compliance/INSERT.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,70 @@
require('../cds')
const cds = require('../cds')

const { Readable } = require('stream')

describe('INSERT', () => {
const { data, expect } = cds.test(__dirname + '/resources')
data.autoIsolation(true)
data.autoReset()

describe('into', () => {
test.skip('missing', () => {
throw new Error('not supported')
})
})

describe('entries', () => {
test.skip('missing', () => {
throw new Error('not supported')
const genCount = 100
const gen = function* () {
for (let i = 0; i < genCount; i++)
yield { uuid: cds.utils.uuid() }
}

test('array', async () => {
const { uuid } = cds.entities('basic.literals')

await INSERT([...gen()]).into(uuid)

const result = await SELECT.from(uuid)
expect(result.length).to.eq(genCount)
})

test('iterator', async () => {
const { uuid } = cds.entities('basic.literals')

await INSERT(gen()).into(uuid)

const result = await SELECT.from(uuid)
expect(result.length).to.eq(genCount)
})

test('Readable (Object Mode)', async () => {
const { uuid } = cds.entities('basic.literals')

await INSERT(Readable.from(gen())).into(uuid)

const result = await SELECT.from(uuid)
expect(result.length).to.eq(genCount)
})

test('Readable (Raw Mode)', async () => {
const { uuid } = cds.entities('basic.literals')

const raw = function* (src) {
yield '['
let sep = ''
for (const obj of src) {
yield sep
yield JSON.stringify(obj)
sep = ','
}
yield ']'
}

await INSERT(Readable.from(raw(gen()), { objectMode: false })).into(uuid)

const result = await SELECT.from(uuid)
expect(result.length).to.eq(genCount)
})
})

Expand Down
Loading