Skip to content

Commit

Permalink
Merge pull request #154 from paustint/bug-153
Browse files Browse the repository at this point in the history
parseQuery fails when bind variable contains parenthesis #153
  • Loading branch information
paustint authored Jun 6, 2021
2 parents d881d41 + cfdf366 commit bb973f7
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 265 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## 4.1.0

June 6, 2021

#153 - A new parser option has been added named `ignoreParseErrors`, which will remove invalid parts of a query if there are parsing errors.

The general structure of the query must be valid and the `SELECT` and `WHERE` clauses must both be valid, but any other clause may be removed from the parsed output if there are errors parsing the query and `ignoreParseErrors` is set to `true`.

This option has been added to the documentation application.

## 4.0.0

April 13, 20201
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
This library uses [Chevrotain](https://github.com/SAP/chevrotain) to parse queries. Prior to version 2.0.0, [antlr4](https://github.com/antlr/antlr4) was used.

Migrating from version 1 to version 2? [Check out the changelog](CHANGELOG.md#200) for a full list of changes.

Migrating from version 2 to version 3? [Check out the changelog](CHANGELOG.md#300) for a full list of changes.

Want to try it out? [Check out the demo](https://paustint.github.io/soql-parser-js/).
Expand Down Expand Up @@ -80,10 +81,11 @@ Many of hte utility functions are provided to easily determine the shape of spec

**ParseQueryConfig**

| Property | Type | Description | required | default |
| ---------------------- | ------- | ---------------------------------------------------------------------------------------------------- | -------- | ------- |
| allowApexBindVariables | boolean | Determines if apex variables are allowed in parsed query. Example: `WHERE Id IN :accountIds`. | FALSE | FALSE |
| logErrors | boolean | If true, then additional detail will be logged to the console if there is a lexing or parsing error. | FALSE | FALSE |
| Property | Type | Description | required | default |
| ---------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | ------- |
| allowApexBindVariables | boolean | Determines if apex variables are allowed in parsed query. Example: `WHERE Id IN :accountIds`. Only simple Apex is supported. Function calls are not supported. (e.x. `accountMap.keyset()` is not supported) | FALSE | FALSE |
| ignoreParseErrors | boolean | If set to true, then queries with partially invalid syntax will still be parsed, but any clauses with invalid parts will be omitted. The SELECT clause and FROM clause must always be valid, but all other clauses can contain invalid parts. | FALSE | FALSE |
| logErrors | boolean | If true, parsing and lexing errors will be logged to the console. | FALSE | FALSE |

**SoqlComposeConfig**

Expand Down
9 changes: 9 additions & 0 deletions docs/src/modules/my/queryParser/queryParser.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ <h4 class="flex justify-between text-md font-bold leading-7 text-gray-100 sm:tru
PARSED QUERY
<ui-copy-to-clipboard value={parsedQueryJson}></ui-copy-to-clipboard>
</h4>
<div class="ml-2">
<ui-checkbox
name="allowApexBindVariables"
value={allowApexBindVariables}
label="Allow apex bind variables"
onchange={handleChange}
></ui-checkbox>
<ui-checkbox name="ignoreParseErrors" value={ignoreParseErrors} label="Ignore parsing errors" onchange={handleChange}></ui-checkbox>
</div>
<div class="mb-1">
<pre><code class="javascript" lwc:dom="manual"></code></pre>
</div>
Expand Down
38 changes: 33 additions & 5 deletions docs/src/modules/my/queryParser/queryParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,25 @@ export default class QueryParser extends LightningElement {
@track parsedQuery: Query;
@track parsedQueryJson: string;
@track composedQuery: string;
@track allowApexBindVariables = false;
@track ignoreParseErrors = false;
hasError = false;
hasRendered = false;

renderedCallback() {
if (!this.hasRendered) {
// @ts-ignore type-mismatch
const element = this.template.querySelector('code.javascript');
element.innerText = `// parseQuery(soqlQuery);`;
hljs.highlightBlock(element);
this.setExampleJs();
this.hasRendered = true;
}
}

parseQuery() {
try {
this.parsedQuery = parseQuery(this._query || '');
this.parsedQuery = parseQuery(this._query || '', {
allowApexBindVariables: this.allowApexBindVariables,
ignoreParseErrors: this.ignoreParseErrors,
logErrors: true
});
this.parsedQueryJson = JSON.stringify(this.parsedQuery, null, 2);
this.hasError = false;
} catch (ex) {
Expand All @@ -47,6 +50,13 @@ export default class QueryParser extends LightningElement {
this.dispatchEvent(new CustomEvent('queryerror', { detail: this.hasError }));
}

setExampleJs() {
// @ts-ignore type-mismatch
const element = this.template.querySelector('code.javascript');
element.innerText = `parseQuery(soqlQuery, { allowApexBindVariables: ${this.allowApexBindVariables}, ignoreParseErrors: ${this.ignoreParseErrors} });`;
hljs.highlightBlock(element);
}

highlight() {
// @ts-ignore type-mismatch
const element = this.template.querySelector('code.json');
Expand All @@ -55,4 +65,22 @@ export default class QueryParser extends LightningElement {
hljs.highlightBlock(element);
}
}

handleChange(event) {
const { name, value } = event.detail;
switch (name) {
case 'allowApexBindVariables': {
this.allowApexBindVariables = value;
break;
}
case 'ignoreParseErrors': {
this.ignoreParseErrors = value;
break;
}
default:
break;
}
this.setExampleJs();
this.parseQuery();
}
}
Loading

0 comments on commit bb973f7

Please sign in to comment.