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
2 changes: 1 addition & 1 deletion debug/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var soqlParserJs = require('../dist');

const query = `
SELECT Company, toLabel(Status) FROM Lead WHERE toLabel(Status) = 'le Draft'
SELECT Account.Name, (SELECT Contact.LastName FROM Account.Contact.Foo.Bars) FROM Account
`;

const parsedQuery = soqlParserJs.parseQuery(query, { logging: true });
Expand Down
10 changes: 8 additions & 2 deletions lib/SoqlComposer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,21 @@ export class Compose {
fields.forEach((field, i) => {
if (field.match(this.subqueryFieldRegex)) {
const subquery = query.subqueries.find(
subquery => subquery.sObject === field.replace(this.subqueryFieldReplaceRegex, '')
subquery => subquery.sObjectRelationshipName === field.replace(this.subqueryFieldReplaceRegex, '')
);
if (subquery) {
fields[i] = `(${this.parseQuery(subquery)})`;
}
}
});
output += ` ${fields.join(', ').trim()} FROM`;
output += ` ${utils.get(query.sObjectPrefix, '.')}${query.sObject}${utils.get(query.sObjectAlias, '', ' ')}`;
if (query.sObjectRelationshipName) {
const sObjectPrefix = query.sObjectPrefix || [];
sObjectPrefix.push(query.sObjectRelationshipName);
output += ` ${sObjectPrefix.join('.')}${utils.get(query.sObjectAlias, '', ' ')}`;
} else {
output += ` ${query.sObject}${utils.get(query.sObjectAlias, '', ' ')}`;
}
this.log(output);

if (query.where) {
Expand Down
17 changes: 13 additions & 4 deletions lib/SoqlListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,11 +844,20 @@ export class Listener implements SOQLListener {
if (this.config.logging) {
console.log('enterObject_spec:', ctx.text);
}
this.getSoqlQuery().sObject = ctx.getChild(0).text;
if (this.config.includeSubqueryAsField && this.context.isSubQuery) {

if (!this.context.isSubQuery) {
this.getSoqlQuery().sObject = ctx.getChild(0).text;
} else {
this.getSoqlQuery().sObjectRelationshipName = ctx.getChild(0).text;
if (ctx.getChild(0).text.includes('.')) {
this.getSoqlQuery().sObject = ctx.getChild(1).text;
this.getSoqlQuery().sObjectPrefix = ctx.getChild(0).text.replace('.', '');
this.getSoqlQuery().sObjectRelationshipName = ctx.getChild(1).text;
const prefixList: string[] = [];
for (let i = 0; i < ctx.getChild(0).childCount; i++) {
if (ctx.getChild(0).getChild(i) instanceof Parser.Object_nameContext) {
prefixList.push(ctx.getChild(0).getChild(i).text);
}
}
this.getSoqlQuery().sObjectPrefix = prefixList;
this.soqlQuery.fields.push({
subqueryObjName: ctx.getChild(1).text,
});
Expand Down
5 changes: 3 additions & 2 deletions lib/models/SoqlQuery.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ export type UpdateClause = 'TRACKING' | 'VIEWSTAT';
export interface Query {
fields: Field[];
subqueries: Query[];
sObject: string;
sObject?: string;
sObjectAlias?: string;
sObjectPrefix?: string;
sObjectPrefix?: string[];
sObjectRelationshipName?: string;
where?: WhereClause;
limit?: number;
offset?: number;
Expand Down
47 changes: 38 additions & 9 deletions test/TestCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ export const testCases: TestCase[] = [
},
],
subqueries: [],
sObject: 'Contacts',
sObjectRelationshipName: 'Contacts',
},
],
sObject: 'Account',
Expand Down Expand Up @@ -334,8 +334,8 @@ export const testCases: TestCase[] = [
},
],
subqueries: [],
sObject: 'Contacts',
sObjectPrefix: 'Account',
sObjectRelationshipName: 'Contacts',
sObjectPrefix: ['Account'],
},
],
sObject: 'Account',
Expand All @@ -362,7 +362,7 @@ export const testCases: TestCase[] = [
},
],
subqueries: [],
sObject: 'Contacts',
sObjectRelationshipName: 'Contacts',
where: {
left: {
field: 'CreatedBy.Alias',
Expand Down Expand Up @@ -430,7 +430,7 @@ export const testCases: TestCase[] = [
},
],
subqueries: [],
sObject: 'Line_Items__r',
sObjectRelationshipName: 'Line_Items__r',
},
],
sObject: 'Merchandise__c',
Expand Down Expand Up @@ -565,7 +565,7 @@ export const testCases: TestCase[] = [
},
],
subqueries: [],
sObject: 'Notes',
sObjectRelationshipName: 'Notes',
},
],
sObject: 'Account',
Expand Down Expand Up @@ -609,7 +609,7 @@ export const testCases: TestCase[] = [
},
],
subqueries: [],
sObject: 'OpportunityLineItems',
sObjectRelationshipName: 'OpportunityLineItems',
},
],
sObject: 'Opportunity',
Expand Down Expand Up @@ -1007,7 +1007,7 @@ export const testCases: TestCase[] = [
},
],
subqueries: [],
sObject: 'ChildAccounts',
sObjectRelationshipName: 'ChildAccounts',
sObjectAlias: 'a2',
},
{
Expand All @@ -1018,7 +1018,7 @@ export const testCases: TestCase[] = [
},
],
subqueries: [],
sObject: 'ChildAccounts1',
sObjectRelationshipName: 'ChildAccounts1',
sObjectAlias: 'a1',
},
],
Expand Down Expand Up @@ -1440,5 +1440,34 @@ export const testCases: TestCase[] = [
},
},
},
{
testCase: 50,
soql: 'SELECT Account.Name, (SELECT Contact.LastName FROM Account.Contact.Foo.Bars) FROM Account',
output: {
fields: [
{
text: 'Account.Name',
relationshipFields: ['Account', 'Name'],
},
{
subqueryObjName: 'Bars',
},
],
subqueries: [
{
fields: [
{
text: 'Contact.LastName',
relationshipFields: ['Contact', 'LastName'],
},
],
subqueries: [],
sObjectRelationshipName: 'Bars',
sObjectPrefix: ['Account', 'Contact', 'Foo'],
},
],
sObject: 'Account',
},
},
];
export default testCases;