node-postgres/lib/index.js

278 lines
6.5 KiB
JavaScript
Raw Normal View History

var EventEmitter = require('events').EventEmitter;
var sys = require('sys');
var net = require('net');
var Client = function(config) {
EventEmitter.call(this);
config = config || {};
this.user = config.user;
this.database = config.database;
this.port = config.port || 5432;
2010-10-07 08:54:02 +08:00
this.host = config.host;
2010-09-29 14:01:52 +08:00
this.queryQueue = [];
2010-10-07 08:54:02 +08:00
this.stream = config.stream || new net.Stream();
2010-10-08 08:38:27 +08:00
this.queryQueue = [];
};
2010-10-01 13:27:42 +08:00
sys.inherits(Client, EventEmitter);
Client.prototype.connect = function() {
2010-10-08 08:39:43 +08:00
if(this.stream.readyState == 'closed'){
this.stream.connect(this.port, this.host);
2010-10-07 08:54:02 +08:00
}
var self = this;
2010-10-08 08:39:43 +08:00
this.stream.on('connect', function() {
2010-10-11 11:32:04 +08:00
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);
2010-10-11 06:30:33 +08:00
self.stream.write(fullBuffer);
});
2010-10-08 09:00:49 +08:00
2010-10-08 08:39:43 +08:00
this.stream.on('data', function(data) {
2010-09-29 13:43:28 +08:00
var parser = new Parser(data);
2010-10-11 08:09:24 +08:00
var msg = parser.parseMessage();
while(msg) {
2010-09-29 14:01:52 +08:00
self.emit('message', msg);
self.emit(msg.name, msg);
2010-10-11 08:09:24 +08:00
msg = parser.parseMessage();
}
});
2010-10-08 08:39:43 +08:00
2010-10-08 09:00:49 +08:00
this.on('ReadyForQuery', function() {
2010-10-09 15:48:41 +08:00
self.readyForQuery = true;
2010-10-08 09:00:49 +08:00
self.pulseQueryQueue();
});
};
2010-10-03 13:45:10 +08:00
Client.prototype.disconnect = function() {
2010-10-11 11:32:04 +08:00
var terminationBuffer = new Buffer([58,0,0,0,4]);
2010-10-08 08:39:43 +08:00
this.stream.write(terminationBuffer);
2010-10-03 13:45:10 +08:00
};
2010-10-08 09:00:49 +08:00
Client.prototype.query = function(text) {
2010-10-08 08:38:27 +08:00
var query = new Query();
query.client = this;
2010-10-08 09:00:49 +08:00
query.text = text;
this.queryQueue.push(query);
2010-10-09 15:48:41 +08:00
this.pulseQueryQueue();
2010-10-08 08:38:27 +08:00
return query;
2010-10-03 14:08:04 +08:00
};
2010-10-08 09:00:49 +08:00
Client.prototype.pulseQueryQueue = function() {
2010-10-09 15:48:41 +08:00
if(!this.readyForQuery) {
return;
};
2010-10-08 09:00:49 +08:00
var query = this.queryQueue.shift();
if(query) {
2010-10-09 15:48:41 +08:00
var self = this;
this.readyForQuery = false;
2010-10-08 09:00:49 +08:00
this.stream.write(query.toBuffer());
2010-10-09 15:48:41 +08:00
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);
2010-10-08 09:00:49 +08:00
}
};
2010-10-03 14:14:19 +08:00
var Query = function() {
EventEmitter.call(this);
};
2010-10-08 09:00:49 +08:00
2010-10-03 14:14:19 +08:00
sys.inherits(Query, EventEmitter);
2010-10-08 09:00:49 +08:00
Query.prototype.toBuffer = function() {
var textBuffer = new Buffer(this.text+'\0','utf8');
2010-10-09 12:17:09 +08:00
var len = textBuffer.length + 4;
var fullBuffer = new Buffer(len + 1);
2010-10-08 09:00:49 +08:00
fullBuffer[0] = 0x51;
fullBuffer[1] = len >>> 24;
fullBuffer[2] = len >>> 16;
fullBuffer[3] = len >>> 8;
fullBuffer[4] = len >>> 0;
textBuffer.copy(fullBuffer,5,0);
return fullBuffer;
};
2010-10-03 14:14:19 +08:00
2010-09-29 13:30:35 +08:00
var Parser = function(buffer) {
2010-09-29 13:20:10 +08:00
this.offset = 0;
2010-09-29 13:30:35 +08:00
this.buffer = buffer;
2010-09-29 13:12:04 +08:00
};
2010-09-29 13:12:04 +08:00
var p = Parser.prototype;
2010-09-30 13:40:06 +08:00
p.parseMessage = function() {
2010-09-29 13:43:28 +08:00
if(this.buffer.length == this.offset) {
return false;
}
var messageID = this.buffer[this.offset];
2010-10-11 08:03:44 +08:00
return this["parse"+messageID]();
2010-09-29 13:12:04 +08:00
};
2010-10-11 08:03:44 +08:00
//parse 'R' message
p.parse82 = function() {
2010-09-29 13:30:35 +08:00
var type = this.buffer[this.offset++];
var length = this.parseLength();
2010-09-29 13:12:04 +08:00
if(length == 8) {
2010-09-29 13:43:28 +08:00
this.offset += 4;
2010-09-29 13:08:53 +08:00
return {
2010-09-29 13:12:04 +08:00
name: 'AuthenticationOk',
id: 'R',
length: length
2010-09-29 13:08:53 +08:00
}
2010-10-11 08:10:39 +08:00
}
2010-09-29 13:12:04 +08:00
throw new Error("Unknown AuthenticatinOk message type");
};
2010-09-29 13:43:28 +08:00
2010-10-11 08:03:44 +08:00
//parse 'S' message
p.parse83 = function(buffer) {
2010-09-30 13:27:56 +08:00
var msg = this.parseStart('ParameterStatus');
msg.parameterName = this.parseCString();
msg.parameterValue = this.parseCString();
return msg;
2010-09-29 13:12:04 +08:00
};
2010-10-11 08:03:44 +08:00
//parse 'K' message
p.parse75 = function() {
2010-09-30 13:27:56 +08:00
var msg = this.parseStart('BackendKeyData');
msg.processID = this.readInt32();
msg.secretKey = this.readInt32();
return msg;
};
2010-10-11 08:03:44 +08:00
//parse 'C' message
p.parse67 = function() {
2010-09-29 14:01:52 +08:00
var msg = this.parseStart('CommandComplete');
msg.text = this.parseCString();
return msg;
};
2010-09-30 13:27:56 +08:00
//parses common start of message packets
p.parseStart = function(name) {
2010-09-30 13:14:41 +08:00
return {
2010-09-30 13:27:56 +08:00
name: name,
id: this.readChar(),
length: this.readInt32()
2010-09-30 13:14:41 +08:00
}
};
2010-09-30 13:27:56 +08:00
p.readChar = function() {
return Buffer([this.buffer[this.offset++]]).toString('utf8');
};
2010-10-11 08:03:44 +08:00
//parse 'Z' message
p.parse90 = function() {
2010-09-30 13:27:56 +08:00
var msg = this.parseStart('ReadyForQuery');
msg.status = this.readChar();
return msg;
2010-09-30 13:14:41 +08:00
};
2010-10-11 08:03:44 +08:00
//parse 'T' message
p.parse84 = function() {
2010-09-29 15:46:44 +08:00
var msg = this.parseStart('RowDescription');
msg.fieldCount = this.readInt16();
var fields = [];
for(var i = 0; i < msg.fieldCount; i++){
fields[i] = this.parseField();
2010-09-29 15:46:44 +08:00
}
msg.fields = fields;
2010-09-29 15:46:44 +08:00
return msg;
};
2010-10-01 11:48:50 +08:00
p.parseField = function() {
var row = {
name: this.parseCString(),
tableID: this.readInt32(),
columnID: this.readInt16(),
dataType: this.readInt32(),
dataTypeSize: this.readInt16(),
dataTypeModifier: this.readInt32(),
format: this.readInt16() == 0 ? 'text' : 'binary'
};
return row;
2010-09-29 15:46:44 +08:00
};
2010-10-11 08:03:44 +08:00
//parse 'D' message
p.parse68 = function() {
var msg = this.parseStart('DataRow');
2010-10-01 14:02:52 +08:00
var fieldCount = this.readInt16();
var fields = [];
for(var i = 0; i < fieldCount; i++) {
fields[i] = this.readString(this.readInt32());
};
msg.fieldCount = fieldCount;
msg.fields = fields;
return msg;
};
2010-10-11 08:03:44 +08:00
//parse 'E' message
p.parse69 = function() {
var msg = this.parseStart('Error');
2010-10-11 07:15:16 +08:00
var fields = {};
var fieldType = this.readString(1);
2010-10-11 07:15:16 +08:00
while(fieldType != '\0') {
fields[fieldType] = this.parseCString();
fieldType = this.readString(1);
}
2010-10-11 07:15:16 +08:00
msg.severity = fields.S;
msg.code = fields.C;
msg.message = fields.M;
msg.detail = fields.D;
msg.hint = fields.H;
msg.position = fields.P;
msg.internalPosition = fields.p;
msg.internalQuery = fields.q;
msg.where = fields.W;
msg.file = fields.F;
msg.line = fields.L;
msg.routine = fields.R;
return msg;
};
2010-09-30 13:14:41 +08:00
p.readInt32 = function() {
2010-09-29 13:30:35 +08:00
var buffer = this.buffer;
return ((buffer[this.offset++] << 24) +
(buffer[this.offset++] << 16) +
(buffer[this.offset++] << 8) +
buffer[this.offset++]);
2010-09-30 13:14:41 +08:00
};
2010-09-29 13:30:35 +08:00
2010-09-29 15:46:44 +08:00
p.readInt16 = function() {
return ((this.buffer[this.offset++] << 8) + (this.buffer[this.offset++] << 0));
};
2010-09-30 13:14:41 +08:00
p.parseLength = function() {
return this.readInt32();
};
2010-10-01 14:02:52 +08:00
p.readString = function(length) {
return this.buffer.toString('utf8', this.offset, (this.offset += length));
};
p.parseCString = function() {
2010-09-29 13:20:10 +08:00
var start = this.offset;
2010-09-29 13:30:35 +08:00
while(this.buffer[this.offset++]) { };
return this.buffer.toString('utf8',start, this.offset - 1);
2010-09-29 13:20:10 +08:00
};
module.exports = {
Client: Client,
Parser: Parser
};