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

Commit 089cedc8 by Joel Trost Committed by Matt Broadstone

Fixes for lib/instance passing where clause

1 parent 198f3501
......@@ -160,7 +160,10 @@ module.exports = (function() {
If you use a string, you have to escape it on your own.
*/
updateQuery: function(tableName, attrValueHash, where, options, attributes) {
console.log(where);
// if(where){
// throw new Error();
// }
console.log('here', where);
var query = [
SqlGenerator.updateSql(tableName, attrValueHash, attributes),
this.getWhereConditions(where)
......
......@@ -22,6 +22,11 @@ module.exports = (function() {
};
Utils.inherit(Query, AbstractQuery);
Query.prototype.getInsertIdField = function() {
return 'id';
};
Query.prototype.run = function(sql) {
console.log(sql);
var self = this;
......@@ -65,7 +70,7 @@ module.exports = (function() {
*/
Query.prototype.formatResults = function(data) {
var result = this.callee;
//console.log(data);
if (this.isInsertQuery(data)) {
this.handleInsertQuery(data);
} else if (this.isShowTableQuery()) {
......@@ -176,5 +181,16 @@ module.exports = (function() {
});
};
AbstractQuery.prototype.handleInsertQuery = function(results, metaData) {
if (this.callee) {
// add the inserted row id to the instance
var autoIncrementField = this.callee.Model.autoIncrementField
, id = null;
id = id || (results && results[0][this.getInsertIdField()]);
id = id || (metaData && metaData[this.getInsertIdField()]);
this.callee[autoIncrementField] = id;
}
};
return Query;
})();
......@@ -225,8 +225,10 @@ module.exports = {
valueHash = Utils.removeNullValuesFromHash(valueHash, _options.omitNull);
var selFields = [];
var insertKey = false;
for (var key in valueHash) {
selFields.push('INSERTED.' + quoteIdentifier(key));
if(modelAttributeMap[key].autoIncrement){
insertKey = true;
delete valueHash[key];
......@@ -236,7 +238,7 @@ module.exports = {
var replacements = {
tableName: quoteIdentifier(tableName),
attributes: fieldsToSql(valueHash, false),
selFields: fieldsToSql(valueHash, true),
selFields: selFields.join(','),
values: valuesToSql(valueHash, modelAttributeMap)
};
......@@ -247,7 +249,32 @@ module.exports = {
// }
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 %>';
for (var key in valueHash) {
var value = valueHash[key];
selFields.push('INSERTED.' + quoteIdentifier(key));
values.push(quoteIdentifier(key) + '=' + escape(value, (!!attributes && !!attributes[key] ? attributes[key] : undefined)));
}
var replacements = {
tableName: quoteIdentifier(tableName),
attributes: fieldsToSql(valueHash, false),
selFields: selFields.join(','),
values: values.join(','),
};
return Utils._.template(query)(replacements);
},
addColumnSql: function(key, dataType){
var attribute = Utils._.template('<%= key %> <%= definition %>')({
key: quoteIdentifier(key),
......@@ -515,7 +542,11 @@ module.exports = {
getWhereClause: function(where, tableName){
var query = ['WHERE'];
for(var key in where){
if(tableName){
query.push(quoteIdentifier(tableName) + '.' + quoteIdentifier(key));
}else{
query.push(quoteIdentifier(key));
}
query.push('=');
query.push(where[key]);
}
......
......@@ -562,7 +562,6 @@ module.exports = (function() {
}
}
}
if (identifier === null && self.__options.whereCollection !== null) {
identifier = self.__options.whereCollection;
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!