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

Commit 2a2cd48a by Matt Broadstone

remove mssql specific insert and update queries

Code was merged into master to allow modification of the abstract
dialect to support mssql specific cases. This removes the custom
implementations
1 parent af294fb3
...@@ -12,9 +12,16 @@ var MssqlDialect = function(sequelize) { ...@@ -12,9 +12,16 @@ var MssqlDialect = function(sequelize) {
}; };
MssqlDialect.prototype.supports = _.merge(_.cloneDeep(Abstract.prototype.supports), { MssqlDialect.prototype.supports = _.merge(_.cloneDeep(Abstract.prototype.supports), {
'RETURNING': true, 'RETURNING': false,
'OUTPUT': true,
'DEFAULT VALUES': true,
'LIMIT ON UPDATE': true, 'LIMIT ON UPDATE': true,
lock: false, lock: false,
autoIncrement: {
identityInsert: true,
defaultValue: false,
update: false
},
constraints: { constraints: {
restrict: false restrict: false
}, },
......
...@@ -135,21 +135,6 @@ module.exports = (function() { ...@@ -135,21 +135,6 @@ module.exports = (function() {
return query; return query;
}, },
insertQuery: function(table, valueHash, modelAttributes, options) {
var modelAttributeMap = {};
if (modelAttributes) {
Utils._.each(modelAttributes, function(attribute, key) {
if (attribute.field) {
modelAttributeMap[attribute.field] = attribute;
} else {
modelAttributeMap[key] = attribute;
}
});
}
return SqlGenerator.insertSql(table, valueHash, modelAttributeMap, options);
},
bulkInsertQuery: function(tableName, attrValueHashes, options, attributes) { bulkInsertQuery: function(tableName, attrValueHashes, options, attributes) {
var query = 'INSERT<%= ignoreDuplicates %> INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;' var query = 'INSERT<%= ignoreDuplicates %> INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;'
, emptyQuery = 'INSERT INTO <%= table %> DEFAULT VALUES' , emptyQuery = 'INSERT INTO <%= table %> DEFAULT VALUES'
...@@ -208,35 +193,6 @@ module.exports = (function() { ...@@ -208,35 +193,6 @@ module.exports = (function() {
return generatedQuery; return generatedQuery;
}, },
updateQuery: function(tableName, attrValueHash, where, options, attributes) {
var query;
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, false, options);
for (var key in attributes) {
var aliasKey = attributes[key].field || key;
if (attributes[key].primaryKey && attrValueHash[aliasKey]) {
delete attrValueHash[aliasKey];
}
if (attrValueHash[aliasKey] && attrValueHash[aliasKey].fn) {
}
}
if (!Object.keys(attrValueHash).length) {
return '';
//return ['SELECT * FROM ', tableName, 'WHERE', this.getWhereConditions(where) + ';'].join(' ');
}
query = [
SqlGenerator.updateSql(tableName, attrValueHash, attributes),
'WHERE',
this.getWhereConditions(where)
].join(' ') + ';';
return query;
},
deleteQuery: function(tableName, where, options) { deleteQuery: function(tableName, where, options) {
var query = [ var query = [
SqlGenerator.deleteSql(tableName), SqlGenerator.deleteSql(tableName),
......
...@@ -57,156 +57,6 @@ function wrapSingleQuote(identifier){ ...@@ -57,156 +57,6 @@ function wrapSingleQuote(identifier){
return Utils.addTicks(identifier, "'"); return Utils.addTicks(identifier, "'");
} }
function nameIndexes(indexes, rawTablename) {
return Utils._.map(indexes, function (index) {
if (!index.hasOwnProperty('name')) {
var onlyAttributeNames = index.fields.map(function(attribute) {
return (typeof attribute === 'string') ? attribute : attribute.attribute;
}.bind(this));
index.name = Utils.inflection.underscore(rawTablename + '_' + onlyAttributeNames.join('_'));
}
return index;
});
}
function fieldsToSql(fields, singleQuote){
var fieldStr = [];
for (var key in fields) {
if (fields.hasOwnProperty(key)) {
if(singleQuote){
fieldStr.push(wrapSingleQuote(key));
}else{
fieldStr.push(quoteIdentifier(key));
}
}
}
if(fieldStr){
if(fieldStr.length > 0){
return fieldStr.join(',');
}
}
return '';
}
function processValue(val, modelAttribute){
var sql;
if(val instanceof Utils.cast){
sql = 'CAST(';
if(val.val._isSequelizeMethod){
sql += processValue(val.val, modelAttribute) + ' AS ' + val.type.toUpperCase();
}else{
sql += escape(val.val) + ' AS ' + val.type.toUpperCase();
}
return sql + ')';
} else if(val instanceof Utils.literal){
return val.val;
} else if (val instanceof Utils.fn) {
sql = val.fn + '(' + val.args.map(function(arg) {
if (arg._isSequelizeMethod) {
return processValue(arg, modelAttribute);
} else{
return escape(arg);
}
}).join(', ') + ')';
return sql;
} else if (val === true || val === false){
return wrapSingleQuote(val.toString());
} else if (val instanceof Utils.col) {
if (Array.isArray(val.col)) {
if (!modelAttribute) {
throw new Error('Cannot call Sequelize.col() with array outside of order / group clause');
}
} else if (val.col.indexOf('*') === 0) {
return '*';
}
return quoteIdentifier(val.col);
} else{
return escape(val, modelAttribute);
}
}
function valuesToSql(fields, modelAttributeMap, isUpdate){
var values = [];
for (var key in fields) {
if (fields.hasOwnProperty(key)) {
if(isUpdate){
values.push(quoteIdentifier(key) + '=' + processValue(fields[key], (modelAttributeMap && modelAttributeMap[key]) || undefined));
} else {
values.push(processValue(fields[key], (modelAttributeMap && modelAttributeMap[key]) || undefined));
}
}
}
if(values){
if(values.length > 0){
return values.join(',');
}
}
return '';
}
function loadColumn(attributes){
var attrStr = [];
for (var attr in attributes) {
var dataType = attributes[attr];
attrStr.push(quoteIdentifier(dataType));
}
return attrStr;
}
//select stuff
function loadFields(attributes){
var attrStr = [];
for (var attr in attributes) {
attrStr.push(quoteIdentifier(attr));
}
return attrStr;
}
function processField(attribute, tableName){
var sql = '';
if(tableName){
sql += quoteIdentifier(tableName) + '.';
}
if(attribute[0] !== attribute[1]){
return sql + quoteIdentifier(attribute[0]) + ' AS ' + quoteIdentifier(attribute[1]);
}else{
return sql + quoteIdentifier(attribute[0]);
}
}
function loadFieldsWithName(attributes, tableName){
var attrStr = [];
if(Array.isArray(attributes[0])){
for (var i = 0; i < attributes.length; i++) {
attrStr.push(processField(attributes[i], tableName));
}
}else{
for (var attr in attributes) {
if(tableName){
attrStr.push(quoteIdentifier(tableName) + "." + quoteIdentifier(attr));
}else{
attrStr.push(quoteIdentifier(attr));
}
}
}
return attrStr.join(',');
}
function joinFields(attributes, tableName){
var attrStr = [];
if(tableName){
for (var attr in attributes) {
attrStr.push(quoteIdentifier(tableName)
+ "."
+ quoteIdentifier(attr)
+ " AS " + quoteIdentifier(tableName + "." + attr));
}
}
return attrStr.join(',');
}
module.exports = { module.exports = {
get options(){ get options(){
return _options; return _options;
...@@ -244,10 +94,6 @@ module.exports = { ...@@ -244,10 +94,6 @@ module.exports = {
return escape(value,field); return escape(value,field);
}, },
processValue: function(val, modelAttribute){
return processValue(val, modelAttribute);
},
quoteTable: function(param, as) { quoteTable: function(param, as) {
var table = ''; var table = '';
if (as === true) { if (as === true) {
...@@ -286,76 +132,6 @@ module.exports = { ...@@ -286,76 +132,6 @@ module.exports = {
return Utils._.template(query)(value); return Utils._.template(query)(value);
}, },
insertSql: function(tableName, valueHash, modelAttributeMap, options) {
var query
, valueQuery = 'INSERT INTO <%= tableName %> (<%= attributes %>)'
, emptyQuery = 'INSERT INTO <%= tableName %>';
valueQuery += ' OUTPUT <%= selFields %> VALUES (<%= values %>)';
if(modelAttributeMap){
emptyQuery += ' OUTPUT <%= selFields %>';
}
emptyQuery += ' DEFAULT VALUES';
valueHash = Utils.removeNullValuesFromHash(valueHash, this.options.omitNull);
var selFields = [];
var insertKey = false;
for (var key in modelAttributeMap) {
if (Utils._.isFunction(modelAttributeMap[key].type) &&
modelAttributeMap[key].type() === undefined) // VIRTUAL field
continue;
selFields.push('INSERTED.' + quoteIdentifier((modelAttributeMap[key].field || key)));
if (modelAttributeMap[key].autoIncrement &&
(valueHash[key] === 'DEFAULT' || valueHash[key] === null)) {
delete valueHash[key];
} else if(valueHash[key] && modelAttributeMap[key].autoIncrement) {
insertKey = true;
}
}
var replacements = {
tableName: quoteIdentifier(tableName.toString()),
attributes: fieldsToSql(valueHash, false),
selFields: selFields.join(','),
values: valuesToSql(valueHash, modelAttributeMap)
};
query = (replacements.attributes.length ? valueQuery : emptyQuery) + ';';
if(insertKey){
query = this.identityInsertWrapper(query, tableName);
}
return Utils._.template(query)(replacements);
},
updateSql: function(tableName, valueHash, where, options, attributes){
options = options || {};
valueHash = Utils.removeNullValuesFromHash(valueHash, this.options.omitNull, options);
var query
, selFields = []
, values = [];
query = 'UPDATE <%= tableName %> SET <%= values %> OUTPUT <%= selFields %>';
var parsedValues = valuesToSql(valueHash, attributes, true);
for (var key in valueHash) {
var value = valueHash[key];
selFields.push('INSERTED.' + quoteIdentifier(key));
}
var replacements = {
tableName: quoteIdentifier(tableName.toString()),
attributes: fieldsToSql(valueHash, false),
selFields: selFields.join(','),
values: parsedValues,
};
return Utils._.template(query)(replacements);
},
incrementSql: function(tableName, attrValueHash, options) { incrementSql: function(tableName, attrValueHash, options) {
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull); attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!