sfmc-sql-converter 0.1.0 → 0.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 +57 -38
- package/package.json +17 -5
- package/src/clauses.js +27 -0
- package/src/convert.js +108 -49
- package/src/emit-mce.js +1 -1
- package/src/emit-mcn.js +788 -0
- package/src/index.js +12 -1
- package/src/messages.js +5 -0
- package/src/parse-ampscript.js +228 -10
- package/src/parse-handlebars.js +956 -0
- package/src/print-sql.js +34 -21
- package/src/sql-parser.js +6 -1
package/src/index.js
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
export { parseSql } from './sql-parser.js';
|
|
2
2
|
export * from './messages.js';
|
|
3
|
-
export { convert
|
|
3
|
+
export { convert } from './convert.js';
|
|
4
|
+
export {
|
|
5
|
+
MCN_TARGETS,
|
|
6
|
+
NEXT_RULES,
|
|
7
|
+
emitHandlebarsQuery,
|
|
8
|
+
emitHandlebarsQueryFirst,
|
|
9
|
+
emitHandlebarsQueryMo,
|
|
10
|
+
emitAmpscriptRso,
|
|
11
|
+
emitAmpscriptLookupRowsMo,
|
|
12
|
+
hbString,
|
|
13
|
+
} from './emit-mcn.js';
|
|
14
|
+
export { parseHandlebars } from './parse-handlebars.js';
|
|
4
15
|
export {
|
|
5
16
|
MCE_TARGETS,
|
|
6
17
|
emitSsjsWsproxy,
|
package/src/messages.js
CHANGED
|
@@ -64,6 +64,7 @@ export const LIKE_CHECKED_IN_SCRIPT = 'LIKE_CHECKED_IN_SCRIPT';
|
|
|
64
64
|
export const EMPTY_STRING_AS_SPACE = 'EMPTY_STRING_AS_SPACE';
|
|
65
65
|
export const TOP_ABOVE_LOOKUP_LIMIT = 'TOP_ABOVE_LOOKUP_LIMIT';
|
|
66
66
|
export const FIRST_BATCH_ONLY = 'FIRST_BATCH_ONLY';
|
|
67
|
+
export const ORDER_BY_NEEDS_TOP = 'ORDER_BY_NEEDS_TOP';
|
|
67
68
|
|
|
68
69
|
// --- reverse parsers (code to SQL) ------------------------------------------------------
|
|
69
70
|
export const NO_LOOKUP_FOUND = 'NO_LOOKUP_FOUND';
|
|
@@ -85,6 +86,7 @@ export const FROM_NAME_FROM_KEY = 'FROM_NAME_FROM_KEY';
|
|
|
85
86
|
export const LOOKUP_FIRST_ROW_ONLY = 'LOOKUP_FIRST_ROW_ONLY';
|
|
86
87
|
export const CASE_SENSITIVE_LOOKUP = 'CASE_SENSITIVE_LOOKUP';
|
|
87
88
|
export const LIKE_MATCHES_ANYWHERE = 'LIKE_MATCHES_ANYWHERE';
|
|
89
|
+
export const MO_ONE_MATCH = 'MO_ONE_MATCH';
|
|
88
90
|
|
|
89
91
|
/**
|
|
90
92
|
* All message codes, keyed by themselves (handy for iteration and lookups).
|
|
@@ -138,6 +140,7 @@ export const MESSAGE_CODES = Object.freeze({
|
|
|
138
140
|
EMPTY_STRING_AS_SPACE,
|
|
139
141
|
TOP_ABOVE_LOOKUP_LIMIT,
|
|
140
142
|
FIRST_BATCH_ONLY,
|
|
143
|
+
ORDER_BY_NEEDS_TOP,
|
|
141
144
|
NO_LOOKUP_FOUND,
|
|
142
145
|
ARGUMENT_COUNT,
|
|
143
146
|
INVALID_ARGUMENT,
|
|
@@ -157,6 +160,7 @@ export const MESSAGE_CODES = Object.freeze({
|
|
|
157
160
|
LOOKUP_FIRST_ROW_ONLY,
|
|
158
161
|
CASE_SENSITIVE_LOOKUP,
|
|
159
162
|
LIKE_MATCHES_ANYWHERE,
|
|
163
|
+
MO_ONE_MATCH,
|
|
160
164
|
});
|
|
161
165
|
|
|
162
166
|
/**
|
|
@@ -183,6 +187,7 @@ export const REVERSE_MESSAGE_CODES = Object.freeze({
|
|
|
183
187
|
LOOKUP_FIRST_ROW_ONLY,
|
|
184
188
|
CASE_SENSITIVE_LOOKUP,
|
|
185
189
|
LIKE_MATCHES_ANYWHERE,
|
|
190
|
+
MO_ONE_MATCH,
|
|
186
191
|
});
|
|
187
192
|
|
|
188
193
|
/**
|
package/src/parse-ampscript.js
CHANGED
|
@@ -5,15 +5,21 @@
|
|
|
5
5
|
* The code is read as text; it is never executed. Never throws.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
+
import { nextLikeInner, nextLikeWildcardMessage } from './clauses.js';
|
|
9
|
+
import { lookupRowsRepeatedFieldMessage, rsoRepeatedFieldMessage } from './emit-mcn.js';
|
|
8
10
|
import { SYNTAX_ERROR } from './messages.js';
|
|
9
11
|
import {
|
|
10
12
|
ARGUMENT_COUNT,
|
|
11
13
|
CASE_SENSITIVE_LOOKUP,
|
|
12
14
|
INVALID_ARGUMENT,
|
|
13
15
|
INVALID_COUNT,
|
|
16
|
+
INVALID_FILTER,
|
|
17
|
+
INVALID_FILTER_VALUE,
|
|
18
|
+
LIKE_MATCHES_ANYWHERE,
|
|
14
19
|
LOOKUP_FIRST_ROW_ONLY,
|
|
15
20
|
MULTIPLE_LOOKUPS,
|
|
16
21
|
NO_LOOKUP_FOUND,
|
|
22
|
+
UNKNOWN_FILTER_OPERATOR,
|
|
17
23
|
UNSUPPORTED_EXPRESSION,
|
|
18
24
|
VARIABLE_VALUE,
|
|
19
25
|
} from './messages.js';
|
|
@@ -54,6 +60,36 @@ const LOOKUP_FUNCTIONS = new Map(
|
|
|
54
60
|
}),
|
|
55
61
|
);
|
|
56
62
|
|
|
63
|
+
// Marketing Cloud Next: LookupRows on a marketing object and RetrieveSalesforceObjects
|
|
64
|
+
const NEXT_FUNCTIONS = new Map(
|
|
65
|
+
Object.entries({
|
|
66
|
+
lookuprows: { target: 'ampscript-lookuprows-mo', caseSensitive: false },
|
|
67
|
+
retrievesalesforceobjects: { target: 'ampscript-rso', caseSensitive: false },
|
|
68
|
+
}),
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
// calls that exist in AMPscript but fail or are unproven in Next
|
|
72
|
+
const NEXT_REJECTED = new Map(
|
|
73
|
+
Object.entries({
|
|
74
|
+
lookuporderedrows:
|
|
75
|
+
'LookupOrderedRows fails in Marketing Cloud Next. Use LookupRows and sort in your code.',
|
|
76
|
+
lookuporderedrowscs:
|
|
77
|
+
'LookupOrderedRowsCS fails in Marketing Cloud Next. Use LookupRows and sort in your code.',
|
|
78
|
+
lookuprowscs:
|
|
79
|
+
'LookupRowsCS is not proven in Marketing Cloud Next. Use LookupRows (it ignores case).',
|
|
80
|
+
}),
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
const RSO_OPERATORS = {
|
|
84
|
+
'=': '=',
|
|
85
|
+
'!=': '<>',
|
|
86
|
+
'<>': '<>',
|
|
87
|
+
'<': '<',
|
|
88
|
+
'<=': '<=',
|
|
89
|
+
'>': '>',
|
|
90
|
+
'>=': '>=',
|
|
91
|
+
};
|
|
92
|
+
|
|
57
93
|
/**
|
|
58
94
|
* Parses one argument: a string or number literal. Variables and nested calls are errors.
|
|
59
95
|
*
|
|
@@ -169,14 +205,26 @@ function requireString(context, literal, what) {
|
|
|
169
205
|
*
|
|
170
206
|
* @param {object} context parse context
|
|
171
207
|
* @param {AmpLiteral[]} pairs alternating column names and values
|
|
208
|
+
* @param {boolean} [isNext] true for Marketing Cloud Next (a repeated field fails)
|
|
172
209
|
* @returns {object} expression node
|
|
173
210
|
*/
|
|
174
|
-
function readPairs(context, pairs) {
|
|
211
|
+
function readPairs(context, pairs, isNext = false) {
|
|
175
212
|
const conditions = [];
|
|
213
|
+
const seen = new Set();
|
|
176
214
|
for (let index = 0; index < pairs.length; index += 2) {
|
|
177
215
|
const columnLiteral = pairs[index];
|
|
178
216
|
const valueLiteral = pairs[index + 1];
|
|
179
217
|
const column = requireString(context, columnLiteral, 'Each column name');
|
|
218
|
+
if (isNext && seen.has(column.toLowerCase())) {
|
|
219
|
+
fail(
|
|
220
|
+
context,
|
|
221
|
+
INVALID_FILTER,
|
|
222
|
+
`${lookupRowsRepeatedFieldMessage()} (${column}).`,
|
|
223
|
+
columnLiteral.start,
|
|
224
|
+
columnLiteral.end,
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
seen.add(column.toLowerCase());
|
|
180
228
|
conditions.push({
|
|
181
229
|
type: 'compare',
|
|
182
230
|
op: '=',
|
|
@@ -247,34 +295,194 @@ function readCount(context, literal) {
|
|
|
247
295
|
* Finds every lookup call.
|
|
248
296
|
*
|
|
249
297
|
* @param {object} context parse context
|
|
298
|
+
* @param {Map<string, object>} functions lookup functions by lower-case name
|
|
250
299
|
* @returns {{index: number, name: string}[]} calls in source order
|
|
251
300
|
*/
|
|
252
|
-
function findCalls(context) {
|
|
301
|
+
function findCalls(context, functions) {
|
|
253
302
|
const calls = [];
|
|
254
303
|
for (const [index, token] of context.tokens.entries()) {
|
|
255
304
|
const name = token.type === 'ident' ? token.value.toLowerCase() : '';
|
|
256
|
-
if (
|
|
305
|
+
if (functions.has(name) && isPunct(tokenAt(context, index + 1), '(')) {
|
|
257
306
|
calls.push({ index, name });
|
|
258
307
|
}
|
|
259
308
|
}
|
|
260
309
|
return calls;
|
|
261
310
|
}
|
|
262
311
|
|
|
312
|
+
/**
|
|
313
|
+
* Fails when the code uses a call that fails or is unproven in Next.
|
|
314
|
+
*
|
|
315
|
+
* @param {object} context parse context
|
|
316
|
+
*/
|
|
317
|
+
function rejectNextCalls(context) {
|
|
318
|
+
for (const [index, token] of context.tokens.entries()) {
|
|
319
|
+
const reason = token.type === 'ident' && NEXT_REJECTED.get(token.value.toLowerCase());
|
|
320
|
+
if (reason && isPunct(tokenAt(context, index + 1), '(')) {
|
|
321
|
+
fail(context, UNSUPPORTED_EXPRESSION, reason, token.start, token.end);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Reads the field list of RetrieveSalesforceObjects ("A,B") into column nodes.
|
|
328
|
+
*
|
|
329
|
+
* @param {object} context parse context
|
|
330
|
+
* @param {AmpLiteral} literal field list argument
|
|
331
|
+
* @returns {{name: string, loc: object}[]} columns
|
|
332
|
+
*/
|
|
333
|
+
function readRsoFields(context, literal) {
|
|
334
|
+
const text = requireString(context, literal, 'The field list');
|
|
335
|
+
const isExact = context.text.slice(literal.start + 1, literal.end - 1) === text;
|
|
336
|
+
const columns = [];
|
|
337
|
+
let offset = 0;
|
|
338
|
+
for (const part of text.split(',')) {
|
|
339
|
+
const name = part.trim();
|
|
340
|
+
if (!name) {
|
|
341
|
+
fail(
|
|
342
|
+
context,
|
|
343
|
+
INVALID_ARGUMENT,
|
|
344
|
+
'The field list has an empty entry. Separate the field names with single commas.',
|
|
345
|
+
literal.start,
|
|
346
|
+
literal.end,
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
const start = isExact ? literal.start + 1 + offset + part.indexOf(name) : literal.start;
|
|
350
|
+
columns.push({
|
|
351
|
+
name,
|
|
352
|
+
loc: makeLoc(context, start, isExact ? start + name.length : literal.end),
|
|
353
|
+
});
|
|
354
|
+
offset += part.length + 1;
|
|
355
|
+
}
|
|
356
|
+
return columns;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Reads the field / operator / value triplets of RetrieveSalesforceObjects into an AND chain.
|
|
361
|
+
* Next matches LIKE as "contains", so plain text such as `Ro%` becomes `%Ro%` with a warning;
|
|
362
|
+
* `_`, `[` or `%` inside the text fail. A field used twice fails (no rows in Next).
|
|
363
|
+
*
|
|
364
|
+
* @param {object} context parse context
|
|
365
|
+
* @param {AmpLiteral[]} triplets alternating field, operator and value
|
|
366
|
+
* @returns {object} expression node
|
|
367
|
+
*/
|
|
368
|
+
function readTriplets(context, triplets) {
|
|
369
|
+
const conditions = [];
|
|
370
|
+
const seen = new Set();
|
|
371
|
+
for (let index = 0; index < triplets.length; index += 3) {
|
|
372
|
+
const [columnLiteral, operatorLiteral, valueLiteral] = triplets.slice(index, index + 3);
|
|
373
|
+
const column = requireString(context, columnLiteral, 'Each field name');
|
|
374
|
+
if (seen.has(column.toLowerCase())) {
|
|
375
|
+
fail(
|
|
376
|
+
context,
|
|
377
|
+
INVALID_FILTER,
|
|
378
|
+
rsoRepeatedFieldMessage(column),
|
|
379
|
+
columnLiteral.start,
|
|
380
|
+
columnLiteral.end,
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
seen.add(column.toLowerCase());
|
|
384
|
+
const operator = requireString(context, operatorLiteral, 'Each operator').trim();
|
|
385
|
+
const value = makeValue(
|
|
386
|
+
context,
|
|
387
|
+
valueLiteral.value,
|
|
388
|
+
valueLiteral.start,
|
|
389
|
+
valueLiteral.end,
|
|
390
|
+
valueLiteral.kind === 'number',
|
|
391
|
+
);
|
|
392
|
+
const loc = makeLoc(context, columnLiteral.start, valueLiteral.end);
|
|
393
|
+
if (operator.toUpperCase() === 'LIKE') {
|
|
394
|
+
const parts = nextLikeInner(value.value);
|
|
395
|
+
if (!parts) {
|
|
396
|
+
fail(
|
|
397
|
+
context,
|
|
398
|
+
INVALID_FILTER_VALUE,
|
|
399
|
+
nextLikeWildcardMessage(value.value),
|
|
400
|
+
valueLiteral.start,
|
|
401
|
+
valueLiteral.end,
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
const { inner } = parts;
|
|
405
|
+
if (!parts.isContains) {
|
|
406
|
+
report(
|
|
407
|
+
context,
|
|
408
|
+
'warning',
|
|
409
|
+
LIKE_MATCHES_ANYWHERE,
|
|
410
|
+
`Next matches LIKE as "contains" and ignores wildcards, so "${value.value}" is converted as '%${inner}%'.`,
|
|
411
|
+
valueLiteral.start,
|
|
412
|
+
valueLiteral.end,
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
const pattern = { ...value, kind: 'string', value: `%${inner}%` };
|
|
416
|
+
conditions.push({ type: 'like', column, pattern, loc });
|
|
417
|
+
continue;
|
|
418
|
+
}
|
|
419
|
+
const op = RSO_OPERATORS[operator];
|
|
420
|
+
if (!op) {
|
|
421
|
+
fail(
|
|
422
|
+
context,
|
|
423
|
+
UNKNOWN_FILTER_OPERATOR,
|
|
424
|
+
`"${operator}" is not an operator of RetrieveSalesforceObjects. Use =, !=, <, <=, >, >= or LIKE.`,
|
|
425
|
+
operatorLiteral.start,
|
|
426
|
+
operatorLiteral.end,
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
conditions.push({ type: 'compare', op, column, value, loc });
|
|
430
|
+
}
|
|
431
|
+
return andChain(context, conditions);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Builds the AST of a RetrieveSalesforceObjects call.
|
|
436
|
+
*
|
|
437
|
+
* @param {object} context parse context
|
|
438
|
+
* @param {AmpLiteral[]} args arguments
|
|
439
|
+
* @param {object} callLoc location of the call
|
|
440
|
+
* @returns {object} SELECT AST
|
|
441
|
+
*/
|
|
442
|
+
function rsoAst(context, args, callLoc) {
|
|
443
|
+
if (args.length < 5 || (args.length - 2) % 3 !== 0) {
|
|
444
|
+
fail(
|
|
445
|
+
context,
|
|
446
|
+
ARGUMENT_COUNT,
|
|
447
|
+
'Wrong number of arguments. Write it as RetrieveSalesforceObjects("Object", "Field1,Field2", "Field", "=", "value"[, …]): every condition needs a field, an operator and a value.',
|
|
448
|
+
callLoc.start,
|
|
449
|
+
callLoc.end,
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
const name = requireString(context, args[0], 'The object name');
|
|
453
|
+
return {
|
|
454
|
+
type: 'select',
|
|
455
|
+
top: null,
|
|
456
|
+
columns: readRsoFields(context, args[1]),
|
|
457
|
+
from: { name, loc: makeLoc(context, args[0].start, args[0].end) },
|
|
458
|
+
where: readTriplets(context, args.slice(2)),
|
|
459
|
+
orderBy: [],
|
|
460
|
+
loc: callLoc,
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
|
|
263
464
|
/**
|
|
264
465
|
* Parses the first lookup call and builds the AST.
|
|
265
466
|
*
|
|
266
467
|
* @param {object} context parse context
|
|
267
468
|
* @param {object} meta meta data (mutated)
|
|
469
|
+
* @param {boolean} isNext true for Marketing Cloud Next
|
|
268
470
|
* @returns {object} SELECT AST
|
|
269
471
|
*/
|
|
270
|
-
function parseProgram(context, meta) {
|
|
472
|
+
function parseProgram(context, meta, isNext) {
|
|
271
473
|
const header = readHeader(context);
|
|
272
|
-
|
|
474
|
+
if (isNext) {
|
|
475
|
+
rejectNextCalls(context);
|
|
476
|
+
}
|
|
477
|
+
const functions = isNext ? NEXT_FUNCTIONS : LOOKUP_FUNCTIONS;
|
|
478
|
+
const calls = findCalls(context, functions);
|
|
273
479
|
if (calls.length === 0) {
|
|
274
480
|
fail(
|
|
275
481
|
context,
|
|
276
482
|
NO_LOOKUP_FOUND,
|
|
277
|
-
|
|
483
|
+
isNext
|
|
484
|
+
? 'No supported lookup was found. Paste a LookupRows or RetrieveSalesforceObjects call.'
|
|
485
|
+
: 'No supported lookup was found. Paste a LookupRows, LookupOrderedRows or Lookup call.',
|
|
278
486
|
0,
|
|
279
487
|
context.text.length,
|
|
280
488
|
);
|
|
@@ -295,9 +503,14 @@ function parseProgram(context, meta) {
|
|
|
295
503
|
context.pos = call.index + 1;
|
|
296
504
|
const { args, end } = parseArguments(context);
|
|
297
505
|
const callLoc = makeLoc(context, callToken.start, end);
|
|
298
|
-
const definition =
|
|
506
|
+
const definition = functions.get(call.name);
|
|
299
507
|
const kind = call.name.replace(/cs$/, '');
|
|
300
508
|
meta.target = definition.target;
|
|
509
|
+
if (kind === 'retrievesalesforceobjects') {
|
|
510
|
+
const ast = rsoAst(context, args, callLoc);
|
|
511
|
+
meta.deName = ast.from.name;
|
|
512
|
+
return ast;
|
|
513
|
+
}
|
|
301
514
|
if (definition.caseSensitive) {
|
|
302
515
|
report(
|
|
303
516
|
context,
|
|
@@ -341,7 +554,7 @@ function parseProgram(context, meta) {
|
|
|
341
554
|
top,
|
|
342
555
|
columns,
|
|
343
556
|
from: { name, loc: makeLoc(context, args[0].start, args[0].end) },
|
|
344
|
-
where: readPairs(context, pairs),
|
|
557
|
+
where: readPairs(context, pairs, isNext),
|
|
345
558
|
orderBy,
|
|
346
559
|
loc: callLoc,
|
|
347
560
|
};
|
|
@@ -356,17 +569,22 @@ function parseProgram(context, meta) {
|
|
|
356
569
|
* converted; loops and output code around it are ignored. A `/* SQL: SELECT a, b *\/` comment
|
|
357
570
|
* supplies the column list LookupRows cannot carry.
|
|
358
571
|
*
|
|
572
|
+
* With `{ platform: 'mcn' }` the Marketing Cloud Next calls are read instead: `LookupRows` on a
|
|
573
|
+
* marketing object and `RetrieveSalesforceObjects("Object", "F1,F2", "Field", "op", "v"[, …])`;
|
|
574
|
+
* LookupOrderedRows and the CS variants are errors there.
|
|
575
|
+
*
|
|
359
576
|
* @param {string} text AMPscript code
|
|
577
|
+
* @param {{platform?: 'mce'|'mcn'}} [options] platform (default 'mce')
|
|
360
578
|
* @returns {{ast: object|null, messages: import('./messages.js').Message[], meta: {target: string|null, deName?: string, deKey?: string}}} AST (null on error), messages and meta data
|
|
361
579
|
*/
|
|
362
|
-
export function parseAmpscript(text) {
|
|
580
|
+
export function parseAmpscript(text, options = {}) {
|
|
363
581
|
const source = typeof text === 'string' ? text : '';
|
|
364
582
|
const context = createContext(source);
|
|
365
583
|
const meta = { target: null };
|
|
366
584
|
const ast = runParser(context, () => {
|
|
367
585
|
tokenize(context, 'ampscript', findAmpscriptRegions(source));
|
|
368
586
|
rejectEmpty(context);
|
|
369
|
-
return parseProgram(context, meta);
|
|
587
|
+
return parseProgram(context, meta, options.platform === 'mcn');
|
|
370
588
|
});
|
|
371
589
|
return { ast, messages: context.messages, meta };
|
|
372
590
|
}
|