node-postgres/lib/client.js

97 lines
2.4 KiB
JavaScript
Raw Normal View History

2010-10-11 11:37:30 +08:00
var EventEmitter = require('events').EventEmitter;
var net = require('net');
var Query = require(__dirname+'/query');
var Parser = require(__dirname+'/parser');
var Client = function(config) {
EventEmitter.call(this);
config = config || {};
this.user = config.user;
this.database = config.database;
this.port = config.port || 5432;
this.host = config.host;
this.queryQueue = [];
this.stream = config.stream || new net.Stream();
this.queryQueue = [];
};
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
if(this.stream.readyState == 'closed'){
this.stream.connect(this.port, this.host);
}
var self = this;
this.stream.on('connect', function() {
var data = ['user',self.user,'database', self.database, '\0'].join('\0');
var dataBuffer = Buffer(data);
var fullBuffer = Buffer(8 + dataBuffer.length);
fullBuffer[0] = fullBuffer.length >>> 24;
fullBuffer[1] = fullBuffer.length >>> 16;
fullBuffer[2] = fullBuffer.length >>> 8;
fullBuffer[3] = fullBuffer.length >>> 0;
fullBuffer[4] = 0;
fullBuffer[5] = 3;
fullBuffer[6] = 0;
fullBuffer[7] = 0;
fullBuffer.write(data,8);
self.stream.write(fullBuffer);
});
this.stream.on('data', function(data) {
var parser = new Parser(data);
var msg = parser.parseMessage();
while(msg) {
self.emit('message', msg);
self.emit(msg.name, msg);
msg = parser.parseMessage();
}
});
this.on('ReadyForQuery', function() {
self.readyForQuery = true;
self.pulseQueryQueue();
});
};
2010-10-11 11:44:13 +08:00
p.disconnect = function() {
2010-10-11 11:37:30 +08:00
var terminationBuffer = new Buffer([58,0,0,0,4]);
this.stream.write(terminationBuffer);
};
2010-10-11 11:44:13 +08:00
p.query = function(text) {
2010-10-11 11:37:30 +08:00
var query = new Query();
query.client = this;
query.text = text;
this.queryQueue.push(query);
this.pulseQueryQueue();
return query;
};
2010-10-11 11:44:13 +08:00
p.pulseQueryQueue = function() {
2010-10-11 11:37:30 +08:00
if(!this.readyForQuery) {
return;
};
var query = this.queryQueue.shift();
if(query) {
var self = this;
this.readyForQuery = false;
this.stream.write(query.toBuffer());
var rowHandler = function(msg) {
query.emit('row',msg.fields)
};
var endHandler;
endHandler = function(msg) {
query.emit('end');
self.removeListener('CommandComplete', endHandler);
self.removeListener('DataRow', rowHandler);
};
this.on('DataRow', rowHandler);
this.on('CommandComplete', endHandler);
}
};
module.exports = Client;