Skip to content

Commit 4b038ee

Browse files
committed
fixed bug with #4
1 parent 00d7de9 commit 4b038ee

File tree

8 files changed

+92
-18
lines changed

8 files changed

+92
-18
lines changed

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ tsconfig.json
99
SOQL.g4
1010
ISSUE_TEMPLATE
1111
CONTRIBUTING.md
12-
CHANGELOG.md
12+
CHANGELOG.md
13+
debug/

debug/test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var soqlParserJs = require('../dist');
2+
3+
const query = `
4+
SELECT a.Id, a.Name,
5+
(SELECT a2.Id FROM ChildAccounts a2),
6+
(SELECT a1.Id FROM ChildAccounts1 a1)
7+
FROM Account a
8+
`;
9+
10+
const parsedQuery = soqlParserJs.parseQuery(query, { logging: true });
11+
12+
console.log(JSON.stringify(parsedQuery, null, 2));

dist/SoqlListener.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ class Listener {
151151
console.log('enterAlias_name:', ctx.text);
152152
}
153153
if (this.context.currentItem === 'from') {
154-
this.soqlQuery.sObjectAlias = ctx.text;
154+
this.getSoqlQuery().sObjectAlias = ctx.text;
155155
// All fields need to update to remove the alias from relationships
156-
this.soqlQuery.fields.forEach(field => {
157-
if (field.text.startsWith(`${ctx.text}.`)) {
156+
this.getSoqlQuery().fields.forEach(field => {
157+
if (field.text && field.text.startsWith(`${ctx.text}.`)) {
158158
field.alias = ctx.text;
159159
field.text = field.text.replace(`${ctx.text}.`, '');
160160
if (field.relationshipFields.length > 2) {
@@ -743,9 +743,16 @@ class Listener {
743743
}
744744
this.getSoqlQuery().sObject = ctx.getChild(0).text;
745745
if (this.config.includeSubqueryAsField && this.context.isSubQuery) {
746-
this.soqlQuery.fields.push({
747-
subqueryObjName: ctx.text,
748-
});
746+
if (ctx.getChild(0).text.includes('.')) {
747+
this.soqlQuery.fields.push({
748+
subqueryObjName: ctx.text,
749+
});
750+
}
751+
else {
752+
this.soqlQuery.fields.push({
753+
subqueryObjName: ctx.getChild(0).text,
754+
});
755+
}
749756
}
750757
}
751758
exitObject_spec(ctx) {

docs/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"react-copy-to-clipboard": "^5.0.1",
1212
"react-dom": "^16.5.2",
1313
"react-scripts-ts": "3.1.0",
14-
"soql-parser-js": "^0.1.1"
14+
"soql-parser-js": "^0.1.5"
1515
},
1616
"scripts": {
1717
"start": "react-scripts-ts start",

lib/SoqlListener.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ export class Listener implements SOQLListener {
185185
console.log('enterAlias_name:', ctx.text);
186186
}
187187
if (this.context.currentItem === 'from') {
188-
this.soqlQuery.sObjectAlias = ctx.text;
188+
this.getSoqlQuery().sObjectAlias = ctx.text;
189189
// All fields need to update to remove the alias from relationships
190-
this.soqlQuery.fields.forEach(field => {
191-
if (field.text.startsWith(`${ctx.text}.`)) {
190+
this.getSoqlQuery().fields.forEach(field => {
191+
if (field.text && field.text.startsWith(`${ctx.text}.`)) {
192192
field.alias = ctx.text;
193193
field.text = field.text.replace(`${ctx.text}.`, '');
194194
if (field.relationshipFields.length > 2) {
@@ -777,9 +777,15 @@ export class Listener implements SOQLListener {
777777
}
778778
this.getSoqlQuery().sObject = ctx.getChild(0).text;
779779
if (this.config.includeSubqueryAsField && this.context.isSubQuery) {
780-
this.soqlQuery.fields.push({
781-
subqueryObjName: ctx.text,
782-
});
780+
if (ctx.getChild(0).text.includes('.')) {
781+
this.soqlQuery.fields.push({
782+
subqueryObjName: ctx.text,
783+
});
784+
} else {
785+
this.soqlQuery.fields.push({
786+
subqueryObjName: ctx.getChild(0).text,
787+
});
788+
}
783789
}
784790
}
785791
exitObject_spec(ctx: Parser.Object_specContext) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "soql-parser-js",
3-
"version": "0.1.3",
3+
"version": "0.1.5",
44
"description": "Salesforce.com SOQL parser.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

test/TestCases.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,5 +957,53 @@ export const testCases: TestCase[] = [
957957
},
958958
},
959959
},
960+
{
961+
testCase: 34,
962+
soql: `SELECT a.Id, a.Name, (SELECT a2.Id FROM ChildAccounts a2), (SELECT a1.Id FROM ChildAccounts1 a1) FROM Account a`,
963+
output: {
964+
fields: [
965+
{
966+
text: 'Id',
967+
alias: 'a',
968+
},
969+
{
970+
text: 'Name',
971+
alias: 'a',
972+
},
973+
{
974+
subqueryObjName: 'ChildAccounts',
975+
},
976+
{
977+
subqueryObjName: 'ChildAccounts1',
978+
},
979+
],
980+
subqueries: [
981+
{
982+
fields: [
983+
{
984+
text: 'Id',
985+
alias: 'a2',
986+
},
987+
],
988+
subqueries: [],
989+
sObject: 'ChildAccounts',
990+
sObjectAlias: 'a2',
991+
},
992+
{
993+
fields: [
994+
{
995+
text: 'Id',
996+
alias: 'a1',
997+
},
998+
],
999+
subqueries: [],
1000+
sObject: 'ChildAccounts1',
1001+
sObjectAlias: 'a1',
1002+
},
1003+
],
1004+
sObject: 'Account',
1005+
sObjectAlias: 'a',
1006+
},
1007+
},
9601008
];
9611009
export default testCases;

0 commit comments

Comments
 (0)