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

Commit 59605e88 by Sascha Depold

use new private method style

1 parent 77c55541
Showing with 119 additions and 110 deletions
var Utils = require("./utils")
var Query = module.exports = function(client, callee, options) {
var self = this
this.client = client
this.callee = callee
this.options = options || {}
this.bindClientFunction = function(err) { self.onFailure(err) }
}
Utils.addEventEmitter(Query)
module.exports = (function() {
var Query = function(client, callee, options) {
var self = this
Query.prototype.run = function(query) {
var self = this
this.sql = query
this.client = client
this.callee = callee
this.options = options || {}
this.bindClientFunction = function(err) { onFailure.call(self, err) }
}
Utils.addEventEmitter(Query)
this.bindClient()
Query.prototype.run = function(query) {
var self = this
this.sql = query
if(this.options.logging)
console.log('Executing: ' + this.sql)
bindClient.call(this)
this.client.query(this.sql, function(err, results, fields) {
err ? self.onFailure(err) : self.onSuccess(self.sql, results, fields)
}).setMaxListeners(100)
if(this.options.logging)
console.log('Executing: ' + this.sql)
return this
}
this.client.query(this.sql, function(err, results, fields) {
err ? onFailure.call(self, err) : onSuccess.call(self, self.sql, results, fields)
}).setMaxListeners(100)
Query.prototype.bindClient = function() {
this.client.on('error', this.bindClientFunction)
}
return this
}
Query.prototype.unbindClient = function() {
this.client.removeListener('error', this.bindClientFunction)
}
var bindClient = function() {
this.client.on('error', this.bindClientFunction)
}
Query.prototype.onSuccess = function(query, results, fields) {
var result = this.callee
, self = this
var unbindClient = function() {
this.client.removeListener('error', this.bindClientFunction)
}
// add the inserted row id to the instance
if (this.callee && (query.indexOf('INSERT INTO') == 0) && (results.hasOwnProperty('insertId')))
this.callee[this.callee.__definition.autoIncrementField] = results.insertId
var onSuccess = function(query, results, fields) {
var result = this.callee
, self = this
// transform results into real model instances
// return the first real model instance if options.plain is set (e.g. Model.find)
if (query.indexOf('SELECT') == 0) {
result = results.map(function(result) { return self.callee.build(result, {isNewRecord: false}) })
// add the inserted row id to the instance
if (this.callee && (query.indexOf('INSERT INTO') == 0) && (results.hasOwnProperty('insertId')))
this.callee[this.callee.__definition.autoIncrementField] = results.insertId
if(this.options.plain)
result = (result.length == 0) ? null : result[0]
// transform results into real model instances
// return the first real model instance if options.plain is set (e.g. Model.find)
if (query.indexOf('SELECT') == 0) {
result = results.map(function(result) { return self.callee.build(result, {isNewRecord: false}) })
if(this.options.plain)
result = (result.length == 0) ? null : result[0]
}
unbindClient.call(this)
this.emit('success', result)
}
this.unbindClient()
this.emit('success', result)
}
var onFailure = function(err) {
unbindClient.call(this)
this.emit('failure', err, this.callee)
}
Query.prototype.onFailure = function(err) {
this.unbindClient()
this.emit('failure', err, this.callee)
}
return Query
})()
......@@ -3,90 +3,94 @@ var Utils = require("./utils")
, DataTypes = require('./data-types')
, ModelManager = require("./model-manager")
var Sequelize = module.exports = function(database, username, password, options) {
options = options || {}
Utils._.reject(options, function(_, key) {
return ["host", "port", "disableTableNameModification"].indexOf(key) > -1
})
this.options = options
this.options.connector = this.options.connector || 'mysql'
this.config = {
database: database,
username: username,
password: (( (["", null, false].indexOf(password) > -1) || (typeof password == 'undefined')) ? null : password),
host : options.host || 'localhost',
port : options.port || 3306
module.exports = (function() {
var Sequelize = function(database, username, password, options) {
options = options || {}
Utils._.reject(options, function(_, key) {
return ["host", "port", "disableTableNameModification"].indexOf(key) > -1
})
this.options = options
this.options.connector = this.options.connector || 'mysql'
this.config = {
database: database,
username: username,
password: (( (["", null, false].indexOf(password) > -1) || (typeof password == 'undefined')) ? null : password),
host : options.host || 'localhost',
port : options.port || 3306
}
var ConnectorManager = require("./connectors/" + this.options.connector + "/connector-manager")
this.modelManager = new ModelManager(this)
this.connectorManager = new ConnectorManager(this, this.config)
}
var ConnectorManager = require("./connectors/" + this.options.connector + "/connector-manager")
Sequelize.Utils = Utils
Sequelize.Utils._.map(DataTypes, function(sql, accessor) { Sequelize[accessor] = sql})
this.modelManager = new ModelManager(this)
this.connectorManager = new ConnectorManager(this, this.config)
}
Sequelize.prototype.define = function(modelName, attributes, options) {
options = options || {}
Sequelize.Utils = Utils
Sequelize.Utils._.map(DataTypes, function(sql, accessor) { Sequelize[accessor] = sql})
if(this.options.define)
options = Sequelize.Utils.merge(options, this.options.define)
Sequelize.prototype.define = function(modelName, attributes, options) {
options = options || {}
var model = this.modelManager.addModel(new ModelFactory(modelName, attributes, options))
if(this.options.define)
options = Sequelize.Utils.merge(options, this.options.define)
var model = this.modelManager.addModel(new ModelFactory(modelName, attributes, options))
return model
}
return model
}
Sequelize.prototype.import = function(path) {
var defineCall = require(path)
return defineCall(this, DataTypes)
}
Sequelize.prototype.import = function(path) {
var defineCall = require(path)
return defineCall(this, DataTypes)
}
Sequelize.prototype.query = function(sql, callee, options) {
options = options || {}
Sequelize.prototype.query = function(sql, callee, options) {
options = options || {}
if(this.options.query)
options = Sequelize.Utils.merge(options, this.options.query)
if(this.options.query)
options = Sequelize.Utils.merge(options, this.options.query)
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) {
options = options || {}
Sequelize.prototype.sync = function(options) {
options = options || {}
if(this.options.sync)
options = Sequelize.Utils.merge(options, this.options.sync)
if(this.options.sync)
options = Sequelize.Utils.merge(options, this.options.sync)
var self = this
var eventEmitter = new Utils.CustomEventEmitter(function() {
var chainer = new Utils.QueryChainer
var self = this
var eventEmitter = new Utils.CustomEventEmitter(function() {
var chainer = new Utils.QueryChainer
self.modelManager.models.forEach(function(model) { chainer.add(model.sync(options)) })
self.modelManager.models.forEach(function(model) { chainer.add(model.sync(options)) })
chainer
.run()
.on('success', function() { eventEmitter.emit('success', null) })
.on('failure', function(err) { eventEmitter.emit('failure', err) })
})
return eventEmitter.run()
}
chainer
.run()
.on('success', function() { eventEmitter.emit('success', null) })
.on('failure', function(err) { eventEmitter.emit('failure', err) })
})
return eventEmitter.run()
}
Sequelize.prototype.drop = function() {
var self = this
Sequelize.prototype.drop = function() {
var self = this
return new Utils.CustomEventEmitter(function(emitter) {
var chainer = new Utils.QueryChainer
return new Utils.CustomEventEmitter(function(emitter) {
var chainer = new Utils.QueryChainer
self.modelManager.models.forEach(function(model) { chainer.add(model.drop()) })
self.modelManager.models.forEach(function(model) { chainer.add(model.drop()) })
chainer
.run()
.on('success', function() { emitter.emit('success', null) })
.on('failure', function(err) { emitter.emit('failure', err) })
}).run()
}
chainer
.run()
.on('success', function() { emitter.emit('success', null) })
.on('failure', function(err) { emitter.emit('failure', err) })
}).run()
}
return Sequelize
})()
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!