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

Commit d590fe20 by Sascha Depold

use the stored sql instead of passing it to the function

1 parent b9820b7d
Showing with 7 additions and 6 deletions
...@@ -14,9 +14,10 @@ module.exports = (function() { ...@@ -14,9 +14,10 @@ module.exports = (function() {
} }
Utils.addEventEmitter(Query) Utils.addEventEmitter(Query)
Query.prototype.run = function(query) { Query.prototype.run = function(sql) {
var self = this var self = this
this.sql = query
this.sql = sql
bindClient.call(this) bindClient.call(this)
...@@ -24,7 +25,7 @@ module.exports = (function() { ...@@ -24,7 +25,7 @@ module.exports = (function() {
console.log('Executing: ' + this.sql) console.log('Executing: ' + this.sql)
this.client.query(this.sql, function(err, results, fields) { this.client.query(this.sql, function(err, results, fields) {
err ? onFailure.call(self, err) : onSuccess.call(self, self.sql, results, fields) err ? onFailure.call(self, err) : onSuccess.call(self, results, fields)
}).setMaxListeners(100) }).setMaxListeners(100)
return this return this
...@@ -50,17 +51,17 @@ module.exports = (function() { ...@@ -50,17 +51,17 @@ module.exports = (function() {
this.client.removeListener('error', this.bindClientFunction) this.client.removeListener('error', this.bindClientFunction)
} }
var onSuccess = function(query, results, fields) { var onSuccess = function(results, fields) {
var result = this.callee var result = this.callee
, self = this , self = this
// add the inserted row id to the instance // add the inserted row id to the instance
if (this.callee && (query.indexOf('INSERT INTO') == 0) && (results.hasOwnProperty('insertId'))) if (this.callee && (this.sql.indexOf('INSERT INTO') == 0) && (results.hasOwnProperty('insertId')))
this.callee[this.callee.__definition.autoIncrementField] = results.insertId this.callee[this.callee.__definition.autoIncrementField] = results.insertId
// transform results into real model instances // transform results into real model instances
// return the first real model instance if options.plain is set (e.g. Model.find) // return the first real model instance if options.plain is set (e.g. Model.find)
if (query.indexOf('SELECT') == 0) { if (this.sql.indexOf('SELECT') == 0) {
result = results.map(function(result) { return self.callee.build(result, {isNewRecord: false}) }) result = results.map(function(result) { return self.callee.build(result, {isNewRecord: false}) })
if(this.options.plain) if(this.options.plain)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!