sfmc-sql-converter 0.0.0-stage → 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/src/index.js ADDED
@@ -0,0 +1,28 @@
1
+ export { parseSql } from './sql-parser.js';
2
+ export * from './messages.js';
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';
15
+ export {
16
+ MCE_TARGETS,
17
+ emitSsjsWsproxy,
18
+ emitSsjsRowsRetrieve,
19
+ emitSsjsLookupRows,
20
+ emitSsjsLookupOrderedRows,
21
+ emitAmpscriptLookupRows,
22
+ emitAmpscriptLookupOrderedRows,
23
+ emitAmpscriptLookup,
24
+ ampVariableNames,
25
+ } from './emit-mce.js';
26
+ export { printSql, printSqlWithSpans, normalizeSql } from './print-sql.js';
27
+ export { parseSsjs } from './parse-ssjs.js';
28
+ export { parseAmpscript } from './parse-ampscript.js';
package/src/like.js ADDED
@@ -0,0 +1,44 @@
1
+ /**
2
+ * SQL LIKE matching as observed in Marketing Cloud Engagement query activities: anchored,
3
+ * case-insensitive, `%` = any text, `_` = one character, `[…]` / `[^…]` = one character of a set
4
+ * (so `[%]` matches a literal %). Used by the parser to evaluate an ISNULL / COALESCE fallback
5
+ * at conversion time, and by the tests.
6
+ */
7
+
8
+ // one LIKE token: %, _, a character set such as [%] / [^a-c], or any single character
9
+ const LIKE_TOKEN = /%|_|\[\^?[^\]]+\]|[\s\S]/g;
10
+
11
+ /**
12
+ * Converts a LIKE pattern into a regular expression.
13
+ *
14
+ * @param {string} pattern LIKE pattern
15
+ * @returns {RegExp} case-insensitive, anchored expression
16
+ */
17
+ export function likeToRegExp(pattern) {
18
+ const source = pattern.replaceAll(LIKE_TOKEN, (token) => {
19
+ if (token === '%') {
20
+ return String.raw`[\s\S]*`;
21
+ }
22
+ if (token === '_') {
23
+ return String.raw`[\s\S]`;
24
+ }
25
+ if (token.length > 2 && token.startsWith('[')) {
26
+ const isNegated = token[1] === '^';
27
+ const body = token.slice(isNegated ? 2 : 1, -1).replaceAll(/[\\^]/g, String.raw`\$&`);
28
+ return `[${isNegated ? '^' : ''}${body}]`;
29
+ }
30
+ return token.replaceAll(/[.*+?^${}()|[\]\\/]/g, String.raw`\$&`);
31
+ });
32
+ return new RegExp(`^${source}$`, 'i');
33
+ }
34
+
35
+ /**
36
+ * Checks a value against a LIKE pattern; trailing spaces of the value are ignored, as in SQL.
37
+ *
38
+ * @param {string} value the value
39
+ * @param {string} pattern LIKE pattern
40
+ * @returns {boolean} true when the value matches
41
+ */
42
+ export function sqlLike(value, pattern) {
43
+ return likeToRegExp(pattern).test(String(value).replace(/ +$/, ''));
44
+ }
@@ -0,0 +1,235 @@
1
+ /**
2
+ * Message codes and the message factory shared by the parser and (later) the emitters.
3
+ *
4
+ * Every message has the shape
5
+ * `{ severity: 'error'|'warning'|'info', code, message, line, column, endLine, endColumn }`.
6
+ * `line` / `column` are 1-based; `endLine` / `endColumn` point just after the last character
7
+ * (exclusive end), which matches how most editors mark a range.
8
+ */
9
+
10
+ // --- input / lexer -------------------------------------------------------------------------
11
+ export const EMPTY_INPUT = 'EMPTY_INPUT';
12
+ export const UNEXPECTED_CHARACTER = 'UNEXPECTED_CHARACTER';
13
+ export const UNTERMINATED_STRING = 'UNTERMINATED_STRING';
14
+ export const UNTERMINATED_IDENTIFIER = 'UNTERMINATED_IDENTIFIER';
15
+ export const UNTERMINATED_COMMENT = 'UNTERMINATED_COMMENT';
16
+ export const EMPTY_IDENTIFIER = 'EMPTY_IDENTIFIER';
17
+ export const UNSUPPORTED_LINE_SEPARATOR = 'UNSUPPORTED_LINE_SEPARATOR';
18
+
19
+ // --- syntax -------------------------------------------------------------------------------
20
+ export const SYNTAX_ERROR = 'SYNTAX_ERROR';
21
+ export const EXPECTED_SELECT = 'EXPECTED_SELECT';
22
+ export const EXPECTED_FROM = 'EXPECTED_FROM';
23
+ export const EXPECTED_IDENTIFIER = 'EXPECTED_IDENTIFIER';
24
+ export const EXPECTED_OPERATOR = 'EXPECTED_OPERATOR';
25
+ export const EXPECTED_VALUE = 'EXPECTED_VALUE';
26
+ export const EXPECTED_STRING = 'EXPECTED_STRING';
27
+ export const MISSING_CLOSING_PAREN = 'MISSING_CLOSING_PAREN';
28
+ export const INVALID_TOP = 'INVALID_TOP';
29
+ export const LITERAL_ON_LEFT = 'LITERAL_ON_LEFT';
30
+ export const NULL_COMPARISON = 'NULL_COMPARISON';
31
+ export const NESTING_TOO_DEEP = 'NESTING_TOO_DEEP';
32
+ export const INTERNAL_ERROR = 'INTERNAL_ERROR';
33
+
34
+ // --- unsupported constructs -----------------------------------------------------------------
35
+ export const UNSUPPORTED_JOIN = 'UNSUPPORTED_JOIN';
36
+ export const UNSUPPORTED_SUBQUERY = 'UNSUPPORTED_SUBQUERY';
37
+ export const UNSUPPORTED_GROUP_BY = 'UNSUPPORTED_GROUP_BY';
38
+ export const UNSUPPORTED_HAVING = 'UNSUPPORTED_HAVING';
39
+ export const UNSUPPORTED_AGGREGATE = 'UNSUPPORTED_AGGREGATE';
40
+ export const UNSUPPORTED_DISTINCT = 'UNSUPPORTED_DISTINCT';
41
+ export const UNSUPPORTED_FUNCTION = 'UNSUPPORTED_FUNCTION';
42
+ export const UNSUPPORTED_COLUMN_COMPARISON = 'UNSUPPORTED_COLUMN_COMPARISON';
43
+ export const UNSUPPORTED_ALIAS = 'UNSUPPORTED_ALIAS';
44
+ export const UNSUPPORTED_QUALIFIED_NAME = 'UNSUPPORTED_QUALIFIED_NAME';
45
+ export const UNSUPPORTED_NOT_LIKE = 'UNSUPPORTED_NOT_LIKE';
46
+ export const UNSUPPORTED_NOT_BETWEEN = 'UNSUPPORTED_NOT_BETWEEN';
47
+ export const UNSUPPORTED_NOT_CONDITION = 'UNSUPPORTED_NOT_CONDITION';
48
+ export const UNSUPPORTED_STAR_WITH_COLUMNS = 'UNSUPPORTED_STAR_WITH_COLUMNS';
49
+ export const UNSUPPORTED_UNION = 'UNSUPPORTED_UNION';
50
+ export const UNSUPPORTED_MULTIPLE_STATEMENTS = 'UNSUPPORTED_MULTIPLE_STATEMENTS';
51
+ export const UNSUPPORTED_OFFSET_FETCH = 'UNSUPPORTED_OFFSET_FETCH';
52
+
53
+ // --- warnings / info ------------------------------------------------------------------------
54
+ export const SYSTEM_DATA_VIEW = 'SYSTEM_DATA_VIEW';
55
+ export const TABLE_HINT_IGNORED = 'TABLE_HINT_IGNORED';
56
+ export const NULL_FALLBACK_REWRITTEN = 'NULL_FALLBACK_REWRITTEN';
57
+
58
+ // --- emitters / convert() -------------------------------------------------------------------
59
+ export const ORDER_BY_DROPPED = 'ORDER_BY_DROPPED';
60
+ export const TOP_DROPPED = 'TOP_DROPPED';
61
+ export const DE_KEY_ASSUMED = 'DE_KEY_ASSUMED';
62
+ export const SELECT_STAR_NEEDS_COLUMNS = 'SELECT_STAR_NEEDS_COLUMNS';
63
+ export const LIKE_CHECKED_IN_SCRIPT = 'LIKE_CHECKED_IN_SCRIPT';
64
+ export const EMPTY_STRING_AS_SPACE = 'EMPTY_STRING_AS_SPACE';
65
+ export const TOP_ABOVE_LOOKUP_LIMIT = 'TOP_ABOVE_LOOKUP_LIMIT';
66
+ export const FIRST_BATCH_ONLY = 'FIRST_BATCH_ONLY';
67
+ export const ORDER_BY_NEEDS_TOP = 'ORDER_BY_NEEDS_TOP';
68
+
69
+ // --- reverse parsers (code to SQL) ------------------------------------------------------
70
+ export const NO_LOOKUP_FOUND = 'NO_LOOKUP_FOUND';
71
+ export const ARGUMENT_COUNT = 'ARGUMENT_COUNT';
72
+ export const INVALID_ARGUMENT = 'INVALID_ARGUMENT';
73
+ export const INVALID_OBJECT_TYPE = 'INVALID_OBJECT_TYPE';
74
+ export const INVALID_COUNT = 'INVALID_COUNT';
75
+ export const INVALID_ORDER_BY = 'INVALID_ORDER_BY';
76
+ export const INVALID_FILTER = 'INVALID_FILTER';
77
+ export const INVALID_FILTER_VALUE = 'INVALID_FILTER_VALUE';
78
+ export const INVALID_LOGICAL_OPERATOR = 'INVALID_LOGICAL_OPERATOR';
79
+ export const UNKNOWN_FILTER_OPERATOR = 'UNKNOWN_FILTER_OPERATOR';
80
+ export const UNSUPPORTED_FILTER_OPERATOR = 'UNSUPPORTED_FILTER_OPERATOR';
81
+ export const UNSUPPORTED_EXPRESSION = 'UNSUPPORTED_EXPRESSION';
82
+ export const VARIABLE_VALUE = 'VARIABLE_VALUE';
83
+ export const MULTIPLE_LOOKUPS = 'MULTIPLE_LOOKUPS';
84
+ export const FROM_UNKNOWN = 'FROM_UNKNOWN';
85
+ export const FROM_NAME_FROM_KEY = 'FROM_NAME_FROM_KEY';
86
+ export const LOOKUP_FIRST_ROW_ONLY = 'LOOKUP_FIRST_ROW_ONLY';
87
+ export const CASE_SENSITIVE_LOOKUP = 'CASE_SENSITIVE_LOOKUP';
88
+ export const LIKE_MATCHES_ANYWHERE = 'LIKE_MATCHES_ANYWHERE';
89
+ export const MO_ONE_MATCH = 'MO_ONE_MATCH';
90
+
91
+ /**
92
+ * All message codes, keyed by themselves (handy for iteration and lookups).
93
+ */
94
+ export const MESSAGE_CODES = Object.freeze({
95
+ EMPTY_INPUT,
96
+ UNEXPECTED_CHARACTER,
97
+ UNTERMINATED_STRING,
98
+ UNTERMINATED_IDENTIFIER,
99
+ UNTERMINATED_COMMENT,
100
+ EMPTY_IDENTIFIER,
101
+ UNSUPPORTED_LINE_SEPARATOR,
102
+ SYNTAX_ERROR,
103
+ EXPECTED_SELECT,
104
+ EXPECTED_FROM,
105
+ EXPECTED_IDENTIFIER,
106
+ EXPECTED_OPERATOR,
107
+ EXPECTED_VALUE,
108
+ EXPECTED_STRING,
109
+ MISSING_CLOSING_PAREN,
110
+ INVALID_TOP,
111
+ LITERAL_ON_LEFT,
112
+ NULL_COMPARISON,
113
+ NESTING_TOO_DEEP,
114
+ INTERNAL_ERROR,
115
+ UNSUPPORTED_JOIN,
116
+ UNSUPPORTED_SUBQUERY,
117
+ UNSUPPORTED_GROUP_BY,
118
+ UNSUPPORTED_HAVING,
119
+ UNSUPPORTED_AGGREGATE,
120
+ UNSUPPORTED_DISTINCT,
121
+ UNSUPPORTED_FUNCTION,
122
+ UNSUPPORTED_COLUMN_COMPARISON,
123
+ UNSUPPORTED_ALIAS,
124
+ UNSUPPORTED_QUALIFIED_NAME,
125
+ UNSUPPORTED_NOT_LIKE,
126
+ UNSUPPORTED_NOT_BETWEEN,
127
+ UNSUPPORTED_NOT_CONDITION,
128
+ UNSUPPORTED_STAR_WITH_COLUMNS,
129
+ UNSUPPORTED_UNION,
130
+ UNSUPPORTED_MULTIPLE_STATEMENTS,
131
+ UNSUPPORTED_OFFSET_FETCH,
132
+ SYSTEM_DATA_VIEW,
133
+ TABLE_HINT_IGNORED,
134
+ NULL_FALLBACK_REWRITTEN,
135
+ ORDER_BY_DROPPED,
136
+ TOP_DROPPED,
137
+ DE_KEY_ASSUMED,
138
+ SELECT_STAR_NEEDS_COLUMNS,
139
+ LIKE_CHECKED_IN_SCRIPT,
140
+ EMPTY_STRING_AS_SPACE,
141
+ TOP_ABOVE_LOOKUP_LIMIT,
142
+ FIRST_BATCH_ONLY,
143
+ ORDER_BY_NEEDS_TOP,
144
+ NO_LOOKUP_FOUND,
145
+ ARGUMENT_COUNT,
146
+ INVALID_ARGUMENT,
147
+ INVALID_OBJECT_TYPE,
148
+ INVALID_COUNT,
149
+ INVALID_ORDER_BY,
150
+ INVALID_FILTER,
151
+ INVALID_FILTER_VALUE,
152
+ INVALID_LOGICAL_OPERATOR,
153
+ UNKNOWN_FILTER_OPERATOR,
154
+ UNSUPPORTED_FILTER_OPERATOR,
155
+ UNSUPPORTED_EXPRESSION,
156
+ VARIABLE_VALUE,
157
+ MULTIPLE_LOOKUPS,
158
+ FROM_UNKNOWN,
159
+ FROM_NAME_FROM_KEY,
160
+ LOOKUP_FIRST_ROW_ONLY,
161
+ CASE_SENSITIVE_LOOKUP,
162
+ LIKE_MATCHES_ANYWHERE,
163
+ MO_ONE_MATCH,
164
+ });
165
+
166
+ /**
167
+ * The message codes used only by the reverse parsers (`parseSsjs`, `parseAmpscript`), keyed by
168
+ * themselves.
169
+ */
170
+ export const REVERSE_MESSAGE_CODES = Object.freeze({
171
+ NO_LOOKUP_FOUND,
172
+ ARGUMENT_COUNT,
173
+ INVALID_ARGUMENT,
174
+ INVALID_OBJECT_TYPE,
175
+ INVALID_COUNT,
176
+ INVALID_ORDER_BY,
177
+ INVALID_FILTER,
178
+ INVALID_FILTER_VALUE,
179
+ INVALID_LOGICAL_OPERATOR,
180
+ UNKNOWN_FILTER_OPERATOR,
181
+ UNSUPPORTED_FILTER_OPERATOR,
182
+ UNSUPPORTED_EXPRESSION,
183
+ VARIABLE_VALUE,
184
+ MULTIPLE_LOOKUPS,
185
+ FROM_UNKNOWN,
186
+ FROM_NAME_FROM_KEY,
187
+ LOOKUP_FIRST_ROW_ONLY,
188
+ CASE_SENSITIVE_LOOKUP,
189
+ LIKE_MATCHES_ANYWHERE,
190
+ MO_ONE_MATCH,
191
+ });
192
+
193
+ /**
194
+ * @typedef {'error'|'warning'|'info'} Severity
195
+ */
196
+
197
+ /**
198
+ * @typedef {object} Location
199
+ * @property {number} line 1-based line of the first character
200
+ * @property {number} column 1-based column of the first character
201
+ * @property {number} endLine 1-based line of the exclusive end
202
+ * @property {number} endColumn 1-based column of the exclusive end
203
+ */
204
+
205
+ /**
206
+ * @typedef {object} Message
207
+ * @property {Severity} severity how serious the finding is
208
+ * @property {string} code one of the exported code constants
209
+ * @property {string} message plain-language text: what is wrong and how to fix it
210
+ * @property {number} line 1-based start line
211
+ * @property {number} column 1-based start column
212
+ * @property {number} endLine 1-based end line
213
+ * @property {number} endColumn 1-based exclusive end column
214
+ */
215
+
216
+ /**
217
+ * Builds a message object.
218
+ *
219
+ * @param {Severity} severity 'error', 'warning' or 'info'
220
+ * @param {string} code one of the exported code constants
221
+ * @param {string} message plain-language text: what is wrong and how to fix it
222
+ * @param {Location} location where the problem is (any object with line/column/endLine/endColumn, e.g. an AST `loc`)
223
+ * @returns {Message} the message
224
+ */
225
+ export function createMessage(severity, code, message, location) {
226
+ return {
227
+ severity,
228
+ code,
229
+ message,
230
+ line: location.line,
231
+ column: location.column,
232
+ endLine: location.endLine,
233
+ endColumn: location.endColumn,
234
+ };
235
+ }