Skip to content

Commit

Permalink
added selectStar
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdew committed Nov 28, 2012
1 parent be7d58f commit 419e0ae
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
10 changes: 10 additions & 0 deletions lib/database.js
@@ -1,5 +1,6 @@
// This class represents a Database

var assert = require('assert');
var util = require('util');
var events = require('events');
var table = require('./table');
Expand Down Expand Up @@ -36,12 +37,21 @@ Database.prototype.insert = function(spec, callback) {
return this;
}

Database.prototype.selectStar = function(spec, callback) {
// TODO: handle multi table selects
assert(spec.from.length === 1);
var table = this.tablesByName[spec.from[0]];
if (!table) return callback("Error: table '" + spec.from[0] + "' does not exist.");
table.selectStar(callback);
return this;
}

// command is a Javascript object, the result of parsing a line
Database.prototype.exec = function(command, callback) {
//console.log(command);
for (var p in command) { // there will only be one
if (p === 'createTable') this.createTable(command[p], callback);
if (p === 'insert') this.insert(command[p], callback);
if (p === 'selectStar') this.selectStar(command[p], callback);
}
}
5 changes: 1 addition & 4 deletions lib/session.js
Expand Up @@ -43,19 +43,16 @@ Session.prototype.exec = function(line, callback) {
, 'Syntax error: ' + e.message
].join('\n'));
}
try {
this.db.once('createTable', function() {
console.log('emitting result Table created.');
return callback(null, 'Table created.');
});
this.db.exec(command, function(err, res) {
console.log("command", command);
console.log("err", err);
console.log("res", res);
callback(err, res);
});
} catch (e) {
return callback(e.message);
}
}

// the unregisters all the resources this session is using
Expand Down
10 changes: 10 additions & 0 deletions lib/table.js
Expand Up @@ -87,6 +87,16 @@ Table.prototype.update = function(spec) {
}
}

Table.prototype.selectStar = function(callback) {
console.log("Table::selectStar");

var ret = [];
for (var p in this.rowsByPk) {
ret.push(this.rowsByPk[p]);
}
callback(null, ret);
}

function createFn(expr) {
console.log("createFn", expr);
if (typeof(expr) === "string") {
Expand Down
4 changes: 4 additions & 0 deletions test/grammar-test.js
Expand Up @@ -39,6 +39,10 @@ describe('grammar', function() {
assert.deepEqual({"select":{"exprs":[{"fn":"func","args":[2]}]}},
parser.parse('select func(2)'));
});
it('select * from bar', function() {
assert.deepEqual({"selectStar":{from:["bar"]}},
parser.parse('select * from bar'));
});
/*
This test takes 25s to run! All the others take 149ms in total.
Expand Down
6 changes: 6 additions & 0 deletions test/session-test.js
Expand Up @@ -47,6 +47,12 @@ describe('Session', function() {
done();
});
});
it('should execute a query', function(done) {
sess2.exec('select * from bar', function(err, res) {
assert.deepEqual([{"_version":1,"id":1,"foo":"hello"}], res);
done();
});
});

});

0 comments on commit 419e0ae

Please sign in to comment.