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