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

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