Navigation Menu

Skip to content

Commit

Permalink
Format results to a hash if "_key" column is used
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Aug 9, 2012
1 parent a6a0975 commit 335e046
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions lib/wrapped-nroonga.js
Expand Up @@ -118,26 +118,43 @@ function formatResults(results) {
return [];

var columns = {};
var keyColumnIndex = -1;
results[0].forEach(function(column, index) {
columns[index] = column[0];
if (column[0] == '_key')
keyColumnIndex = index;
});

var hashResults = [];
results.slice(1).forEach(function(result) {
var hashResult = {};
result.forEach(function(column, index) {
hashResult[columns[index]] = column;
var hashResults;
if (keyColumnIndex > -1) {
hashResults = {};
results.slice(1).forEach(function(result) {
var hashResult = {};
result.forEach(function(column, index) {
if (index == keyColumnIndex) return;
hashResult[columns[index]] = column;
});
hashResults[result[keyColumnIndex]] = hashResult;
});
hashResults.push(hashResult);
});
return hashResults;
return hashResults;
} else {
hashResults = [];
results.slice(1).forEach(function(result) {
var hashResult = {};
result.forEach(function(column, index) {
hashResult[columns[index]] = column;
});
hashResults.push(hashResult);
});
return hashResults;
}
}
exports.formatResults = formatResults;

function formatSelectResult(result) {
return {
count: result[0][0],
results: formatResults(result.slice(1))
count: result[0][0][0],
results: formatResults(result[0].slice(1))
};
}
exports.formatSelectResult = formatSelectResult;

0 comments on commit 335e046

Please sign in to comment.