node-postgres/lib/index.js

238 lines
5.4 KiB
JavaScript
Raw Normal View History

var EventEmitter = require('events').EventEmitter;
var sys = require('sys');
var net = require('net');
var NUL = '\0';
2010-09-29 15:46:44 +08:00
var chars = Buffer('RSKZQCT','utf8');
var UTF8 = {
2010-09-29 13:08:53 +08:00
R: chars[0],
2010-09-30 13:14:41 +08:00
S: chars[1],
K: chars[2],
2010-09-29 14:01:52 +08:00
Z: chars[3],
Q: chars[4],
2010-09-29 15:46:44 +08:00
C: chars[5],
T: chars[6]
};
var Client = function(config) {
EventEmitter.call(this);
config = config || {};
this.user = config.user;
this.database = config.database;
this.port = config.port || 5432;
2010-09-29 14:01:52 +08:00
this.queryQueue = [];
};
sys.inherits(Client, EventEmitter);
Client.prototype.connect = function() {
var con = net.createConnection(this.port);
var self = this;
con.on('connect', function() {
var data = ['user',self.user,'database', self.database,NUL].join(NUL);
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);
con.write(fullBuffer);
});
con.on('data', function(data) {
2010-09-29 13:43:28 +08:00
var parser = new Parser(data);
var result = parser.parse();
2010-09-30 13:40:06 +08:00
result.forEach(function(msg) {
2010-09-29 14:01:52 +08:00
self.emit('message', msg);
self.emit(msg.name, msg);
2010-09-30 13:40:06 +08:00
});
});
2010-09-29 14:01:52 +08:00
this.con = con;
this.on('ReadyForQuery', function() {
self.readyForQuery = true;
self.pulseQueryQueue();
});
this.on('message', function(msg) {
console.log(msg.name);
});
};
Client.prototype.query = function(queryText) {
this.queryQueue.push(new Query({
text: queryText
}));
this.pulseQueryQueue();
};
Client.prototype.pulseQueryQueue = function() {
if(!this.readyForQuery) {
return;
}
var query = this.queryQueue.shift();
if(!query) {
return;
}
var txt = query.text + "\0"
var queryTextBuffer = Buffer(txt);
var len = queryTextBuffer.length+4;
var messageBuffer = Buffer(queryTextBuffer.length + 5);
messageBuffer[0] = UTF8.Q;
messageBuffer[1] = len >>> 24;
messageBuffer[2] = len >>> 16;
messageBuffer[3] = len >>> 8;
messageBuffer[4] = len >>> 0;
messageBuffer.write(txt,5);
this.con.write(messageBuffer);
this.readyForQuery = false;
};
var Query = function(config) {
this.text = config.text;
};
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.parse = function() {
var messages = [];
var message = this.parseMessage();
while(message) {
messages.push(message);
message = this.parseMessage();
}
return messages;
};
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];
switch(messageID) {
2010-09-29 13:12:04 +08:00
case UTF8.R:
2010-09-29 13:30:35 +08:00
return this.parseR();
2010-09-29 13:12:04 +08:00
case UTF8.S:
2010-09-29 13:30:35 +08:00
return this.parseS();
2010-09-30 13:14:41 +08:00
case UTF8.K:
return this.parseK();
case UTF8.Z:
return this.parseZ();
2010-09-29 14:01:52 +08:00
case UTF8.C:
return this.parseC();
2010-09-29 15:46:44 +08:00
case UTF8.T:
return this.parseT();
2010-09-29 13:12:04 +08:00
default:
throw new Error("Unsupported message ID: " + Buffer([messageID]).toString('utf8') + " (" + messageID.toString(16) + ")");
2010-09-29 13:12:04 +08:00
}
};
2010-09-29 13:30:35 +08:00
p.parseR = function() {
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-09-29 13:43:28 +08:00
}p
2010-09-29 13:12:04 +08:00
throw new Error("Unknown AuthenticatinOk message type");
};
2010-09-29 13:43:28 +08:00
2010-09-29 13:20:10 +08:00
p.parseS = 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-09-30 13:14:41 +08:00
p.parseK = 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-09-29 14:01:52 +08:00
p.parseC = function() {
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-09-30 13:14:41 +08:00
p.parseZ = 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-09-29 15:46:44 +08:00
p.parseT = function() {
var msg = this.parseStart('RowDescription');
msg.fieldCount = this.readInt16();
msg.fields = [];
for(var i = 0; i < msg.fieldCount; i++){
2010-10-01 11:48:50 +08:00
msg.fields[i] = this.parseField();
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'
};
2010-09-29 15:46:44 +08:00
};
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-09-29 13:20:10 +08:00
p.parseCString = function(buffer) {
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
};