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

Commit 68d5e45f by Sascha Depold

minor cleanup

1 parent 9419770e
Showing with 11 additions and 15 deletions
...@@ -13,7 +13,6 @@ module.exports = (function() { ...@@ -13,7 +13,6 @@ module.exports = (function() {
}, options || {}) }, options || {})
this.checkLoggingOption() this.checkLoggingOption()
this.bindClientFunction = function(err) { onFailure.call(this, err) }.bind(this)
} }
Utils.inherit(Query, AbstractQuery) Utils.inherit(Query, AbstractQuery)
...@@ -28,16 +27,18 @@ module.exports = (function() { ...@@ -28,16 +27,18 @@ module.exports = (function() {
} }
this.client.query(this.sql, function(err, results, fields) { this.client.query(this.sql, function(err, results, fields) {
unbindClient.call(this)
this.emit('sql', this.sql) this.emit('sql', this.sql)
if (err) { if (err) {
onFailure.call(this, err) this.emit('error', err, this.callee)
} else { } else {
if (Array.isArray(results) && !!results[0] && Utils._.keys(results[0]).length === 1) { if (Array.isArray(results) && !!results[0] && Utils._.keys(results[0]).length === 1) {
results = results.map(function(result){ return Utils._.values(result)[0] }) results = results.map(function(result){ return Utils._.values(result)[0] })
} }
onSuccess.call(this, results, fields) this.emit('success', this.formatResults(results))
} }
}.bind(this)).setMaxListeners(100) }.bind(this)).setMaxListeners(100)
...@@ -46,22 +47,17 @@ module.exports = (function() { ...@@ -46,22 +47,17 @@ module.exports = (function() {
//private //private
var bindClient = function() { var onClientError = function(err) {
this.client.on('error', this.bindClientFunction) unbindClient.call(this)
} this.emit('error', err, this.callee)
var unbindClient = function() {
this.client.removeListener('error', this.bindClientFunction)
} }
var onSuccess = function(results, fields) { var bindClient = function() {
unbindClient.call(this) this.client.on('error', onClientError.bind(this))
this.emit('success', this.formatResults(results))
} }
var onFailure = function(err) { var unbindClient = function() {
unbindClient.call(this) this.client.removeListener('error', onClientError.bind(this))
this.emit('error', err, this.callee)
} }
return Query return Query
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!