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

refactor: hana like search for escaped quotes #978

Merged
merged 9 commits into from
Jan 20, 2025
13 changes: 10 additions & 3 deletions hana/lib/cql-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,16 @@ const StandardFunctions = {
if (Array.isArray(arg)) arg = [{ val: arg.filter(a => a.val).map(a => a.val).join(' ') }]
else arg = [arg]
const searchTerms = arg[0].val
.match(/("")|("(?:[^"]|\\")*(?:[^\\]|\\\\)")|(\S*)/g)
.filter(el => el.length).map(el => `%${el.replace(/^\"|\"$/g, '').toLowerCase()}%`)

.match(/("")|("(?:[^"]|\\")*(?:[^\\]|\\\\)")|(\S*)/g)
.filter(el => el.length)
.map(el => {
try {
return `%${JSON.parse(el).toLowerCase()}%`
} catch {
return `%${el.toLowerCase()}%`
}
})

const columns = ref.list
const xpr = []
for (const s of searchTerms) {
Expand Down
15 changes: 14 additions & 1 deletion hana/test/fuzzy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('search', () => {
const res = await cqn
expect(res.length).to.be(2) // Eleonora and Jane Eyre
})

test('fallback - 2 search terms', async () => {
const { Books } = cds.entities('sap.capire.bookshop')
const cqn = SELECT.from(Books).search('"autobio"', '"Jane"').columns('1')
Expand All @@ -74,5 +74,18 @@ describe('search', () => {
const res = await cqn
expect(res.length).to.be(1) // Jane Eyre
})

test('fallback - 3 search terms with special characters', async () => {
const { Books } = cds.entities('sap.capire.bookshop')
const cqn = SELECT.from(Books).search('"1847"', '1846', '"\\"Ellis Bell\\""').columns('1')
const { sql, values } = cqn.toSQL()
// 5 columns to be searched createdBy, modifiedBy, title, descr, currency_code
expect(sql.match(/(like)/g).length).to.be(15)
expect(values).to.include('%1847%')
expect(values).to.include('%1846%')
expect(values).to.include('%"ellis bell"%')
const res = await cqn
expect(res.length).to.be(1) // Emily Brontë
})
})
})
Loading