Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Enabling use of different node-mysql "implementations".

  • Loading branch information...
commit 118fff5162d4002d9ff2363757f0ed55b8286d24 1 parent d3b506c
@Kijewski authored
Showing with 73 additions and 59 deletions.
  1. +73 −59 lib/mysql-pool/pool.js
View
132 lib/mysql-pool/pool.js
@@ -2,7 +2,6 @@
var util = require("util");
var EventEmitter = require("events").EventEmitter;
-var Cnx = require('mysql').Client;
function argumentsArray(args) {
var result = [];
@@ -21,11 +20,23 @@ function MySQLPool(properties) {
EventEmitter.call(this);
this.poolSize = 1;
+ this.Client = null;
+ this.properties = {};
this._waitingPool = [];
this._connections = [];
for(var key in properties) {
- this[key] = properties[key];
+ switch(key) {
+ case "client":
+ this.Client = properties[key];
+ break;
+ case "poolSize":
+ this.poolSize = properties[key];
+ break;
+ default:
+ this.properties[key] = properties[key];
+ break;
+ }
}
return this;
@@ -33,8 +44,8 @@ function MySQLPool(properties) {
util.inherits(MySQLPool, EventEmitter);
exports.MySQLPool = MySQLPool;
-MySQLPool.prototype._avail = function _avail(cnx) {
- this._waitingPool.push(cnx);
+MySQLPool.prototype._avail = function _avail(client) {
+ this._waitingPool.push(client);
this.emit("_awake");
}
@@ -59,37 +70,37 @@ MySQLPool.prototype.end = function connect(cb) {
MySQLPool.prototype.connectPool = function connectPool(n, cb) {
var pool = this;
- if(n) {
- this.poolSize = n;
+ if(!this.Client) {
+ this.Client = require('mysql').Client;
}
- var params = {};
- for(var key in this) {
- if(!key.match(/^_|^poolSize$/) && this.hasOwnProperty(key)) {
- params[key] = this[key];
- }
+ this._populate();
+
+ if(typeof n == "function") {
+ cb = n;
+ n = undefined;
}
- var poolSize = this.poolSize;
+ var poolSize = n || this.poolSize;
var calledBack = 0;
var availableConnections = [];
- var failedConnections = [];
+ var errors = [];
- function mkCallback(cnx) {
+ function mkCallback(client) {
return function(err) {
if(err) {
- failedConnections.push(cnx);
+ errors.push(err);
--pool.poolSize;
} else {
- availableConnections.push(cnx);
- pool._connections.push(availableConnections[i]);
+ availableConnections.push(client);
+ pool._connections.push(client);
}
if(++calledBack >= poolSize) {
if(availableConnections.length > 0) {
if(cb) {
- if(failedConnections.length == 0) {
- cb(err, {avail:availableConnections});
+ if(errors.length == 0) {
+ cb(null, {connections:availableConnections.length});
} else {
- cb(err, {avail:availableConnections, failed:failedConnections});
+ cb(null, {connections:availableConnections.length, errors:errors});
}
}
for(var i in availableConnections) {
@@ -108,24 +119,26 @@ MySQLPool.prototype.connectPool = function connectPool(n, cb) {
}
for(var i = 0; i < poolSize; ++i) {
- var cnx = new Cnx(params);
- //cnx._nthPoolMember = i;
- //cnx._connectionPool = this;
- cnx.connect(mkCallback(cnx));
+ var client = this.Client(this.properties);
+ //client._nthPoolMember = i;
+ //client._connectionPool = this;
+ client.connect(mkCallback(client));
}
+ delete this.Client;
return this;
}
MySQLPool.prototype.endPool = function endPool(cb) {
- var pool = this;;
+ // TODO: callback _once_
+ var pool = this;
this._avail = [];
for(var i in this._connections) {
- var cnx = this._connections[i];
- cnx.end(function(err) {
- --pool.poolSize;
+ var client = this._connections[i];
+ client.end(function(err) {
+ --pool._poolSize;
if(cb) {
- cb.apply(cnx, arguments);
+ cb.apply(client, arguments);
} else if(err) {
pool.emit("error", err);
}
@@ -134,37 +147,38 @@ MySQLPool.prototype.endPool = function endPool(cb) {
this._connections = [];
}
-function mkPrototypeMethod(method) {
- return function result() {
- var pool = this;
- var args = argumentsArray(arguments);
-
- var cnx = this._waitingPool.shift();
- if(!cnx) {
- this.once("_awake", function() {
- result.apply(pool, args);
- });
- return this;
- }
-
- var cb = args.pop()
- function callback(err) {
- pool._avail(cnx);
- if(cb) {
- cb.apply(cnx, arguments);
- } else if(err) {
- pool.emit("error", err);
+MySQLPool.prototype._populate = function _populate() {
+ var pool = this;
+
+ function mkPrototypeMethod(method) {
+ return function result() {
+ var args = argumentsArray(arguments);
+
+ var client = pool._waitingPool.shift();
+ if(!client) {
+ pool.once("_awake", function() {
+ result.apply(pool, args);
+ });
+ return pool;
}
- }
- args.push(callback);
- method.apply(cnx, args);
- return this;
- };
-}
+
+ var cb = args.pop()
+ args.push(function(err) {
+ pool._avail(client);
+ if(cb) {
+ cb.apply(client, arguments);
+ } else if(err) {
+ pool.emit("error", err);
+ }
+ });
+ method.apply(client, args);
+ return pool;
+ };
+ }
-for(var key in Cnx.prototype) {
- if(key.match(/^_/) || key in MySQLPool.prototype) {
- continue;
+ for(key in this.Client.prototype) {
+ if(!key.match(/^_/) && !(key in this) && !(key in EventEmitter.prototype)) {
+ this[key] = mkPrototypeMethod(this.Client.prototype[key]);
+ }
}
- MySQLPool.prototype[key] = mkPrototypeMethod(Cnx.prototype[key]);
}
Please sign in to comment.
Something went wrong with that request. Please try again.