Comparing version 1.0.0 to 1.0.1
@@ -7,3 +7,2 @@ var crypto = require('crypto'); | ||
var Query = require(__dirname + '/query'); | ||
var utils = require(__dirname + '/utils'); | ||
var defaults = require(__dirname + '/defaults'); | ||
@@ -10,0 +9,0 @@ var Connection = require(__dirname + '/connection'); |
@@ -9,2 +9,4 @@ var net = require('net'); | ||
var TEXT_MODE = 0; | ||
var BINARY_MODE = 1; | ||
var Connection = function(config) { | ||
@@ -23,2 +25,3 @@ EventEmitter.call(this); | ||
this._ending = false; | ||
this._mode = TEXT_MODE; | ||
}; | ||
@@ -479,3 +482,3 @@ | ||
for(var i = 0; i < msg.fieldCount; i++){ | ||
fields[i] = this.parseField(); | ||
fields.push(this.parseField()); | ||
} | ||
@@ -494,4 +497,11 @@ msg.fields = fields; | ||
dataTypeModifier: this.parseInt32(), | ||
format: this.parseInt16() === 0 ? 'text' : 'binary' | ||
format: undefined | ||
}; | ||
if(this.parseInt16() === TEXT_MODE) { | ||
this._mode = TEXT_MODE; | ||
field.format = 'text'; | ||
} else { | ||
this._mode = BINARY_MODE; | ||
field.format = 'binary'; | ||
} | ||
return field; | ||
@@ -505,3 +515,11 @@ }; | ||
var length = this.parseInt32(); | ||
fields[i] = (length === -1 ? null : this.readBytes(length)); | ||
var value = null; | ||
if(length !== -1) { | ||
if(this._mode === TEXT_MODE) { | ||
value = this.readString(length); | ||
} else { | ||
value = this.readBytes(length); | ||
} | ||
} | ||
fields.push(value); | ||
} | ||
@@ -561,7 +579,9 @@ msg.fieldCount = fieldCount; | ||
Connection.prototype.parseGH = function (msg) { | ||
msg.binary = Boolean(this.parseInt8()); | ||
var isBinary = this.buffer[this.offset] !== 0; | ||
this.offset++; | ||
msg.binary = isBinary; | ||
var columnCount = this.parseInt16(); | ||
msg.columnTypes = []; | ||
for(var i = 0; i<columnCount; i++) { | ||
msg.columnTypes[i] = this.parseInt16(); | ||
msg.columnTypes.push(this.parseInt16()); | ||
} | ||
@@ -571,14 +591,8 @@ return msg; | ||
Connection.prototype.parseInt8 = function () { | ||
var value = Number(this.buffer[this.offset]); | ||
this.offset++; | ||
return value; | ||
}; | ||
Connection.prototype.readChar = function() { | ||
return Buffer([this.buffer[this.offset++]]).toString(this.encoding); | ||
return this.readString(1); | ||
}; | ||
Connection.prototype.parseInt32 = function() { | ||
var value = this.peekInt32(); | ||
var value = this.buffer.readInt32BE(this.offset, true); | ||
this.offset += 4; | ||
@@ -588,20 +602,10 @@ return value; | ||
Connection.prototype.peekInt32 = function(offset) { | ||
offset = offset || this.offset; | ||
var buffer = this.buffer; | ||
return ((buffer[offset++] << 24) + | ||
(buffer[offset++] << 16) + | ||
(buffer[offset++] << 8) + | ||
buffer[offset++]); | ||
}; | ||
Connection.prototype.parseInt16 = function() { | ||
return ((this.buffer[this.offset++] << 8) + | ||
(this.buffer[this.offset++] << 0)); | ||
var value = this.buffer.readInt16BE(this.offset, true); | ||
this.offset += 2; | ||
return value; | ||
}; | ||
Connection.prototype.readString = function(length) { | ||
return this.buffer.toString(this.encoding, this.offset, | ||
(this.offset += length)); | ||
return this.buffer.toString(this.encoding, this.offset, (this.offset += length)); | ||
}; | ||
@@ -615,3 +619,3 @@ | ||
var start = this.offset; | ||
while(this.buffer[this.offset++]) { } | ||
while(this.buffer[this.offset++] !== 0) { } | ||
return this.buffer.toString(this.encoding, start, this.offset - 1); | ||
@@ -618,0 +622,0 @@ }; |
@@ -11,3 +11,3 @@ //result object returned from query | ||
var matchRegexp = /([A-Za-z]+) (\d+ )?(\d+)?/; | ||
var matchRegexp = /([A-Za-z]+) ?(\d+ )?(\d+)?/; | ||
@@ -14,0 +14,0 @@ //adds a command complete message |
{ | ||
"name": "pg", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "PostgreSQL client - pure javascript & libpq with the same API", | ||
@@ -25,3 +25,4 @@ "keywords": [ | ||
"devDependencies": { | ||
"jshint": "1.1.0" | ||
"jshint": "1.1.0", | ||
"semver": "~1.1.4" | ||
}, | ||
@@ -28,0 +29,0 @@ "scripts": { |
#node-postgres | ||
[![Build Status](https://secure.travis-ci.org/brianc/node-postgres.png)](http://travis-ci.org/brianc/node-postgres) | ||
[![Build Status](https://secure.travis-ci.org/brianc/node-postgres.png?branch=master)](http://travis-ci.org/brianc/node-postgres) | ||
@@ -5,0 +5,0 @@ PostgreSQL client for node.js. Pure JavaScript and native libpq bindings. |
@@ -8,17 +8,25 @@ var helper = require(__dirname + '/test-helper'); | ||
var sleepQuery = 'select pg_sleep(5)'; | ||
client.query(sleepQuery, assert.calls(function(err, result) { | ||
assert(err); | ||
client.end(); | ||
})); | ||
var client2 = new Client(helper.args); | ||
client2.connect(assert.success(function() { | ||
var killIdleQuery = "SELECT procpid, (SELECT pg_terminate_backend(procpid)) AS killed FROM pg_stat_activity WHERE current_query = $1"; | ||
client2.query(killIdleQuery, [sleepQuery], assert.calls(function(err, res) { | ||
assert.ifError(err); | ||
assert.equal(res.rowCount, 1); | ||
client2.end(); | ||
assert.emits(client2, 'end'); | ||
var pidColName = 'procpid' | ||
var queryColName = 'current_query'; | ||
helper.versionGTE(client, '9.2.0', assert.success(function(isGreater) { | ||
if(isGreater) { | ||
pidColName = 'pid'; | ||
queryColName = 'query'; | ||
} | ||
client.query(sleepQuery, assert.calls(function(err, result) { | ||
assert(err); | ||
client.end(); | ||
})); | ||
var client2 = new Client(helper.args); | ||
client2.connect(assert.success(function() { | ||
var killIdleQuery = "SELECT " + pidColName + ", (SELECT pg_terminate_backend(" + pidColName + ")) AS killed FROM pg_stat_activity WHERE " + queryColName + " = $1"; | ||
client2.query(killIdleQuery, [sleepQuery], assert.calls(function(err, res) { | ||
assert.ifError(err); | ||
assert.equal(res.rows.length, 1); | ||
client2.end(); | ||
assert.emits(client2, 'end'); | ||
})); | ||
})); | ||
})); | ||
})); | ||
}); |
@@ -8,27 +8,29 @@ var helper = require(__dirname + "/test-helper"); | ||
client.query("CREATE TEMP TABLE zugzug(name varchar(10))", assert.calls(function(err, result) { | ||
assert.isNull(err); | ||
assert.equal(result.oid, null); | ||
assert.equal(result.command, 'CREATE'); | ||
helper.versionGTE(client, '9.0.0', assert.success(function(hasRowCount) { | ||
client.query("CREATE TEMP TABLE zugzug(name varchar(10))", assert.calls(function(err, result) { | ||
assert.isNull(err); | ||
assert.equal(result.oid, null); | ||
assert.equal(result.command, 'CREATE'); | ||
var q = client.query("INSERT INTO zugzug(name) VALUES('more work?')", assert.calls(function(err, result) { | ||
assert.equal(result.command, "INSERT"); | ||
assert.equal(result.rowCount, 1); | ||
var q = client.query("INSERT INTO zugzug(name) VALUES('more work?')", assert.calls(function(err, result) { | ||
assert.equal(result.command, "INSERT"); | ||
assert.equal(result.rowCount, 1); | ||
client.query('SELECT * FROM zugzug', assert.calls(function(err, result) { | ||
assert.isNull(err); | ||
assert.equal(result.rowCount, 1); | ||
assert.equal(result.command, 'SELECT'); | ||
process.nextTick(pg.end.bind(pg)); | ||
client.query('SELECT * FROM zugzug', assert.calls(function(err, result) { | ||
assert.isNull(err); | ||
if(hasRowCount) assert.equal(result.rowCount, 1); | ||
assert.equal(result.command, 'SELECT'); | ||
process.nextTick(pg.end.bind(pg)); | ||
})); | ||
})); | ||
})); | ||
assert.emits(q, 'end', function(result) { | ||
assert.equal(result.command, "INSERT"); | ||
assert.equal(result.rowCount, 1); | ||
done(); | ||
}); | ||
assert.emits(q, 'end', function(result) { | ||
assert.equal(result.command, "INSERT"); | ||
if(hasRowCount) assert.equal(result.rowCount, 1); | ||
done(); | ||
}); | ||
})); | ||
})); | ||
})); | ||
}); |
@@ -8,3 +8,2 @@ var helper = require(__dirname + "/../test-helper"); | ||
var killIdleQuery = 'SELECT procpid, (SELECT pg_terminate_backend(procpid)) AS killed FROM pg_stat_activity WHERE current_query LIKE \'<IDLE>\''; | ||
@@ -14,18 +13,27 @@ //get first client | ||
client.id = 1; | ||
pg.connect(helper.config, assert.success(function(client2, done2) { | ||
client2.id = 2; | ||
done2(); | ||
//subscribe to the pg error event | ||
assert.emits(pg, 'error', function(error, brokenClient) { | ||
assert.ok(error); | ||
assert.ok(brokenClient); | ||
assert.equal(client.id, brokenClient.id); | ||
}); | ||
//kill the connection from client | ||
client2.query(killIdleQuery, assert.success(function(res) { | ||
//check to make sure client connection actually was killed | ||
assert.lengthIs(res.rows, 1); | ||
pg.end(); | ||
pg.connect(helper.config, assert.success(function(client2, done2) { | ||
client2.id = 2; | ||
var pidColName = 'procpid' | ||
helper.versionGTE(client2, '9.2.0', assert.success(function(isGreater) { | ||
var killIdleQuery = 'SELECT pid, (SELECT pg_terminate_backend(pid)) AS killed FROM pg_stat_activity WHERE state = $1'; | ||
var params = ['idle']; | ||
if(!isGreater) { | ||
killIdleQuery = 'SELECT procpid, (SELECT pg_terminate_backend(procpid)) AS killed FROM pg_stat_activity WHERE current_query LIKE $1'; | ||
params = ['%IDLE%'] | ||
} | ||
done2(); | ||
//subscribe to the pg error event | ||
assert.emits(pg, 'error', function(error, brokenClient) { | ||
assert.ok(error); | ||
assert.ok(brokenClient); | ||
assert.equal(client.id, brokenClient.id); | ||
}); | ||
//kill the connection from client | ||
client2.query(killIdleQuery, params, assert.success(function(res) { | ||
//check to make sure client connection actually was killed | ||
assert.lengthIs(res.rows, 1); | ||
pg.end(); | ||
})); | ||
})); | ||
})); | ||
})); | ||
})); |
@@ -16,4 +16,13 @@ var helper = require(__dirname + '/../test-helper'); | ||
var semver = require('semver'); | ||
helper.versionGTE = function(client, versionString, callback) { | ||
client.query('SELECT version()', assert.calls(function(err, result) { | ||
if(err) return callback(err); | ||
var version = result.rows[0].version.split(' ')[1]; | ||
return callback(null, semver.gte(version, versionString)); | ||
})); | ||
}; | ||
//export parent helper stuffs | ||
module.exports = helper; | ||
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
264574
109
6992
2
200