diff --git a/hana/lib/cql-functions.js b/hana/lib/cql-functions.js index c90524b9c..a226ea9e5 100644 --- a/hana/lib/cql-functions.js +++ b/hana/lib/cql-functions.js @@ -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) { diff --git a/hana/test/fuzzy.test.js b/hana/test/fuzzy.test.js index a2ae4f103..b7f80aa42 100644 --- a/hana/test/fuzzy.test.js +++ b/hana/test/fuzzy.test.js @@ -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') @@ -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ë + }) }) }) \ No newline at end of file