Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# apex-parser - Changelog

## Unreleased

- Restrict SOQL `FORMULA()` expressions to `WHERE` clauses; `HAVING FORMULA(...)` now reports a syntax error.

## 5.1.0-beta.1

- Allow functions in `GROUP BY` clause of SOQL queries
Expand Down
18 changes: 15 additions & 3 deletions antlr/BaseApexParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,20 @@ usingScope
: USING SCOPE soqlId;

whereClause
: WHERE logicalExpression;
: WHERE whereLogicalExpression;

whereLogicalExpression
: whereConditionalExpression (SOQLAND whereConditionalExpression)*
| whereConditionalExpression (SOQLOR whereConditionalExpression)*
| NOT whereConditionalExpression;

whereConditionalExpression
: LPAREN whereLogicalExpression RPAREN
| whereFieldExpression;

whereFieldExpression
: fieldExpression
| FORMULA LPAREN StringLiteral RPAREN comparisonOperator value;

logicalExpression
: conditionalExpression (SOQLAND conditionalExpression)*
Expand All @@ -706,8 +719,7 @@ conditionalExpression

fieldExpression
: fieldName comparisonOperator value
| soqlFunction comparisonOperator value
| FORMULA LPAREN StringLiteral RPAREN comparisonOperator value;
| soqlFunction comparisonOperator value;

comparisonOperator
: ASSIGN | NOTEQUAL | LT | GT | LT ASSIGN | GT ASSIGN | LESSANDGREATER | LIKE | IN | NOT IN | INCLUDES | EXCLUDES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,14 @@ void testFormulaFunctionWithEscapedQuotes() {
assertNotNull(context);
assertEquals(0, parserAndCounter.getValue().getNumErrors());
}

@Test
void testFormulaFunctionNotAllowedInHaving() {
Map.Entry<ApexParser, SyntaxErrorCounter> parserAndCounter = createParser(
"SELECT Name FROM Account GROUP BY Name HAVING FORMULA('1+1') = 2"
);
ApexParser.QueryContext context = parserAndCounter.getKey().query();
assertNotNull(context);
assertTrue(parserAndCounter.getValue().getNumErrors() > 0);
}
}
11 changes: 11 additions & 0 deletions npm/test/SOQLParserTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,14 @@ test("testFormulaFunctionWithEscapedQuotes", () => {
expect(context).toBeInstanceOf(QueryContext);
expect(errorCounter.getNumErrors()).toEqual(0);
});

test("testFormulaFunctionNotAllowedInHaving", () => {
const [parser, errorCounter] = createParser(
"SELECT Name FROM Account GROUP BY Name HAVING FORMULA('1+1') = 2"
);

const context = parser.query();

expect(context).toBeInstanceOf(QueryContext);
expect(errorCounter.getNumErrors()).toBeGreaterThan(0);
});