Skip to content

Commit

Permalink
fix: contains not evaluting to bool (#980)
Browse files Browse the repository at this point in the history
Compare the result of sqlite contains realization using `instr` to 0, to
make it evaluate to a boolean. The same problem does not exist for HANA
or Postgres.
  • Loading branch information
PDT42 authored Jan 15, 2025
1 parent d0c949d commit 760484b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion db-service/lib/cql-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const StandardFunctions = {
* @param {...string} args
* @returns {string}
*/
contains: (...args) => `ifnull(instr(${args}),0)`,
contains: (...args) => `(ifnull(instr(${args}),0) > 0)`,
/**
* Generates SQL statement that produces the number of elements in a given collection
* @param {string} x
Expand Down
24 changes: 24 additions & 0 deletions test/scenarios/bookshop/funcs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,30 @@ describe('Bookshop - Functions', () => {
expect(res.data.value.length).to.be.eq(2)
})

test('contains with search string that can not be found', async () => {
const res = await GET(`/browse/Books?$filter=contains(author,'string that can not be found in any author name')`)
expect(res.status).to.be.eq(200)
expect(res.data.value.length).to.be.eq(0)
})

test('contains with search string null', async () => {
const res = await GET(`/browse/Books?$filter=contains(author,null)`)
expect(res.status).to.be.eq(200)
expect(res.data.value.length).to.be.eq(0)
})

test('contains with explicit equals boolean value', async () => {
const res = await GET("/browse/Books?$filter=contains(author,'Allen') eq true")
expect(res.status).to.be.eq(200)
expect(res.data.value.length).to.be.eq(2)
})

test('contains with explicit not equals boolean value', async () => {
const res = await GET("/browse/Books?$filter=contains(author,'Allen') ne false")
expect(res.status).to.be.eq(200)
expect(res.data.value.length).to.be.eq(2)
})

test('avg', async () => {
const { Books } = cds.entities
const res = await cds.run(CQL`SELECT from ${Books} {
Expand Down

0 comments on commit 760484b

Please sign in to comment.