Skip to content

Commit

Permalink
fix: select without columns from unknown entity (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobdenOs authored Feb 16, 2024
1 parent fbd552f commit eb857de
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
6 changes: 5 additions & 1 deletion db-service/lib/SQLService.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ class SQLService extends DatabaseService {
* @type {Handler}
*/
async onSELECT({ query, data }) {
query.SELECT.expand = 'root'
if (query.target && !query.target._unresolved) {
// Will return multiple rows with objects inside
query.SELECT.expand = 'root'
}

const { sql, values, cqn } = this.cqn2sql(query, data)
let ps = await this.prepare(sql)
let rows = await ps.all(values)
Expand Down
31 changes: 18 additions & 13 deletions hana/lib/HANAService.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ class HANAService extends SQLService {
this.ensureDBC().set(variables)
}

async onSELECT({ query, data }) {
async onSELECT(req) {
const { query, data } = req
if (!query.target || query.target._unresolved) {
return super.onSELECT(req)
}

// REVISIT: disable this for queries like (SELECT 1)
// Will return multiple rows with objects inside
query.SELECT.expand = 'root'
Expand Down Expand Up @@ -249,7 +254,7 @@ class HANAService extends SQLService {
this.temporary = this.temporary || []
this.temporaryValues = this.temporaryValues || []

const { limit, one, orderBy, expand, columns, localized, count, parent } = q.SELECT
const { limit, one, orderBy, expand, columns = ['*'], localized, count, parent } = q.SELECT

const walkAlias = q => {
if (q.args) return q.as || walkAlias(q.args[0])
Expand Down Expand Up @@ -795,17 +800,17 @@ class HANAService extends SQLService {
return false
}

list(list) {
const first = list.list[0]
// If the list only contains of lists it is replaced with a json function and a placeholder
if (this.values && first.list && !first.list.find(v => !v.val)) {
const extraction = first.list.map((v, i) => `"${i}" ${this.constructor.InsertTypeMap[typeof v.val]()} PATH '$.V${i}'`)
this.values.push(JSON.stringify(list.list.map(l => l.list.reduce((l, c, i) => { l[`V${i}`] = c.val; return l }, {}))))
return `(SELECT * FROM JSON_TABLE(?, '$' COLUMNS(${extraction})))`
}
// Call super for normal SQL behavior
return super.list(list)
}
list(list) {
const first = list.list[0]
// If the list only contains of lists it is replaced with a json function and a placeholder
if (this.values && first.list && !first.list.find(v => !v.val)) {
const extraction = first.list.map((v, i) => `"${i}" ${this.constructor.InsertTypeMap[typeof v.val]()} PATH '$.V${i}'`)
this.values.push(JSON.stringify(list.list.map(l => l.list.reduce((l, c, i) => { l[`V${i}`] = c.val; return l }, {}))))
return `(SELECT * FROM JSON_TABLE(?, '$' COLUMNS(${extraction})))`
}
// Call super for normal SQL behavior
return super.list(list)
}

quote(s) {
// REVISIT: casing in quotes when reading from entities it uppercase
Expand Down
8 changes: 6 additions & 2 deletions test/scenarios/bookshop/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('Bookshop - Update', () => {
expect(update.data.footnotes).to.be.eql(['one'])
})

test('programmatic insert/upsert/update/delete with unknown entity', async () => {
test('programmatic insert/upsert/update/select/delete with unknown entity', async () => {
const books = 'sap_capire_bookshop_Books'
const ID = 999
let affectedRows = await INSERT.into(books)
Expand Down Expand Up @@ -96,7 +96,11 @@ describe('Bookshop - Update', () => {
createdAt: (new Date()).toISOString(),
})
await expect(affectedRows).rejected


const result = await SELECT.from(books)
.where({ ID })
expect(result.length).to.be.eq(1)

affectedRows = await DELETE(books)
.where({ ID })
expect(affectedRows | 0).to.be.eq(1)
Expand Down

0 comments on commit eb857de

Please sign in to comment.