Skip to content

Commit

Permalink
odata-filter: use sql.identifier() in preference to raw() (#1238)
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn authored Oct 30, 2024
1 parent 1833f82 commit ee3cd5a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/data/odata-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const odataFilter = (expr, odataToColumnMap) => {
const lowerName = fn.toLowerCase();
const params = node.value.parameters;
if (extractFunctions.includes(lowerName)) {
return sql`extract(${raw(lowerName)} from ${op(params[0])})`; // eslint-disable-line no-use-before-define
return sql`extract(${sql.identifier([lowerName])} from ${op(params[0])})`; // eslint-disable-line no-use-before-define
} else if (fn === 'now') {
return sql`now()`;
} else {
Expand Down
6 changes: 3 additions & 3 deletions test/unit/data/odata-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ describe('OData filter query transformer', () => {
});

it('should transform date extraction method calls', () => {
odataFilter('2020 eq year(2020-01-01)').should.eql(sql`(${'2020'} is not distinct from extract(year from ${'2020-01-01'}))`);
odataFilter('2020 eq year(__system/submissionDate)').should.eql(sql`(${'2020'} is not distinct from extract(year from ${sql.identifier([ 'submissions', 'createdAt' ])}))`);
odataFilter('2020 eq year(2020-01-01)').should.eql(sql`(${'2020'} is not distinct from extract("year" from ${'2020-01-01'}))`);
odataFilter('2020 eq year(__system/submissionDate)').should.eql(sql`(${'2020'} is not distinct from extract("year" from ${sql.identifier([ 'submissions', 'createdAt' ])}))`);
});

it('should transform now method calls', () => {
odataFilter('2020 eq year(now())').should.eql(sql`(${'2020'} is not distinct from extract(year from now()))`);
odataFilter('2020 eq year(now())').should.eql(sql`(${'2020'} is not distinct from extract("year" from now()))`);
});

it('should reject unparseable expressions', () => {
Expand Down

0 comments on commit ee3cd5a

Please sign in to comment.