Skip to content

Commit

Permalink
feat: also support lowercase matchespattern function (#528)
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-vogel authored Mar 20, 2024
1 parent e95fd17 commit 6ea574e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
7 changes: 7 additions & 0 deletions db-service/lib/cql-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ const StandardFunctions = {
* @returns {string}
*/
matchesPattern: (x, y) => `(${x} regexp ${y})`,
/**
* Generates SQL statement that matches the given string against a regular expression
* @param {string} x
* @param {string} y
* @returns {string}
*/
matchespattern: (x, y) => `(${x} regexp ${y})`,
/**
* Generates SQL statement that produces the lower case value of a given string
* @param {string} x
Expand Down
2 changes: 2 additions & 0 deletions hana/lib/cql-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const StandardFunctions = {
indexof: (x, y) => `locate(${x},${y}) - 1`, // locate is 1 indexed
startswith: (x, y) => `(CASE WHEN locate(${x},${y}) = 1 THEN TRUE ELSE FALSE END)`, // locate is 1 indexed
endswith: (x, y) => `(CASE WHEN substring(${x},length(${x})+1 - length(${y})) = ${y} THEN TRUE ELSE FALSE END)`,
matchesPattern: (x, y) => `(CASE WHEN ${x} LIKE_REGEXPR ${y} THEN TRUE ELSE FALSE END)`,
matchespattern: (x, y) => `(CASE WHEN ${x} LIKE_REGEXPR ${y} THEN TRUE ELSE FALSE END)`,
substring: (x, y, z) =>
z
? `substring( ${x}, case when ${y} < 0 then length(${x}) + ${y} + 1 else ${y} + 1 end, ${z} )`
Expand Down
2 changes: 2 additions & 0 deletions postgres/lib/func.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const StandardFunctions = {
indexof: (x, y) => `strpos(${x},${y}) - 1`, // sqlite instr is 1 indexed
startswith: (x, y) => `strpos(${x},${y}) = 1`, // sqlite instr is 1 indexed
endswith: (x, y) => `substr(${x},length(${x}) + 1 - length(${y})) = ${y}`,
matchesPattern: (x, y) => `regexp_like(${x}, ${y})`,
matchespattern: (x, y) => `regexp_like(${x}, ${y})`,

// Date and Time Functions
year: x => `date_part('year', ${castVal(x)})`,
Expand Down
20 changes: 14 additions & 6 deletions test/scenarios/bookshop/funcs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,20 @@ describe('Bookshop - Functions', () => {
expect(negative.data.value.length).to.be.eq(2)
})

test.skip('matchesPattern', async () => {
// REVISIT: ERROR: Property 'matchesPattern' does not exist in type 'CatalogService.Books'
const res = await GET(`/browse/Books?$filter=matchesPattern(author,'^A.*e$')`)

expect(res.status).to.be.eq(200)
expect(res.data.value.length).to.be.eq(2)
test('matchesPattern', async () => {
// We use QL API as the OData adapter does not yet support matchesPattern
const res1 = await SELECT.from('CatalogService.Books')
.columns('author', 'title')
.where`matchesPattern(author,${'^Ed'})`

// function is case insensitive
const res2 = await SELECT.from('CatalogService.Books')
.columns('author', 'title')
.where`matchespattern(author,${'^Ed'})`

expect(res1.length).to.eq(res2.length).to.be.eq(2)
expect(res1).to.deep.eq(res2).to.deep.include({author: 'Edgar Allen Poe', title: 'Eleonora'})
expect(res1).to.deep.eq(res2).to.deep.include({author: 'Edgar Allen Poe', title: 'The Raven'})
})

test('tolower', async () => {
Expand Down

0 comments on commit 6ea574e

Please sign in to comment.