不要怂,就是干,撸起袖子干!

Commit 6c362cc5 by Harshith Kashyap Committed by Sushant

V3 backport of MSSQL BulkInsertQuery - #6782 (#6792)

* V3 backport of MSSQL BulkInsertQuery - #6782

* Update changelog.md
1 parent 15ee1c1c
# FUTURE
- [FIXED] MSSQL bulkInsertQuery when options and attributes are not passed [#6782]
# 3.24.6
- [FIXED] groupedLimit.through.where support
......
......@@ -230,6 +230,8 @@ var QueryGenerator = {
},
bulkInsertQuery: function(tableName, attrValueHashes, options, attributes) {
options = options || {};
attributes = attributes || {};
var query = 'INSERT INTO <%= table %> (<%= attributes %>)<%= output %> VALUES <%= tuples %>;'
, emptyQuery = 'INSERT INTO <%= table %><%= output %> DEFAULT VALUES'
, tuples = []
......@@ -245,14 +247,15 @@ var QueryGenerator = {
Utils._.forEach(attrValueHashes, function(attrValueHash) {
// special case for empty objects with primary keys
var fields = Object.keys(attrValueHash);
if (fields.length === 1 && attributes[fields[0]].autoIncrement && attrValueHash[fields[0]] === null) {
var firstAttr = attributes[fields[0]];
if (fields.length === 1 && firstAttr && firstAttr.autoIncrement && attrValueHash[fields[0]] === null) {
allQueries.push(emptyQuery);
return;
}
// normal case
Utils._.forOwn(attrValueHash, function(value, key) {
if (value !== null && attributes[key].autoIncrement) {
if (value !== null && attributes[key] && attributes[key].autoIncrement) {
needIdentityInsertWrapper = true;
}
......
......@@ -24,6 +24,27 @@ if (current.dialect.name === 'mssql') {
});
});
test('bulkInsertQuery', function() {
//normal cases
expectsql(QueryGenerator.bulkInsertQuery('myTable', [{ name: 'foo' }, {name: 'bar'}]), {
mssql: "INSERT INTO [myTable] ([name]) VALUES (N'foo'),(N'bar');"
});
expectsql(QueryGenerator.bulkInsertQuery('myTable', [{ username: 'username', firstName: 'firstName', lastName: 'lastName' }, { firstName: 'user1FirstName', lastName: 'user1LastName'}]), {
mssql: "INSERT INTO [myTable] ([username],[firstName],[lastName]) VALUES (N'username',N'firstName',N'lastName'),(NULL,N'user1FirstName',N'user1LastName');"
});
expectsql(QueryGenerator.bulkInsertQuery('myTable', [{ firstName: 'firstName', lastName: 'lastName' }, { firstName: 'user1FirstName', lastName: 'user1LastName'}]), {
mssql: "INSERT INTO [myTable] ([firstName],[lastName]) VALUES (N'firstName',N'lastName'),(N'user1FirstName',N'user1LastName');"
});
//Bulk Insert With autogenerated primary key
var attributes = { id: { autoIncrement: true }};
expectsql(QueryGenerator.bulkInsertQuery('myTable', [{ id: null }], {}, attributes), {
mssql: "INSERT INTO [myTable] DEFAULT VALUES"
});
});
test('selectFromTableFragment', function() {
var modifiedGen = _.clone(QueryGenerator);
// Test newer versions first
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!