Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added selectStar

  • Loading branch information...
commit 419e0aee8e152fb716ae4145f2544f03eea2c49c 1 parent be7d58f
chrisdew authored
View
10 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');
@@ -36,6 +37,14 @@ 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) {
@@ -43,5 +52,6 @@ Database.prototype.exec = function(command, callback) {
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);
}
}
View
5 lib/session.js
@@ -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
View
10 lib/table.js
@@ -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") {
View
4 test/grammar-test.js
@@ -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.
View
6 test/session-test.js
@@ -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();
+ });
+ });
});
Please sign in to comment.
Something went wrong with that request. Please try again.