@jetstreamapp/soql-parser-js 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/AUTHORS.md +1 -0
  2. package/CHANGELOG.md +959 -0
  3. package/LICENSE.txt +12 -0
  4. package/README.md +949 -0
  5. package/bin/soql-parser-js +3 -0
  6. package/dist/cjs/index.js +2 -0
  7. package/dist/cjs/index.js.LICENSE.txt +1 -0
  8. package/dist/cjs/main.licenses.txt +898 -0
  9. package/dist/cjs/src/api/api-models.d.ts +175 -0
  10. package/dist/cjs/src/api/api-models.js +2 -0
  11. package/dist/cjs/src/api/public-utils.d.ts +29 -0
  12. package/dist/cjs/src/api/public-utils.js +175 -0
  13. package/dist/cjs/src/composer/composer.d.ts +32 -0
  14. package/dist/cjs/src/composer/composer.js +295 -0
  15. package/dist/cjs/src/formatter/formatter.d.ts +42 -0
  16. package/dist/cjs/src/formatter/formatter.js +236 -0
  17. package/dist/cjs/src/index.d.ts +5 -0
  18. package/dist/cjs/src/index.js +23 -0
  19. package/dist/cjs/src/models.d.ts +266 -0
  20. package/dist/cjs/src/models.js +2 -0
  21. package/dist/cjs/src/parser/lexer.d.ts +186 -0
  22. package/dist/cjs/src/parser/lexer.js +954 -0
  23. package/dist/cjs/src/parser/parser.d.ts +89 -0
  24. package/dist/cjs/src/parser/parser.js +697 -0
  25. package/dist/cjs/src/parser/visitor.d.ts +4 -0
  26. package/dist/cjs/src/parser/visitor.js +856 -0
  27. package/dist/cjs/src/utils.d.ts +42 -0
  28. package/dist/cjs/src/utils.js +245 -0
  29. package/dist/cli/index.js +113 -0
  30. package/dist/esm/api/api-models.js +1 -0
  31. package/dist/esm/api/public-utils.js +153 -0
  32. package/dist/esm/composer/composer.js +273 -0
  33. package/dist/esm/formatter/formatter.js +223 -0
  34. package/dist/esm/index.js +4 -0
  35. package/dist/esm/models.js +1 -0
  36. package/dist/esm/parser/lexer.js +947 -0
  37. package/dist/esm/parser/parser.js +667 -0
  38. package/dist/esm/parser/visitor.js +823 -0
  39. package/dist/esm/utils.js +200 -0
  40. package/dist/src/api/api-models.js +2 -0
  41. package/dist/src/api/public-utils.js +203 -0
  42. package/dist/src/composer/composer.js +358 -0
  43. package/dist/src/formatter/formatter.js +263 -0
  44. package/dist/src/index.js +28 -0
  45. package/dist/src/models.js +2 -0
  46. package/dist/src/parser/lexer.js +997 -0
  47. package/dist/src/parser/parser.js +711 -0
  48. package/dist/src/parser/visitor.js +862 -0
  49. package/dist/src/utils.js +251 -0
  50. package/package.json +78 -0
