Comparing version 0.10.0 to 0.10.2
@@ -169,4 +169,5 @@ var crypto = require('crypto'); | ||
} | ||
else if (client.queryQueue.indexOf(query) != -1) | ||
else if (client.queryQueue.indexOf(query) != -1) { | ||
client.queryQueue.splice(client.queryQueue.indexOf(query), 1); | ||
} | ||
}; | ||
@@ -189,19 +190,8 @@ | ||
p.query = function(config, values, callback) { | ||
//can take in strings or config objects | ||
config = (typeof(config) == 'string') ? { text: config } : config; | ||
if (this.binary && !('binary' in config)) { | ||
config.binary = true; | ||
//can take in strings, config object or query object | ||
var query = (config instanceof Query) ? config : new Query(config, values, callback); | ||
if (this.binary && !query.binary) { | ||
query.binary = true; | ||
} | ||
if(values) { | ||
if(typeof values === 'function') { | ||
callback = values; | ||
} else { | ||
config.values = values; | ||
} | ||
} | ||
config.callback = callback; | ||
var query = new Query(config); | ||
this.queryQueue.push(query); | ||
@@ -233,2 +223,5 @@ this._pulseQueryQueue(); | ||
// expose a Query constructor | ||
Client.Query = Query; | ||
module.exports = Client; |
@@ -16,2 +16,3 @@ var EventEmitter = require('events').EventEmitter; | ||
this.Connection = require(__dirname + '/connection'); | ||
this.Query = clientConstructor.Query | ||
this.defaults = defaults; | ||
@@ -18,0 +19,0 @@ }; |
@@ -53,6 +53,6 @@ //require the c++ bindings & export to javascript | ||
p.query = function(config, values, callback) { | ||
var q = new NativeQuery(config, values, callback); | ||
this._queryQueue.push(q); | ||
var query = (config instanceof NativeQuery) ? config : new NativeQuery(config, values, callback); | ||
this._queryQueue.push(query); | ||
this._pulseQueryQueue(); | ||
return q; | ||
return query; | ||
} | ||
@@ -175,2 +175,5 @@ | ||
// expose a Query constructor | ||
clientBuilder.Query = NativeQuery; | ||
module.exports = clientBuilder; |
@@ -9,31 +9,16 @@ var EventEmitter = require('events').EventEmitter; | ||
//event emitter proxy | ||
var NativeQuery = function(text, values, callback) { | ||
var NativeQuery = function(config, values, callback) { | ||
// use of "new" optional | ||
if (!(this instanceof NativeQuery)) return new NativeQuery(config, values, callback); | ||
EventEmitter.call(this); | ||
this.text = null; | ||
this.values = null; | ||
this.callback = null; | ||
this.name = null; | ||
config = utils.normalizeQueryConfig(config, values, callback); | ||
this.name = config.name; | ||
this.text = config.text; | ||
this.values = config.values; | ||
this.callback = config.callback; | ||
//allow 'config object' as first parameter | ||
if(typeof text == 'object') { | ||
this.text = text.text; | ||
this.values = text.values; | ||
this.name = text.name; | ||
if(typeof values === 'function') { | ||
this.callback = values; | ||
} else if(values) { | ||
this.values = values; | ||
this.callback = callback; | ||
} | ||
} else { | ||
this.text = text; | ||
this.values = values; | ||
this.callback = callback; | ||
if(typeof values == 'function') { | ||
this.values = null; | ||
this.callback = values; | ||
} | ||
} | ||
this.result = new Result(); | ||
this._result = new Result(); | ||
//normalize values | ||
@@ -63,5 +48,5 @@ if(this.values) { | ||
if(this.callback) { | ||
this.result.addRow(row); | ||
this._result.addRow(row); | ||
} | ||
this.emit('row', row, this.result); | ||
this.emit('row', row, this._result); | ||
}; | ||
@@ -79,10 +64,11 @@ | ||
p.handleReadyForQuery = function(meta) { | ||
if(meta) { | ||
this._result.addCommandComplete(meta); | ||
} | ||
if(this.callback) { | ||
this.result.command = meta.command.split(' ')[0]; | ||
this.result.rowCount = parseInt(meta.value); | ||
this.callback(null, this.result); | ||
this.callback(null, this._result); | ||
} | ||
this.emit('end'); | ||
this.emit('end', this._result); | ||
}; | ||
module.exports = NativeQuery; |
@@ -8,3 +8,8 @@ var EventEmitter = require('events').EventEmitter; | ||
var Query = function(config) { | ||
var Query = function(config, values, callback) { | ||
// use of "new" optional | ||
if (!(this instanceof Query)) return new Query(config, values, callback); | ||
config = utils.normalizeQueryConfig(config, values, callback); | ||
this.text = config.text; | ||
@@ -30,3 +35,14 @@ this.values = config.values; | ||
p.requiresPreparation = function() { | ||
return (this.values || 0).length > 0 || this.name || this.rows || this.binary; | ||
//named queries must always be prepared | ||
if(this.name) return true; | ||
//always prepare if there are max number of rows expected per | ||
//portal execution | ||
if(this.rows) return true; | ||
//don't prepare empty text queries | ||
if(!this.text) return false; | ||
//binary should be prepared to specify results should be in binary | ||
//unless there are no parameters | ||
if(this.binary && !this.values) return false; | ||
//prepare if there are values | ||
return (this.values || 0).length > 0; | ||
}; | ||
@@ -130,3 +146,5 @@ | ||
}, true); | ||
connection.parsedStatements[this.name] = true; | ||
if(this.name) { | ||
connection.parsedStatements[this.name] = true; | ||
} | ||
} | ||
@@ -133,0 +151,0 @@ |
@@ -17,3 +17,9 @@ //result object returned from query | ||
p.addCommandComplete = function(msg) { | ||
var match = matchRegexp.exec(msg.text); | ||
if(msg.text) { | ||
//pure javascript | ||
var match = matchRegexp.exec(msg.text); | ||
} else { | ||
//native bindings | ||
var match = matchRegexp.exec(msg.command); | ||
} | ||
if(match) { | ||
@@ -23,3 +29,4 @@ this.command = match[1]; | ||
if(match[3]) { | ||
this.rowCount = parseInt(match[3]); | ||
//msg.value is from native bindings | ||
this.rowCount = parseInt(match[3] || msg.value); | ||
this.oid = parseInt(match[2]); | ||
@@ -26,0 +33,0 @@ } else { |
@@ -108,2 +108,18 @@ var url = require('url'); | ||
function normalizeQueryConfig (config, values, callback) { | ||
//can take in strings or config objects | ||
config = (typeof(config) == 'string') ? { text: config } : config; | ||
if(values) { | ||
if(typeof values === 'function') { | ||
config.callback = values; | ||
} else { | ||
config.values = values; | ||
} | ||
} | ||
if (callback) { | ||
config.callback = callback; | ||
} | ||
return config; | ||
} | ||
module.exports = { | ||
@@ -116,3 +132,4 @@ normalizeConnectionInfo: normalizeConnectionInfo, | ||
parseConnectionString: parseConnectionString, | ||
prepareValue: prepareValue | ||
prepareValue: prepareValue, | ||
normalizeQueryConfig: normalizeQueryConfig | ||
} |
{ "name": "pg", | ||
"version": "0.10.0", | ||
"version": "0.10.2", | ||
"description": "PostgreSQL client - pure javascript & libpq with the same API", | ||
@@ -4,0 +4,0 @@ "keywords" : ["postgres", "pg", "libpq", "postgre", "database", "rdbms"], |
@@ -8,40 +8,40 @@ var helper = require(__dirname+"/test-helper"); | ||
var qry = client.query("select name from person order by name"); | ||
var qry = "select name from person order by name"; | ||
client.on('drain', client.end.bind(client)); | ||
var rows1 = 0, rows2 = 0, rows3 = 0, rows4 = 0; | ||
var rows1 = 0, rows2 = 0, rows3 = 0, rows4 = 0; | ||
var query1 = client.query(qry); | ||
query1.on('row', function(row) { | ||
rows1++; | ||
}); | ||
var query2 = client.query(qry); | ||
query2.on('row', function(row) { | ||
rows2++; | ||
}); | ||
var query3 = client.query(qry); | ||
query3.on('row', function(row) { | ||
rows3++; | ||
}); | ||
var query4 = client.query(qry); | ||
query4.on('row', function(row) { | ||
rows4++; | ||
}); | ||
var query1 = client.query(qry); | ||
query1.on('row', function(row) { | ||
rows1++; | ||
}); | ||
var query2 = client.query(qry); | ||
query2.on('row', function(row) { | ||
rows2++; | ||
}); | ||
var query3 = client.query(qry); | ||
query3.on('row', function(row) { | ||
rows3++; | ||
}); | ||
var query4 = client.query(qry); | ||
query4.on('row', function(row) { | ||
rows4++; | ||
}); | ||
helper.pg.cancel(helper.config, client, query1); | ||
helper.pg.cancel(helper.config, client, query2); | ||
helper.pg.cancel(helper.config, client, query4); | ||
helper.pg.cancel(helper.config, client, query1); | ||
helper.pg.cancel(helper.config, client, query2); | ||
helper.pg.cancel(helper.config, client, query4); | ||
setTimeout(function() { | ||
assert.equal(rows1, 0); | ||
assert.equal(rows2, 0); | ||
assert.equal(rows4, 0); | ||
}, 2000); | ||
setTimeout(function() { | ||
assert.equal(rows1, 0); | ||
assert.equal(rows2, 0); | ||
assert.equal(rows4, 0); | ||
}, 2000); | ||
assert.emits(query3, 'end', function() { | ||
test("returned right number of rows", function() { | ||
assert.equal(rows3, 26); | ||
}); | ||
}); | ||
test("returned right number of rows", function() { | ||
assert.equal(rows3, 26); | ||
}); | ||
}); | ||
}); |
@@ -8,7 +8,7 @@ var helper = require(__dirname+'/test-helper'); | ||
}); | ||
client.query({text: "", binary: false}); | ||
client.query({text: ""}); | ||
}); | ||
test('callback supported', assert.calls(function() { | ||
client.query({text: "", binary: false}, function(err, result) { | ||
client.query("", function(err, result) { | ||
assert.isNull(err); | ||
@@ -15,0 +15,0 @@ assert.empty(result.rows); |
@@ -7,2 +7,3 @@ var helper = require(__dirname + "/test-helper"); | ||
assert.isNull(err); | ||
client.query("CREATE TEMP TABLE zugzug(name varchar(10))", assert.calls(function(err, result) { | ||
@@ -12,5 +13,7 @@ assert.isNull(err); | ||
assert.equal(result.command, 'CREATE'); | ||
client.query("INSERT INTO zugzug(name) VALUES('more work?')", assert.calls(function(err, result) { | ||
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) { | ||
@@ -21,6 +24,12 @@ assert.isNull(err); | ||
process.nextTick(pg.end.bind(pg)); | ||
})) | ||
})) | ||
})) | ||
})) | ||
}) | ||
})); | ||
})); | ||
assert.emits(q, 'end', function(result) { | ||
assert.equal(result.command, "INSERT"); | ||
assert.equal(result.rowCount, 1); | ||
}); | ||
})); | ||
})); | ||
}); |
@@ -41,3 +41,3 @@ var helper = require(__dirname+"/test-helper"); | ||
var client = helper.client(); | ||
client.query({ text: "create temp table bang(id serial, name varchar(5));insert into bang(name) VALUES('boom');", binary: false }) | ||
client.query({ text: "create temp table bang(id serial, name varchar(5));insert into bang(name) VALUES('boom');"}) | ||
client.query("insert into bang(name) VALUES ('yes');"); | ||
@@ -56,5 +56,5 @@ var query = client.query("select name from bang"); | ||
var client = helper.client(); | ||
client.query({text: "create temp table boom(age integer); insert into boom(age) values(1); insert into boom(age) values(2); insert into boom(age) values(3)", binary: false}); | ||
client.query({text: "create temp table bang(name varchar(5)); insert into bang(name) values('zoom');", binary: false}); | ||
var result = client.query({text: "select age from boom where age < 2; select name from bang", binary: false}); | ||
client.query("create temp table boom(age integer); insert into boom(age) values(1); insert into boom(age) values(2); insert into boom(age) values(3)"); | ||
client.query({text: "create temp table bang(name varchar(5)); insert into bang(name) values('zoom');"}); | ||
var result = client.query({text: "select age from boom where age < 2; select name from bang"}); | ||
assert.emits(result, 'row', function(row) { | ||
@@ -61,0 +61,0 @@ assert.strictEqual(row['age'], 1); |
@@ -31,3 +31,3 @@ //make assert a global... | ||
var id = setTimeout(function() { | ||
test("Should have called " + eventName, function() { | ||
test("Should have called '" + eventName + "' event", function() { | ||
assert.ok(called, message || "Expected '" + eventName + "' to be called.") | ||
@@ -146,3 +146,4 @@ }); | ||
test.testCount ++; | ||
var result = action(); | ||
test[name] = action; | ||
var result = test[name](); | ||
if(result === false) { | ||
@@ -149,0 +150,0 @@ process.stdout.write('?'); |
@@ -166,1 +166,21 @@ require(__dirname + '/test-helper'); | ||
}); | ||
test('normalizing query configs', function() { | ||
var config | ||
var callback = function () {} | ||
config = utils.normalizeQueryConfig({text: 'TEXT'}) | ||
assert.same(config, {text: 'TEXT'}) | ||
config = utils.normalizeQueryConfig({text: 'TEXT'}, [10]) | ||
assert.deepEqual(config, {text: 'TEXT', values: [10]}) | ||
config = utils.normalizeQueryConfig({text: 'TEXT', values: [10]}) | ||
assert.deepEqual(config, {text: 'TEXT', values: [10]}) | ||
config = utils.normalizeQueryConfig('TEXT', [10], callback) | ||
assert.deepEqual(config, {text: 'TEXT', values: [10], callback: callback}) | ||
config = utils.normalizeQueryConfig({text: 'TEXT', values: [10]}, callback) | ||
assert.deepEqual(config, {text: 'TEXT', values: [10], callback: callback}) | ||
}) |
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
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
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
227520
6109
0