refactored to increase performance

This commit is contained in:
brianc 2010-10-31 18:36:35 -05:00
parent 0d2d6b5107
commit 0f926ca62e

View File

@ -53,48 +53,51 @@ p.connect = function(port, host) {
};
p.startup = function(config) {
var buffer = new Writer()
var bodyBuffer = new Writer()
.addInt16(3)
.addInt16(0)
.addCString('user')
.addCString(config.user)
.addCString('database')
.addCString(config.database)
.addCString('');
.addCString('').join();
//this message is sent without a code
this.send(false, buffer.join());
var length = bodyBuffer.length + 4;
var buffer = new Writer()
.addInt32(length)
.add(bodyBuffer)
.join();
this.stream.write(buffer);
};
p.password = function(password) {
this.send('p', Buffer(password + '\0', this.encoding));
//0x70 = 'p'
this.send(0x70, Buffer(password + '\0', this.encoding));
};
p.send = function(code, bodyBuffer) {
var length = bodyBuffer.length + 4;
var buffer = Buffer(length + (code ? 1 : 0));
var buffer = Buffer(length + 1);
var offset = 0;
if(code) {
buffer[offset++] = Buffer(code, this.encoding) [0];
}
this.writeInt32(buffer, offset, length);
bodyBuffer.copy(buffer, offset+4, 0);
buffer[offset++] = code;
buffer[offset++] = length >>> 24 & 0xFF;
buffer[offset++] = length >>> 16 & 0xFF;
buffer[offset++] = length >>> 8 & 0xFF;
buffer[offset++] = length >>> 0 & 0xFF;
bodyBuffer.copy(buffer, offset, 0);
return this.stream.write(buffer);
};
p.writeInt32 = function(buffer, offset, value) {
buffer[offset++] = value >>> 24 & 0xFF;
buffer[offset++] = value >>> 16 & 0xFF;
buffer[offset++] = value >>> 8 & 0xFF;
buffer[offset++] = value >>> 0 & 0xFF;
};
var termBuffer = new Buffer([0x58, 0, 0, 0, 4]);
p.end = function() {
var terminationBuffer = new Buffer([0x58,0,0,0,4]);
var wrote = this.stream.write(terminationBuffer);
var wrote = this.stream.write(termBuffer);
};
p.query = function(text) {
this.send('Q', new Buffer(text + '\0', this.encoding));
//0x51 = Q
this.send(0x51, new Buffer(text + '\0', this.encoding));
};
p.parse = function(query) {
@ -116,7 +119,8 @@ p.parse = function(query) {
buffer.addInt32(query.types[i]);
}
this.send('P', buffer.join());
//0x50 = 'P'
this.send(0x50, buffer.join());
return this;
};
@ -144,7 +148,8 @@ p.bind = function(config) {
}
}
buffer.addInt16(0); //no format codes, use text
this.send('B', buffer.join());
//0x42 = 'B'
this.send(0x42, buffer.join());
};
p.execute = function(config) {
@ -155,25 +160,30 @@ p.execute = function(config) {
.addCString(config.portal)
.addInt32(config.rows)
.join();
this.send('E', buffer);
//0x45 = 'E'
this.send(0x45, buffer);
};
p.flush = function() {
this.send('H',Buffer(0));
//0x48 = 'H'
this.send(0x48,Buffer(0));
}
p.sync = function() {
this.send('S', Buffer(0));
//0x53 = 'S'
this.send(0x53, Buffer(0));
};
p.end = function() {
this.send('X', Buffer(0));
//0x58 = 'X'
this.send(0x58, Buffer(0));
};
p.describe = function(msg) {
var str = msg.type + (msg.name || "" ) + '\0';
var buffer = Buffer(str, this.encoding);
this.send('D', buffer);
this.send(0x44, buffer);
};
//parsing methods