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

Commit cbe4ae7d by Sascha Depold

private method scoping

1 parent 7af32e54
var ConnectorManager = module.exports = function(sequelize, config) { module.exports = (function(){
throw new Error('Define the constructor!') var ConnectorManager = function(sequelize, config) {
} 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,106 +2,110 @@ var Query = require("../../query") ...@@ -2,106 +2,110 @@ 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() {
this.sequelize = sequelize var ConnectorManager = function(sequelize, config) {
this.client = null this.sequelize = sequelize
this.config = config || {} this.client = null
this.disconnectTimeoutId = null this.config = config || {}
this.queue = [] this.disconnectTimeoutId = null
this.activeQueue = [] this.queue = []
this.maxConcurrentQueries = (this.config.maxConcurrentQueries || 50) this.activeQueue = []
} this.maxConcurrentQueries = (this.config.maxConcurrentQueries || 50)
Utils._.extend(ConnectorManager.prototype, require("../connector-manager").prototype)
ConnectorManager.prototype.query = function(sql, callee, options) {
if(!this.isConnected) this.connect()
var queueItem = {
query: new Query(this.client, callee, options || {}),
sql: sql
} }
Utils._.extend(ConnectorManager.prototype, require("../connector-manager").prototype)
this._enqueue(queueItem) ConnectorManager.prototype.query = function(sql, callee, options) {
if(!this.isConnected) this.connect()
return queueItem.query var queueItem = {
} query: new Query(this.client, callee, options || {}),
sql: sql
}
ConnectorManager.prototype.connect = function() { enqueue.call(this, queueItem)
var self = this
this.client = require("mysql").createClient({ return queueItem.query
user: this.config.username, }
password: this.config.password,
host: this.config.host,
port: this.config.port,
database: this.config.database
})
this.client.setMaxListeners(this.maxConcurrentQueries) ConnectorManager.prototype.connect = function() {
} var self = this
ConnectorManager.prototype.disconnect = function() { this.client = require("mysql").createClient({
var self = this user: this.config.username,
this.client.end(function() { self.client = null }) password: this.config.password,
} host: this.config.host,
port: this.config.port,
database: this.config.database
})
// private this.client.setMaxListeners(this.maxConcurrentQueries)
}
ConnectorManager.prototype._enqueue = function(queueItem) { ConnectorManager.prototype.disconnect = function() {
if(this.activeQueue.length < this.maxConcurrentQueries) { var self = this
this.activeQueue.push(queueItem) this.client.end(function() { self.client = null })
this._execQueueItem(queueItem)
} else {
this.queue.push(queueItem)
} }
}
// private
ConnectorManager.prototype._dequeue = function(queueItem) {
this.activeQueue = without(this.activeQueue, queueItem) var enqueue = function(queueItem) {
} if(this.activeQueue.length < this.maxConcurrentQueries) {
this.activeQueue.push(queueItem)
ConnectorManager.prototype._transferQueuedItems = function(count) { execQueueItem.call(this, queueItem)
for(var i = 0; i < count; i++) { } else {
var queueItem = this.queue[0] this.queue.push(queueItem)
if(queueItem) {
this._enqueue(queueItem)
this.queue = without(this.queue, queueItem)
} }
} }
}
ConnectorManager.prototype._afterQuery = function(queueItem) { var dequeue = function(queueItem) {
var self = this this.activeQueue = without(this.activeQueue, queueItem)
}
this._dequeue(queueItem) var transferQueuedItems = function(count) {
this._transferQueuedItems(this.maxConcurrentQueries - this.activeQueue.length) for(var i = 0; i < count; i++) {
this._disconnectIfNoConnections() var queueItem = this.queue[0]
} if(queueItem) {
enqueue.call(this, queueItem)
this.queue = without(this.queue, queueItem)
}
}
}
var afterQuery = function(queueItem) {
var self = this
ConnectorManager.prototype._execQueueItem = function(queueItem) { dequeue.call(this, queueItem)
var self = this transferQueuedItems.call(this, this.maxConcurrentQueries - this.activeQueue.length)
disconnectIfNoConnections.call(this)
}
var execQueueItem = function(queueItem) {
var self = this
queueItem.query
.on('success', function(){ afterQuery.call(self, queueItem) })
.on('failure', function(){ afterQuery.call(self, queueItem) })
queueItem.query queueItem.query.run(queueItem.sql)
.on('success', function(){ self._afterQuery(queueItem) }) }
.on('failure', function(){ self._afterQuery(queueItem) })
queueItem.query.run(queueItem.sql) ConnectorManager.prototype.__defineGetter__('hasNoConnections', function() {
} return (this.queue.length == 0) && (this.activeQueue.length == 0) && this.client._queue && (this.client._queue.length == 0)
})
ConnectorManager.prototype.__defineGetter__('hasNoConnections', function() { ConnectorManager.prototype.__defineGetter__('isConnected', function() {
return (this.queue.length == 0) && (this.activeQueue.length == 0) && this.client._queue && (this.client._queue.length == 0) return this.client != null
}) })
ConnectorManager.prototype.__defineGetter__('isConnected', function() { var disconnectIfNoConnections = function() {
return this.client != null var self = this
})
ConnectorManager.prototype._disconnectIfNoConnections = function() { this.disconnectTimeoutId && clearTimeout(this.disconnectTimeoutId)
var self = this this.disconnectTimeoutId = setTimeout(function() {
self.isConnected && self.hasNoConnections && self.disconnect()
}, 100)
}
this.disconnectTimeoutId && clearTimeout(this.disconnectTimeoutId) return ConnectorManager
this.disconnectTimeoutId = setTimeout(function() { })()
self.isConnected && self.hasNoConnections && self.disconnect()
}, 100)
}
var QueryGenerator = module.exports = { module.exports = (function() {
/* var QueryGenerator = {
Returns a query for creating a table. /*
Attributes should have the format: {attributeName: type, attr2: type2} --> {title: 'VARCHAR(255)'} Returns a query for creating a table.
*/ Attributes should have the format: {attributeName: type, attr2: type2} --> {title: 'VARCHAR(255)'}
createTableQuery: function(tableName, attributes, options) { */
throw new Error('Define the method createTableQuery!') createTableQuery: function(tableName, attributes, options) {
}, throw new Error('Define the method createTableQuery!')
},
/* /*
Returns a query for dropping a table. Returns a query for dropping a table.
*/ */
dropTableQuery: function(tableName, options) { dropTableQuery: function(tableName, options) {
throw new Error('Define the method dropTableQuery!') throw new Error('Define the method dropTableQuery!')
}, },
/* /*
Returns a query for selecting elements in the table <tableName>. Returns a query for selecting elements in the table <tableName>.
Options: Options:
- attributes -> An array of attributes (e.g. ['name', 'birthday']). Default: * - attributes -> An array of attributes (e.g. ['name', 'birthday']). Default: *
- where -> A hash with conditions (e.g. {name: 'foo'}) - where -> A hash with conditions (e.g. {name: 'foo'})
OR an ID as integer OR an ID as integer
OR a string with conditions (e.g. 'name="foo"'). OR a string with conditions (e.g. 'name="foo"').
If you use a string, you have to escape it on your own. If you use a string, you have to escape it on your own.
- order -> e.g. 'id DESC' - order -> e.g. 'id DESC'
- group - group
- limit -> The maximum count you want to get. - limit -> The maximum count you want to get.
- offset -> An offset value to start from. Only useable with limit! - offset -> An offset value to start from. Only useable with limit!
*/ */
selectQuery: function(tableName, options) { selectQuery: function(tableName, options) {
throw new Error('Define the method selectQuery!') throw new Error('Define the method selectQuery!')
}, },
/* /*
Returns a query for counting elements in the table <tableName>. Returns a query for counting elements in the table <tableName>.
Options are the very same as in selectQuery. Options are the very same as in selectQuery.
*/ */
countQuery: function(tableName, options) { countQuery: function(tableName, options) {
throw new Error('Define the method countQuery!') throw new Error('Define the method countQuery!')
}, },
/* /*
Returns a query for getting the max value of a field in the table <tableName>. Returns a query for getting the max value of a field in the table <tableName>.
Options are the very same as in selectQuery. Options are the very same as in selectQuery.
*/ */
maxQuery: function(tableName, field, options) { maxQuery: function(tableName, field, options) {
throw new Error('Define the method maxQuery!') throw new Error('Define the method maxQuery!')
}, },
/* /*
Returns a query for getting the min value of a field in the table <tableName>. Returns a query for getting the min value of a field in the table <tableName>.
Options are the very same as in selectQuery. Options are the very same as in selectQuery.
*/ */
minQuery: function(tableName, field, options) { minQuery: function(tableName, field, options) {
throw new Error('Define the method minQuery!') throw new Error('Define the method minQuery!')
}, },
/* /*
Returns an insert into command. Parameters: table name + hash of attribute-value-pairs. Returns an insert into command. Parameters: table name + hash of attribute-value-pairs.
*/ */
insertQuery: function(tableName, attrValueHash) { insertQuery: function(tableName, attrValueHash) {
throw new Error('Define the method insertQuery!') throw new Error('Define the method insertQuery!')
}, },
/* /*
Returns an update query. Returns an update query.
Parameters: Parameters:
- tableName -> Name of the table - tableName -> Name of the table
- values -> A hash with attribute-value-pairs - values -> A hash with attribute-value-pairs
- where -> A hash with conditions (e.g. {name: 'foo'}) - where -> A hash with conditions (e.g. {name: 'foo'})
OR an ID as integer OR an ID as integer
OR a string with conditions (e.g. 'name="foo"'). OR a string with conditions (e.g. 'name="foo"').
If you use a string, you have to escape it on your own. If you use a string, you have to escape it on your own.
*/ */
updateQuery: function(tableName, values, where) { updateQuery: function(tableName, values, where) {
throw new Error('Define the method updateQuery!') throw new Error('Define the method updateQuery!')
}, },
/* /*
Returns a deletion query. Returns a deletion query.
Parameters: Parameters:
- tableName -> Name of the table - tableName -> Name of the table
- where -> A hash with conditions (e.g. {name: 'foo'}) - where -> A hash with conditions (e.g. {name: 'foo'})
OR an ID as integer OR an ID as integer
OR a string with conditions (e.g. 'name="foo"'). OR a string with conditions (e.g. 'name="foo"').
If you use a string, you have to escape it on your own. If you use a string, you have to escape it on your own.
Options: Options:
- limit -> Maximaum count of lines to delete - limit -> Maximaum count of lines to delete
*/ */
deleteQuery: function(tableName, where, options) { deleteQuery: function(tableName, where, options) {
throw new Error('Define the method deleteQuery!') throw new Error('Define the method deleteQuery!')
}, },
/* /*
Takes something and transforms it into values of a where condition. Takes something and transforms it into values of a where condition.
*/ */
getWhereConditions: function(smth) { getWhereConditions: function(smth) {
throw new Error('Define the method getWhereConditions!') throw new Error('Define the method getWhereConditions!')
}, },
/* /*
Takes a hash and transforms it into a mysql where condition: {key: value, key2: value2} ==> key=value AND key2=value2 Takes a hash and transforms it into a mysql where condition: {key: value, key2: value2} ==> key=value AND key2=value2
The values are transformed by the relevant datatype. The values are transformed by the relevant datatype.
*/ */
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!