2010-10-11 12:02:11 +08:00
|
|
|
var sys = require('sys');
|
2010-10-24 01:46:27 +08:00
|
|
|
var net = require('net');
|
2010-10-23 07:16:40 +08:00
|
|
|
var crypto = require('crypto');
|
2010-10-24 01:46:27 +08:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2010-11-11 11:45:39 +08:00
|
|
|
var url = require('url');
|
2010-11-03 13:27:11 +08:00
|
|
|
var Query = require(__dirname + '/query');
|
2010-10-24 01:45:37 +08:00
|
|
|
var utils = require(__dirname + '/utils');
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2010-11-21 04:09:18 +08:00
|
|
|
var defaults = require(__dirname + '/defaults');
|
2010-10-24 08:02:13 +08:00
|
|
|
var Connection = require(__dirname + '/connection');
|
2010-10-24 01:46:27 +08:00
|
|
|
|
2010-11-11 11:45:39 +08:00
|
|
|
var parseConnectionString = function(str) {
|
|
|
|
var result = url.parse(str);
|
|
|
|
result.host = result.hostname;
|
2010-12-29 00:44:33 +08:00
|
|
|
result.database = result.pathname ? result.pathname.slice(1) : null
|
2010-11-11 11:45:39 +08:00
|
|
|
var auth = (result.auth || ':').split(':');
|
|
|
|
result.user = auth[0];
|
|
|
|
result.password = auth[1];
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2010-10-11 11:37:30 +08:00
|
|
|
var Client = function(config) {
|
|
|
|
EventEmitter.call(this);
|
2010-11-11 11:45:39 +08:00
|
|
|
if(typeof config === 'string') {
|
|
|
|
config = parseConnectionString(config)
|
|
|
|
}
|
2010-10-11 11:37:30 +08:00
|
|
|
config = config || {};
|
2010-11-21 04:09:18 +08:00
|
|
|
this.user = config.user || defaults.user;
|
|
|
|
this.database = config.database || defaults.database;
|
|
|
|
this.port = config.port || defaults.port;
|
|
|
|
this.host = config.host || defaults.host;
|
2010-10-11 11:37:30 +08:00
|
|
|
this.queryQueue = [];
|
2010-10-25 11:52:12 +08:00
|
|
|
this.connection = config.connection || new Connection({stream: config.stream || new net.Stream()});
|
2010-10-11 11:37:30 +08:00
|
|
|
this.queryQueue = [];
|
2010-11-21 04:09:18 +08:00
|
|
|
this.password = config.password || defaults.password;
|
2010-10-20 13:34:16 +08:00
|
|
|
this.encoding = 'utf8';
|
2010-10-11 11:37:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
sys.inherits(Client, EventEmitter);
|
|
|
|
|
2010-10-11 11:44:13 +08:00
|
|
|
var p = Client.prototype;
|
|
|
|
|
|
|
|
p.connect = function() {
|
2010-10-11 11:37:30 +08:00
|
|
|
var self = this;
|
2010-10-24 08:02:13 +08:00
|
|
|
var con = this.connection;
|
2010-10-24 09:26:24 +08:00
|
|
|
con.connect(this.port, this.host);
|
|
|
|
|
2010-10-25 11:52:12 +08:00
|
|
|
//once connection is established send startup message
|
2010-10-24 09:26:24 +08:00
|
|
|
con.on('connect', function() {
|
2010-10-25 10:28:10 +08:00
|
|
|
con.startup({
|
2010-10-24 09:26:24 +08:00
|
|
|
user: self.user,
|
|
|
|
database: self.database
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2010-10-25 11:52:12 +08:00
|
|
|
//password request handling
|
2010-10-24 08:02:13 +08:00
|
|
|
con.on('authenticationCleartextPassword', function() {
|
2010-10-25 10:26:18 +08:00
|
|
|
con.password(self.password);
|
2010-10-20 12:29:23 +08:00
|
|
|
});
|
|
|
|
|
2010-10-25 11:52:12 +08:00
|
|
|
//password request handling
|
2010-10-24 08:21:01 +08:00
|
|
|
con.on('authenticationMD5Password', function(msg) {
|
2010-10-24 09:26:24 +08:00
|
|
|
var inner = Client.md5(self.password + self.user);
|
|
|
|
var outer = Client.md5(inner + msg.salt.toString('binary'));
|
2010-10-24 08:21:01 +08:00
|
|
|
var md5password = "md5" + outer;
|
2010-10-25 10:26:18 +08:00
|
|
|
con.password(md5password);
|
2010-10-23 07:16:40 +08:00
|
|
|
});
|
2010-10-24 08:21:01 +08:00
|
|
|
|
2011-02-05 08:51:23 +08:00
|
|
|
con.once('readyForQuery', function() {
|
|
|
|
//hook up query handling events to connection
|
|
|
|
//after the connection initially becomes ready for queries
|
|
|
|
var ready = function() {
|
2011-02-05 09:15:57 +08:00
|
|
|
if(self.activeQuery) {
|
|
|
|
self.activeQuery.handleReadyForQuery();
|
|
|
|
}
|
2011-02-05 08:51:23 +08:00
|
|
|
self.readyForQuery = true;
|
|
|
|
this.activeQuery = null;
|
|
|
|
self.pulseQueryQueue();
|
|
|
|
};
|
|
|
|
|
2011-02-05 09:03:23 +08:00
|
|
|
con.on('rowDescription', function(msg) {
|
|
|
|
self.activeQuery.handleRowDescription(msg);
|
|
|
|
});
|
|
|
|
|
|
|
|
con.on('dataRow', function(msg) {
|
|
|
|
self.activeQuery.handleDataRow(msg);
|
|
|
|
});
|
|
|
|
|
|
|
|
con.on('commandComplete', function(msg) {
|
|
|
|
self.activeQuery.handleCommandComplete(msg);
|
2011-02-05 09:15:57 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
con.on('readyForQuery', function() {
|
|
|
|
ready();
|
|
|
|
});
|
2011-02-05 09:03:23 +08:00
|
|
|
|
2011-02-05 08:51:23 +08:00
|
|
|
ready();
|
|
|
|
|
2010-10-25 11:52:12 +08:00
|
|
|
});
|
2010-10-26 14:51:12 +08:00
|
|
|
|
|
|
|
con.on('error', function(error) {
|
2010-11-15 06:50:38 +08:00
|
|
|
if(!self.activeQuery) {
|
|
|
|
self.emit('error', error);
|
2011-02-05 09:15:57 +08:00
|
|
|
} else {
|
|
|
|
self.activeQuery.handleError(error);
|
|
|
|
self.activeQuery = null;
|
2010-11-15 06:50:38 +08:00
|
|
|
}
|
2010-10-26 14:51:12 +08:00
|
|
|
});
|
2010-10-24 02:12:49 +08:00
|
|
|
};
|
|
|
|
|
2010-10-25 11:52:12 +08:00
|
|
|
p.pulseQueryQueue = function() {
|
2010-10-30 08:48:31 +08:00
|
|
|
if(this.readyForQuery===true) {
|
|
|
|
if(this.queryQueue.length > 0) {
|
|
|
|
this.readyForQuery = false;
|
|
|
|
var query = this.queryQueue.shift();
|
2010-11-15 06:50:38 +08:00
|
|
|
this.activeQuery = query;
|
2010-12-03 07:47:54 +08:00
|
|
|
this.hasExecuted = true;
|
2010-10-30 08:48:31 +08:00
|
|
|
query.submit(this.connection);
|
2010-12-03 07:47:54 +08:00
|
|
|
} else if(this.hasExecuted) {
|
2010-12-11 06:56:10 +08:00
|
|
|
this.activeQuery = null;
|
2010-12-03 07:47:54 +08:00
|
|
|
this.emit('drain')
|
2010-10-30 08:48:31 +08:00
|
|
|
}
|
2010-10-11 11:37:30 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-11-15 14:42:38 +08:00
|
|
|
p.query = function(config, values, callback) {
|
2010-10-28 07:58:58 +08:00
|
|
|
//can take in strings or config objects
|
2010-11-15 14:10:21 +08:00
|
|
|
config = (config.text || config.name) ? config : { text: config };
|
2010-11-15 14:42:38 +08:00
|
|
|
if(values) {
|
|
|
|
if(typeof values === 'function') {
|
|
|
|
callback = values;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
config.values = values;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(callback) {
|
|
|
|
config.callback = callback;
|
|
|
|
}
|
|
|
|
|
2010-11-15 14:10:21 +08:00
|
|
|
var query = new Query(config);
|
2010-10-25 12:32:18 +08:00
|
|
|
this.queryQueue.push(query);
|
2010-10-25 11:52:12 +08:00
|
|
|
this.pulseQueryQueue();
|
2010-10-25 12:32:18 +08:00
|
|
|
return query;
|
2010-10-24 02:12:49 +08:00
|
|
|
};
|
|
|
|
|
2010-10-26 10:24:17 +08:00
|
|
|
p.end = function() {
|
|
|
|
this.connection.end();
|
|
|
|
};
|
|
|
|
|
2010-10-25 11:52:12 +08:00
|
|
|
Client.md5 = function(string) {
|
|
|
|
return crypto.createHash('md5').update(string).digest('hex');
|
2010-10-24 02:12:49 +08:00
|
|
|
};
|
|
|
|
|
2010-10-11 11:37:30 +08:00
|
|
|
module.exports = Client;
|