Commit 04b72695 by Konstantin Käfer

add #map() function that returns a hash instead of an array

parent c7f3a8bd
......@@ -29,7 +29,7 @@ Database.prototype.run = function(sql) {
var statement = new Statement(this, sql, errorCallback(params));
statement.run.apply(statement, params).finalize();
return this;
}
};
// Database#get(sql, [bind1, bind2, ...], [callback])
Database.prototype.get = function(sql) {
......@@ -37,7 +37,7 @@ Database.prototype.get = function(sql) {
var statement = new Statement(this, sql, errorCallback(params));
statement.get.apply(statement, params).finalize();
return this;
}
};
// Database#all(sql, [bind1, bind2, ...], [callback])
Database.prototype.all = function(sql) {
......@@ -45,7 +45,7 @@ Database.prototype.all = function(sql) {
var statement = new Statement(this, sql, errorCallback(params));
statement.all.apply(statement, params).finalize();
return this;
}
};
// Database#each(sql, [bind1, bind2, ...], [callback], [complete])
Database.prototype.each = function(sql) {
......@@ -53,7 +53,40 @@ Database.prototype.each = function(sql) {
var statement = new Statement(this, sql, errorCallback(params));
statement.each.apply(statement, params).finalize();
return this;
}
};
Database.prototype.map = function(sql) {
var params = Array.prototype.slice.call(arguments, 1);
var statement = new Statement(this, sql, errorCallback(params));
statement.map.apply(statement, params).finalize();
return this;
};
Statement.prototype.map = function() {
var params = Array.prototype.slice.call(arguments);
var callback = params.pop();
params.push(function(err, rows) {
if (err) return callback(err);
var result = {};
if (rows.length) {
var keys = Object.keys(rows[0]), key = keys[0];
if (keys.length > 2) {
// Value is an object
for (var i = 0; i < rows.length; i++) {
result[rows[i][key]] = rows[i];
}
} else {
var value = keys[1];
// Value is a plain value
for (var i = 0; i < rows.length; i++) {
result[rows[i][key]] = rows[i][value];
}
}
}
callback(err, result);
});
return this.all.apply(this, params);
};
var isVerbose = false;
......@@ -66,6 +99,7 @@ sqlite3.verbose = function() {
trace.extendTrace(Database.prototype, 'run');
trace.extendTrace(Database.prototype, 'all');
trace.extendTrace(Database.prototype, 'each');
trace.extendTrace(Database.prototype, 'map');
trace.extendTrace(Database.prototype, 'exec');
trace.extendTrace(Database.prototype, 'close');
trace.extendTrace(Statement.prototype, 'bind');
......@@ -73,6 +107,7 @@ sqlite3.verbose = function() {
trace.extendTrace(Statement.prototype, 'run');
trace.extendTrace(Statement.prototype, 'all');
trace.extendTrace(Statement.prototype, 'each');
trace.extendTrace(Statement.prototype, 'map');
trace.extendTrace(Statement.prototype, 'reset');
trace.extendTrace(Statement.prototype, 'finalize');
isVerbose = true;
......
var sqlite3 = require('sqlite3');
var assert = require('assert');
if (process.setMaxListeners) process.setMaxListeners(0);
exports['test Database#map() with two columns'] = function(beforeExit) {
var db = new sqlite3.Database(':memory:');
var count = 10;
var inserted = 0;
var retrieved = false;
db.serialize(function() {
db.run("CREATE TABLE foo (id INT, value TEXT)");
var stmt = db.prepare("INSERT INTO foo VALUES(?, ?)");
for (var i = 5; i < count; i++) {
stmt.run(i, 'Value for ' + i, function(err) {
if (err) throw err;
inserted++;
});
}
stmt.finalize();
db.map("SELECT * FROM foo", function(err, map) {
retrieved = true;
assert.deepEqual(map, { 5: 'Value for 5', 6: 'Value for 6', 7: 'Value for 7', 8: 'Value for 8', 9: 'Value for 9' });
});
});
beforeExit(function() {
assert.equal(inserted, 5);
assert.ok(retrieved);
});
};
exports['test Database#map() with three columns'] = function(beforeExit) {
var db = new sqlite3.Database(':memory:');
var count = 10;
var inserted = 0;
var retrieved = false;
db.serialize(function() {
db.run("CREATE TABLE foo (id INT, value TEXT, other TEXT)");
var stmt = db.prepare("INSERT INTO foo VALUES(?, ?, ?)");
for (var i = 5; i < count; i++) {
stmt.run(i, 'Value for ' + i, null, function(err) {
if (err) throw err;
inserted++;
});
}
stmt.finalize();
db.map("SELECT * FROM foo", function(err, map) {
retrieved = true;
assert.deepEqual(map, {
5: { id: 5, value: 'Value for 5', other: null },
6: { id: 6, value: 'Value for 6', other: null },
7: { id: 7, value: 'Value for 7', other: null },
8: { id: 8, value: 'Value for 8', other: null },
9: { id: 9, value: 'Value for 9', other: null }
});
});
});
beforeExit(function() {
assert.equal(inserted, 5);
assert.ok(retrieved);
});
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment