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

Commit c9ace4d8 by Jason Jung Committed by Matt Broadstone

fixed identity column with Insert

1 parent 79b80a8e
var common;
common.quoteTable: function(param, as) {
var table = '';
if (as === true) {
as = param.as || param.name || param;
}
if (_.isObject(param)) {
if (common._dialect.supports.schemas) {
if (param.schema) {
table += common.quoteIdentifier(param.schema) + '.';
}
table += common.quoteIdentifier(param.tableName);
} else {
if (param.schema) {
table += param.schema + param.delimiter;
}
table += param.tableName;
table = common.quoteIdentifier(table);
}
} else {
table = common.quoteIdentifier(param);
}
if (as) {
table += ' AS ' + common.quoteIdentifier(as);
}
return table;
};
/*
Escape an identifier (e.g. a table or attribute name)
*/
/* istanbul ignore next */
common.quoteIdentifier: function(identifier, force) {
throwMethodUndefined('quoteIdentifier');
},
/*
Split an identifier into .-separated tokens and quote each part
*/
common.quoteIdentifiers: function(identifiers, force) {
if (identifiers.indexOf('.') !== -1) {
identifiers = identifiers.split('.');
return common.quoteIdentifier(identifiers.slice(0, identifiers.length - 1).join('.')) + '.' + common.quoteIdentifier(identifiers[identifiers.length - 1]);
} else {
return common.quoteIdentifier(identifiers);
}
},
module.exports = function (queryGenerator) {
common = queryGenerator;
};
...@@ -46,7 +46,7 @@ function quoteIdentifiers(identifiers, force) { ...@@ -46,7 +46,7 @@ function quoteIdentifiers(identifiers, force) {
return quoteIdentifier(identifiers); return quoteIdentifier(identifiers);
} }
} }
function wrapSingleQuote(identifier){ function wrapSingleQuote(identifier){
return Utils.addTicks(identifier, "'"); return Utils.addTicks(identifier, "'");
} }
function nameIndexes(indexes, rawTablename) { function nameIndexes(indexes, rawTablename) {
...@@ -69,7 +69,7 @@ function fieldsToSql(fields, singleQuote){ ...@@ -69,7 +69,7 @@ function fieldsToSql(fields, singleQuote){
if (fields.hasOwnProperty(key)) { if (fields.hasOwnProperty(key)) {
if(singleQuote){ if(singleQuote){
fieldStr.push(wrapSingleQuote(key)); fieldStr.push(wrapSingleQuote(key));
}else{ }else{
fieldStr.push(quoteIdentifier(key)); fieldStr.push(quoteIdentifier(key));
} }
} }
...@@ -79,7 +79,7 @@ function fieldsToSql(fields, singleQuote){ ...@@ -79,7 +79,7 @@ function fieldsToSql(fields, singleQuote){
return fieldStr.join(','); return fieldStr.join(',');
} }
} }
return ''; return '';
} }
function valuesToSql(fields, modelAttributeMap){ function valuesToSql(fields, modelAttributeMap){
var values = []; var values = [];
...@@ -88,7 +88,7 @@ function valuesToSql(fields, modelAttributeMap){ ...@@ -88,7 +88,7 @@ function valuesToSql(fields, modelAttributeMap){
var value = fields[key]; var value = fields[key];
values.push(escape(value, (modelAttributeMap && modelAttributeMap[key]) || undefined)); values.push(escape(value, (modelAttributeMap && modelAttributeMap[key]) || undefined));
} }
} }
if(values){ if(values){
if(values.length > 0){ if(values.length > 0){
return values.join(','); return values.join(',');
...@@ -123,13 +123,6 @@ function addTableExistsWrapper(query, exists){ ...@@ -123,13 +123,6 @@ function addTableExistsWrapper(query, exists){
"END" "END"
].join(" "); ].join(" ");
} }
function identityInsertOnWrapper(query){
return [
'SET IDENTITY_INSERT <%= tableName %> ON;',
query,
'SET IDENTITY_INSERT <%= tableName %> OFF;'
].join(' ');
}
//select stuff //select stuff
function loadFields(attributes){ function loadFields(attributes){
...@@ -139,11 +132,11 @@ function loadFields(attributes){ ...@@ -139,11 +132,11 @@ function loadFields(attributes){
} }
return attrStr; return attrStr;
} }
function loadFields(attributes, tableName){ function loadFieldsWithName(attributes, tableName){
var attrStr = []; var attrStr = [];
for (var attr in attributes) { for (var attr in attributes) {
if(tableName){ if(tableName){
attrStr.push(quoteIdentifier(tableName) + "." + quoteIdentifier(attr)); attrStr.push(quoteIdentifier(tableName) + "." + quoteIdentifier(attr));
}else{ }else{
attrStr.push(quoteIdentifier(attr)); attrStr.push(quoteIdentifier(attr));
} }
...@@ -154,8 +147,8 @@ function joinFields(attributes, tableName){ ...@@ -154,8 +147,8 @@ function joinFields(attributes, tableName){
var attrStr = []; var attrStr = [];
if(tableName){ if(tableName){
for (var attr in attributes) { for (var attr in attributes) {
attrStr.push(quoteIdentifier(tableName) attrStr.push(quoteIdentifier(tableName)
+ "." + "."
+ quoteIdentifier(attr) + quoteIdentifier(attr)
+ " AS " + quoteIdentifier(tableName + "." + attr)); + " AS " + quoteIdentifier(tableName + "." + attr));
} }
...@@ -180,11 +173,11 @@ module.exports = { ...@@ -180,11 +173,11 @@ module.exports = {
get sequelize(){ get sequelize(){
return _sequelize; return _sequelize;
}, },
set sequelize(seq) { set sequelize(seq) {
_sequelize = seq; _sequelize = seq;
}, },
showTableSql: function(){ showTableSql: function(){
return 'SELECT name FROM sys.Tables;'; return 'SELECT name FROM sys.Tables;';
}, },
getCreateTableSql: function(tableName, attributes, options) { getCreateTableSql: function(tableName, attributes, options) {
...@@ -204,7 +197,7 @@ module.exports = { ...@@ -204,7 +197,7 @@ module.exports = {
}; };
query = addTableExistsWrapper(query); query = addTableExistsWrapper(query);
return Utils._.template(query)(values).trim() + ";"; return Utils._.template(query)(values).trim() + ";";
}, },
alterTableSql: function(tableName){ alterTableSql: function(tableName){
var query = 'ALTER TABLE <%= tableName %>'; var query = 'ALTER TABLE <%= tableName %>';
var value = { var value = {
...@@ -218,7 +211,7 @@ module.exports = { ...@@ -218,7 +211,7 @@ module.exports = {
unquotedTable: tableName, unquotedTable: tableName,
tableName: quoteIdentifier(tableName) tableName: quoteIdentifier(tableName)
}; };
query = addTableExistsWrapper(query, true); query = addTableExistsWrapper(query, true);
return Utils._.template(query)(values).trim() + ";"; return Utils._.template(query)(values).trim() + ";";
}, },
...@@ -236,6 +229,7 @@ module.exports = { ...@@ -236,6 +229,7 @@ module.exports = {
for (var key in valueHash) { for (var key in valueHash) {
if(modelAttributeMap[key].autoIncrement){ if(modelAttributeMap[key].autoIncrement){
insertKey = true; insertKey = true;
delete valueHash[key];
} }
} }
...@@ -245,11 +239,12 @@ module.exports = { ...@@ -245,11 +239,12 @@ module.exports = {
selFields: fieldsToSql(valueHash, true), selFields: fieldsToSql(valueHash, true),
values: valuesToSql(valueHash, modelAttributeMap) values: valuesToSql(valueHash, modelAttributeMap)
}; };
query = (replacements.attributes.length ? valueQuery : emptyQuery) + ';'; query = (replacements.attributes.length ? valueQuery : emptyQuery) + ';';
if(insertKey){ // if(insertKey){
query = identityInsertOnWrapper(query); // query = identityInsertOnWrapper(query);
} // }
return Utils._.template(query)(replacements); return Utils._.template(query)(replacements);
}, },
...@@ -355,7 +350,7 @@ module.exports = { ...@@ -355,7 +350,7 @@ module.exports = {
var template = []; var template = [];
//special enum query //special enum query
if (attribute.type.toString() === DataTypes.ENUM.toString()) { if (attribute.type.toString() === DataTypes.ENUM.toString()) {
template.push(this.getEnumSql(attribute)); template.push(this.getEnumSql(attribute));
} else { } else {
//the everything else //the everything else
template.push(attribute.type.toString()); template.push(attribute.type.toString());
...@@ -366,7 +361,7 @@ module.exports = { ...@@ -366,7 +361,7 @@ module.exports = {
}else if(attribute.allowNull === false){ }else if(attribute.allowNull === false){
template.push(attributeMap.notNull); template.push(attributeMap.notNull);
//not nullable //not nullable
}else{ }else{
template.push(attributeMap.allowNull); template.push(attributeMap.allowNull);
} }
} }
...@@ -389,7 +384,7 @@ module.exports = { ...@@ -389,7 +384,7 @@ module.exports = {
template.push(attributeMap.unique); template.push(attributeMap.unique);
} }
if (attribute.references) { if (attribute.references) {
template.push(attributeMap.references + quoteIdentifier(attribute.references)); template.push(attributeMap.references + quoteIdentifier(attribute.references));
...@@ -438,7 +433,7 @@ module.exports = { ...@@ -438,7 +433,7 @@ module.exports = {
"FROM", "FROM",
"INFORMATION_SCHEMA.TABLE_CONSTRAINTS C", "INFORMATION_SCHEMA.TABLE_CONSTRAINTS C",
"WHERE C.CONSTRAINT_TYPE != 'PRIMARY KEY'", "WHERE C.CONSTRAINT_TYPE != 'PRIMARY KEY'",
"AND C.TABLE_NAME = ", wrapSingleQuote(tableName) "AND C.TABLE_NAME = ", wrapSingleQuote(tableName)
].join(" "); ].join(" ");
}, },
dropSql: function(val){ dropSql: function(val){
...@@ -465,8 +460,8 @@ module.exports = { ...@@ -465,8 +460,8 @@ module.exports = {
//add join table //add join table
if(options.include){ if(options.include){
query.push(loadFields(model.rawAttributes, model.name)); query.push(loadFieldsWithName(model.rawAttributes, model.name));
for(var i = 0; i < options.include.length; i++){ for(var i = 0; i < options.include.length; i++){
if(options.include[i].as) { if(options.include[i].as) {
query.push(joinFields(options.include[i].model.rawAttributes query.push(joinFields(options.include[i].model.rawAttributes
, options.include[i].as)); , options.include[i].as));
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!