@@ -0,0 +1,823 @@
1
+ import { isString, isSubqueryFromFlag, isToken, isWhereClauseWithRightCondition } from '../utils';
2
+ import { parse, SoqlParser } from './parser';
3
+ const parser = new SoqlParser();
4
+ const BaseSoqlVisitor = parser.getBaseCstVisitorConstructor();
5
+ const BOOLEANS = ['TRUE', 'FALSE'];
6
+ const DATE_LITERALS = [
7
+ 'YESTERDAY',
8
+ 'TODAY',
9
+ 'TOMORROW',
10
+ 'LAST_WEEK',
11
+ 'THIS_WEEK',
12
+ 'NEXT_WEEK',
13
+ 'LAST_MONTH',
14
+ 'THIS_MONTH',
15
+ 'NEXT_MONTH',
16
+ 'LAST_90_DAYS',
17
+ 'NEXT_90_DAYS',
18
+ 'THIS_QUARTER',
19
+ 'LAST_QUARTER',
20
+ 'NEXT_QUARTER',
21
+ 'THIS_YEAR',
22
+ 'LAST_YEAR',
23
+ 'NEXT_YEAR',
24
+ 'THIS_FISCAL_QUARTER',
25
+ 'LAST_FISCAL_QUARTER',
26
+ 'NEXT_FISCAL_QUARTER',
27
+ 'THIS_FISCAL_YEAR',
28
+ 'LAST_FISCAL_YEAR',
29
+ 'NEXT_FISCAL_YEAR',
30
+ ];
31
+ const DATE_N_LITERALS = [
32
+ 'NEXT_N_DAYS',
33
+ 'LAST_N_DAYS',
34
+ 'N_DAYS_AGO',
35
+ 'NEXT_N_WEEKS',
36
+ 'LAST_N_WEEKS',
37
+ 'N_WEEKS_AGO',
38
+ 'NEXT_N_MONTHS',
39
+ 'LAST_N_MONTHS',
40
+ 'N_MONTHS_AGO',
41
+ 'NEXT_N_QUARTERS',
42
+ 'LAST_N_QUARTERS',
43
+ 'N_QUARTERS_AGO',
44
+ 'NEXT_N_YEARS',
45
+ 'LAST_N_YEARS',
46
+ 'N_YEARS_AGO',
47
+ 'NEXT_N_FISCAL_QUARTERS',
48
+ 'LAST_N_FISCAL_QUARTERS',
49
+ 'N_FISCAL_QUARTERS_AGO',
50
+ 'NEXT_N_FISCAL_YEARS',
51
+ 'LAST_N_FISCAL_YEARS',
52
+ 'N_FISCAL_YEARS_AGO',
53
+ ];
54
+ class SOQLVisitor extends BaseSoqlVisitor {
55
+ constructor() {
56
+ super();
57
+ this.validateVisitor();
58
+ }
59
+ helpers = {
60
+ $_getFieldFunction: (ctx, isAggregateFn = false, includeType = true) => {
61
+ const args = ctx.functionExpression ? ctx.functionExpression.map((node) => this.visit(node, { includeType })).flat() : [];
62
+ const output = {};
63
+ if (includeType) {
64
+ output.type = 'FieldFunctionExpression';
65
+ }
66
+ output.functionName = ctx.fn[0].tokenType.name;
67
+ output.parameters = args;
68
+ if (includeType && isAggregateFn) {
69
+ output.isAggregateFn = isAggregateFn;
70
+ }
71
+ output.rawValue = `${ctx.fn[0].image}(${args.map((arg) => (typeof arg === 'string' ? arg : arg.rawValue)).join(', ')})`;
72
+ return output;
73
+ },
74
+ $_getLiteralTypeFromTokenType: (tokenTypeName) => {
75
+ if (tokenTypeName === 'REAL_NUMBER') {
76
+ return 'DECIMAL';
77
+ }
78
+ else if (tokenTypeName === 'CURRENCY_PREFIXED_DECIMAL') {
79
+ return 'DECIMAL_WITH_CURRENCY_PREFIX';
80
+ }
81
+ else if (tokenTypeName === 'CURRENCY_PREFIXED_INTEGER') {
82
+ return 'INTEGER_WITH_CURRENCY_PREFIX';
83
+ }
84
+ else if (tokenTypeName === 'SIGNED_DECIMAL') {
85
+ return 'DECIMAL';
86
+ }
87
+ else if (tokenTypeName === 'UNSIGNED_DECIMAL') {
88
+ return 'DECIMAL';
89
+ }
90
+ else if (tokenTypeName === 'UNSIGNED_INTEGER') {
91
+ return 'INTEGER';
92
+ }
93
+ else if (tokenTypeName === 'SIGNED_INTEGER') {
94
+ return 'INTEGER';
95
+ }
96
+ else if (tokenTypeName === 'DATETIME') {
97
+ return 'DATETIME';
98
+ }
99
+ else if (tokenTypeName === 'DATE') {
100
+ return 'DATE';
101
+ }
102
+ else if (tokenTypeName === 'NULL') {
103
+ return 'NULL';
104
+ }
105
+ else if (tokenTypeName === 'StringIdentifier') {
106
+ return 'STRING';
107
+ }
108
+ else if (tokenTypeName === 'Identifier') {
109
+ return 'STRING';
110
+ }
111
+ else if (BOOLEANS.includes(tokenTypeName)) {
112
+ return 'BOOLEAN';
113
+ }
114
+ else if (DATE_LITERALS.includes(tokenTypeName)) {
115
+ return 'DATE_LITERAL';
116
+ }
117
+ else if (DATE_N_LITERALS.includes(tokenTypeName)) {
118
+ return 'DATE_N_LITERAL';
119
+ }
120
+ else {
121
+ return 'STRING';
122
+ }
123
+ },
124
+ $_parseSelect(ctx, options) {
125
+ const { isSubquery } = options || { isSubquery: false };
126
+ let output = {};
127
+ if (ctx.selectClause) {
128
+ output.fields = this.visit(ctx.selectClause);
129
+ }
130
+ if (ctx.fromClause) {
131
+ if (isSubqueryFromFlag(output, isSubquery)) {
132
+ const { sObject, alias, sObjectPrefix } = this.visit(ctx.fromClause);
133
+ output.relationshipName = sObject;
134
+ if (alias) {
135
+ output.sObjectAlias = alias;
136
+ }
137
+ if (sObjectPrefix) {
138
+ output.sObjectPrefix = sObjectPrefix;
139
+ }
140
+ }
141
+ else {
142
+ const { sObject, alias } = this.visit(ctx.fromClause);
143
+ output.sObject = sObject;
144
+ if (alias) {
145
+ output.sObjectAlias = alias;
146
+ }
147
+ }
148
+ }
149
+ if (Array.isArray(output.fields)) {
150
+ if (!!output.sObjectAlias) {
151
+ output.fields.forEach((field) => {
152
+ if (field.relationships && field.relationships[0] === output.sObjectAlias) {
153
+ field.relationships = field.relationships.slice(1);
154
+ field.objectPrefix = output.sObjectAlias;
155
+ }
156
+ if (field.relationships && field.relationships.length === 0) {
157
+ delete field.relationships;
158
+ field.type = 'Field';
159
+ }
160
+ });
161
+ }
162
+ }
163
+ output = { ...output, ...this.visit(ctx.clauseStatements) };
164
+ return output;
165
+ },
166
+ };
167
+ selectStatement(ctx, options) {
168
+ return this.helpers.$_parseSelect.bind(this)(ctx, options);
169
+ }
170
+ selectStatementPartial(ctx, options) {
171
+ return this.helpers.$_parseSelect.bind(this)(ctx, options);
172
+ }
173
+ clauseStatements(ctx) {
174
+ const query = {};
175
+ if (ctx.usingScopeClause && !ctx.usingScopeClause[0].recoveredNode) {
176
+ query.usingScope = this.visit(ctx.usingScopeClause);
177
+ }
178
+ if (ctx.whereClause && !ctx.whereClause[0].recoveredNode) {
179
+ query.where = this.visit(ctx.whereClause);
180
+ }
181
+ if (ctx.withClause) {
182
+ ctx.withClause
183
+ .filter(item => !item.recoveredNode)
184
+ .forEach(item => {
185
+ const { withSecurityEnforced, withAccessLevel, withDataCategory } = this.visit(item);
186
+ if (withSecurityEnforced) {
187
+ query.withSecurityEnforced = withSecurityEnforced;
188
+ }
189
+ if (withAccessLevel) {
190
+ query.withAccessLevel = withAccessLevel;
191
+ }
192
+ if (withDataCategory) {
193
+ query.withDataCategory = withDataCategory;
194
+ }
195
+ });
196
+ }
197
+ if (ctx.groupByClause && !ctx.groupByClause[0].recoveredNode) {
198
+ query.groupBy = this.visit(ctx.groupByClause);
199
+ }
200
+ if (ctx.havingClause && !ctx.havingClause[0].recoveredNode) {
201
+ query.having = this.visit(ctx.havingClause);
202
+ }
203
+ if (ctx.orderByClause && !ctx.orderByClause[0].recoveredNode) {
204
+ query.orderBy = this.visit(ctx.orderByClause);
205
+ }
206
+ if (ctx.limitClause && !ctx.limitClause[0].recoveredNode) {
207
+ query.limit = Number(this.visit(ctx.limitClause));
208
+ }
209
+ if (ctx.offsetClause && !ctx.offsetClause[0].recoveredNode) {
210
+ query.offset = Number(this.visit(ctx.offsetClause));
211
+ }
212
+ if (ctx.forViewOrReference && !ctx.forViewOrReference[0].recoveredNode) {
213
+ query.for = this.visit(ctx.forViewOrReference);
214
+ }
215
+ if (ctx.updateTrackingViewstat && !ctx.updateTrackingViewstat[0].recoveredNode) {
216
+ query.update = this.visit(ctx.updateTrackingViewstat);
217
+ }
218
+ return query;
219
+ }
220
+ selectClause(ctx) {
221
+ if (ctx.field) {
222
+ return ctx.field.map(item => {
223
+ if (isToken(item)) {
224
+ const field = item.image;
225
+ let output;
226
+ if (!field.includes('.')) {
227
+ output = {
228
+ type: 'Field',
229
+ field: field,
230
+ };
231
+ }
232
+ else {
233
+ const splitFields = field.split('.');
234
+ output = {
235
+ type: 'FieldRelationship',
236
+ field: splitFields[splitFields.length - 1],
237
+ relationships: splitFields.slice(0, splitFields.length - 1),
238
+ rawValue: field,
239
+ };
240
+ }
241
+ return output;
242
+ }
243
+ else {
244
+ return this.visit(item);
245
+ }
246
+ });
247
+ }
248
+ return [];
249
+ }
250
+ selectClauseFunctionIdentifier(ctx) {
251
+ let output = {
252
+ ...this.visit(ctx.fn),
253
+ };
254
+ if (ctx.alias) {
255
+ output.alias = ctx.alias[0].image;
256
+ }
257
+ return output;
258
+ }
259
+ selectClauseSubqueryIdentifier(ctx) {
260
+ return {
261
+ type: 'FieldSubquery',
262
+ subquery: this.visit(ctx.selectStatement, { isSubquery: true }),
263
+ };
264
+ }
265
+ selectClauseTypeOf(ctx) {
266
+ let conditions = ctx.selectClauseTypeOfThen.map((item) => this.visit(item));
267
+ if (ctx.selectClauseTypeOfElse) {
268
+ conditions.push(this.visit(ctx.selectClauseTypeOfElse));
269
+ }
270
+ return {
271
+ type: 'FieldTypeof',
272
+ field: ctx.typeOfField[0].image,
273
+ conditions,
274
+ };
275
+ }
276
+ selectClauseIdentifier(ctx) {
277
+ const item = ctx.field[0];
278
+ const alias = !!ctx.alias ? ctx.alias[0].image : undefined;
279
+ const field = item.image;
280
+ let output;
281
+ if (!field.includes('.')) {
282
+ output = {
283
+ type: 'Field',
284
+ field: field,
285
+ };
286
+ }
287
+ else {
288
+ const splitFields = field.split('.');
289
+ output = {
290
+ type: 'FieldRelationship',
291
+ field: splitFields[splitFields.length - 1],
292
+ relationships: splitFields.slice(0, splitFields.length - 1),
293
+ rawValue: field,
294
+ };
295
+ }
296
+ if (alias) {
297
+ output.alias = alias;
298
+ }
299
+ return output;
300
+ }
301
+ selectClauseTypeOfThen(ctx) {
302
+ return {
303
+ type: 'WHEN',
304
+ objectType: ctx.typeOfField[0].image,
305
+ fieldList: ctx.field.map((item) => item.image),
306
+ };
307
+ }
308
+ selectClauseTypeOfElse(ctx) {
309
+ return {
310
+ type: 'ELSE',
311
+ fieldList: ctx.field.map((item) => item.image),
312
+ };
313
+ }
314
+ fromClause(ctx) {
315
+ let sObject = ctx.Identifier[0].image;
316
+ let output;
317
+ if (sObject.includes('.')) {
318
+ const sObjectPrefix = sObject.split('.');
319
+ output = {
320
+ sObjectPrefix: sObjectPrefix.slice(0, sObjectPrefix.length - 1),
321
+ sObject: sObjectPrefix[sObjectPrefix.length - 1],
322
+ };
323
+ }
324
+ else {
325
+ output = {
326
+ sObject,
327
+ };
328
+ }
329
+ if (ctx.alias && ctx.alias[0]) {
330
+ output.alias = ctx.alias[0].image;
331
+ }
332
+ return output;
333
+ }
334
+ usingScopeClause(ctx) {
335
+ return ctx.UsingScopeEnumeration[0].image;
336
+ }
337
+ whereClauseSubqueryIdentifier(ctx) {
338
+ return this.visit(ctx.selectStatement, { isSubquery: false });
339
+ }
340
+ whereClause(ctx) {
341
+ const where = ctx.conditionExpression.reduce((expressions, currExpression) => {
342
+ let tempExpression;
343
+ if (!expressions.expressionTree) {
344
+ tempExpression = this.visit(currExpression);
345
+ expressions.expressionTree = tempExpression;
346
+ }
347
+ else {
348
+ tempExpression = this.visit(currExpression, { prevExpression: expressions.prevExpression });
349
+ expressions.prevExpression.right = tempExpression;
350
+ }
351
+ let currentRightExpression = tempExpression.right;
352
+ let nextRightExpression = tempExpression.right;
353
+ while (isWhereClauseWithRightCondition(nextRightExpression)) {
354
+ currentRightExpression = nextRightExpression;
355
+ nextRightExpression = nextRightExpression.right;
356
+ }
357
+ expressions.prevExpression = nextRightExpression || tempExpression;
358
+ return expressions;
359
+ }, { prevExpression: undefined, expressionTree: undefined });
360
+ return where.expressionTree;
361
+ }
362
+ conditionExpression(ctx, options) {
363
+ options = options || {};
364
+ if (options.prevExpression && ctx.logicalOperator) {
365
+ options.prevExpression.operator = ctx.logicalOperator[0].tokenType.name;
366
+ }
367
+ let baseExpression = {};
368
+ let currExpression = baseExpression;
369
+ if (Array.isArray(ctx.expressionNegation)) {
370
+ if (ctx.expressionNegation.length === 1) {
371
+ baseExpression = this.visit(ctx.expressionNegation);
372
+ currExpression = baseExpression.right;
373
+ }
374
+ else {
375
+ baseExpression = this.visit(ctx.expressionNegation[0]);
376
+ const currNestedExpression = baseExpression;
377
+ ctx.expressionNegation.slice(1).forEach(item => {
378
+ currNestedExpression.right = this.visit(item);
379
+ currExpression = currNestedExpression.right.right;
380
+ });
381
+ }
382
+ }
383
+ currExpression.left = this.visit(ctx.expression);
384
+ return baseExpression;
385
+ }
386
+ withClause(ctx) {
387
+ if (ctx.withSecurityEnforced) {
388
+ return {
389
+ withSecurityEnforced: true,
390
+ };
391
+ }
392
+ if (ctx.withAccessLevel) {
393
+ return {
394
+ withAccessLevel: ctx.withAccessLevel[0].image,
395
+ };
396
+ }
397
+ return {
398
+ withDataCategory: {
399
+ conditions: this.visit(ctx.withDataCategory),
400
+ },
401
+ };
402
+ }
403
+ withDataCategory(ctx) {
404
+ return ctx.withDataCategoryArr.map(item => this.visit(item));
405
+ }
406
+ withDataCategoryArr(ctx) {
407
+ return {
408
+ groupName: ctx.dataCategoryGroupName[0].image,
409
+ selector: ctx.filteringSelector[0].image,
410
+ parameters: ctx.dataCategoryName.map((item) => item.image),
411
+ };
412
+ }
413
+ groupByClause(ctx) {
414
+ return ctx.groupBy.map((groupBy) => (isToken(groupBy) ? { field: groupBy.image } : { fn: this.visit(groupBy, { includeType: false }) }));
415
+ }
416
+ havingClause(ctx) {
417
+ const having = ctx.conditionExpression.reduce((expressions, currExpression) => {
418
+ if (!expressions.expressionTree) {
419
+ expressions.expressionTree = this.visit(currExpression);
420
+ expressions.prevExpression = expressions.expressionTree;
421
+ }
422
+ else {
423
+ expressions.prevExpression.right = this.visit(currExpression, {
424
+ prevExpression: expressions.prevExpression,
425
+ });
426
+ expressions.prevExpression = expressions.prevExpression.right;
427
+ }
428
+ return expressions;
429
+ }, { prevExpression: undefined, expressionTree: undefined });
430
+ return having.expressionTree;
431
+ }
432
+ orderByClause(ctx) {
433
+ return ctx.orderByExpressionOrFn.map(item => this.visit(item));
434
+ }
435
+ orderByExpression(ctx) {
436
+ const orderByClause = {
437
+ field: ctx.Identifier[0].image,
438
+ };
439
+ if (ctx.order && ctx.order[0]) {
440
+ orderByClause.order = ctx.order[0].tokenType.name;
441
+ }
442
+ if (ctx.nulls && ctx.nulls[0]) {
443
+ orderByClause.nulls = ctx.nulls[0].tokenType.name;
444
+ }
445
+ return orderByClause;
446
+ }
447
+ orderByGroupingFunctionExpression(ctx) {
448
+ const orderByClause = {
449
+ fn: this.helpers.$_getFieldFunction(ctx, false, false),
450
+ };
451
+ if (ctx.order && ctx.order[0]) {
452
+ orderByClause.order = ctx.order[0].tokenType.name;
453
+ }
454
+ if (ctx.nulls && ctx.nulls[0]) {
455
+ orderByClause.nulls = ctx.nulls[0].tokenType.name;
456
+ }
457
+ return orderByClause;
458
+ }
459
+ orderBySpecialFunctionExpression(ctx) {
460
+ const orderByClause = {};
461
+ if (ctx.aggregateFunction) {
462
+ orderByClause.fn = this.visit(ctx.aggregateFunction, { includeType: false });
463
+ }
464
+ else if (ctx.dateFunction) {
465
+ orderByClause.fn = this.visit(ctx.dateFunction, { includeType: false });
466
+ }
467
+ else if (ctx.locationFunction) {
468
+ orderByClause.fn = this.visit(ctx.locationFunction, { includeType: false });
469
+ }
470
+ if (ctx.order && ctx.order[0]) {
471
+ orderByClause.order = ctx.order[0].tokenType.name;
472
+ }
473
+ if (ctx.nulls && ctx.nulls[0]) {
474
+ orderByClause.nulls = ctx.nulls[0].tokenType.name;
475
+ }
476
+ return orderByClause;
477
+ }
478
+ limitClause(ctx) {
479
+ return ctx.value[0].image;
480
+ }
481
+ offsetClause(ctx) {
482
+ return ctx.value[0].image;
483
+ }
484
+ dateFunction(ctx, options = { includeType: true }) {
485
+ return this.helpers.$_getFieldFunction(ctx, true, options.includeType);
486
+ }
487
+ aggregateFunction(ctx, options = { includeType: true }) {
488
+ return this.helpers.$_getFieldFunction(ctx, true, options.includeType);
489
+ }
490
+ fieldsFunction(ctx, options = { includeType: true }) {
491
+ let output = {};
492
+ if (options.includeType) {
493
+ output.type = 'FieldFunctionExpression';
494
+ }
495
+ output = {
496
+ ...output,
497
+ ...{
498
+ functionName: 'FIELDS',
499
+ parameters: [ctx.params[0].image],
500
+ },
501
+ };
502
+ output.rawValue = `FIELDS(${output.parameters[0]})`;
503
+ return output;
504
+ }
505
+ otherFunction(ctx, options = { includeType: true }) {
506
+ return this.helpers.$_getFieldFunction(ctx, false, options.includeType);
507
+ }
508
+ cubeFunction(ctx) {
509
+ return this.helpers.$_getFieldFunction(ctx, false, false);
510
+ }
511
+ rollupFunction(ctx) {
512
+ return this.helpers.$_getFieldFunction(ctx, false, false);
513
+ }
514
+ locationFunction(ctx, options = { includeType: true }) {
515
+ let output = {};
516
+ if (options.includeType) {
517
+ output.type = 'FieldFunctionExpression';
518
+ }
519
+ output = {
520
+ ...output,
521
+ ...{
522
+ functionName: 'DISTANCE',
523
+ parameters: [
524
+ ctx.location1[0].image,
525
+ isToken(ctx.location2) ? ctx.location2[0].image : this.visit(ctx.location2, options),
526
+ ctx.unit[0].image,
527
+ ],
528
+ },
529
+ };
530
+ if (options.includeType) {
531
+ output.isAggregateFn = true;
532
+ }
533
+ output.rawValue = `DISTANCE(${output.parameters[0]}, ${isString(output.parameters[1]) ? output.parameters[1] : output.parameters[1].rawValue}, ${output.parameters[2]})`;
534
+ return output;
535
+ }
536
+ geolocationFunction(ctx, options = { includeType: true }) {
537
+ let output = {};
538
+ if (options.includeType) {
539
+ output.type = 'FieldFunctionExpression';
540
+ }
541
+ output = {
542
+ ...output,
543
+ ...{
544
+ functionName: 'GEOLOCATION',
545
+ parameters: [ctx.latitude[0].image, ctx.longitude[0].image],
546
+ rawValue: `GEOLOCATION(${ctx.latitude[0].image}, ${ctx.longitude[0].image})`,
547
+ },
548
+ };
549
+ return output;
550
+ }
551
+ functionExpression(ctx, options = { includeType: true }) {
552
+ if (ctx.params) {
553
+ return ctx.params.map((item) => {
554
+ if (item.image) {
555
+ return item.image;
556
+ }
557
+ return this.visit(item, options);
558
+ });
559
+ }
560
+ return [];
561
+ }
562
+ expression(ctx) {
563
+ const { value, literalType, dateLiteralVariable, operator } = this.visit(ctx.operator, { returnLiteralType: true });
564
+ const output = {};
565
+ if (isToken(ctx.lhs)) {
566
+ output.field = ctx.lhs[0].image;
567
+ }
568
+ else {
569
+ output.fn = this.visit(ctx.lhs, { includeType: false });
570
+ }
571
+ output.operator = operator;
572
+ output.literalType = literalType;
573
+ if (literalType === 'SUBQUERY') {
574
+ output.valueQuery = value;
575
+ }
576
+ else {
577
+ output.value = value;
578
+ }
579
+ if (dateLiteralVariable) {
580
+ output.dateLiteralVariable = dateLiteralVariable;
581
+ }
582
+ if (ctx.L_PAREN) {
583
+ output.openParen = ctx.L_PAREN.length;
584
+ }
585
+ if (ctx.R_PAREN) {
586
+ output.closeParen = ctx.R_PAREN.length;
587
+ }
588
+ return output;
589
+ }
590
+ expressionPartWithNegation(ctx) {
591
+ const output = {
592
+ left: ctx.L_PAREN ? { openParen: ctx.L_PAREN.length } : null,
593
+ operator: 'NOT',
594
+ right: {
595
+ left: {},
596
+ },
597
+ };
598
+ return output;
599
+ }
600
+ expressionWithRelationalOperator(ctx) {
601
+ if (ctx.relationalOperator) {
602
+ return {
603
+ operator: this.visit(ctx.relationalOperator),
604
+ ...this.visit(ctx.rhs, { returnLiteralType: true }),
605
+ };
606
+ }
607
+ return {
608
+ operator: this.visit(ctx.setOperator),
609
+ ...this.visit(ctx.rhs, { returnLiteralType: true }),
610
+ };
611
+ }
612
+ expressionWithSetOperator(ctx) {
613
+ if (ctx.relationalOperator) {
614
+ return {
615
+ operator: this.visit(ctx.relationalOperator),
616
+ ...this.visit(ctx.rhs, { returnLiteralType: true }),
617
+ };
618
+ }
619
+ return {
620
+ operator: this.visit(ctx.setOperator),
621
+ ...this.visit(ctx.rhs, { returnLiteralType: true }),
622
+ };
623
+ }
624
+ atomicExpression(ctx, options) {
625
+ options = options || {};
626
+ let value;
627
+ let literalType;
628
+ let dateLiteralVariable;
629
+ if (ctx.apexBindVariableExpression) {
630
+ value = this.visit(ctx.apexBindVariableExpression);
631
+ literalType = 'APEX_BIND_VARIABLE';
632
+ }
633
+ else if (ctx.NumberIdentifier) {
634
+ value = ctx.NumberIdentifier[0].image;
635
+ literalType = this.helpers.$_getLiteralTypeFromTokenType(ctx.NumberIdentifier[0].tokenType.name);
636
+ }
637
+ else if (ctx.UnsignedInteger) {
638
+ value = ctx.UnsignedInteger[0].image;
639
+ literalType = 'INTEGER';
640
+ }
641
+ else if (ctx.SignedInteger) {
642
+ value = ctx.SignedInteger[0].image;
643
+ literalType = 'INTEGER';
644
+ }
645
+ else if (ctx.RealNumber) {
646
+ value = ctx.RealNumber[0].image;
647
+ literalType = 'DECIMAL';
648
+ }
649
+ else if (ctx.DateIdentifier) {
650
+ value = ctx.DateIdentifier[0].image;
651
+ literalType = this.helpers.$_getLiteralTypeFromTokenType(ctx.DateIdentifier[0].tokenType.name);
652
+ }
653
+ else if (ctx.CurrencyPrefixedInteger) {
654
+ value = ctx.CurrencyPrefixedInteger[0].image;
655
+ literalType = 'INTEGER_WITH_CURRENCY_PREFIX';
656
+ }
657
+ else if (ctx.CurrencyPrefixedDecimal) {
658
+ value = ctx.CurrencyPrefixedDecimal[0].image;
659
+ literalType = 'DECIMAL_WITH_CURRENCY_PREFIX';
660
+ }
661
+ else if (ctx.DateTime) {
662
+ value = ctx.DateTime[0].image;
663
+ literalType = 'DATETIME';
664
+ }
665
+ else if (ctx.date) {
666
+ value = ctx.DateToken[0].image;
667
+ literalType = 'DATE';
668
+ }
669
+ else if (ctx.NULL) {
670
+ value = 'NULL';
671
+ literalType = 'NULL';
672
+ }
673
+ else if (ctx.StringIdentifier) {
674
+ value = ctx.StringIdentifier[0].image;
675
+ literalType = 'STRING';
676
+ }
677
+ else if (ctx.Identifier) {
678
+ value = ctx.Identifier[0].image;
679
+ literalType = 'STRING';
680
+ }
681
+ else if (ctx.booleanValue) {
682
+ value = this.visit(ctx.booleanValue);
683
+ literalType = 'BOOLEAN';
684
+ }
685
+ else if (ctx.DateLiteral) {
686
+ value = ctx.DateLiteral[0].image;
687
+ literalType = 'DATE_LITERAL';
688
+ }
689
+ else if (ctx.dateNLiteral) {
690
+ const valueAndVariable = this.visit(ctx.dateNLiteral);
691
+ value = valueAndVariable.value;
692
+ dateLiteralVariable = valueAndVariable.variable;
693
+ literalType = 'DATE_N_LITERAL';
694
+ }
695
+ else if (ctx.arrayExpression) {
696
+ const arrayValues = this.visit(ctx.arrayExpression);
697
+ value = arrayValues.map((item) => item.value);
698
+ const dateLiteralTemp = arrayValues.map((item) => item.variable || null);
699
+ const hasDateLiterals = dateLiteralTemp.some(item => item !== null);
700
+ if (new Set(arrayValues.map((item) => item.type)).size === 1) {
701
+ literalType = this.helpers.$_getLiteralTypeFromTokenType(arrayValues[0].type);
702
+ }
703
+ else {
704
+ literalType = arrayValues.map((item) => this.helpers.$_getLiteralTypeFromTokenType(item.type));
705
+ }
706
+ if (hasDateLiterals) {
707
+ dateLiteralVariable = dateLiteralTemp;
708
+ }
709
+ literalType = literalType || 'STRING';
710
+ }
711
+ else if (ctx.whereClauseSubqueryIdentifier) {
712
+ value = this.visit(ctx.whereClauseSubqueryIdentifier);
713
+ literalType = 'SUBQUERY';
714
+ }
715
+ if (options.returnLiteralType) {
716
+ return {
717
+ value,
718
+ literalType: literalType,
719
+ dateLiteralVariable,
720
+ };
721
+ }
722
+ else {
723
+ return value;
724
+ }
725
+ }
726
+ apexBindVariableExpression(ctx) {
727
+ return ctx.apex.map(item => this.visit(item)).join('.');
728
+ }
729
+ apexBindVariableIdentifier(ctx) {
730
+ let output = ctx.Identifier[0].image;
731
+ if (ctx.apexBindVariableFunctionArrayAccessor) {
732
+ output += this.visit(ctx.apexBindVariableFunctionArrayAccessor[0]);
733
+ }
734
+ return output;
735
+ }
736
+ apexBindVariableNewInstantiation(ctx) {
737
+ let output = `new ${ctx.function[0].image}`;
738
+ if (ctx.apexBindVariableGeneric) {
739
+ output += this.visit(ctx.apexBindVariableGeneric[0]);
740
+ }
741
+ output += this.visit(ctx.apexBindVariableFunctionParams[0]);
742
+ if (ctx.apexBindVariableFunctionArrayAccessor) {
743
+ output += this.visit(ctx.apexBindVariableFunctionArrayAccessor[0]);
744
+ }
745
+ return output;
746
+ }
747
+ apexBindVariableFunctionCall(ctx) {
748
+ let output = `${ctx.function[0].image}${this.visit(ctx.apexBindVariableFunctionParams[0])}`;
749
+ if (ctx.apexBindVariableFunctionArrayAccessor) {
750
+ output += this.visit(ctx.apexBindVariableFunctionArrayAccessor[0]);
751
+ }
752
+ return output;
753
+ }
754
+ apexBindVariableGeneric(ctx) {
755
+ return `<${ctx.parameter.map(item => item.image).join(', ')}>`;
756
+ }
757
+ apexBindVariableFunctionParams(ctx) {
758
+ const params = Array.isArray(ctx.parameter) ? ctx.parameter : [];
759
+ return `(${params.map(item => item.image).join(', ')})`;
760
+ }
761
+ apexBindVariableFunctionArrayAccessor(ctx) {
762
+ return `[${ctx.value[0].image}]`;
763
+ }
764
+ arrayExpression(ctx) {
765
+ return ctx.value.map((item) => {
766
+ if (isToken(item)) {
767
+ return {
768
+ type: item.tokenType.name,
769
+ value: item.image,
770
+ };
771
+ }
772
+ return this.visit(item, { includeType: true });
773
+ });
774
+ }
775
+ relationalOperator(ctx) {
776
+ return ctx.operator[0].image;
777
+ }
778
+ setOperator(ctx) {
779
+ if (Array.isArray(ctx.operator)) {
780
+ return ctx.operator[0].tokenType.name.replace('_', ' ');
781
+ }
782
+ if (Array.isArray(ctx.notIn)) {
783
+ return this.visit(ctx.notIn);
784
+ }
785
+ }
786
+ notInOperator(ctx) {
787
+ return 'NOT IN';
788
+ }
789
+ booleanValue(ctx) {
790
+ return ctx.boolean[0].tokenType.name;
791
+ }
792
+ dateNLiteral(ctx, options) {
793
+ const output = {
794
+ value: `${ctx.dateNLiteral[0].image}:${ctx.variable[0].image}`,
795
+ variable: Number(ctx.variable[0].image),
796
+ };
797
+ if (options && options.includeType) {
798
+ output.type = ctx.dateNLiteral[0].tokenType.name;
799
+ }
800
+ return output;
801
+ }
802
+ forViewOrReference(ctx) {
803
+ return ctx.value[0].tokenType.name;
804
+ }
805
+ updateTrackingViewstat(ctx) {
806
+ return ctx.value[0].tokenType.name;
807
+ }
808
+ }
809
+ const visitor = new SOQLVisitor();
810
+ export function parseQuery(soql, options) {
811
+ const { cst } = parse(soql, options);
812
+ const query = visitor.visit(cst);
813
+ return query;
814
+ }
815
+ export function isQueryValid(soql, options) {
816
+ try {
817
+ const { parseErrors } = parse(soql, options);
818
+ return parseErrors.length === 0 ? true : false;
819
+ }
820
+ catch (ex) {
821
+ return false;
822
+ }
823
+ }