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/src/print-sql.js CHANGED
@@ -6,6 +6,8 @@
6
6
  * name needs them, text in single quotes with `''` escaping, numbers unquoted. A text value
7
7
  * that is a plain number without leading zeros (e.g. '500', '-2.5') prints unquoted, because
8
8
  * the generated code carries every value as a string and the two forms compare as equal.
9
+ * Marketing Cloud Next keeps the type (`p1="5"` is text, `p1=5` a number): with `isTyped` the
10
+ * value kind alone decides, in printing and in the round-trip comparison.
9
11
  */
10
12
 
11
13
  import { CodeWriter, collectClauses } from './clauses.js';
@@ -47,10 +49,11 @@ export function formatName(name) {
47
49
  * Formats a literal value.
48
50
  *
49
51
  * @param {Value} value value node
52
+ * @param {boolean} [isTyped] true to quote every text value, even a plain number
50
53
  * @returns {string} SQL literal
51
54
  */
52
- export function formatValue(value) {
53
- if (value.kind === 'number' || isPlainNumber(value.value)) {
55
+ export function formatValue(value, isTyped = false) {
56
+ if (value.kind === 'number' || (!isTyped && isPlainNumber(value.value))) {
54
57
  return value.value;
55
58
  }
56
59
  return `'${value.value.replaceAll("'", "''")}'`;
@@ -63,41 +66,44 @@ export function formatValue(value) {
63
66
  * @param {object} expr expression node
64
67
  * @param {Map<object, string>} idOf node → clause id
65
68
  * @param {'and'|'or'|null} parent type of the enclosing node
69
+ * @param {boolean} isTyped true to quote every text value
66
70
  */
67
- function writeExpr(out, expr, idOf, parent) {
71
+ function writeExpr(out, expr, idOf, parent, isTyped) {
68
72
  if (expr.type === 'and' || expr.type === 'or') {
69
73
  const isNeedsBrackets = expr.type === 'or' && parent === 'and';
70
74
  out.write(isNeedsBrackets ? '(' : '');
71
- writeExpr(out, expr.left, idOf, expr.type);
75
+ writeExpr(out, expr.left, idOf, expr.type, isTyped);
72
76
  out.write(` ${expr.type.toUpperCase()} `);
73
- writeExpr(out, expr.right, idOf, expr.type);
77
+ writeExpr(out, expr.right, idOf, expr.type, isTyped);
74
78
  out.write(isNeedsBrackets ? ')' : '');
75
79
  return;
76
80
  }
77
- out.mark(idOf.get(expr), formatCondition(expr));
81
+ out.mark(idOf.get(expr), formatCondition(expr, isTyped));
78
82
  }
79
83
 
80
84
  /**
81
85
  * Formats a single condition.
82
86
  *
83
87
  * @param {object} expr condition node
88
+ * @param {boolean} isTyped true to quote every text value
84
89
  * @returns {string} SQL text
85
90
  */
86
- function formatCondition(expr) {
91
+ function formatCondition(expr, isTyped) {
87
92
  const column = formatName(expr.column);
93
+ const literal = (value) => formatValue(value, isTyped);
88
94
  switch (expr.type) {
89
95
  case 'compare': {
90
- return `${column} ${expr.op} ${formatValue(expr.value)}`;
96
+ return `${column} ${expr.op} ${literal(expr.value)}`;
91
97
  }
92
98
  case 'like': {
93
99
  return `${column} LIKE '${expr.pattern.value.replaceAll("'", "''")}'`;
94
100
  }
95
101
  case 'in': {
96
- const values = expr.values.map((value) => formatValue(value)).join(', ');
102
+ const values = expr.values.map((value) => literal(value)).join(', ');
97
103
  return `${column} ${expr.negated ? 'NOT IN' : 'IN'} (${values})`;
98
104
  }
99
105
  case 'between': {
100
- return `${column} BETWEEN ${formatValue(expr.low)} AND ${formatValue(expr.high)}`;
106
+ return `${column} BETWEEN ${literal(expr.low)} AND ${literal(expr.high)}`;
101
107
  }
102
108
  case 'null': {
103
109
  return `${column} IS ${expr.negated ? 'NOT NULL' : 'NULL'}`;
@@ -112,9 +118,11 @@ function formatCondition(expr) {
112
118
  * Prints an AST as canonical SQL and records where each clause is in the text.
113
119
  *
114
120
  * @param {SelectAst} ast statement
121
+ * @param {{isTyped?: boolean}} [options] isTyped: quote every text value (Next keeps types)
115
122
  * @returns {{sql: string, spans: Span[]}} SQL text and clause spans
116
123
  */
117
- export function printSqlWithSpans(ast) {
124
+ export function printSqlWithSpans(ast, options = {}) {
125
+ const isTyped = Boolean(options.isTyped);
118
126
  const { idOf } = collectClauses(ast);
119
127
  const out = new CodeWriter();
120
128
  out.write('SELECT ');
@@ -131,7 +139,7 @@ export function printSqlWithSpans(ast) {
131
139
  out.write('\nFROM ').mark('from', formatName(ast.from.name));
132
140
  if (ast.where) {
133
141
  out.write('\nWHERE ');
134
- writeExpr(out, ast.where, idOf, null);
142
+ writeExpr(out, ast.where, idOf, null, isTyped);
135
143
  }
136
144
  if (ast.orderBy?.length) {
137
145
  out.write('\nORDER BY ');
@@ -150,10 +158,11 @@ export function printSqlWithSpans(ast) {
150
158
  * Prints an AST as canonical SQL.
151
159
  *
152
160
  * @param {SelectAst} ast statement
161
+ * @param {{isTyped?: boolean}} [options] isTyped: quote every text value (Next keeps types)
153
162
  * @returns {string} SQL text
154
163
  */
155
- export function printSql(ast) {
156
- return printSqlWithSpans(ast).sql;
164
+ export function printSql(ast, options = {}) {
165
+ return printSqlWithSpans(ast, options).sql;
157
166
  }
158
167
 
159
168
  // ------------------------------------------------------------------------------------------
@@ -169,16 +178,20 @@ export function printSql(ast) {
169
178
  * @property {boolean} [expandBetween] treat `BETWEEN a AND b` like `>= a AND <= b`
170
179
  * @property {boolean} [dropTop] ignore TOP (the target dropped it)
171
180
  * @property {boolean} [dropOrderBy] ignore ORDER BY (the target dropped it)
181
+ * @property {boolean} [isTyped] compare text and number values by their kind: '5' differs from
182
+ * 5 (Next keeps the type)
172
183
  */
173
184
 
174
185
  /**
175
- * Normalizes a value: plain numbers compare by numeric value, everything else as text.
186
+ * Normalizes a value: plain numbers compare by numeric value, everything else as text. With
187
+ * `isTyped` the value kind decides instead of the text.
176
188
  *
177
189
  * @param {Value} value value node
190
+ * @param {NormalizeOptions} options flags
178
191
  * @returns {string} normalized value
179
192
  */
180
- function normalizeValue(value) {
181
- if (isPlainNumber(value.value)) {
193
+ function normalizeValue(value, options) {
194
+ if (options.isTyped ? value.kind === 'number' : isPlainNumber(value.value)) {
182
195
  return `n:${Number(value.value)}`;
183
196
  }
184
197
  return `s:${value.value}`;
@@ -214,13 +227,13 @@ function normalizeExpr(expr, options) {
214
227
  ]);
215
228
  }
216
229
  case 'compare': {
217
- return ['compare', expr.op, column, normalizeValue(expr.value)];
230
+ return ['compare', expr.op, column, normalizeValue(expr.value, options)];
218
231
  }
219
232
  case 'like': {
220
233
  return ['like', column, `s:${expr.pattern.value}`];
221
234
  }
222
235
  case 'in': {
223
- const values = expr.values.map((value) => normalizeValue(value));
236
+ const values = expr.values.map((value) => normalizeValue(value, options));
224
237
  if (expr.negated && options.expandNotIn) {
225
238
  return junction(
226
239
  'and',
@@ -236,8 +249,8 @@ function normalizeExpr(expr, options) {
236
249
  return ['in', expr.negated, column, values];
237
250
  }
238
251
  case 'between': {
239
- const low = normalizeValue(expr.low);
240
- const high = normalizeValue(expr.high);
252
+ const low = normalizeValue(expr.low, options);
253
+ const high = normalizeValue(expr.high, options);
241
254
  if (options.expandBetween) {
242
255
  return junction('and', [
243
256
  ['compare', '>=', column, low],
package/src/sql-parser.js CHANGED
@@ -880,7 +880,12 @@ function compareLiterals(left, right) {
880
880
  return Number(left.value) - Number(right.value);
881
881
  }
882
882
  if (ISO_DATE_PATTERN.test(left.value) && ISO_DATE_PATTERN.test(right.value)) {
883
- const toTime = (text) => Date.parse(text.replace(' ', 'T'));
883
+ // Read every value as UTC: Date.parse reads a date-only text as UTC but a text with a
884
+ // time as local time, which would shift one side by the local offset.
885
+ const toTime = (text) => {
886
+ const iso = text.replace(' ', 'T');
887
+ return Date.parse(`${iso.includes('T') ? iso : `${iso}T00:00`}Z`);
888
+ };
884
889
  return toTime(left.value) - toTime(right.value);
885
890
  }
886
891
  const a = left.value.trimEnd().toLowerCase();