Skip to content

Commit

Permalink
fix: issue with reused select cqns (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-vogel authored Mar 6, 2024
1 parent e2bbd87 commit 916d175
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
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

0 comments on commit 916d175

Please sign in to comment.