Rows are now associative arrays rather than straight arrays.

This commit is contained in:
Julian Birch 2010-12-19 09:41:11 +00:00
parent 1237db1f4a
commit 721cf56eb3
5 changed files with 34 additions and 2 deletions

BIN
lib/.query.js.swp Normal file

Binary file not shown.

View File

@ -43,7 +43,7 @@ p.submit = function(connection) {
}; };
}; };
var handleDatarow = function(msg) { var handleDatarow = function(msg) {
var result = []; var result = {};
for(var i = 0; i < msg.fields.length; i++) { for(var i = 0; i < msg.fields.length; i++) {
var rawValue = msg.fields[i]; var rawValue = msg.fields[i];
result[names[i]] = rawValue === null ? null : converters[i](rawValue); result[names[i]] = rawValue === null ? null : converters[i](rawValue);

View File

@ -4,7 +4,7 @@ test("simple query interface", function() {
var client = helper.client(); var client = helper.client();
var query = client.query("select name from person"); var query = client.query("select name from person order by name");
client.on('drain', client.end.bind(client)); client.on('drain', client.end.bind(client));
@ -12,6 +12,17 @@ test("simple query interface", function() {
query.on('row', function(row) { query.on('row', function(row) {
rows.push(row['name']) rows.push(row['name'])
}); });
query.once('row', function(row) {
test('Can iterate through columns', function () {
var columnCount = 0;
for (column in row) {
columnCount++;
};
if ('length' in row) {
assert.length(row, columnCount, 'Iterating through the columns gives a different length from calling .length.');
}
});
});
assert.emits(query, 'end', function() { assert.emits(query, 'end', function() {
test("returned right number of rows", function() { test("returned right number of rows", function() {

View File

@ -10,6 +10,17 @@ module.exports = {
host: helper.args.host, host: helper.args.host,
port: helper.args.port port: helper.args.port
}); });
client.on('error', function(e, d) {
console.log(e);
});
var rawQuery = client.query;
client.query = function() {
var q = rawQuery.apply(this, arguments);
q.on('error', function(e) {
console.log(e);
});
return q;
};
client.connect(); client.connect();
return client; return client;
}, },

View File

@ -10,6 +10,15 @@ buffers = require(__dirname + '/test-buffers');
Connection = require('connection'); Connection = require('connection');
var args = require(__dirname + '/cli'); var args = require(__dirname + '/cli');
process.on('uncaughtException', function(d) {
if ('stack' in d && 'message' in d) {
console.log("Message: " + d.message);
console.log(d.stack);
} else {
console.log(d);
}
});
assert.same = function(actual, expected) { assert.same = function(actual, expected) {
for(var key in expected) { for(var key in expected) {
assert.equal(actual[key], expected[key]); assert.equal(actual[key], expected[key]);
@ -102,6 +111,7 @@ assert.isNull = function(item, message) {
test = function(name, action) { test = function(name, action) {
test.testCount ++; test.testCount ++;
console.log('\n' + name);
var result = action(); var result = action();
if(result === false) { if(result === false) {
test.ignored.push(name); test.ignored.push(name);