mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
connection now handles ending
This commit is contained in:
parent
d156a2cb9a
commit
ed909cbc66
@ -44,44 +44,12 @@ p.connect = function() {
|
||||
var md5password = "md5" + outer;
|
||||
con.passwordMessage(md5password);
|
||||
});
|
||||
|
||||
this.on('readyForQuery', function() {
|
||||
self.readyForQuery = true;
|
||||
self.pulseQueryQueue();
|
||||
});
|
||||
|
||||
this.on('rowDescription', function(msg) {
|
||||
self.processRowDescription(msg);
|
||||
});
|
||||
|
||||
this.on('dataRow', function(msg) {
|
||||
self.processDataRow(msg);
|
||||
});
|
||||
};
|
||||
|
||||
p.md5 = function(string) {
|
||||
return crypto.createHash('md5').update(string).digest('hex');
|
||||
};
|
||||
|
||||
p.send = function(code, bodyBuffer) {
|
||||
var length = bodyBuffer.length + 4;
|
||||
var buffer = Buffer(length + (code ? 1 : 0));
|
||||
var offset = 0;
|
||||
if(code) {
|
||||
buffer[offset++] = Buffer(code, this.encoding) [0];
|
||||
}
|
||||
this.writeInt32(buffer, offset, length);
|
||||
bodyBuffer.copy(buffer, offset+4, 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;
|
||||
};
|
||||
|
||||
p.end = function() {
|
||||
var terminationBuffer = new Buffer([0x58,0,0,0,4]);
|
||||
var wrote = this.stream.end(terminationBuffer);
|
||||
|
||||
@ -140,6 +140,10 @@ p.sync = function() {
|
||||
this.send('S', Buffer(0));
|
||||
};
|
||||
|
||||
p.end = function() {
|
||||
this.send('X', Buffer(0));
|
||||
};
|
||||
|
||||
//parsing methods
|
||||
p.setBuffer = function(buffer) {
|
||||
if(this.lastBuffer) { //we have unfinished biznaz
|
||||
|
||||
@ -92,3 +92,9 @@ test('sends sync command', function() {
|
||||
var expected = new BufferList().join(true,'S');
|
||||
assert.recieved(stream, expected);
|
||||
});
|
||||
|
||||
test('sends end command', function() {
|
||||
con.end();
|
||||
var expected = new Buffer([0x58, 0, 0, 0, 4]);
|
||||
assert.recieved(stream, expected);
|
||||
});
|
||||
|
||||
@ -1,40 +0,0 @@
|
||||
require(__dirname+'/test-helper');
|
||||
|
||||
var dataTypes = {
|
||||
char: 18
|
||||
};
|
||||
|
||||
test('simple query', function() {
|
||||
var stream = new MemoryStream();
|
||||
stream.readyState = 'open';
|
||||
var client = new Client({
|
||||
stream: stream
|
||||
});
|
||||
client.connect();
|
||||
assert.ok(stream.emit('data', buffers.readyForQuery()));
|
||||
|
||||
var query = client.query('!');
|
||||
test('stream got packet', function() {
|
||||
assert.length(stream.packets, 1);
|
||||
});
|
||||
|
||||
stream.emit('data', buffers.rowDescription([{
|
||||
name: 'id',
|
||||
dataTypeID: dataTypes.char,
|
||||
dataTypeSize: 1
|
||||
}]));
|
||||
|
||||
var rowData = [];
|
||||
query.on('row',function(data) {
|
||||
rowData = data;
|
||||
});
|
||||
|
||||
stream.emit('data', buffers.dataRow(["!"]));
|
||||
|
||||
|
||||
test('row has correct data', function() {
|
||||
assert.length(rowData, 1);
|
||||
assert.equal(rowData[0], "!");
|
||||
});
|
||||
|
||||
});
|
||||
@ -1,103 +0,0 @@
|
||||
require(__dirname+"/test-helper");
|
||||
|
||||
|
||||
var queryResult = function(dataTypeID, value) {
|
||||
var stream = new MemoryStream();
|
||||
|
||||
var client = new Client({
|
||||
stream: stream
|
||||
});
|
||||
|
||||
client.connect();
|
||||
|
||||
var query = client.query('whatever');
|
||||
|
||||
var lastRow = [];
|
||||
|
||||
query.on('row', function(row) {
|
||||
lastRow = row;
|
||||
});
|
||||
|
||||
stream.emit('data', buffers.readyForQuery());
|
||||
|
||||
stream.emit('data', buffers.rowDescription([{
|
||||
name: 'col',
|
||||
dataTypeID: dataTypeID
|
||||
}]));
|
||||
|
||||
stream.emit('data', buffers.dataRow([value]));
|
||||
|
||||
stream.emit('data', buffers.commandComplete());
|
||||
|
||||
assert.length(lastRow, 1);
|
||||
|
||||
return lastRow[0];
|
||||
};
|
||||
|
||||
var testForType = function(nameAsString, typeID, stringVal, expected) {
|
||||
test(nameAsString, function() {
|
||||
var result = queryResult(typeID, stringVal);
|
||||
assert.strictEqual(result, expected);
|
||||
return result;
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
test('parses character types', function() {
|
||||
testForType('character (char)', 18, 'xyz', 'xyz');
|
||||
testForType('character varying (varchar)', 1043, 'xyz!', 'xyz!');
|
||||
testForType('text', 25, 'asdfasdf asdf', 'asdfasdf asdf');
|
||||
});
|
||||
|
||||
test('parses numeric types', function() {
|
||||
testForType('bigInt (int8)', 20, "1234567890", 1234567890);
|
||||
testForType('integer (int4)', 23, '1234567', 1234567);
|
||||
testForType('smallint (int2)', 21, '123', 123);
|
||||
testForType('numeric (decimal)', 1700, '123.456', 123.456);
|
||||
testForType('real (float4)', 700, '123.457', 123.457);
|
||||
testForType('doubl precision (float8)', 701, '123.4567', 123.4567);
|
||||
testForType('oid', 26, '1038', 1038);
|
||||
});
|
||||
|
||||
test('parses binary data types', function() {
|
||||
//TODO
|
||||
});
|
||||
|
||||
test('parses date/time', function() {
|
||||
test('time', function() {
|
||||
var result = queryResult(1083, '07:37:16-05');
|
||||
assert.equal(result.getHours(), 7);
|
||||
assert.equal(result.getMinutes(), 37);
|
||||
assert.equal(result.getSeconds(), 16);
|
||||
});
|
||||
|
||||
test('time with timezone (timetz)', function() {
|
||||
var result = queryResult(1266, '07:37:16-05');
|
||||
//this is not recommended
|
||||
//and i'm not sure how to handle time with timezone (timez)
|
||||
//please see
|
||||
//http://www.postgresql.org/docs/7.4/interactive/datatype-datetime.html
|
||||
//section 8.5.3
|
||||
assert.equal(result.getHours(), 7);
|
||||
assert.equal(result.getMinutes(), 37);
|
||||
assert.equal(result.getSeconds(), 16);
|
||||
});
|
||||
|
||||
test('timestamp (withouth timezone)', function() {
|
||||
return false;
|
||||
var result = queryResult(1114, '1997-12-17 07:37:16-05');
|
||||
console.log(result);
|
||||
assert.equal(result.getFullYear(), 1997);
|
||||
});
|
||||
|
||||
test('timestamp (timestamptz)', function() {
|
||||
return false;
|
||||
var result = queryResult(1184, '1997-12-17 07:37:16-05');
|
||||
|
||||
console.log(result);
|
||||
assert.equal(result.getFullYear(), 1997);
|
||||
|
||||
});
|
||||
// testForType('timestamp', 1114);
|
||||
// testForType('timestampz', 1184);
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user