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