MCPcopy
hub / github.com/sequelize/sequelize / bulkInsertQuery

Method bulkInsertQuery

lib/dialects/db2/query-generator.js:288–347  ·  view source on GitHub ↗
(tableName, attrValueHashes, options, attributes)

Source from the content-addressed store, hash-verified

286 }
287
288 bulkInsertQuery(tableName, attrValueHashes, options, attributes) {
289 options = options || {};
290 attributes = attributes || {};
291 let query = 'INSERT INTO <%= table %> (<%= attributes %>)<%= output %> VALUES <%= tuples %>;';
292 if (options.returning) {
293 query = 'SELECT * FROM FINAL TABLE( INSERT INTO <%= table %> (<%= attributes %>)<%= output %> VALUES <%= tuples %>);';
294 }
295 const emptyQuery = 'INSERT INTO <%= table %>',
296 tuples = [],
297 allAttributes = [],
298 allQueries = [];
299
300 let outputFragment;
301 const valuesForEmptyQuery = [];
302
303 if (options.returning) {
304 outputFragment = '';
305 }
306 _.forEach(attrValueHashes, attrValueHash => {
307 // special case for empty objects with primary keys
308 const fields = Object.keys(attrValueHash);
309 const firstAttr = attributes[fields[0]];
310 if (fields.length === 1 && firstAttr && firstAttr.autoIncrement && attrValueHash[fields[0]] === null) {
311 valuesForEmptyQuery.push(`(${ this.autoGenValue++ })`);
312 return;
313 }
314
315 // normal case
316 _.forOwn(attrValueHash, (value, key) => {
317 if (allAttributes.indexOf(key) === -1) {
318 if (value === null && attributes[key] && attributes[key].autoIncrement)
319 return;
320
321 allAttributes.push(key);
322 }
323 });
324 });
325 if (valuesForEmptyQuery.length > 0) {
326 allQueries.push(`${emptyQuery } VALUES ${ valuesForEmptyQuery.join(',')}`);
327 }
328
329 if (allAttributes.length > 0) {
330 _.forEach(attrValueHashes, attrValueHash => {
331 tuples.push(`(${
332 allAttributes.map(key =>
333 this.escape(attrValueHash[key]), undefined, { context: 'INSERT' }).join(',')})`);
334 });
335 allQueries.push(query);
336 }
337 const replacements = {
338 table: this.quoteTable(tableName),
339 attributes: allAttributes.map(attr =>
340 this.quoteIdentifier(attr)).join(','),
341 tuples,
342 output: outputFragment
343 };
344
345 const generatedQuery = _.template(allQueries.join(';'), this._templateSettings)(replacements);

Callers 2

insert.test.jsFile · 0.45

Calls 3

quoteIdentifierMethod · 0.95
quoteTableMethod · 0.80
escapeMethod · 0.45

Tested by

no test coverage detected