@jetstreamapp/soql-parser-js 6.1.0 → 6.2.1
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/CHANGELOG.md +12 -0
- package/README.md +2 -3
- package/dist/cjs/index.js +143 -2
- package/dist/cli/index.js +148 -113
- package/dist/esm/index.mjs +143 -0
- package/dist/{cjs/src → types}/index.d.ts +1 -1
- package/package.json +23 -25
- 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/index.js +0 -4
- 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
|
@@ -1,358 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Compose = exports.composeQuery = exports.formatQuery = void 0;
|
|
4
|
-
const utils = require("../utils");
|
|
5
|
-
const formatter_1 = require("../formatter/formatter");
|
|
6
|
-
const visitor_1 = require("../parser/visitor");
|
|
7
|
-
/**
|
|
8
|
-
* Formats query - This will compose and then parse a query with the provided format options
|
|
9
|
-
* or the defaults if omitted
|
|
10
|
-
* @param soql
|
|
11
|
-
* @param [formatOptions]
|
|
12
|
-
* @returns
|
|
13
|
-
*/
|
|
14
|
-
function formatQuery(soql, formatOptions, parseOptions) {
|
|
15
|
-
return composeQuery((0, visitor_1.parseQuery)(soql, parseOptions), { format: true, formatOptions });
|
|
16
|
-
}
|
|
17
|
-
exports.formatQuery = formatQuery;
|
|
18
|
-
/**
|
|
19
|
-
* Composes a parsed query back to a SOQL query
|
|
20
|
-
* The parsing methods are public in case there is a need to parse just a part of a query,
|
|
21
|
-
* but the common case is to call the "start()" method
|
|
22
|
-
* @param soql
|
|
23
|
-
* @param [config]
|
|
24
|
-
* @returns query
|
|
25
|
-
*/
|
|
26
|
-
function composeQuery(soql, config = {}) {
|
|
27
|
-
if (!soql) {
|
|
28
|
-
return '';
|
|
29
|
-
}
|
|
30
|
-
config = config || {};
|
|
31
|
-
config.format = config.format ? true : false;
|
|
32
|
-
if (config.logging) {
|
|
33
|
-
console.time('composer');
|
|
34
|
-
console.log('Composing Query:', soql);
|
|
35
|
-
console.log('Format output:', config.format);
|
|
36
|
-
}
|
|
37
|
-
const query = new Compose(soql, config).query;
|
|
38
|
-
if (config.logging) {
|
|
39
|
-
console.timeEnd('composer');
|
|
40
|
-
}
|
|
41
|
-
return query;
|
|
42
|
-
}
|
|
43
|
-
exports.composeQuery = composeQuery;
|
|
44
|
-
/**
|
|
45
|
-
* Compose
|
|
46
|
-
* This class handles all the logic for turning a Query into a SOQL query
|
|
47
|
-
* This depends on the Format class for parts of the processing
|
|
48
|
-
*/
|
|
49
|
-
class Compose {
|
|
50
|
-
soql;
|
|
51
|
-
logging = false;
|
|
52
|
-
format = false;
|
|
53
|
-
query;
|
|
54
|
-
formatter;
|
|
55
|
-
constructor(soql, config = {}) {
|
|
56
|
-
this.soql = soql;
|
|
57
|
-
config = { autoCompose: true, ...config };
|
|
58
|
-
const { logging, format } = config;
|
|
59
|
-
this.logging = !!logging;
|
|
60
|
-
this.format = !!format;
|
|
61
|
-
this.query = '';
|
|
62
|
-
this.formatter = new formatter_1.Formatter(this.format, {
|
|
63
|
-
logging: this.logging,
|
|
64
|
-
...config.formatOptions,
|
|
65
|
-
});
|
|
66
|
-
if (config.autoCompose) {
|
|
67
|
-
this.start();
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Starts compose
|
|
72
|
-
*/
|
|
73
|
-
start() {
|
|
74
|
-
this.query = this.parseQuery(this.soql);
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* If logging is enabled, print the query to the console
|
|
78
|
-
* @param soql
|
|
79
|
-
*/
|
|
80
|
-
log(soql) {
|
|
81
|
-
if (this.logging) {
|
|
82
|
-
console.log('Current SOQL:', soql);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Parses FunctionExp object
|
|
87
|
-
* Prefers functionName if populated, otherwise will fallback to rawValue
|
|
88
|
-
* @param fn
|
|
89
|
-
* @returns fn
|
|
90
|
-
*/
|
|
91
|
-
parseFn(fn) {
|
|
92
|
-
let output;
|
|
93
|
-
if (fn.rawValue) {
|
|
94
|
-
output = fn.rawValue;
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
output = fn.functionName;
|
|
98
|
-
output += `(${(fn.parameters || []).map(param => (utils.isString(param) ? param : this.parseFn(param))).join(', ')})`;
|
|
99
|
-
}
|
|
100
|
-
if (fn.alias) {
|
|
101
|
-
output += ` ${fn.alias}`;
|
|
102
|
-
}
|
|
103
|
-
return output;
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Parses query
|
|
107
|
-
* Base entry point for the query
|
|
108
|
-
* this may be called multiple times recursively for subqueries and WHERE queries
|
|
109
|
-
* @param query
|
|
110
|
-
* @returns query
|
|
111
|
-
*/
|
|
112
|
-
parseQuery(query) {
|
|
113
|
-
const fieldData = {
|
|
114
|
-
fields: this.parseFields(query.fields || []).map(field => ({
|
|
115
|
-
text: field.text,
|
|
116
|
-
typeOfClause: field.typeOfClause,
|
|
117
|
-
isSubquery: field.text.startsWith('('),
|
|
118
|
-
prefix: '',
|
|
119
|
-
suffix: '',
|
|
120
|
-
})),
|
|
121
|
-
isSubquery: utils.isSubquery(query),
|
|
122
|
-
lineBreaks: [],
|
|
123
|
-
};
|
|
124
|
-
let output = '';
|
|
125
|
-
if (query.fields) {
|
|
126
|
-
output += this.formatter.formatClause('SELECT').trimStart();
|
|
127
|
-
}
|
|
128
|
-
// Format fields based on configuration
|
|
129
|
-
this.formatter.formatFields(fieldData);
|
|
130
|
-
let fieldsOutput = '';
|
|
131
|
-
fieldData.fields.forEach(field => {
|
|
132
|
-
if (Array.isArray(field.typeOfClause)) {
|
|
133
|
-
fieldsOutput += `${field.prefix}${this.formatter.formatTyeOfField(field.text, field.typeOfClause)}${field.suffix}`;
|
|
134
|
-
}
|
|
135
|
-
else {
|
|
136
|
-
fieldsOutput += `${field.prefix}${field.text}${field.suffix}`;
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
output += this.formatter.formatText(fieldsOutput);
|
|
140
|
-
if (!!(utils.isSubquery(query) ? query.relationshipName : query.sObject)) {
|
|
141
|
-
output += this.formatter.formatClause('FROM');
|
|
142
|
-
}
|
|
143
|
-
if (utils.isSubquery(query)) {
|
|
144
|
-
const sObjectPrefix = query.sObjectPrefix || [];
|
|
145
|
-
sObjectPrefix.push(query.relationshipName);
|
|
146
|
-
output += this.formatter.formatText(`${sObjectPrefix.join('.')}${utils.get(query.sObjectAlias, '', ' ')}`);
|
|
147
|
-
}
|
|
148
|
-
else if (query.sObject) {
|
|
149
|
-
output += this.formatter.formatText(`${query.sObject}${utils.get(query.sObjectAlias, '', ' ')}`);
|
|
150
|
-
}
|
|
151
|
-
this.log(output);
|
|
152
|
-
if (query.usingScope) {
|
|
153
|
-
output += this.formatter.formatClause('USING SCOPE');
|
|
154
|
-
output += this.formatter.formatText(query.usingScope);
|
|
155
|
-
this.log(output);
|
|
156
|
-
}
|
|
157
|
-
if (query.where) {
|
|
158
|
-
output += this.formatter.formatClause('WHERE');
|
|
159
|
-
output += this.formatter.formatText(this.parseWhereOrHavingClause(query.where));
|
|
160
|
-
this.log(output);
|
|
161
|
-
}
|
|
162
|
-
if (query.groupBy) {
|
|
163
|
-
output += this.formatter.formatClause('GROUP BY');
|
|
164
|
-
output += this.formatter.formatText(this.parseGroupByClause(query.groupBy));
|
|
165
|
-
this.log(output);
|
|
166
|
-
if (query.having) {
|
|
167
|
-
output += this.formatter.formatClause('HAVING');
|
|
168
|
-
output += this.formatter.formatText(this.parseWhereOrHavingClause(query.having));
|
|
169
|
-
this.log(output);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
if (query.orderBy && (!Array.isArray(query.orderBy) || query.orderBy.length > 0)) {
|
|
173
|
-
output += this.formatter.formatClause('ORDER BY');
|
|
174
|
-
output += this.formatter.formatText(this.parseOrderBy(query.orderBy));
|
|
175
|
-
this.log(output);
|
|
176
|
-
}
|
|
177
|
-
if (utils.isNumber(query.limit)) {
|
|
178
|
-
output += this.formatter.formatClause('LIMIT');
|
|
179
|
-
output += this.formatter.formatText(`${query.limit}`);
|
|
180
|
-
this.log(output);
|
|
181
|
-
}
|
|
182
|
-
if (utils.isNumber(query.offset)) {
|
|
183
|
-
output += this.formatter.formatClause('OFFSET');
|
|
184
|
-
output += this.formatter.formatText(`${query.offset}`);
|
|
185
|
-
this.log(output);
|
|
186
|
-
}
|
|
187
|
-
if (query.withDataCategory) {
|
|
188
|
-
output += this.formatter.formatClause('WITH DATA CATEGORY');
|
|
189
|
-
output += this.formatter.formatText(this.parseWithDataCategory(query.withDataCategory));
|
|
190
|
-
this.log(output);
|
|
191
|
-
}
|
|
192
|
-
if (query.withSecurityEnforced) {
|
|
193
|
-
output += this.formatter.formatClause('WITH SECURITY_ENFORCED');
|
|
194
|
-
this.log(output);
|
|
195
|
-
}
|
|
196
|
-
if (query.withAccessLevel) {
|
|
197
|
-
output += this.formatter.formatClause(`WITH ${query.withAccessLevel}`);
|
|
198
|
-
this.log(output);
|
|
199
|
-
}
|
|
200
|
-
if (query.for) {
|
|
201
|
-
output += this.formatter.formatClause('FOR');
|
|
202
|
-
output += this.formatter.formatText(query.for);
|
|
203
|
-
this.log(output);
|
|
204
|
-
}
|
|
205
|
-
if (query.update) {
|
|
206
|
-
output += this.formatter.formatClause('UPDATE');
|
|
207
|
-
output += this.formatter.formatText(query.update);
|
|
208
|
-
this.log(output);
|
|
209
|
-
}
|
|
210
|
-
return output.trim();
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* Parses fields
|
|
214
|
-
* e.x.: SELECT amount, FORMAT(amount) Amt, (SELECT Id, Name FROM Contacts)
|
|
215
|
-
* @param fields
|
|
216
|
-
* @returns fields
|
|
217
|
-
*/
|
|
218
|
-
parseFields(fields) {
|
|
219
|
-
return fields.map(field => {
|
|
220
|
-
let text = '';
|
|
221
|
-
let typeOfClause;
|
|
222
|
-
const objPrefix = field.objectPrefix ? `${field.objectPrefix}.` : '';
|
|
223
|
-
switch (field.type) {
|
|
224
|
-
case 'Field': {
|
|
225
|
-
text = `${objPrefix}${field.field}${field.alias ? ` ${field.alias}` : ''}`;
|
|
226
|
-
break;
|
|
227
|
-
}
|
|
228
|
-
case 'FieldFunctionExpression': {
|
|
229
|
-
let params = '';
|
|
230
|
-
if (field.parameters) {
|
|
231
|
-
params = field.parameters
|
|
232
|
-
.map(param => (utils.isString(param) ? param : this.parseFields([param]).map(param => param.text)))
|
|
233
|
-
.join(', ');
|
|
234
|
-
}
|
|
235
|
-
text = `${field.functionName}(${params})${field.alias ? ` ${field.alias}` : ''}`;
|
|
236
|
-
break;
|
|
237
|
-
}
|
|
238
|
-
case 'FieldRelationship': {
|
|
239
|
-
text = `${objPrefix}${field.relationships.join('.')}.${field.field}${utils.hasAlias(field) ? ` ${field.alias}` : ''}`;
|
|
240
|
-
break;
|
|
241
|
-
}
|
|
242
|
-
case 'FieldSubquery': {
|
|
243
|
-
text = this.formatter.formatSubquery(this.parseQuery(field.subquery));
|
|
244
|
-
break;
|
|
245
|
-
}
|
|
246
|
-
case 'FieldTypeof': {
|
|
247
|
-
typeOfClause = this.parseTypeOfField(field);
|
|
248
|
-
text = typeOfClause.join(' ');
|
|
249
|
-
break;
|
|
250
|
-
}
|
|
251
|
-
default:
|
|
252
|
-
break;
|
|
253
|
-
}
|
|
254
|
-
return { text, typeOfClause };
|
|
255
|
-
});
|
|
256
|
-
}
|
|
257
|
-
/**
|
|
258
|
-
* Parses type of Field
|
|
259
|
-
* e.x.: TYPEOF What WHEN Account THEN Phone, NumberOfEmployees WHEN Opportunity THEN Amount, CloseDate ELSE Name
|
|
260
|
-
* @param typeOfField
|
|
261
|
-
* @returns type of field
|
|
262
|
-
*/
|
|
263
|
-
parseTypeOfField(typeOfField) {
|
|
264
|
-
const output = [`TYPEOF ${typeOfField.field}`].concat(typeOfField.conditions.map(condition => this.formatter.formatTypeofFieldCondition(condition)));
|
|
265
|
-
output.push(`END`);
|
|
266
|
-
return output;
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
* Parses where clause
|
|
270
|
-
* e.x.: WHERE LoginTime > 2010-09-20T22:16:30.000Z AND LoginTime < 2010-09-21T22:16:30.000Z
|
|
271
|
-
* WHERE Id IN (SELECT AccountId FROM Contact WHERE LastName LIKE 'apple%') AND Id IN (SELECT AccountId FROM Opportunity WHERE isClosed = false)
|
|
272
|
-
* @param where
|
|
273
|
-
* @param priorIsNegationOperator - do not set this when calling manually. Recursive call will set this to ensure proper formatting.
|
|
274
|
-
* @returns where clause
|
|
275
|
-
*/
|
|
276
|
-
parseWhereOrHavingClause(whereOrHaving, tabOffset = 0, priorConditionIsNegation = false) {
|
|
277
|
-
let output = '';
|
|
278
|
-
const left = whereOrHaving.left;
|
|
279
|
-
let trimPrecedingOutput = false;
|
|
280
|
-
if (left) {
|
|
281
|
-
output += this.formatter.formatParens(left.openParen, '(', utils.isNegationCondition(left));
|
|
282
|
-
if (!utils.isNegationCondition(left)) {
|
|
283
|
-
tabOffset = tabOffset + (left.openParen || 0) - (left.closeParen || 0);
|
|
284
|
-
if (priorConditionIsNegation) {
|
|
285
|
-
tabOffset++;
|
|
286
|
-
}
|
|
287
|
-
let expression = '';
|
|
288
|
-
expression += utils.isValueFunctionCondition(left) ? this.parseFn(left.fn) : left.field;
|
|
289
|
-
expression += ` ${left.operator} `;
|
|
290
|
-
if (utils.isValueQueryCondition(left)) {
|
|
291
|
-
expression += this.formatter.formatSubquery(this.parseQuery(left.valueQuery), 1, true);
|
|
292
|
-
}
|
|
293
|
-
else {
|
|
294
|
-
expression += utils.getAsArrayStr(utils.getWhereValue(left.value, left.literalType, left.operator));
|
|
295
|
-
}
|
|
296
|
-
output += this.formatter.formatWithIndent(expression);
|
|
297
|
-
output += this.formatter.formatParens(left.closeParen, ')', priorConditionIsNegation);
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
if (utils.isWhereOrHavingClauseWithRightCondition(whereOrHaving)) {
|
|
301
|
-
const operator = utils.get(whereOrHaving.operator);
|
|
302
|
-
trimPrecedingOutput = operator === 'NOT';
|
|
303
|
-
const formattedData = this.formatter.formatWhereClauseOperators(operator, this.parseWhereOrHavingClause(whereOrHaving.right, tabOffset, utils.isNegationCondition(left)), tabOffset);
|
|
304
|
-
return `${trimPrecedingOutput ? output.trimRight() : output}${formattedData}`.trim();
|
|
305
|
-
}
|
|
306
|
-
else {
|
|
307
|
-
return output.trim();
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
/**
|
|
311
|
-
* Parses group by clause
|
|
312
|
-
* e.x.: GROUP BY CampaignId
|
|
313
|
-
* @param groupBy
|
|
314
|
-
* @returns group by clause
|
|
315
|
-
*/
|
|
316
|
-
parseGroupByClause(groupBy) {
|
|
317
|
-
return (Array.isArray(groupBy) ? groupBy : [groupBy])
|
|
318
|
-
.map(clause => (utils.isGroupByField(clause) ? clause.field : this.parseFn(clause.fn)))
|
|
319
|
-
.join(', ');
|
|
320
|
-
}
|
|
321
|
-
/**
|
|
322
|
-
* Parses order by
|
|
323
|
-
* e.x.: ORDER BY BillingPostalCode ASC NULLS LAST
|
|
324
|
-
* @param orderBy
|
|
325
|
-
* @returns order by
|
|
326
|
-
*/
|
|
327
|
-
parseOrderBy(orderBy) {
|
|
328
|
-
if (Array.isArray(orderBy)) {
|
|
329
|
-
return this.formatter.formatOrderByArray(orderBy.map(ob => this.parseOrderBy(ob)));
|
|
330
|
-
}
|
|
331
|
-
else {
|
|
332
|
-
let output = '';
|
|
333
|
-
if (utils.isOrderByField(orderBy)) {
|
|
334
|
-
output = `${utils.get(orderBy.field, ' ')}`;
|
|
335
|
-
}
|
|
336
|
-
else {
|
|
337
|
-
output += `${this.parseFn(orderBy.fn)} `;
|
|
338
|
-
}
|
|
339
|
-
output += `${utils.get(orderBy.order, ' ')}${utils.get(orderBy.nulls, '', 'NULLS ')}`;
|
|
340
|
-
return output.trim();
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
/**
|
|
344
|
-
* Parses with data category
|
|
345
|
-
* e.x.: WITH DATA CATEGORY Geography__c AT (usa__c, uk__c)
|
|
346
|
-
* @param withDataCategory
|
|
347
|
-
* @returns with data category
|
|
348
|
-
*/
|
|
349
|
-
parseWithDataCategory(withDataCategory) {
|
|
350
|
-
return withDataCategory.conditions
|
|
351
|
-
.map(condition => {
|
|
352
|
-
const params = condition.parameters.length > 1 ? `(${condition.parameters.join(', ')})` : `${condition.parameters.join(', ')}`;
|
|
353
|
-
return `${condition.groupName} ${condition.selector} ${params}`;
|
|
354
|
-
})
|
|
355
|
-
.join(' AND ');
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
exports.Compose = Compose;
|
|
@@ -1,263 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Formatter = void 0;
|
|
4
|
-
const utils_1 = require("../utils");
|
|
5
|
-
/**
|
|
6
|
-
* Formatter
|
|
7
|
-
* This class aids in building a SOQL query from a parse query
|
|
8
|
-
* and optionally formats parts of the query based on the configuration options passed in
|
|
9
|
-
*/
|
|
10
|
-
class Formatter {
|
|
11
|
-
enabled;
|
|
12
|
-
options;
|
|
13
|
-
currIndent = 1;
|
|
14
|
-
constructor(enabled, options) {
|
|
15
|
-
this.enabled = enabled;
|
|
16
|
-
this.options = {
|
|
17
|
-
numIndent: options.numIndent ?? 1,
|
|
18
|
-
fieldMaxLineLength: options.fieldMaxLineLength ?? 60,
|
|
19
|
-
fieldSubqueryParensOnOwnLine: options.fieldSubqueryParensOnOwnLine ?? true,
|
|
20
|
-
whereClauseOperatorsIndented: true,
|
|
21
|
-
newLineAfterKeywords: options.newLineAfterKeywords ?? false,
|
|
22
|
-
logging: options.logging ?? false,
|
|
23
|
-
};
|
|
24
|
-
if (this.options.newLineAfterKeywords) {
|
|
25
|
-
this.options.fieldSubqueryParensOnOwnLine = true;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
log(data) {
|
|
29
|
-
if (this.options.logging) {
|
|
30
|
-
console.log(data);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
getIndent(additionalIndent = 0) {
|
|
34
|
-
return this.repeatChar((this.currIndent + additionalIndent) * (this.options.numIndent || 1), '\t');
|
|
35
|
-
}
|
|
36
|
-
repeatChar(numTimes, char) {
|
|
37
|
-
return new Array(numTimes).fill(char).join('');
|
|
38
|
-
}
|
|
39
|
-
setSubquery(isSubquery) {
|
|
40
|
-
this.currIndent = isSubquery ? (this.currIndent += 1) : (this.currIndent -= 1);
|
|
41
|
-
}
|
|
42
|
-
stepCurrIndex(num) {
|
|
43
|
-
this.currIndent += num;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Format fields
|
|
47
|
-
* @param fieldData
|
|
48
|
-
*/
|
|
49
|
-
formatFields(fieldData) {
|
|
50
|
-
function trimPrevSuffix(currIdx) {
|
|
51
|
-
if (fieldData.fields[currIdx - 1]) {
|
|
52
|
-
fieldData.fields[currIdx - 1].suffix = fieldData.fields[currIdx - 1].suffix.trim();
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
fieldData.fields.forEach((field, i) => {
|
|
56
|
-
field.suffix = fieldData.fields.length - 1 === i ? '' : ', ';
|
|
57
|
-
});
|
|
58
|
-
if (this.enabled) {
|
|
59
|
-
let lineLen = 0;
|
|
60
|
-
let newLineAndIndentNext = false;
|
|
61
|
-
fieldData.fields.forEach((field, i) => {
|
|
62
|
-
if (field.isSubquery) {
|
|
63
|
-
// Subquery should always be on a stand-alone line
|
|
64
|
-
trimPrevSuffix(i);
|
|
65
|
-
field.prefix = `\n${this.getIndent()}`;
|
|
66
|
-
field.suffix = fieldData.fields.length - 1 === i ? '' : ', ';
|
|
67
|
-
lineLen = 0;
|
|
68
|
-
newLineAndIndentNext = true;
|
|
69
|
-
}
|
|
70
|
-
else if (Array.isArray(field.typeOfClause)) {
|
|
71
|
-
trimPrevSuffix(i);
|
|
72
|
-
// always show on a new line
|
|
73
|
-
field.prefix = `\n${this.getIndent()}`;
|
|
74
|
-
newLineAndIndentNext = true;
|
|
75
|
-
}
|
|
76
|
-
else if ((0, utils_1.isNumber)(this.options.fieldMaxLineLength)) {
|
|
77
|
-
// If max line length is specified, create a new line when needed
|
|
78
|
-
// Add two to account for ", "
|
|
79
|
-
lineLen += field.text.length + field.suffix.length;
|
|
80
|
-
if (lineLen > this.options.fieldMaxLineLength || newLineAndIndentNext) {
|
|
81
|
-
trimPrevSuffix(i);
|
|
82
|
-
if (!this.options.newLineAfterKeywords || i > 0) {
|
|
83
|
-
field.prefix += `\n${this.getIndent()}`;
|
|
84
|
-
}
|
|
85
|
-
lineLen = 0;
|
|
86
|
-
newLineAndIndentNext = false;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
this.log(field);
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
formatTyeOfField(text, typeOfClause) {
|
|
94
|
-
if (this.enabled && this.options.newLineAfterKeywords) {
|
|
95
|
-
return typeOfClause
|
|
96
|
-
.map((part, i) => {
|
|
97
|
-
if (i === 0) {
|
|
98
|
-
return part;
|
|
99
|
-
}
|
|
100
|
-
else if (i === typeOfClause.length - 1) {
|
|
101
|
-
return `${this.getIndent()}${part}`;
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
return `${this.getIndent()}\t${part}`;
|
|
105
|
-
}
|
|
106
|
-
})
|
|
107
|
-
.join('\n');
|
|
108
|
-
}
|
|
109
|
-
return text;
|
|
110
|
-
}
|
|
111
|
-
formatTypeofFieldCondition(condition) {
|
|
112
|
-
let output = '';
|
|
113
|
-
const fields = condition.fieldList.join(', ');
|
|
114
|
-
if (this.enabled && this.options.newLineAfterKeywords) {
|
|
115
|
-
const indent = this.getIndent();
|
|
116
|
-
output = `${condition.type}`;
|
|
117
|
-
if (condition.objectType) {
|
|
118
|
-
output += `\n${indent}\t\t${condition.objectType}\n${indent}\tTHEN\n${indent}\t\t${fields}`;
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
output += `\n${indent}\t\t${fields}`;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
else {
|
|
125
|
-
output = condition.type;
|
|
126
|
-
if (condition.objectType) {
|
|
127
|
-
output += ` ${condition.objectType} THEN ${fields}`;
|
|
128
|
-
}
|
|
129
|
-
else {
|
|
130
|
-
output += ` ${fields}`;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
return output;
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Formats subquery with additional indents
|
|
137
|
-
*/
|
|
138
|
-
formatSubquery(queryStr, numTabs = 2, incrementTabsWhereClauseOpIndent = false) {
|
|
139
|
-
if (incrementTabsWhereClauseOpIndent) {
|
|
140
|
-
numTabs++;
|
|
141
|
-
}
|
|
142
|
-
let leftParen = '(';
|
|
143
|
-
let rightParen = ')';
|
|
144
|
-
if (this.enabled) {
|
|
145
|
-
if (this.options.fieldSubqueryParensOnOwnLine || this.options.newLineAfterKeywords) {
|
|
146
|
-
queryStr = queryStr.replace(/\n/g, `\n${this.repeatChar(numTabs, '\t')}`);
|
|
147
|
-
leftParen = `(\n${this.repeatChar(numTabs, '\t')}`;
|
|
148
|
-
rightParen = `\n${this.repeatChar(numTabs - 1, '\t')})`;
|
|
149
|
-
}
|
|
150
|
-
else {
|
|
151
|
-
queryStr = queryStr.replace(/\n/g, '\n\t');
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
return `${leftParen}${queryStr}${rightParen}`;
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Formats all clauses that do not have a more specialized format function
|
|
158
|
-
* If formatting is enabled, then this will put a new line before the clause
|
|
159
|
-
* @param clause
|
|
160
|
-
* @returns clause
|
|
161
|
-
*/
|
|
162
|
-
formatClause(clause) {
|
|
163
|
-
if (this.enabled) {
|
|
164
|
-
return this.options.newLineAfterKeywords ? `\n${clause}\n\t` : `\n${clause}`;
|
|
165
|
-
}
|
|
166
|
-
return ` ${clause}`;
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* If newLineAfterKeywords is true, then no preceding space will be added
|
|
170
|
-
* This is called after clauses
|
|
171
|
-
* @param text
|
|
172
|
-
* @returns text
|
|
173
|
-
*/
|
|
174
|
-
formatText(text) {
|
|
175
|
-
return this.enabled && (this.options.newLineAfterKeywords || text.startsWith('\n')) ? text : ` ${text}`;
|
|
176
|
-
}
|
|
177
|
-
formatWithIndent(text) {
|
|
178
|
-
return this.enabled ? `${this.getIndent()}${text}` : text;
|
|
179
|
-
}
|
|
180
|
-
formatOrderByArray(groupBy) {
|
|
181
|
-
if (this.enabled) {
|
|
182
|
-
let currLen = 0;
|
|
183
|
-
let output = '';
|
|
184
|
-
groupBy.forEach((token, i) => {
|
|
185
|
-
const nextToken = groupBy[i + 1];
|
|
186
|
-
currLen += token.length;
|
|
187
|
-
if (nextToken && (currLen + nextToken.length > (this.options.fieldMaxLineLength || 0) || this.options.newLineAfterKeywords)) {
|
|
188
|
-
output += `${token},\n\t`;
|
|
189
|
-
currLen = 0;
|
|
190
|
-
}
|
|
191
|
-
else {
|
|
192
|
-
output += `${token}${nextToken ? ', ' : ''}`;
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
return output;
|
|
196
|
-
}
|
|
197
|
-
else {
|
|
198
|
-
return groupBy.join(', ');
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Formats parens
|
|
203
|
-
* @param count
|
|
204
|
-
* @param character
|
|
205
|
-
* @param [leadingParenInline] Make the leading paren inline (last for "(" and first for ")") used for negation condition
|
|
206
|
-
* @returns
|
|
207
|
-
*/
|
|
208
|
-
formatParens(count, character, leadingParenInline = false) {
|
|
209
|
-
let output = '';
|
|
210
|
-
if ((0, utils_1.isNumber)(count) && count > 0) {
|
|
211
|
-
if (this.enabled) {
|
|
212
|
-
if (character === '(') {
|
|
213
|
-
for (let i = 0; i < count; i++) {
|
|
214
|
-
if (leadingParenInline && i === count - 1) {
|
|
215
|
-
output += '(';
|
|
216
|
-
}
|
|
217
|
-
else {
|
|
218
|
-
if (i === 0) {
|
|
219
|
-
output += '(\n';
|
|
220
|
-
}
|
|
221
|
-
else {
|
|
222
|
-
this.currIndent++;
|
|
223
|
-
output += `${this.getIndent()}(\n`;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
if (!leadingParenInline || count > 1) {
|
|
228
|
-
// indent the following clause
|
|
229
|
-
this.currIndent++;
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
else {
|
|
233
|
-
for (let i = count - 1; i >= 0; i--) {
|
|
234
|
-
if (leadingParenInline && i === count - 1) {
|
|
235
|
-
output += ')';
|
|
236
|
-
}
|
|
237
|
-
else {
|
|
238
|
-
this.currIndent--;
|
|
239
|
-
output += `\n${this.getIndent()})`;
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
else {
|
|
245
|
-
output += (0, utils_1.generateParens)(count, character);
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
return output;
|
|
249
|
-
}
|
|
250
|
-
formatWhereClauseOperators(operator, whereClause, additionalIndent = 0) {
|
|
251
|
-
const skipNewLineAndIndent = operator === 'NOT';
|
|
252
|
-
if (this.enabled && !skipNewLineAndIndent) {
|
|
253
|
-
return `\n${this.getIndent(additionalIndent)}${operator} ${whereClause}`;
|
|
254
|
-
}
|
|
255
|
-
else {
|
|
256
|
-
return `${skipNewLineAndIndent ? '' : ' '}${operator} ${whereClause}`;
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
formatAddNewLine(alt = ' ', skipNewLineAndIndent) {
|
|
260
|
-
return this.enabled && !skipNewLineAndIndent ? `\n` : alt;
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
exports.Formatter = Formatter;
|
package/dist/src/index.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.isQueryValid = exports.parseQuery = void 0;
|
|
18
|
-
/*
|
|
19
|
-
* Copyright (c) Austin Turner
|
|
20
|
-
* The software in this package is published under the terms of the MIT license,
|
|
21
|
-
* a copy of which has been included with this distribution in the LICENSE.txt file.
|
|
22
|
-
*/
|
|
23
|
-
var visitor_1 = require("./parser/visitor");
|
|
24
|
-
Object.defineProperty(exports, "parseQuery", { enumerable: true, get: function () { return visitor_1.parseQuery; } });
|
|
25
|
-
Object.defineProperty(exports, "isQueryValid", { enumerable: true, get: function () { return visitor_1.isQueryValid; } });
|
|
26
|
-
__exportStar(require("./api/api-models"), exports);
|
|
27
|
-
__exportStar(require("./api/public-utils"), exports);
|
|
28
|
-
__exportStar(require("./composer/composer"), exports);
|
package/dist/src/models.js
DELETED