Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

inserts now working on queries

  • Loading branch information...
commit 1123b543080a3af7a47cb01cec15b4a2eee01f5f 1 parent 451772d
chrisdew authored
Showing with 68 additions and 6 deletions.
  1. +25 −4 lib/query.js
  2. +1 −1  test/database-test.js
  3. +42 −1 test/demo-test.js
View
29 lib/query.js
@@ -22,21 +22,23 @@ function Query(table, spec) {
table.on('delta', function(delta) {
console.log('received delta from table', delta);
- that.emit('delta', delta);
+ that.handleDelta(delta);
});
- var whereFn = function() { return true; };
+ this.whereFn = function() { return true; };
if (spec.where && spec.where.expr) {
- whereFn = createFn(spec.where.expr);
+ this.whereFn = createFn(spec.where.expr);
}
var ret = [];
for (var p in table.rowsByPk) {
+ // TODO: check that p is increasing in numerical, not alpha numerical, order
var row = table.rowsByPk[p];
- if (whereFn(row)) {
+ if (this.whereFn(row)) {
ret.push(row);
}
}
+ this.rows = ret;
// we make a separate init event, rather than making them all deltas, as
// there needs to be a clear divide beween the
@@ -50,6 +52,25 @@ function Query(table, spec) {
util.inherits(Query, events.EventEmitter);
+Query.prototype.handleDelta = function(delta) {
+ if (delta.op === 'update') {
+ this.emit('delta', delta);
+ } else if (delta.op === 'insert') {
+ console.log('delta op="insert"', delta);
+ if (this.whereFn(delta.row)) {
+ var insertIndex;
+ for (insertIndex = 0; insertIndex < this.rows.length; insertIndex++) {
+ if (this.rows.pk > delta.row.pk) break;
+ }
+ var newDelta = JSON.parse(JSON.stringify(delta));
+ newDelta.pos = insertIndex;
+ this.emit('delta', newDelta);
+ }
+ } else {
+ this.emit('error', 'unhandled delta');
+ }
+}
+
function createFn(expr) {
console.log("createFn", expr);
View
2  test/database-test.js
@@ -59,7 +59,7 @@ describe('Database', function() {
console.log('delta received');
console.log("initilised", initialised);
assert(initialised);
- assert.deepEqual({"op":"insert","table":"mytable","row":{"_version":1,"id":2,"foo":"world"}}, delta);
+ assert.deepEqual({"op":"insert","table":"mytable","row":{"_version":1,"id":2,"foo":"world"},"pos":1}, delta);
done();
});
//console.log('query', query.constructor, JSON.stringify(query));
View
43 test/demo-test.js
@@ -27,7 +27,7 @@ describe('Session', function() {
});
var db2 = new database.Database();
var sess2 = new session.Session(db2);
- it('should run a complex realtime script', function(done) {
+ it('should run a complex realtime script2', function(done) {
sess2.exec('create table events (id integer primary key auto_increment, name varchar, start integer, duration integer)', function(err, res) {
assert.equal('Table created.', res);
sess2.exec('insert into events (name, start, duration) values ("dentist", 1000, 100)', function(err, res) {
@@ -52,6 +52,47 @@ console.log("AAA", query);
query.once('delta', function(delta) {
console.log("delta", delta);
assert(initialised);
+ assert.deepEqual({"op":"insert","table":"events","row":{"_version":1,"name":"barber","start":3000,"duration":100}, "pos":2}
+, delta);
+ done();
+ });
+ });
+ });
+ });
+ });
+ });
+ var db3 = new database.Database();
+ var sess3 = new session.Session(db3);
+ it('should run a complex realtime script3', function(done) {
+ sess3.exec('create table events (id integer primary key auto_increment, name varchar, start integer, duration integer)', function(err, res) {
+ assert.equal('Table created.', res);
+ sess3.exec('insert into events (name, start, duration) values ("dentist", 1000, 100)', function(err, res) {
+ assert.equal('1 row inserted.', res);
+ sess3.exec('insert into events (name, start, duration) values ("doctor", 3000, 100)', function(err, res) {
+ assert.equal('1 row inserted.', res);
+ sess3.exec('insert into events (name, start, duration) values ("accountant", 5000, 100)', function(err, res) {
+ assert.equal('1 row inserted.', res);
+ var query = sess3.exec('push * from events where start >= 1000 and start < 4000');
+ var initialised = false;
+console.log("AAA", query);
+ query.once('init', function(data) {
+ console.log("init", data);
+ assert.deepEqual([ { _version: 1, name: 'dentist', start: 1000, duration: 100 },
+ { _version: 1, name: 'doctor', start: 3000, duration: 100 } ], data)
+ initialised = true;
+ sess3.exec('insert into events (name, start, duration) values ("mechanic", 4000, 100)', function(err, res) {
+ assert.equal('1 row inserted.', res);
+ sess3.exec('insert into events (name, start, duration) values ("barber", 3000, 100)', function(err, res) {
+ assert.equal('1 row inserted.', res);
+ console.log("got here");
+ });
+ });
+ });
+ query.on('delta', function(delta) {
+ console.log("delta", delta);
+ assert(initialised);
+ assert.deepEqual({"op":"insert","table":"events","row":{"_version":1,"name":"barber","start":3000,"duration":100},"pos":2}
+, delta);
done();
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.