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

Commit aa13dc6d by Harshith Kashyap Committed by Jan Aagaard Meier

Fixes MSSQL bulkInsertQuery when options and attributes are not passed (#6782)

1 parent 5a266dcb
# Future
- [FIXED] MSSQL bulkInsertQuery when options and attributes are not passed
- [FIXED] `DATEONLY` now returns `YYYY-MM-DD` date string [#4858] (https://github.com/sequelize/sequelize/issues/4858)
- [FIXED] Issues with `createFunction` and `dropFunction` (PostgresSQL)
- [FIXED] Issue with belongsTo association and foreign keys [#6400](https://github.com/sequelize/sequelize/issues/6400)
......
......@@ -232,6 +232,8 @@ var QueryGenerator = {
},
bulkInsertQuery(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 = []
......@@ -247,14 +249,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;
}
......@@ -296,7 +299,6 @@ var QueryGenerator = {
'SET IDENTITY_INSERT', this.quoteTable(tableName), 'OFF;'
].join(' ');
}
return generatedQuery;
},
......
......@@ -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!