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

Commit cbe4ae7d by Sascha Depold

private method scoping

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