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: issue with reused select cqns #505

Merged
merged 4 commits into from
Mar 6, 2024
Merged
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
5 changes: 4 additions & 1 deletion db-service/lib/SQLService.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,13 @@ class SQLService extends DatabaseService {
}

const { sql, values, cqn } = this.cqn2sql(query, data)
const expand = query.SELECT.expand
delete query.SELECT.expand

let ps = await this.prepare(sql)
let rows = await ps.all(values)
if (rows.length)
if (cqn.SELECT.expand) rows = rows.map(r => (typeof r._json_ === 'string' ? JSON.parse(r._json_) : r._json_ || r))
if (expand) rows = rows.map(r => (typeof r._json_ === 'string' ? JSON.parse(r._json_) : r._json_ || r))

if (cds.env.features.stream_compat) {
if (query._streaming) {
Expand Down
2 changes: 2 additions & 0 deletions hana/lib/HANAService.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ class HANAService extends SQLService {
// Will return multiple rows with objects inside
query.SELECT.expand = 'root'
const { cqn, temporary, blobs, withclause, values } = this.cqn2sql(query, data)
delete query.SELECT.expand

// REVISIT: add prepare options when param:true is used
const sqlScript = this.wrapTemporary(temporary, withclause, blobs)
let rows = (values?.length || blobs.length > 0)
Expand Down
13 changes: 13 additions & 0 deletions test/scenarios/bookshop/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ describe('Bookshop - Read', () => {
expect(res.length).to.be.eq(2)
})

test('reuse already executed select as subselect', async () => {
let s = SELECT.columns('ID').from('sap.capire.bookshop.Books')
let res = await s

res = await SELECT.one.from('sap.capire.bookshop.Books as b')
.join('sap.capire.bookshop.Authors as a')
.on('a.ID = b.author_ID')
.columns('a.name', 'b.title')
.where('b.ID in', s)
.orderBy('b.ID')
expect(res).to.deep.eq({"name": "Emily Brontë", "title": "Wuthering Heights"})
})

test('Expand Book', async () => {
const res = await GET(
'/admin/Books(252)?$select=title&$expand=author($select=name;$expand=books($select=title))',
Expand Down