Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: chrisdew/subsql
base: 6a876621b9
...
head fork: chrisdew/subsql
compare: 51bf71376c
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Commits on Nov 22, 2012
chrisdew renamed parse-test 34a107e
Commits on Nov 23, 2012
chrisdew parses complex select where caluse a11e923
Commits on Nov 27, 2012
chrisdew added function construction 51bf713
View
2  lib/grammar.pegjs
@@ -54,7 +54,7 @@ Value
/ String
Integer
- = head:([1-9]) tail:([0-9]*) { tail.unshift(head); return parseInt(tail.join(''), 10); }
+ = head:([0-9]) tail:([0-9]*) { tail.unshift(head); return parseInt(tail.join(''), 10); }
String
= "\"" value:([a-z]*) "\"" { return value.join(''); }
View
46 lib/table.js
@@ -3,6 +3,7 @@
var util = require('util');
var events = require('events');
var row = require('./row');
+var assert = require('assert');
exports.hello = hello;
exports.Table = Table;
@@ -77,6 +78,7 @@ Table.prototype.update = function(spec) {
for (var pk in this.rowsByPk) {
var r = this.rowsByPk[pk];
+ console.log("evaluate", r, evaluate(r));
if (evaluate(r)) {
count++;
r.update(spec.set);
@@ -87,17 +89,43 @@ Table.prototype.update = function(spec) {
function createFn(expr) {
console.log("createFn", expr);
+ if (typeof(expr) === "string") {
+ return function() {
+ return expr;
+ }
+ }
+ if (typeof(expr) === "number") {
+ return function() {
+ return expr;
+ }
+ }
+ if (expr.field) {
+ return function(r) {
+ return r[expr.field];
+ }
+ }
+
+ // by here we assume that it's a 2-arg function
+ assert(expr.fn);
+ assert(expr.args.length === 2);
+ var fn0 = createFn(expr.args[0]);
+ var fn1 = createFn(expr.args[1]);
if (expr.fn === 'equal') {
- return createEqual(expr.args);
+ return function(r) {
+ return fn0(r) === fn1(r);
+ }
}
-}
-
-function createEqual(args) {
- console.log("createEqual", args);
- if (args[0].field && (typeof args[1] === 'string' || typeof args[1] === 'number')) {
- return function(r) { return r[args[0].field] === args[1]; };
+ if (expr.fn === 'and') {
+ return function(r) {
+ return fn0(r) && fn1(r);
+ }
}
- if (args[1].field && (typeof args[0] === 'string' || typeof args[0] === 'number')) {
- return function(r) { return r[args[1].field] === args[0]; };
+ if (expr.fn === 'gt') {
+ return function(r) {
+ return fn0(r) > fn1(r);
+ }
}
+ throw "cannot handle " + JSON.stringify(expr);
}
+
+
View
4 test/parser-test.js → test/grammar-test.js
@@ -60,4 +60,8 @@ This test takes 25s to run! All the others take 149ms in total.
assert.deepEqual({"selectStar":{"from":["bar"],"where":{"expr":{"fn":"and","args":[{"fn":"equal","args":[{"field":"x"},1]},{"fn":"lt","args":[{"field":"y"},{"fn":"now","args":[]}]}]}}}},
parser.parse('select * from bar where x = 1 and y < now()'));
});
+ it('select * from bar where x = 1 and x > 0', function() {
+ assert.deepEqual({"selectStar":{"from":["bar"],"where":{"expr":{"fn":"and","args":[{"fn":"equal","args":[{"field":"x"},1]},{"fn":"gt","args":[{"field":"x"},0]}]}}}},
+ parser.parse('select * from bar where x = 1 and x > 0'));
+ });
});
View
17 test/table-test.js
@@ -59,5 +59,22 @@ describe('Table', function() {
bar.update({"table":"bar","set":[{"field":"foo","expr":"world"}],"where":{"expr":{"fn":"equal","args":[1, {field:"id"}]}}});
});
+ it('can handle a nested where clause', function(done) {
+ bar.once('delta', function(delta) {
+ assert.deepEqual({"op":"update","table":"bar","pk":"1","set":[{"field":"foo","expr":"world"}]}, delta);
+ done();
+ });
+ bar.update({"table":"bar","set":[{"field":"foo","expr":"world"}],"where":{"expr":{"fn":"equal","args":[1, {field:"id"}]}}});
+ });
+
+ it('can handle a deeply nested where clause', function(done) {
+ bar.once('delta', function(delta) {
+ assert.deepEqual({"op":"update","table":"bar","pk":"1","set":[{"field":"foo","expr":"world2"}]}, delta);
+ done();
+ });
+ bar.update({"table":"bar","set":[{"field":"foo","expr":"world2"}],"where":{"expr":{"fn":"and","args":[{"fn":"equal","args":[{"field":"id"},1]},{"fn":"gt","args":[{"field":"id"},0]}]}}});
+ console.log(JSON.stringify(bar));
+ });
+
});

No commit comments for this range

Something went wrong with that request. Please try again.