Skip to content

Commit

Permalink
fix(hana): improve search inside where clause detection (#538)
Browse files Browse the repository at this point in the history
Improved comparison injection for `HANA` Service. Now when `and` / `or`
are found inside the `where` expression before any other comparator it
will add `= true` to the end of the expression.

```
( x=y or z) -- has to always be compared inside the expression
( x=y or z=true)
(z) -- can be checked outside of the expression
(z)=true / (z=true)
```

---------

Co-authored-by: Johannes Vogel <31311694+johannes-vogel@users.noreply.github.com>
  • Loading branch information
BobdenOs and johannes-vogel authored Mar 20, 2024
1 parent dfd2f0c commit 51b8af3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion hana/lib/HANAService.js
Original file line number Diff line number Diff line change
Expand Up @@ -830,8 +830,11 @@ class HANAService extends SQLService {
if (cur == null) continue
if (typeof cur === 'string') {
const up = cur.toUpperCase()
// When a logic operator is found the expression is not a comparison
// When it is a local check it cannot be compared outside of the xpr
if (up in logicOperators) return !local
// When a compare operator is found the expression is a comparison
if (up in compareOperators || (!local && up in logicOperators)) return true
if (up in compareOperators) return true
// When a case operator is found it is the start of the expression
if (up in caseOperators) break
continue
Expand Down
10 changes: 10 additions & 0 deletions test/scenarios/bookshop/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ describe('Bookshop - Read', () => {
expect(res.data.value[1].descr).to.include('e r')
})

test('Search book with filter', async () => {
const res = await GET('/admin/Books?$search="e R"&$filter=ID eq 251 or ID eq 271', admin)
expect(res.status).to.be.eq(200)
expect(res.data.value.length).to.be.eq(2)
expect(res.data.value[0].title).to.be.eq('The Raven')
expect(res.data.value[1].descr).to.include('e r')
expect(res.data.value[0].ID).to.be.eq(251)
expect(res.data.value[1].ID).to.be.eq(271)
})

test.skip('Expand Book($count,$top,$orderby)', async () => {
// REVISIT: requires changes in @sap/cds to allow $count inside expands
const res = await GET(
Expand Down

0 comments on commit 51b8af3

Please sign in to comment.