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

Commit 59605e88 by Sascha Depold

use new private method style

1 parent 77c55541
Showing with 41 additions and 32 deletions
var Utils = require("./utils") var Utils = require("./utils")
var Query = module.exports = function(client, callee, options) {
module.exports = (function() {
var Query = function(client, callee, options) {
var self = this var self = this
this.client = client this.client = client
this.callee = callee this.callee = callee
this.options = options || {} this.options = options || {}
this.bindClientFunction = function(err) { self.onFailure(err) } this.bindClientFunction = function(err) { onFailure.call(self, err) }
} }
Utils.addEventEmitter(Query) Utils.addEventEmitter(Query)
Query.prototype.run = function(query) { Query.prototype.run = function(query) {
var self = this var self = this
this.sql = query this.sql = query
this.bindClient() bindClient.call(this)
if(this.options.logging) if(this.options.logging)
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 ? self.onFailure(err) : self.onSuccess(self.sql, results, fields) err ? onFailure.call(self, err) : onSuccess.call(self, self.sql, results, fields)
}).setMaxListeners(100) }).setMaxListeners(100)
return this return this
} }
Query.prototype.bindClient = function() { var bindClient = function() {
this.client.on('error', this.bindClientFunction) this.client.on('error', this.bindClientFunction)
} }
Query.prototype.unbindClient = function() { var unbindClient = function() {
this.client.removeListener('error', this.bindClientFunction) this.client.removeListener('error', this.bindClientFunction)
} }
Query.prototype.onSuccess = function(query, results, fields) { var onSuccess = function(query, results, fields) {
var result = this.callee var result = this.callee
, self = this , self = this
...@@ -50,11 +52,14 @@ Query.prototype.onSuccess = function(query, results, fields) { ...@@ -50,11 +52,14 @@ Query.prototype.onSuccess = function(query, results, fields) {
result = (result.length == 0) ? null : result[0] result = (result.length == 0) ? null : result[0]
} }
this.unbindClient() unbindClient.call(this)
this.emit('success', result) this.emit('success', result)
} }
Query.prototype.onFailure = function(err) { var onFailure = function(err) {
this.unbindClient() unbindClient.call(this)
this.emit('failure', err, this.callee) this.emit('failure', err, this.callee)
} }
return Query
})()
...@@ -3,7 +3,8 @@ var Utils = require("./utils") ...@@ -3,7 +3,8 @@ var Utils = require("./utils")
, DataTypes = require('./data-types') , DataTypes = require('./data-types')
, ModelManager = require("./model-manager") , ModelManager = require("./model-manager")
var Sequelize = module.exports = function(database, username, password, options) { module.exports = (function() {
var Sequelize = function(database, username, password, options) {
options = options || {} options = options || {}
Utils._.reject(options, function(_, key) { Utils._.reject(options, function(_, key) {
...@@ -24,12 +25,12 @@ var Sequelize = module.exports = function(database, username, password, options) ...@@ -24,12 +25,12 @@ var Sequelize = module.exports = function(database, username, password, options)
this.modelManager = new ModelManager(this) this.modelManager = new ModelManager(this)
this.connectorManager = new ConnectorManager(this, this.config) this.connectorManager = new ConnectorManager(this, this.config)
} }
Sequelize.Utils = Utils Sequelize.Utils = Utils
Sequelize.Utils._.map(DataTypes, function(sql, accessor) { Sequelize[accessor] = sql}) Sequelize.Utils._.map(DataTypes, function(sql, accessor) { Sequelize[accessor] = sql})
Sequelize.prototype.define = function(modelName, attributes, options) { Sequelize.prototype.define = function(modelName, attributes, options) {
options = options || {} options = options || {}
if(this.options.define) if(this.options.define)
...@@ -38,14 +39,14 @@ Sequelize.prototype.define = function(modelName, attributes, options) { ...@@ -38,14 +39,14 @@ Sequelize.prototype.define = function(modelName, attributes, options) {
var model = this.modelManager.addModel(new ModelFactory(modelName, attributes, options)) var model = this.modelManager.addModel(new ModelFactory(modelName, attributes, options))
return model return model
} }
Sequelize.prototype.import = function(path) { Sequelize.prototype.import = function(path) {
var defineCall = require(path) var defineCall = require(path)
return defineCall(this, DataTypes) return defineCall(this, DataTypes)
} }
Sequelize.prototype.query = function(sql, callee, options) { Sequelize.prototype.query = function(sql, callee, options) {
options = options || {} options = options || {}
if(this.options.query) if(this.options.query)
...@@ -54,9 +55,9 @@ Sequelize.prototype.query = function(sql, callee, options) { ...@@ -54,9 +55,9 @@ Sequelize.prototype.query = function(sql, callee, options) {
options.logging = this.options.hasOwnProperty('logging') ? this.options.logging : true options.logging = this.options.hasOwnProperty('logging') ? this.options.logging : true
return this.connectorManager.query(sql, callee, options) return this.connectorManager.query(sql, callee, options)
} }
Sequelize.prototype.sync = function(options) { Sequelize.prototype.sync = function(options) {
options = options || {} options = options || {}
if(this.options.sync) if(this.options.sync)
...@@ -74,9 +75,9 @@ Sequelize.prototype.sync = function(options) { ...@@ -74,9 +75,9 @@ Sequelize.prototype.sync = function(options) {
.on('failure', function(err) { eventEmitter.emit('failure', err) }) .on('failure', function(err) { eventEmitter.emit('failure', err) })
}) })
return eventEmitter.run() return eventEmitter.run()
} }
Sequelize.prototype.drop = function() { Sequelize.prototype.drop = function() {
var self = this var self = this
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
...@@ -89,4 +90,7 @@ Sequelize.prototype.drop = function() { ...@@ -89,4 +90,7 @@ Sequelize.prototype.drop = function() {
.on('success', function() { emitter.emit('success', null) }) .on('success', function() { emitter.emit('success', null) })
.on('failure', function(err) { emitter.emit('failure', err) }) .on('failure', function(err) { emitter.emit('failure', err) })
}).run() }).run()
} }
return Sequelize
})()
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!