-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsql2008.js
365 lines (316 loc) · 11.3 KB
/
tsql2008.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/// this file generates tsql compatiable with sqlserver2008+
/*jslint node: true */
'use strict';
var getFullyQualifiedName = function(dbName,tblName) {
if (tblName.indexOf('#') > -1) { return tblName; }
return '[' + dbName + '].[dbo].[' + tblName + ']';
};
var getFullyQualifiedDestination = function(config, tbl) {
return getFullyQualifiedName(config.databases.destination, tbl.destination);
};
var getFullyQualifiedSource = function(config, tbl) {
return getFullyQualifiedName(config.databases.source, tbl.source);
};
var sqlMsg = function(message) {
return "PRINT('" + message.replace(/'/g,"''") + "');";
};
var sqlComment = function(sql, isNotCommented) {
if (isNotCommented) { return sql; }
return '-- ' + sql;
};
var sqlSetup = (function(){
var core = function(config) {
var mappr = config.mappr;
console.log(sqlMsg('======== BEGIN SETUP ========'));
console.log(sqlMsg(''));
console.log(sqlMsg('SETUP: Switching to [' + config.databases.destination + ']'));
console.log('USE [' + config.databases.destination + '];');
if(mappr.hasLookupFunctions()) {
console.log('-- BEGIN CREATE FUNCTIONS --\n');
console.log(sqlMsg('SETUP: Creating look-up functions'));
console.log(sqlMsg(''));
console.log('GO\n');
console.log(mappr.getFunctionCreateSql() + '\n');
console.log('-- END CREATE FUNCTIONS --\n');
}
console.log(sqlMsg('SETUP: Executing Setup Commands'));
console.log(sqlMsg(''));
var setupCmds = [];
for(var t in config.tables) {
var tbl = config.tables[t];
if (!tbl.setup || setupCmds.indexOf(tbl.setup) > -1) { continue; }
setupCmds.push(tbl.setup);
console.log(tbl.setup);
}
console.log('');
console.log(sqlMsg('========= END SETUP ========='));
console.log(sqlMsg(''));
};
var testing = function(config) {
if (!config.modes.sqlTest) { return; }
console.log(sqlMsg('==================================================='));
console.log(sqlMsg('=!!!!! TEST MODE: TRANSACTION IS ROLLED BACK !!!!!='));
console.log(sqlMsg('==================================================='));
console.log(sqlMsg(''));
console.log('BEGIN TRANSACTION;');
};
var cleanup = function(config) {
console.log('\n-- BEGIN CLEAN UP SQL --\n');
console.log(sqlMsg('======== BEGIN CLEANUP ========'));
console.log(sqlMsg(''));
var cmds = [],
cmd = '';
// delete things in reverse order
for(var i = config.tables.length-1; i >= 0; --i) {
var tbl = config.tables[i],
fullDestination = getFullyQualifiedDestination(config, tbl),
fullSource = getFullyQualifiedSource(config,tbl);
if (tbl.truncate && !tbl.killWhere) {
cmd = 'TRUNCATE TABLE ' + fullDestination + ';';
if (cmds.indexOf(cmd) < 0) {
console.log(sqlComment(sqlMsg('INFO: Truncating table ' + fullDestination), !tbl.useMerge));
console.log(sqlComment(cmd, !tbl.useMerge));
console.log(sqlComment(sqlMsg(''), !tbl.useMerge));
cmds.push(cmd);
}
}
if (!tbl.truncate && tbl.killWhere) {
cmd = 'DELETE FROM ' + fullDestination + ' WHERE ' + tbl.killWhere +';';
if (cmds.indexOf(cmd) < 0) {
console.log(sqlComment(sqlMsg('INFO: Deleting all records in ' + fullDestination + ' where ' + tbl.killWhere), !tbl.useMerge));
console.log(sqlComment(cmd, !tbl.useMerge));
console.log(sqlComment(sqlMsg(''), !tbl.useMerge));
cmds.push(cmd);
}
}
}
console.log(sqlMsg('========= END CLEANUP ========='));
console.log(sqlMsg(''));
console.log('\n-- END CLEAN UP SQL --\n');
};
return function(config) {
core(config);
testing(config);
cleanup(config);
};
})();
var sqlTeardown = (function(){
var core = function(config) {
console.log(sqlMsg('======== BEGIN TEARDOWN ========'));
console.log(sqlMsg(''));
console.log('GO\n');
if (config.mappr.hasLookupFunctions()) {
console.log('-- BEGIN DROP FUNCTIONS --\n');
console.log(sqlMsg('TEARDOWN: Dropping Look-up Functions'));
console.log(sqlMsg(''));
console.log('GO\n');
console.log(config.mappr.getFunctionDropSql());
console.log('-- END DROP FUNCTIONS --\n');
}
console.log(sqlMsg('TEARDOWN: Executing Teardown Commands'));
console.log(sqlMsg(''));
var teardownCmds = [];
for(var t in config.tables) {
var tbl = config.tables[t];
if (!tbl.teardown || teardownCmds.indexOf(tbl.teardown) > -1) { continue; }
teardownCmds.push(tbl.teardown);
console.log(tbl.teardown);
}
console.log(sqlMsg('========= END TEARDOWN ========='));
};
var testing = function(config) {
if(!config.modes.sqlTest) { return; }
console.log(sqlMsg('==================================================='));
console.log(sqlMsg('=!!!!!! TEST MODE: ROLLING BACK TRANSACTION !!!!!!='));
console.log(sqlMsg('==================================================='));
console.log('rollback;');
console.log(sqlMsg(''));
};
return function(config) {
testing(config);
core(config);
};
})();
var sqlBody = (function() {
var migrateTable = function(config, tbl) {
var processed = processConfig(config,tbl);
if (tbl.useMerge) {
migrateWithMerge(tbl, processed);
} else {
migrateWithInsert(tbl, processed);
}
console.log(sqlMsg('') + '\n');
};
var processConfig = function(config, tbl) {
var destColNames = [],
srcCols = [],
mergeOn = [],
groupbys = [],
aggregates = [];
// for each column name in tbl.columns
for(var colName in tbl.columns) {
// add the colName to the list
var destCol = '[' + colName + ']';
destColNames.push(destCol);
// source mapping
var srcMapping = tbl.columns[colName];
// transform the source
var srcCol = srcMapping.transform.call(null, tbl);
// handle merge
if (srcCol.mergeOn) {
if (srcCol.column) {
srcCols.push('S.' + srcCol.column);
mergeOn.push('D.' + destCol + ' = S.' + srcCol.column);
} else {
if (!!srcCol.raw) {
srcCols.push(srcCol.raw);
mergeOn.push('D.' + destCol + ' = ' + srcCol.raw);
} else {
srcCols.push('NULL');
mergeOn.push('D.' + destCol + ' IS NULL');
}
}
} else {
srcCols.push(srcCol);
}
var qualifiedInner = '';
// handle grouping for single column options
if (tbl.useGrouping && srcMapping.options.isColumn) {
qualifedInner = '[' + srcMapping.options.sourceColumn + ']';
if (groupbys.indexOf(qualifedInner) < 0) {
groupbys.push(qualifedInner);
}
}
// handle grouping for multi-column options
if (tbl.useGrouping && srcMapping.options.isColumns) {
var innerCols = [];
for(var ic in srcMapping.options.columns) {
qualifedInner = '[' + srcMapping.options.columns[ic] + ']';
if (groupbys.indexOf(qualifedInner) < 0) {
groupbys.push(qualifedInner);
}
}
}
// handle grouping w/ aggregate function
if (tbl.useGrouping && srcMapping.options.aggregate) {
var qualifedInner = '[' + srcMapping.options.sourceColumn + ']';
aggregates.push(srcMapping.options.aggregate + '(' + qualifedInner + ') AS ' + qualifedInner);
}
}
return {
fullDestination: getFullyQualifiedDestination.call(null, config, tbl),
fullSource : getFullyQualifiedSource.call(null, config, tbl),
destColNames : destColNames,
srcCols : srcCols,
mergeOn : mergeOn,
groupBys : groupbys,
aggregates : aggregates
};
};
var migrateWithMerge = function(tbl, p) {
console.log(sqlMsg('==== Merging ' + p.fullDestination + ' with ' + p.fullSource + ' ====='));
if (tbl.message) {
console.log(sqlMsg('INFO: ' + tbl.message));
}
// begin sql MERGE
console.log('MERGE ' + p.fullDestination + ' AS D');
// begin sql MERGE USING clause
var using = 'USING ';
// force SELECT if needed
if (tbl.sourceFilter || tbl.useGrouping || tbl.forceDistinct) {
using += '(\n SELECT ';
// force distinct
if (tbl.forceDistinct) {
using += 'DISTINCT\n ';
}
// select group by cols or *
var groupByCols = p.groupBys.join(',\n '),
aggregateCols = p.aggregates.join(',\n ');
if (tbl.useGrouping) {
using += groupByCols + (p.aggregates && p.aggregates.length > 0 ? ',\n ' + aggregateCols : '');
} else {
using += '*';
}
// select FROM
using += '\n FROM ' + p.fullSource;
// WHERE CLAUSE
if (tbl.sourceFilter) {
using += '\n WHERE ' + tbl.sourceFilter;
}
// group by clause
if (tbl.useGrouping) {
using += '\n GROUP BY ' + groupByCols;
}
using += '\n )';
} else {
using += p.fullSource;
}
using += ' AS S';
console.log(using);
console.log(' ON ' + p.mergeOn.join('\n AND '));
var sets = [],
inserts = [];
for(var i in p.destColNames) {
// do not update pks!
if (p.destColNames[i] != '[' + tbl.mergePk + ']') {
sets.push(' ' + p.destColNames[i] + ' = ' + p.srcCols[i]);
}
inserts.push('Inserted.' + p.destColNames[i]);
}
if (!tbl.insertOnly) {
console.log('WHEN MATCHED THEN');
console.log(' UPDATE SET');
console.log(sets.join(',\n'));
}
if (!tbl.updateOnly) {
console.log('WHEN NOT MATCHED THEN');
console.log(' INSERT (\n ' + p.destColNames.join(',\n ') + '\n ) VALUES (');
console.log(' ' + p.srcCols.join(',\n ') + '\n )');
}
console.log('OUTPUT $action,\n ' + inserts.join(',\n ') + ';');
};
var migrateWithInsert = function(tbl, p) {
console.log(sqlMsg('==== Inserting into ' + p.fullDestination + ' with ' + p.fullSource + ' ====='));
if (tbl.message) {
console.log(sqlMsg('MSG: ' + tbl.message));
}
var sql = 'INSERT INTO ' + p.fullDestination + ' (\n';
sql += p.destColNames.join(',\n');
sql += '\n) SELECT ';
if (tbl.forceDistinct) {
sql += 'DISTINCT\n ';
} else {
sql += '\n' + p.srcCols.join(',\n');
if (tbl.useGrouping && p.aggregates && p.aggregates.length > 0) {
sql += p.aggregates.join(',\n');
}
}
sql += '\nFROM ' + p.fullSource;
if (tbl.sourceFilter) {
sql += '\nWHERE ' + tbl.sourceFilter;
}
if (tbl.useGrouping) {
sql += '\nGROUP BY ' + p.groupBys.join(', ');
}
console.log(sql + ';\n');
};
return function(config) {
console.log('\n-- BEGIN INSERT-SELECT SQL --\n');
for(var pass = 0; pass < config.modes.passes; pass++) {
// for each table name in config.tables
console.log(sqlMsg('======== BEGIN PASS ' + (pass + 1) + ' of ' + config.modes.passes + ' ========'));
console.log(sqlMsg(''));
for(var i in config.tables) {
migrateTable(config, config.tables[i]);
}
console.log(sqlMsg('======= END OF PASS ' + (pass + 1) + ' of ' + config.modes.passes + ' ========'));
console.log(sqlMsg(''));
}
console.log('-- END INSERT-SELECT SQL -- \n');
};
})();
exports.GenerateSql = function(config) {
sqlSetup(config);
sqlBody(config);
sqlTeardown(config);
};