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

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) {
return quoteIdentifier(identifiers);
}
}
function wrapSingleQuote(identifier){
function wrapSingleQuote(identifier){
return Utils.addTicks(identifier, "'");
}
function nameIndexes(indexes, rawTablename) {
......@@ -69,7 +69,7 @@ function fieldsToSql(fields, singleQuote){
if (fields.hasOwnProperty(key)) {
if(singleQuote){
fieldStr.push(wrapSingleQuote(key));
}else{
}else{
fieldStr.push(quoteIdentifier(key));
}
}
......@@ -79,7 +79,7 @@ function fieldsToSql(fields, singleQuote){
return fieldStr.join(',');
}
}
return '';
return '';
}
function valuesToSql(fields, modelAttributeMap){
var values = [];
......@@ -88,7 +88,7 @@ function valuesToSql(fields, modelAttributeMap){
var value = fields[key];
values.push(escape(value, (modelAttributeMap && modelAttributeMap[key]) || undefined));
}
}
}
if(values){
if(values.length > 0){
return values.join(',');
......@@ -123,13 +123,6 @@ function addTableExistsWrapper(query, exists){
"END"
].join(" ");
}
function identityInsertOnWrapper(query){
return [
'SET IDENTITY_INSERT <%= tableName %> ON;',
query,
'SET IDENTITY_INSERT <%= tableName %> OFF;'
].join(' ');
}
//select stuff
function loadFields(attributes){
......@@ -139,11 +132,11 @@ function loadFields(attributes){
}
return attrStr;
}
function loadFields(attributes, tableName){
function loadFieldsWithName(attributes, tableName){
var attrStr = [];
for (var attr in attributes) {
if(tableName){
attrStr.push(quoteIdentifier(tableName) + "." + quoteIdentifier(attr));
attrStr.push(quoteIdentifier(tableName) + "." + quoteIdentifier(attr));
}else{
attrStr.push(quoteIdentifier(attr));
}
......@@ -154,8 +147,8 @@ function joinFields(attributes, tableName){
var attrStr = [];
if(tableName){
for (var attr in attributes) {
attrStr.push(quoteIdentifier(tableName)
+ "."
attrStr.push(quoteIdentifier(tableName)
+ "."
+ quoteIdentifier(attr)
+ " AS " + quoteIdentifier(tableName + "." + attr));
}
......@@ -180,11 +173,11 @@ module.exports = {
get sequelize(){
return _sequelize;
},
set sequelize(seq) {
set sequelize(seq) {
_sequelize = seq;
},
showTableSql: function(){
showTableSql: function(){
return 'SELECT name FROM sys.Tables;';
},
getCreateTableSql: function(tableName, attributes, options) {
......@@ -204,7 +197,7 @@ module.exports = {
};
query = addTableExistsWrapper(query);
return Utils._.template(query)(values).trim() + ";";
},
},
alterTableSql: function(tableName){
var query = 'ALTER TABLE <%= tableName %>';
var value = {
......@@ -218,7 +211,7 @@ module.exports = {
unquotedTable: tableName,
tableName: quoteIdentifier(tableName)
};
query = addTableExistsWrapper(query, true);
query = addTableExistsWrapper(query, true);
return Utils._.template(query)(values).trim() + ";";
},
......@@ -236,6 +229,7 @@ module.exports = {
for (var key in valueHash) {
if(modelAttributeMap[key].autoIncrement){
insertKey = true;
delete valueHash[key];
}
}
......@@ -245,11 +239,12 @@ module.exports = {
selFields: fieldsToSql(valueHash, true),
values: valuesToSql(valueHash, modelAttributeMap)
};
query = (replacements.attributes.length ? valueQuery : emptyQuery) + ';';
if(insertKey){
query = identityInsertOnWrapper(query);
}
// if(insertKey){
// query = identityInsertOnWrapper(query);
// }
return Utils._.template(query)(replacements);
},
......@@ -355,7 +350,7 @@ module.exports = {
var template = [];
//special enum query
if (attribute.type.toString() === DataTypes.ENUM.toString()) {
template.push(this.getEnumSql(attribute));
template.push(this.getEnumSql(attribute));
} else {
//the everything else
template.push(attribute.type.toString());
......@@ -366,7 +361,7 @@ module.exports = {
}else if(attribute.allowNull === false){
template.push(attributeMap.notNull);
//not nullable
}else{
}else{
template.push(attributeMap.allowNull);
}
}
......@@ -389,7 +384,7 @@ module.exports = {
template.push(attributeMap.unique);
}
if (attribute.references) {
template.push(attributeMap.references + quoteIdentifier(attribute.references));
......@@ -438,7 +433,7 @@ module.exports = {
"FROM",
"INFORMATION_SCHEMA.TABLE_CONSTRAINTS C",
"WHERE C.CONSTRAINT_TYPE != 'PRIMARY KEY'",
"AND C.TABLE_NAME = ", wrapSingleQuote(tableName)
"AND C.TABLE_NAME = ", wrapSingleQuote(tableName)
].join(" ");
},
dropSql: function(val){
......@@ -465,8 +460,8 @@ module.exports = {
//add join table
if(options.include){
query.push(loadFields(model.rawAttributes, model.name));
for(var i = 0; i < options.include.length; i++){
query.push(loadFieldsWithName(model.rawAttributes, model.name));
for(var i = 0; i < options.include.length; i++){
if(options.include[i].as) {
query.push(joinFields(options.include[i].model.rawAttributes
, 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!