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

Commit cbe4ae7d by Sascha Depold

private method scoping

1 parent 7af32e54
var ConnectorManager = module.exports = function(sequelize, config) { module.exports = (function(){
var ConnectorManager = function(sequelize, config) {
throw new Error('Define the constructor!') throw new Error('Define the constructor!')
} }
ConnectorManager.prototype.getQueryGenerator = function() { ConnectorManager.prototype.getQueryGenerator = function() {
this.__QueryGenerator = this.__QueryGenerator || require(__dirname + '/' + this.sequelize.options.connector + '/query-generator') this.__QueryGenerator = this.__QueryGenerator || require(__dirname + '/' + this.sequelize.options.connector + '/query-generator')
return this.__QueryGenerator return this.__QueryGenerator
} }
ConnectorManager.prototype.query = function(sql, callee, options) { ConnectorManager.prototype.query = function(sql, callee, options) {
throw new Error('Define the query method!') throw new Error('Define the query method!')
} }
ConnectorManager.prototype.connect = function() { ConnectorManager.prototype.connect = function() {
throw new Error('Define the connect method!') throw new Error('Define the connect method!')
} }
ConnectorManager.prototype.disconnect = function() { ConnectorManager.prototype.disconnect = function() {
throw new Error('Define the disconnect method!') throw new Error('Define the disconnect method!')
} }
ConnectorManager.prototype.reconnect = function() { ConnectorManager.prototype.reconnect = function() {
this.disconnect() this.disconnect()
this.connect() this.connect()
} }
return ConnectorManager
})()
...@@ -2,7 +2,8 @@ var Query = require("../../query") ...@@ -2,7 +2,8 @@ var Query = require("../../query")
, Utils = require("../../utils") , Utils = require("../../utils")
, without = function(arr, elem) { return arr.filter(function(e) { return e != elem }) } , without = function(arr, elem) { return arr.filter(function(e) { return e != elem }) }
var ConnectorManager = module.exports = function(sequelize, config) { module.exports = (function() {
var ConnectorManager = function(sequelize, config) {
this.sequelize = sequelize this.sequelize = sequelize
this.client = null this.client = null
this.config = config || {} this.config = config || {}
...@@ -10,10 +11,10 @@ var ConnectorManager = module.exports = function(sequelize, config) { ...@@ -10,10 +11,10 @@ var ConnectorManager = module.exports = function(sequelize, config) {
this.queue = [] this.queue = []
this.activeQueue = [] this.activeQueue = []
this.maxConcurrentQueries = (this.config.maxConcurrentQueries || 50) this.maxConcurrentQueries = (this.config.maxConcurrentQueries || 50)
} }
Utils._.extend(ConnectorManager.prototype, require("../connector-manager").prototype) Utils._.extend(ConnectorManager.prototype, require("../connector-manager").prototype)
ConnectorManager.prototype.query = function(sql, callee, options) { ConnectorManager.prototype.query = function(sql, callee, options) {
if(!this.isConnected) this.connect() if(!this.isConnected) this.connect()
var queueItem = { var queueItem = {
...@@ -21,12 +22,12 @@ ConnectorManager.prototype.query = function(sql, callee, options) { ...@@ -21,12 +22,12 @@ ConnectorManager.prototype.query = function(sql, callee, options) {
sql: sql sql: sql
} }
this._enqueue(queueItem) enqueue.call(this, queueItem)
return queueItem.query return queueItem.query
} }
ConnectorManager.prototype.connect = function() { ConnectorManager.prototype.connect = function() {
var self = this var self = this
this.client = require("mysql").createClient({ this.client = require("mysql").createClient({
...@@ -38,70 +39,73 @@ ConnectorManager.prototype.connect = function() { ...@@ -38,70 +39,73 @@ ConnectorManager.prototype.connect = function() {
}) })
this.client.setMaxListeners(this.maxConcurrentQueries) this.client.setMaxListeners(this.maxConcurrentQueries)
} }
ConnectorManager.prototype.disconnect = function() { ConnectorManager.prototype.disconnect = function() {
var self = this var self = this
this.client.end(function() { self.client = null }) this.client.end(function() { self.client = null })
} }
// private // private
ConnectorManager.prototype._enqueue = function(queueItem) { var enqueue = function(queueItem) {
if(this.activeQueue.length < this.maxConcurrentQueries) { if(this.activeQueue.length < this.maxConcurrentQueries) {
this.activeQueue.push(queueItem) this.activeQueue.push(queueItem)
this._execQueueItem(queueItem) execQueueItem.call(this, queueItem)
} else { } else {
this.queue.push(queueItem) this.queue.push(queueItem)
} }
} }
ConnectorManager.prototype._dequeue = function(queueItem) { var dequeue = function(queueItem) {
this.activeQueue = without(this.activeQueue, queueItem) this.activeQueue = without(this.activeQueue, queueItem)
} }
ConnectorManager.prototype._transferQueuedItems = function(count) { var transferQueuedItems = function(count) {
for(var i = 0; i < count; i++) { for(var i = 0; i < count; i++) {
var queueItem = this.queue[0] var queueItem = this.queue[0]
if(queueItem) { if(queueItem) {
this._enqueue(queueItem) enqueue.call(this, queueItem)
this.queue = without(this.queue, queueItem) this.queue = without(this.queue, queueItem)
} }
} }
} }
ConnectorManager.prototype._afterQuery = function(queueItem) { var afterQuery = function(queueItem) {
var self = this var self = this
this._dequeue(queueItem) dequeue.call(this, queueItem)
this._transferQueuedItems(this.maxConcurrentQueries - this.activeQueue.length) transferQueuedItems.call(this, this.maxConcurrentQueries - this.activeQueue.length)
this._disconnectIfNoConnections() disconnectIfNoConnections.call(this)
} }
ConnectorManager.prototype._execQueueItem = function(queueItem) { var execQueueItem = function(queueItem) {
var self = this var self = this
queueItem.query queueItem.query
.on('success', function(){ self._afterQuery(queueItem) }) .on('success', function(){ afterQuery.call(self, queueItem) })
.on('failure', function(){ self._afterQuery(queueItem) }) .on('failure', function(){ afterQuery.call(self, queueItem) })
queueItem.query.run(queueItem.sql) queueItem.query.run(queueItem.sql)
} }
ConnectorManager.prototype.__defineGetter__('hasNoConnections', function() { ConnectorManager.prototype.__defineGetter__('hasNoConnections', function() {
return (this.queue.length == 0) && (this.activeQueue.length == 0) && this.client._queue && (this.client._queue.length == 0) return (this.queue.length == 0) && (this.activeQueue.length == 0) && this.client._queue && (this.client._queue.length == 0)
}) })
ConnectorManager.prototype.__defineGetter__('isConnected', function() { ConnectorManager.prototype.__defineGetter__('isConnected', function() {
return this.client != null return this.client != null
}) })
ConnectorManager.prototype._disconnectIfNoConnections = function() { var disconnectIfNoConnections = function() {
var self = this var self = this
this.disconnectTimeoutId && clearTimeout(this.disconnectTimeoutId) this.disconnectTimeoutId && clearTimeout(this.disconnectTimeoutId)
this.disconnectTimeoutId = setTimeout(function() { this.disconnectTimeoutId = setTimeout(function() {
self.isConnected && self.hasNoConnections && self.disconnect() self.isConnected && self.hasNoConnections && self.disconnect()
}, 100) }, 100)
} }
return ConnectorManager
})()
var QueryGenerator = module.exports = { module.exports = (function() {
var QueryGenerator = {
/* /*
Returns a query for creating a table. Returns a query for creating a table.
Attributes should have the format: {attributeName: type, attr2: type2} --> {title: 'VARCHAR(255)'} Attributes should have the format: {attributeName: type, attr2: type2} --> {title: 'VARCHAR(255)'}
...@@ -105,4 +106,8 @@ var QueryGenerator = module.exports = { ...@@ -105,4 +106,8 @@ var QueryGenerator = module.exports = {
hashToWhereConditions: function(hash) { hashToWhereConditions: function(hash) {
throw new Error('Define the method hashToWhereConditions!') throw new Error('Define the method hashToWhereConditions!')
} }
} }
return QueryGenerator
})()
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!