@jetstreamapp/soql-parser-js 6.1.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.
- package/README.md +2 -3
- package/dist/cjs/index.js +143 -2
- package/dist/cli/index.js +148 -113
- package/dist/esm/index.js +143 -4
- package/dist/{cjs/src → types}/index.d.ts +1 -1
- package/package.json +12 -20
- package/dist/cjs/index.js.LICENSE.txt +0 -1
- package/dist/cjs/main.licenses.txt +0 -898
- package/dist/cjs/src/api/api-models.js +0 -2
- package/dist/cjs/src/api/public-utils.js +0 -175
- package/dist/cjs/src/composer/composer.js +0 -295
- package/dist/cjs/src/formatter/formatter.js +0 -236
- package/dist/cjs/src/index.js +0 -23
- package/dist/cjs/src/models.js +0 -2
- package/dist/cjs/src/parser/lexer.js +0 -954
- package/dist/cjs/src/parser/parser.js +0 -697
- package/dist/cjs/src/parser/visitor.js +0 -856
- package/dist/cjs/src/utils.js +0 -245
- package/dist/esm/api/api-models.js +0 -1
- package/dist/esm/api/public-utils.js +0 -153
- package/dist/esm/composer/composer.js +0 -273
- package/dist/esm/formatter/formatter.js +0 -223
- package/dist/esm/models.js +0 -1
- package/dist/esm/parser/lexer.js +0 -947
- package/dist/esm/parser/parser.js +0 -667
- package/dist/esm/parser/visitor.js +0 -823
- package/dist/esm/utils.js +0 -200
- package/dist/src/api/api-models.js +0 -2
- package/dist/src/api/public-utils.js +0 -203
- package/dist/src/composer/composer.js +0 -358
- package/dist/src/formatter/formatter.js +0 -263
- package/dist/src/index.js +0 -28
- package/dist/src/models.js +0 -2
- package/dist/src/parser/lexer.js +0 -997
- package/dist/src/parser/parser.js +0 -711
- package/dist/src/parser/visitor.js +0 -862
- package/dist/src/utils.js +0 -251
- /package/dist/{cjs/src → types}/api/api-models.d.ts +0 -0
- /package/dist/{cjs/src → types}/api/public-utils.d.ts +0 -0
- /package/dist/{cjs/src → types}/composer/composer.d.ts +0 -0
- /package/dist/{cjs/src → types}/formatter/formatter.d.ts +0 -0
- /package/dist/{cjs/src → types}/models.d.ts +0 -0
- /package/dist/{cjs/src → types}/parser/lexer.d.ts +0 -0
- /package/dist/{cjs/src → types}/parser/parser.d.ts +0 -0
- /package/dist/{cjs/src → types}/parser/visitor.d.ts +0 -0
- /package/dist/{cjs/src → types}/utils.d.ts +0 -0
package/dist/cjs/src/utils.js
DELETED
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getWhereValue = exports.isArrayOperator = exports.isGroupByFn = exports.isGroupByField = exports.isOrderByFn = exports.isOrderByField = exports.isValueQueryCondition = exports.isNegationCondition = exports.isValueFunctionCondition = exports.isValueWithDateNLiteralCondition = exports.isValueWithDateLiteralCondition = exports.isValueCondition = exports.isWhereOrHavingClauseWithRightCondition = exports.isHavingClauseWithRightCondition = exports.isWhereClauseWithRightCondition = exports.isFieldSubquery = exports.isSubquery = exports.isComposeFieldTypeof = exports.isComposeFieldSubquery = exports.isComposeFieldRelationship = exports.isComposeFieldFunction = exports.isComposeField = exports.hasAlias = exports.isNestedParamAggregateFunction = exports.getParams = exports.generateParens = exports.pad = exports.getAsArrayStr = exports.getLastItem = exports.getIfTrue = exports.get = exports.isNil = exports.isObject = exports.isBoolean = exports.isNumber = exports.isStringArray = exports.isString = exports.isSubqueryFromFlag = exports.isToken = void 0;
|
|
4
|
-
function isToken(val) {
|
|
5
|
-
val = Array.isArray(val) ? val[0] : val;
|
|
6
|
-
return val.image && true;
|
|
7
|
-
}
|
|
8
|
-
exports.isToken = isToken;
|
|
9
|
-
function isSubqueryFromFlag(val, isSubquery) {
|
|
10
|
-
return isSubquery;
|
|
11
|
-
}
|
|
12
|
-
exports.isSubqueryFromFlag = isSubqueryFromFlag;
|
|
13
|
-
function isString(val) {
|
|
14
|
-
return typeof val === 'string';
|
|
15
|
-
}
|
|
16
|
-
exports.isString = isString;
|
|
17
|
-
function isStringArray(val) {
|
|
18
|
-
if (!val) {
|
|
19
|
-
return false;
|
|
20
|
-
}
|
|
21
|
-
return val.every(function (item) { return isString(item); });
|
|
22
|
-
}
|
|
23
|
-
exports.isStringArray = isStringArray;
|
|
24
|
-
function isNumber(val) {
|
|
25
|
-
return Number.isFinite(val);
|
|
26
|
-
}
|
|
27
|
-
exports.isNumber = isNumber;
|
|
28
|
-
function isBoolean(val) {
|
|
29
|
-
return typeof val === typeof true;
|
|
30
|
-
}
|
|
31
|
-
exports.isBoolean = isBoolean;
|
|
32
|
-
function isObject(val) {
|
|
33
|
-
return val instanceof Object;
|
|
34
|
-
}
|
|
35
|
-
exports.isObject = isObject;
|
|
36
|
-
function isNil(val) {
|
|
37
|
-
return val === null || val === undefined;
|
|
38
|
-
}
|
|
39
|
-
exports.isNil = isNil;
|
|
40
|
-
function get(val, suffix, prefix) {
|
|
41
|
-
return isNil(val) ? '' : "".concat(prefix || '').concat(val).concat(suffix || '');
|
|
42
|
-
}
|
|
43
|
-
exports.get = get;
|
|
44
|
-
function getIfTrue(val, returnStr) {
|
|
45
|
-
return isBoolean(val) && val ? returnStr : '';
|
|
46
|
-
}
|
|
47
|
-
exports.getIfTrue = getIfTrue;
|
|
48
|
-
function getLastItem(arr) {
|
|
49
|
-
return arr[arr.length - 1];
|
|
50
|
-
}
|
|
51
|
-
exports.getLastItem = getLastItem;
|
|
52
|
-
function getAsArrayStr(val, alwaysParens) {
|
|
53
|
-
if (alwaysParens === void 0) { alwaysParens = false; }
|
|
54
|
-
if (Array.isArray(val)) {
|
|
55
|
-
if (val.length > 0) {
|
|
56
|
-
return "(".concat(val.join(', '), ")");
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
return alwaysParens ? '()' : '';
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
return alwaysParens ? "(".concat(val || '', ")") : val || '';
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
exports.getAsArrayStr = getAsArrayStr;
|
|
67
|
-
function pad(val, len, left) {
|
|
68
|
-
if (left === void 0) { left = 0; }
|
|
69
|
-
var leftPad = left > 0 ? new Array(left).fill(' ').join('') : '';
|
|
70
|
-
if (val.length > len) {
|
|
71
|
-
return "".concat(leftPad).concat(val);
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
return "".concat(leftPad).concat(val).concat(new Array(len - val.length).fill(' ').join(''));
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
exports.pad = pad;
|
|
78
|
-
function generateParens(count, character, joinCharacter) {
|
|
79
|
-
if (joinCharacter === void 0) { joinCharacter = ''; }
|
|
80
|
-
return isNumber(count) && count > 0 ? new Array(count).fill(character).join(joinCharacter) : '';
|
|
81
|
-
}
|
|
82
|
-
exports.generateParens = generateParens;
|
|
83
|
-
function getParams(functionFieldExp) {
|
|
84
|
-
if (!functionFieldExp.parameters || functionFieldExp.parameters.length === 0) {
|
|
85
|
-
return [];
|
|
86
|
-
}
|
|
87
|
-
if (isStringArray(functionFieldExp.parameters)) {
|
|
88
|
-
return functionFieldExp.parameters;
|
|
89
|
-
}
|
|
90
|
-
if (isString(functionFieldExp.parameters[0])) {
|
|
91
|
-
return [functionFieldExp.parameters[0]];
|
|
92
|
-
}
|
|
93
|
-
return getParams(functionFieldExp.parameters[0]);
|
|
94
|
-
}
|
|
95
|
-
exports.getParams = getParams;
|
|
96
|
-
function isNestedParamAggregateFunction(functionFieldExp) {
|
|
97
|
-
if (!functionFieldExp.parameters || functionFieldExp.parameters.length === 0) {
|
|
98
|
-
return false;
|
|
99
|
-
}
|
|
100
|
-
var parameter = functionFieldExp.parameters[0];
|
|
101
|
-
if (isString(parameter)) {
|
|
102
|
-
return false;
|
|
103
|
-
}
|
|
104
|
-
return !!parameter.isAggregateFn;
|
|
105
|
-
}
|
|
106
|
-
exports.isNestedParamAggregateFunction = isNestedParamAggregateFunction;
|
|
107
|
-
function hasAlias(value) {
|
|
108
|
-
return value && !isNil(value.alias);
|
|
109
|
-
}
|
|
110
|
-
exports.hasAlias = hasAlias;
|
|
111
|
-
function isComposeField(input) {
|
|
112
|
-
return isString(input.field) && !Array.isArray(input.relationships) && !Array.isArray(input.conditions);
|
|
113
|
-
}
|
|
114
|
-
exports.isComposeField = isComposeField;
|
|
115
|
-
function isComposeFieldFunction(input) {
|
|
116
|
-
return !isNil(input.functionName || input.fn);
|
|
117
|
-
}
|
|
118
|
-
exports.isComposeFieldFunction = isComposeFieldFunction;
|
|
119
|
-
function isComposeFieldRelationship(input) {
|
|
120
|
-
return isString(input.field) && Array.isArray(input.relationships);
|
|
121
|
-
}
|
|
122
|
-
exports.isComposeFieldRelationship = isComposeFieldRelationship;
|
|
123
|
-
function isComposeFieldSubquery(input) {
|
|
124
|
-
return !isNil(input.subquery);
|
|
125
|
-
}
|
|
126
|
-
exports.isComposeFieldSubquery = isComposeFieldSubquery;
|
|
127
|
-
function isComposeFieldTypeof(input) {
|
|
128
|
-
return isString(input.field) && Array.isArray(input.conditions);
|
|
129
|
-
}
|
|
130
|
-
exports.isComposeFieldTypeof = isComposeFieldTypeof;
|
|
131
|
-
function isSubquery(query) {
|
|
132
|
-
return isString(query.relationshipName);
|
|
133
|
-
}
|
|
134
|
-
exports.isSubquery = isSubquery;
|
|
135
|
-
function isFieldSubquery(value) {
|
|
136
|
-
return !!value && !!value.type && value.type === 'FieldSubquery';
|
|
137
|
-
}
|
|
138
|
-
exports.isFieldSubquery = isFieldSubquery;
|
|
139
|
-
function isWhereClauseWithRightCondition(value) {
|
|
140
|
-
return !!value && !!value.operator && !!value.right;
|
|
141
|
-
}
|
|
142
|
-
exports.isWhereClauseWithRightCondition = isWhereClauseWithRightCondition;
|
|
143
|
-
function isHavingClauseWithRightCondition(value) {
|
|
144
|
-
return !!value && !!value.operator && !!value.right;
|
|
145
|
-
}
|
|
146
|
-
exports.isHavingClauseWithRightCondition = isHavingClauseWithRightCondition;
|
|
147
|
-
function isWhereOrHavingClauseWithRightCondition(value) {
|
|
148
|
-
return !!value && !!value.operator && !!value.right;
|
|
149
|
-
}
|
|
150
|
-
exports.isWhereOrHavingClauseWithRightCondition = isWhereOrHavingClauseWithRightCondition;
|
|
151
|
-
function isValueCondition(value) {
|
|
152
|
-
return value && isString(value.field) && isString(value.operator) && !isNil(value.value);
|
|
153
|
-
}
|
|
154
|
-
exports.isValueCondition = isValueCondition;
|
|
155
|
-
function isValueWithDateLiteralCondition(value) {
|
|
156
|
-
return (value &&
|
|
157
|
-
isString(value.field) &&
|
|
158
|
-
isString(value.operator) &&
|
|
159
|
-
!isNil(value.value) &&
|
|
160
|
-
(value.literalType === 'DATE_LITERAL' || (Array.isArray(value.literalType) && value.literalType[0] === 'DATE_LITERAL')));
|
|
161
|
-
}
|
|
162
|
-
exports.isValueWithDateLiteralCondition = isValueWithDateLiteralCondition;
|
|
163
|
-
function isValueWithDateNLiteralCondition(value) {
|
|
164
|
-
return value && isString(value.field) && isString(value.operator) && !isNil(value.value) && !isNil(value.dateLiteralVariable);
|
|
165
|
-
}
|
|
166
|
-
exports.isValueWithDateNLiteralCondition = isValueWithDateNLiteralCondition;
|
|
167
|
-
function isValueFunctionCondition(value) {
|
|
168
|
-
return value && !isNil(value.fn) && isString(value.operator) && !isNil(value.value);
|
|
169
|
-
}
|
|
170
|
-
exports.isValueFunctionCondition = isValueFunctionCondition;
|
|
171
|
-
function isNegationCondition(value) {
|
|
172
|
-
return value && isNumber(value.openParen) && isNil(value.operator) && isNil(value.field) && isNil(value.fn) && isNil(value.closeParen);
|
|
173
|
-
}
|
|
174
|
-
exports.isNegationCondition = isNegationCondition;
|
|
175
|
-
function isValueQueryCondition(value) {
|
|
176
|
-
return value && isString(value.field) && isString(value.operator) && !isNil(value.valueQuery) && isNil(value.value);
|
|
177
|
-
}
|
|
178
|
-
exports.isValueQueryCondition = isValueQueryCondition;
|
|
179
|
-
function isOrderByField(value) {
|
|
180
|
-
return value && !isNil(value.field);
|
|
181
|
-
}
|
|
182
|
-
exports.isOrderByField = isOrderByField;
|
|
183
|
-
function isOrderByFn(value) {
|
|
184
|
-
return value && !isNil(value.fn);
|
|
185
|
-
}
|
|
186
|
-
exports.isOrderByFn = isOrderByFn;
|
|
187
|
-
function isGroupByField(value) {
|
|
188
|
-
return value && !isNil(value.field);
|
|
189
|
-
}
|
|
190
|
-
exports.isGroupByField = isGroupByField;
|
|
191
|
-
function isGroupByFn(value) {
|
|
192
|
-
return value && !isNil(value.fn);
|
|
193
|
-
}
|
|
194
|
-
exports.isGroupByFn = isGroupByFn;
|
|
195
|
-
function isArrayOperator(operator) {
|
|
196
|
-
return ['IN', 'NOT IN', 'INCLUDES', 'EXCLUDES'].includes(operator);
|
|
197
|
-
}
|
|
198
|
-
exports.isArrayOperator = isArrayOperator;
|
|
199
|
-
function getWhereValue(value, literalType, operator) {
|
|
200
|
-
if (isNil(literalType)) {
|
|
201
|
-
return value;
|
|
202
|
-
}
|
|
203
|
-
if (operator && literalType !== 'APEX_BIND_VARIABLE' && isArrayOperator(operator) && !Array.isArray(value)) {
|
|
204
|
-
value = [value];
|
|
205
|
-
literalType = Array.isArray(literalType) ? literalType : [literalType];
|
|
206
|
-
}
|
|
207
|
-
if (Array.isArray(literalType) && Array.isArray(value)) {
|
|
208
|
-
return value.map(function (val, i) {
|
|
209
|
-
return whereValueHelper(val, literalType === null || literalType === void 0 ? void 0 : literalType[i]);
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
else {
|
|
213
|
-
if (Array.isArray(literalType)) {
|
|
214
|
-
literalType = literalType[0];
|
|
215
|
-
}
|
|
216
|
-
switch (literalType) {
|
|
217
|
-
case 'STRING': {
|
|
218
|
-
if (Array.isArray(value)) {
|
|
219
|
-
return value.filter(Boolean).map(function (val) { return (isString(val) && val.startsWith("'") ? val : "'".concat(val !== null && val !== void 0 ? val : '', "'")); });
|
|
220
|
-
}
|
|
221
|
-
else {
|
|
222
|
-
value = String(value !== null && value !== void 0 ? value : '');
|
|
223
|
-
return isString(value) && value.startsWith("'") ? value : "'".concat(value !== null && value !== void 0 ? value : '', "'");
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
case 'APEX_BIND_VARIABLE': {
|
|
227
|
-
return ":".concat(value);
|
|
228
|
-
}
|
|
229
|
-
default: {
|
|
230
|
-
return value;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
exports.getWhereValue = getWhereValue;
|
|
236
|
-
function whereValueHelper(value, literalType) {
|
|
237
|
-
switch (literalType) {
|
|
238
|
-
case 'STRING': {
|
|
239
|
-
return isString(value) && value.startsWith("'") ? value : "'".concat(value !== null && value !== void 0 ? value : '', "'");
|
|
240
|
-
}
|
|
241
|
-
default: {
|
|
242
|
-
return value;
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
import { getParams, hasAlias, isComposeField, isComposeFieldFunction, isComposeFieldRelationship, isComposeFieldSubquery, isComposeFieldTypeof, isFieldSubquery, isGroupByField, isGroupByFn, isHavingClauseWithRightCondition, isNegationCondition, isNestedParamAggregateFunction, isOrderByField, isOrderByFn, isString, isSubquery, isValueCondition, isValueFunctionCondition, isValueQueryCondition, isValueWithDateLiteralCondition, isValueWithDateNLiteralCondition, isWhereClauseWithRightCondition, isWhereOrHavingClauseWithRightCondition, } from '../utils';
|
|
2
|
-
export { hasAlias, isFieldSubquery, isGroupByField, isGroupByFn, isHavingClauseWithRightCondition, isNegationCondition, isOrderByField, isOrderByFn, isString, isSubquery, isValueCondition, isValueFunctionCondition, isValueQueryCondition, isValueWithDateLiteralCondition, isValueWithDateNLiteralCondition, isWhereClauseWithRightCondition, isWhereOrHavingClauseWithRightCondition, };
|
|
3
|
-
export function getComposedField(input) {
|
|
4
|
-
return getField(input);
|
|
5
|
-
}
|
|
6
|
-
export function getField(input) {
|
|
7
|
-
if (typeof input === 'string') {
|
|
8
|
-
return {
|
|
9
|
-
type: 'Field',
|
|
10
|
-
field: input,
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
else if (isComposeFieldFunction(input)) {
|
|
14
|
-
let parameters = [];
|
|
15
|
-
if (input.parameters) {
|
|
16
|
-
parameters = (Array.isArray(input.parameters) ? input.parameters : [input.parameters]);
|
|
17
|
-
}
|
|
18
|
-
return {
|
|
19
|
-
type: 'FieldFunctionExpression',
|
|
20
|
-
functionName: (input.functionName || input.fn),
|
|
21
|
-
parameters,
|
|
22
|
-
alias: input.alias,
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
else if (isComposeFieldRelationship(input)) {
|
|
26
|
-
return {
|
|
27
|
-
type: 'FieldRelationship',
|
|
28
|
-
field: input.field,
|
|
29
|
-
relationships: input.relationships,
|
|
30
|
-
objectPrefix: input.objectPrefix,
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
else if (isComposeFieldSubquery(input)) {
|
|
34
|
-
return {
|
|
35
|
-
type: 'FieldSubquery',
|
|
36
|
-
subquery: input.subquery,
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
else if (isComposeFieldTypeof(input)) {
|
|
40
|
-
return {
|
|
41
|
-
type: 'FieldTypeof',
|
|
42
|
-
field: input.field,
|
|
43
|
-
conditions: input.conditions,
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
else if (isComposeField(input)) {
|
|
47
|
-
return {
|
|
48
|
-
type: 'Field',
|
|
49
|
-
field: input.field,
|
|
50
|
-
objectPrefix: input.objectPrefix,
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
throw new TypeError('The input object provided did not match any valid field types');
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
export function getFlattenedFields(query, isAggregateResult) {
|
|
58
|
-
if (!query) {
|
|
59
|
-
return [];
|
|
60
|
-
}
|
|
61
|
-
query = isFieldSubquery(query) ? query.subquery : query;
|
|
62
|
-
const fields = query.fields;
|
|
63
|
-
if (!fields) {
|
|
64
|
-
return [];
|
|
65
|
-
}
|
|
66
|
-
let groupByFields = {};
|
|
67
|
-
if (!!query.groupBy) {
|
|
68
|
-
groupByFields = (Array.isArray(query.groupBy) ? query.groupBy : [query.groupBy]).reduce((output, clause) => {
|
|
69
|
-
if (isGroupByField(clause)) {
|
|
70
|
-
output[clause.field.toLocaleLowerCase()] = clause.field;
|
|
71
|
-
}
|
|
72
|
-
return output;
|
|
73
|
-
}, {});
|
|
74
|
-
}
|
|
75
|
-
let currUnAliasedAggExp = -1;
|
|
76
|
-
let sObject = (isSubquery(query) ? query.relationshipName : query.sObject || '').toLowerCase();
|
|
77
|
-
let sObjectAlias = (query.sObjectAlias || '').toLowerCase();
|
|
78
|
-
const parsedFields = fields
|
|
79
|
-
.flatMap(field => {
|
|
80
|
-
switch (field.type) {
|
|
81
|
-
case 'Field': {
|
|
82
|
-
return field.alias || field.field;
|
|
83
|
-
}
|
|
84
|
-
case 'FieldFunctionExpression': {
|
|
85
|
-
let params = getParams(field);
|
|
86
|
-
params = params.map(param => {
|
|
87
|
-
if (param.includes('.')) {
|
|
88
|
-
let tempParams = param.split('.');
|
|
89
|
-
const firstParam = tempParams[0].toLowerCase();
|
|
90
|
-
if (firstParam === sObjectAlias || firstParam === sObject) {
|
|
91
|
-
tempParams = tempParams.slice(1);
|
|
92
|
-
}
|
|
93
|
-
return tempParams.join('.');
|
|
94
|
-
}
|
|
95
|
-
return param;
|
|
96
|
-
});
|
|
97
|
-
if (field.alias && (field.isAggregateFn || isAggregateResult)) {
|
|
98
|
-
return field.alias;
|
|
99
|
-
}
|
|
100
|
-
if (field.alias) {
|
|
101
|
-
const firstParam = params[0];
|
|
102
|
-
if (firstParam.includes('.')) {
|
|
103
|
-
params = firstParam.split('.').slice(0, -1);
|
|
104
|
-
params.push(field.alias);
|
|
105
|
-
return params.join('.');
|
|
106
|
-
}
|
|
107
|
-
return field.alias;
|
|
108
|
-
}
|
|
109
|
-
if (field.isAggregateFn || isNestedParamAggregateFunction(field) || isAggregateResult) {
|
|
110
|
-
currUnAliasedAggExp++;
|
|
111
|
-
return `expr${currUnAliasedAggExp}`;
|
|
112
|
-
}
|
|
113
|
-
if (params.length > 0) {
|
|
114
|
-
return params.join('.');
|
|
115
|
-
}
|
|
116
|
-
return field.functionName;
|
|
117
|
-
}
|
|
118
|
-
case 'FieldRelationship': {
|
|
119
|
-
const firstRelationship = field.relationships[0].toLowerCase();
|
|
120
|
-
if (hasAlias(field)) {
|
|
121
|
-
return field.alias;
|
|
122
|
-
}
|
|
123
|
-
if (field.rawValue && groupByFields[field.rawValue.toLocaleLowerCase()]) {
|
|
124
|
-
return field.field;
|
|
125
|
-
}
|
|
126
|
-
if (firstRelationship === sObjectAlias || firstRelationship === sObject) {
|
|
127
|
-
return field.relationships.concat([field.field]).slice(1).join('.');
|
|
128
|
-
}
|
|
129
|
-
return field.relationships.concat([field.field]).join('.');
|
|
130
|
-
}
|
|
131
|
-
case 'FieldSubquery': {
|
|
132
|
-
return field.subquery.relationshipName;
|
|
133
|
-
}
|
|
134
|
-
case 'FieldTypeof': {
|
|
135
|
-
const priorFields = new Set();
|
|
136
|
-
const fields = [];
|
|
137
|
-
field.conditions.forEach(condition => {
|
|
138
|
-
condition.fieldList.forEach(currField => {
|
|
139
|
-
if (!priorFields.has(currField)) {
|
|
140
|
-
priorFields.add(currField);
|
|
141
|
-
fields.push(`${field.field}.${currField}`);
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
});
|
|
145
|
-
return fields;
|
|
146
|
-
}
|
|
147
|
-
default:
|
|
148
|
-
break;
|
|
149
|
-
}
|
|
150
|
-
})
|
|
151
|
-
.filter(field => isString(field));
|
|
152
|
-
return parsedFields;
|
|
153
|
-
}
|
|
@@ -1,273 +0,0 @@
|
|
|
1
|
-
import * as utils from '../utils';
|
|
2
|
-
import { Formatter } from '../formatter/formatter';
|
|
3
|
-
import { parseQuery } from '../parser/visitor';
|
|
4
|
-
export function formatQuery(soql, formatOptions, parseOptions) {
|
|
5
|
-
return composeQuery(parseQuery(soql, parseOptions), { format: true, formatOptions });
|
|
6
|
-
}
|
|
7
|
-
export function composeQuery(soql, config = {}) {
|
|
8
|
-
if (!soql) {
|
|
9
|
-
return '';
|
|
10
|
-
}
|
|
11
|
-
config = config || {};
|
|
12
|
-
config.format = config.format ? true : false;
|
|
13
|
-
if (config.logging) {
|
|
14
|
-
console.time('composer');
|
|
15
|
-
console.log('Composing Query:', soql);
|
|
16
|
-
console.log('Format output:', config.format);
|
|
17
|
-
}
|
|
18
|
-
const query = new Compose(soql, config).query;
|
|
19
|
-
if (config.logging) {
|
|
20
|
-
console.timeEnd('composer');
|
|
21
|
-
}
|
|
22
|
-
return query;
|
|
23
|
-
}
|
|
24
|
-
export class Compose {
|
|
25
|
-
soql;
|
|
26
|
-
logging = false;
|
|
27
|
-
format = false;
|
|
28
|
-
query;
|
|
29
|
-
formatter;
|
|
30
|
-
constructor(soql, config = {}) {
|
|
31
|
-
this.soql = soql;
|
|
32
|
-
config = { autoCompose: true, ...config };
|
|
33
|
-
const { logging, format } = config;
|
|
34
|
-
this.logging = !!logging;
|
|
35
|
-
this.format = !!format;
|
|
36
|
-
this.query = '';
|
|
37
|
-
this.formatter = new Formatter(this.format, {
|
|
38
|
-
logging: this.logging,
|
|
39
|
-
...config.formatOptions,
|
|
40
|
-
});
|
|
41
|
-
if (config.autoCompose) {
|
|
42
|
-
this.start();
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
start() {
|
|
46
|
-
this.query = this.parseQuery(this.soql);
|
|
47
|
-
}
|
|
48
|
-
log(soql) {
|
|
49
|
-
if (this.logging) {
|
|
50
|
-
console.log('Current SOQL:', soql);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
parseFn(fn) {
|
|
54
|
-
let output;
|
|
55
|
-
if (fn.rawValue) {
|
|
56
|
-
output = fn.rawValue;
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
output = fn.functionName;
|
|
60
|
-
output += `(${(fn.parameters || []).map(param => (utils.isString(param) ? param : this.parseFn(param))).join(', ')})`;
|
|
61
|
-
}
|
|
62
|
-
if (fn.alias) {
|
|
63
|
-
output += ` ${fn.alias}`;
|
|
64
|
-
}
|
|
65
|
-
return output;
|
|
66
|
-
}
|
|
67
|
-
parseQuery(query) {
|
|
68
|
-
const fieldData = {
|
|
69
|
-
fields: this.parseFields(query.fields || []).map(field => ({
|
|
70
|
-
text: field.text,
|
|
71
|
-
typeOfClause: field.typeOfClause,
|
|
72
|
-
isSubquery: field.text.startsWith('('),
|
|
73
|
-
prefix: '',
|
|
74
|
-
suffix: '',
|
|
75
|
-
})),
|
|
76
|
-
isSubquery: utils.isSubquery(query),
|
|
77
|
-
lineBreaks: [],
|
|
78
|
-
};
|
|
79
|
-
let output = '';
|
|
80
|
-
if (query.fields) {
|
|
81
|
-
output += this.formatter.formatClause('SELECT').trimStart();
|
|
82
|
-
}
|
|
83
|
-
this.formatter.formatFields(fieldData);
|
|
84
|
-
let fieldsOutput = '';
|
|
85
|
-
fieldData.fields.forEach(field => {
|
|
86
|
-
if (Array.isArray(field.typeOfClause)) {
|
|
87
|
-
fieldsOutput += `${field.prefix}${this.formatter.formatTyeOfField(field.text, field.typeOfClause)}${field.suffix}`;
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
fieldsOutput += `${field.prefix}${field.text}${field.suffix}`;
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
output += this.formatter.formatText(fieldsOutput);
|
|
94
|
-
if (!!(utils.isSubquery(query) ? query.relationshipName : query.sObject)) {
|
|
95
|
-
output += this.formatter.formatClause('FROM');
|
|
96
|
-
}
|
|
97
|
-
if (utils.isSubquery(query)) {
|
|
98
|
-
const sObjectPrefix = query.sObjectPrefix || [];
|
|
99
|
-
sObjectPrefix.push(query.relationshipName);
|
|
100
|
-
output += this.formatter.formatText(`${sObjectPrefix.join('.')}${utils.get(query.sObjectAlias, '', ' ')}`);
|
|
101
|
-
}
|
|
102
|
-
else if (query.sObject) {
|
|
103
|
-
output += this.formatter.formatText(`${query.sObject}${utils.get(query.sObjectAlias, '', ' ')}`);
|
|
104
|
-
}
|
|
105
|
-
this.log(output);
|
|
106
|
-
if (query.usingScope) {
|
|
107
|
-
output += this.formatter.formatClause('USING SCOPE');
|
|
108
|
-
output += this.formatter.formatText(query.usingScope);
|
|
109
|
-
this.log(output);
|
|
110
|
-
}
|
|
111
|
-
if (query.where) {
|
|
112
|
-
output += this.formatter.formatClause('WHERE');
|
|
113
|
-
output += this.formatter.formatText(this.parseWhereOrHavingClause(query.where));
|
|
114
|
-
this.log(output);
|
|
115
|
-
}
|
|
116
|
-
if (query.groupBy) {
|
|
117
|
-
output += this.formatter.formatClause('GROUP BY');
|
|
118
|
-
output += this.formatter.formatText(this.parseGroupByClause(query.groupBy));
|
|
119
|
-
this.log(output);
|
|
120
|
-
if (query.having) {
|
|
121
|
-
output += this.formatter.formatClause('HAVING');
|
|
122
|
-
output += this.formatter.formatText(this.parseWhereOrHavingClause(query.having));
|
|
123
|
-
this.log(output);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
if (query.orderBy && (!Array.isArray(query.orderBy) || query.orderBy.length > 0)) {
|
|
127
|
-
output += this.formatter.formatClause('ORDER BY');
|
|
128
|
-
output += this.formatter.formatText(this.parseOrderBy(query.orderBy));
|
|
129
|
-
this.log(output);
|
|
130
|
-
}
|
|
131
|
-
if (utils.isNumber(query.limit)) {
|
|
132
|
-
output += this.formatter.formatClause('LIMIT');
|
|
133
|
-
output += this.formatter.formatText(`${query.limit}`);
|
|
134
|
-
this.log(output);
|
|
135
|
-
}
|
|
136
|
-
if (utils.isNumber(query.offset)) {
|
|
137
|
-
output += this.formatter.formatClause('OFFSET');
|
|
138
|
-
output += this.formatter.formatText(`${query.offset}`);
|
|
139
|
-
this.log(output);
|
|
140
|
-
}
|
|
141
|
-
if (query.withDataCategory) {
|
|
142
|
-
output += this.formatter.formatClause('WITH DATA CATEGORY');
|
|
143
|
-
output += this.formatter.formatText(this.parseWithDataCategory(query.withDataCategory));
|
|
144
|
-
this.log(output);
|
|
145
|
-
}
|
|
146
|
-
if (query.withSecurityEnforced) {
|
|
147
|
-
output += this.formatter.formatClause('WITH SECURITY_ENFORCED');
|
|
148
|
-
this.log(output);
|
|
149
|
-
}
|
|
150
|
-
if (query.withAccessLevel) {
|
|
151
|
-
output += this.formatter.formatClause(`WITH ${query.withAccessLevel}`);
|
|
152
|
-
this.log(output);
|
|
153
|
-
}
|
|
154
|
-
if (query.for) {
|
|
155
|
-
output += this.formatter.formatClause('FOR');
|
|
156
|
-
output += this.formatter.formatText(query.for);
|
|
157
|
-
this.log(output);
|
|
158
|
-
}
|
|
159
|
-
if (query.update) {
|
|
160
|
-
output += this.formatter.formatClause('UPDATE');
|
|
161
|
-
output += this.formatter.formatText(query.update);
|
|
162
|
-
this.log(output);
|
|
163
|
-
}
|
|
164
|
-
return output.trim();
|
|
165
|
-
}
|
|
166
|
-
parseFields(fields) {
|
|
167
|
-
return fields.map(field => {
|
|
168
|
-
let text = '';
|
|
169
|
-
let typeOfClause;
|
|
170
|
-
const objPrefix = field.objectPrefix ? `${field.objectPrefix}.` : '';
|
|
171
|
-
switch (field.type) {
|
|
172
|
-
case 'Field': {
|
|
173
|
-
text = `${objPrefix}${field.field}${field.alias ? ` ${field.alias}` : ''}`;
|
|
174
|
-
break;
|
|
175
|
-
}
|
|
176
|
-
case 'FieldFunctionExpression': {
|
|
177
|
-
let params = '';
|
|
178
|
-
if (field.parameters) {
|
|
179
|
-
params = field.parameters
|
|
180
|
-
.map(param => (utils.isString(param) ? param : this.parseFields([param]).map(param => param.text)))
|
|
181
|
-
.join(', ');
|
|
182
|
-
}
|
|
183
|
-
text = `${field.functionName}(${params})${field.alias ? ` ${field.alias}` : ''}`;
|
|
184
|
-
break;
|
|
185
|
-
}
|
|
186
|
-
case 'FieldRelationship': {
|
|
187
|
-
text = `${objPrefix}${field.relationships.join('.')}.${field.field}${utils.hasAlias(field) ? ` ${field.alias}` : ''}`;
|
|
188
|
-
break;
|
|
189
|
-
}
|
|
190
|
-
case 'FieldSubquery': {
|
|
191
|
-
text = this.formatter.formatSubquery(this.parseQuery(field.subquery));
|
|
192
|
-
break;
|
|
193
|
-
}
|
|
194
|
-
case 'FieldTypeof': {
|
|
195
|
-
typeOfClause = this.parseTypeOfField(field);
|
|
196
|
-
text = typeOfClause.join(' ');
|
|
197
|
-
break;
|
|
198
|
-
}
|
|
199
|
-
default:
|
|
200
|
-
break;
|
|
201
|
-
}
|
|
202
|
-
return { text, typeOfClause };
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
parseTypeOfField(typeOfField) {
|
|
206
|
-
const output = [`TYPEOF ${typeOfField.field}`].concat(typeOfField.conditions.map(condition => this.formatter.formatTypeofFieldCondition(condition)));
|
|
207
|
-
output.push(`END`);
|
|
208
|
-
return output;
|
|
209
|
-
}
|
|
210
|
-
parseWhereOrHavingClause(whereOrHaving, tabOffset = 0, priorConditionIsNegation = false) {
|
|
211
|
-
let output = '';
|
|
212
|
-
const left = whereOrHaving.left;
|
|
213
|
-
let trimPrecedingOutput = false;
|
|
214
|
-
if (left) {
|
|
215
|
-
output += this.formatter.formatParens(left.openParen, '(', utils.isNegationCondition(left));
|
|
216
|
-
if (!utils.isNegationCondition(left)) {
|
|
217
|
-
tabOffset = tabOffset + (left.openParen || 0) - (left.closeParen || 0);
|
|
218
|
-
if (priorConditionIsNegation) {
|
|
219
|
-
tabOffset++;
|
|
220
|
-
}
|
|
221
|
-
let expression = '';
|
|
222
|
-
expression += utils.isValueFunctionCondition(left) ? this.parseFn(left.fn) : left.field;
|
|
223
|
-
expression += ` ${left.operator} `;
|
|
224
|
-
if (utils.isValueQueryCondition(left)) {
|
|
225
|
-
expression += this.formatter.formatSubquery(this.parseQuery(left.valueQuery), 1, true);
|
|
226
|
-
}
|
|
227
|
-
else {
|
|
228
|
-
expression += utils.getAsArrayStr(utils.getWhereValue(left.value, left.literalType, left.operator));
|
|
229
|
-
}
|
|
230
|
-
output += this.formatter.formatWithIndent(expression);
|
|
231
|
-
output += this.formatter.formatParens(left.closeParen, ')', priorConditionIsNegation);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
if (utils.isWhereOrHavingClauseWithRightCondition(whereOrHaving)) {
|
|
235
|
-
const operator = utils.get(whereOrHaving.operator);
|
|
236
|
-
trimPrecedingOutput = operator === 'NOT';
|
|
237
|
-
const formattedData = this.formatter.formatWhereClauseOperators(operator, this.parseWhereOrHavingClause(whereOrHaving.right, tabOffset, utils.isNegationCondition(left)), tabOffset);
|
|
238
|
-
return `${trimPrecedingOutput ? output.trimRight() : output}${formattedData}`.trim();
|
|
239
|
-
}
|
|
240
|
-
else {
|
|
241
|
-
return output.trim();
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
parseGroupByClause(groupBy) {
|
|
245
|
-
return (Array.isArray(groupBy) ? groupBy : [groupBy])
|
|
246
|
-
.map(clause => (utils.isGroupByField(clause) ? clause.field : this.parseFn(clause.fn)))
|
|
247
|
-
.join(', ');
|
|
248
|
-
}
|
|
249
|
-
parseOrderBy(orderBy) {
|
|
250
|
-
if (Array.isArray(orderBy)) {
|
|
251
|
-
return this.formatter.formatOrderByArray(orderBy.map(ob => this.parseOrderBy(ob)));
|
|
252
|
-
}
|
|
253
|
-
else {
|
|
254
|
-
let output = '';
|
|
255
|
-
if (utils.isOrderByField(orderBy)) {
|
|
256
|
-
output = `${utils.get(orderBy.field, ' ')}`;
|
|
257
|
-
}
|
|
258
|
-
else {
|
|
259
|
-
output += `${this.parseFn(orderBy.fn)} `;
|
|
260
|
-
}
|
|
261
|
-
output += `${utils.get(orderBy.order, ' ')}${utils.get(orderBy.nulls, '', 'NULLS ')}`;
|
|
262
|
-
return output.trim();
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
parseWithDataCategory(withDataCategory) {
|
|
266
|
-
return withDataCategory.conditions
|
|
267
|
-
.map(condition => {
|
|
268
|
-
const params = condition.parameters.length > 1 ? `(${condition.parameters.join(', ')})` : `${condition.parameters.join(', ')}`;
|
|
269
|
-
return `${condition.groupName} ${condition.selector} ${params}`;
|
|
270
|
-
})
|
|
271
|
-
.join(' AND ');
|
|
272
|
-
}
|
|
273
|
-
}
|