Repository: paustint/soql-parser-js
Pull request #44 (2018-10-25 13:52:58)

Fixed SObject prefix
Added new property for fields that have an object prefix

atginfo/FuseKit/pull/2226


Patch:
diff --git a/lib/SoqlComposer.ts b/lib/SoqlComposer.ts
index a870153..1f19260 100644
--- a/lib/SoqlComposer.ts
+++ b/lib/SoqlComposer.ts
@@ -69,7 +69,7 @@ export class Compose {
 
   private parseQuery(query: Query, isSubquery: boolean = false): string {
     const fieldData: FieldData = {
-      fields: this.parseFields(query.fields, query.sObjectAlias ? true : false).map(field => ({
+      fields: this.parseFields(query.fields).map(field => ({
         text: field,
         isSubquery: false,
         prefix: '',
@@ -170,12 +170,12 @@ export class Compose {
     return output;
   }
 
-  private parseFields(fields: Field[], hasSObjectAlias: boolean): string[] {
+  private parseFields(fields: Field[]): string[] {
     return fields
       .map(field => {
         if (utils.isString(field.text)) {
-          if (hasSObjectAlias) {
-            return `${utils.get(field.alias, '.')}${field.text}`;
+          if (field.objectPrefix) {
+            return `${utils.get(field.objectPrefix, '.')}${field.text}`;
           } else {
             return `${field.text}${utils.get(field.alias, '', ' ')}`;
           }
diff --git a/lib/SoqlListener.ts b/lib/SoqlListener.ts
index 536ea2c..16a36f8 100644
--- a/lib/SoqlListener.ts
+++ b/lib/SoqlListener.ts
@@ -215,7 +215,7 @@ export class Listener implements SOQLListener {
       // All fields need to update to remove the alias from relationships
       this.getSoqlQuery().fields.forEach(field => {
         if (field.text && field.text.startsWith(`${ctx.text}.`)) {
-          field.alias = ctx.text;
+          field.objectPrefix = ctx.text;
           field.text = field.text.replace(`${ctx.text}.`, '');
           if (field.relationshipFields.length > 2) {
             field.relationshipFields = field.relationshipFields.splice(1);
diff --git a/lib/models/SoqlQuery.model.ts b/lib/models/SoqlQuery.model.ts
index 5b14d42..b95edcd 100644
--- a/lib/models/SoqlQuery.model.ts
+++ b/lib/models/SoqlQuery.model.ts
@@ -31,6 +31,7 @@ export interface SelectStatement {
 export interface Field {
   text?: string;
   alias?: string;
+  objectPrefix?: string;
   relationshipFields?: string[];
   fn?: FunctionExp;
   subqueryObjName?: string; // populated if subquery


Test patch:
diff --git a/debug/test.js b/debug/test.js
index d778e07..47b557b 100644
--- a/debug/test.js
+++ b/debug/test.js
@@ -1,7 +1,9 @@
 var soqlParserJs = require('../dist');
 
 const query = `
-SELECT COUNT_DISTINCT(Company) FROM Lead
+SELECT Id, c.FirstName, c.LastName
+FROM Contact c
+LIMIT 1
 `;
 
 const parsedQuery = soqlParserJs.parseQuery(query, { logging: true });
diff --git a/test/TestCases.ts b/test/TestCases.ts
index 4e39f65..5794606 100644
--- a/test/TestCases.ts
+++ b/test/TestCases.ts
@@ -867,12 +867,12 @@ export const testCases: TestCase[] = [
       fields: [
         {
           text: 'Name',
-          alias: 'c',
+          objectPrefix: 'c',
         },
         {
           text: 'Account.Name',
           relationshipFields: ['Account', 'Name'],
-          alias: 'c',
+          objectPrefix: 'c',
         },
       ],
       subqueries: [],
@@ -989,11 +989,11 @@ export const testCases: TestCase[] = [
       fields: [
         {
           text: 'Id',
-          alias: 'a',
+          objectPrefix: 'a',
         },
         {
           text: 'Name',
-          alias: 'a',
+          objectPrefix: 'a',
         },
         {
           subqueryObjName: 'ChildAccounts',
@@ -1007,7 +1007,7 @@ export const testCases: TestCase[] = [
           fields: [
             {
               text: 'Id',
-              alias: 'a2',
+              objectPrefix: 'a2',
             },
           ],
           subqueries: [],
@@ -1018,7 +1018,7 @@ export const testCases: TestCase[] = [
           fields: [
             {
               text: 'Id',
-              alias: 'a1',
+              objectPrefix: 'a1',
             },
           ],
           subqueries: [],