Permalink
Browse files

added logic

  • Loading branch information...
1 parent 8d00ec2 commit 328bf6457dfb28e68a13e3ad4161071fbb18f631 @chrisdew committed Nov 21, 2012
Showing with 69 additions and 20 deletions.
  1. +37 −14 lib/grammar.pegjs
  2. +7 −1 lib/row.js
  3. +21 −4 test/parser-test.js
  4. +4 −1 test/session-test.js
View
@@ -1,11 +1,19 @@
start
- = "select" _ el:ExprList fc:FromClause
+ = "select" _ "*" fc:FromClause _ wc:WhereClause
+ { return {selectStar:{from:fc,where:wc}}; }
+ / "select" _ "*" fc:FromClause
+ { return {selectStar:{from:fc}}; }
+ / "push" _ "*" fc:FromClause _ wc:WhereClause
+ { return {pushStar:{from:fc,where:wc}}; }
+ / "push" _ "*" fc:FromClause
+ { return {pushStar:{from:fc}}; }
+ / "select" _ el:ExprList fc:FromClause
{ return {select:{exprs:el,from:fc}}; }
/ "select" _ el:ExprList
{ return {select:{exprs:el}}; }
- / "subscribe" _ el:ExprList fc:FromClause?
- { return {subscribe:{exprs:el,from:fc}}; }
+ / "push" _ el:ExprList fc:FromClause?
+ { return {push:{exprs:el,from:fc}}; }
/ "create" _ "table" _ tn:TableName _? "(" _? lines:CreateLines _? ")"
{ return {createTable:{name:tn, fields:lines}}; }
/ "use" _ DatabaseName
@@ -26,8 +34,8 @@ SetPair
{ return {field:f,expr:e}; }
WhereClause
- = "where" _? e:Expr
- { return e; }
+ = "where" _ b:Bool
+ { return {expr:b}; }
FieldList
= head:FieldName tail:( _? "," _? FieldName)*
@@ -79,21 +87,32 @@ ExprList
for (var i in tail) result.push(tail[i][3]);
return result; }
-/*
-Expr
- = f:FieldName _? "=" _? v:Value
- { return {expr:{op:"=",field:f,value:v}}; }
- / Identifier
- / Value
-*/
Expr
= b:Bool
{ return {expr:b}; }
/ Additive
Bool
- = arg0:Primary _? "=" _? arg1:Primary
+ = arg0:Bool1 _ "or" _ arg1:Bool1
+ { return {fn:"or",args:[arg0,arg1]}; }
+ / Bool1
+
+Bool1
+ = arg0:Bool2 _ "and" _ arg1:Bool2
+ { return {fn:"and",args:[arg0,arg1]}; }
+ / Bool2
+
+Bool2
+ = arg0:Additive _? "=" _? arg1:Additive
{ return {fn:"equal",args:[arg0,arg1]}; }
+ / arg0:Additive _? ">" _? arg1:Additive
+ { return {fn:"gt",args:[arg0,arg1]}; }
+ / arg0:Additive _? ">=" _? arg1:Additive
+ { return {fn:"gte",args:[arg0,arg1]}; }
+ / arg0:Additive _? "<" _? arg1:Additive
+ { return {fn:"lt",args:[arg0,arg1]}; }
+ / arg0:Additive _? "<=" _? arg1:Additive
+ { return {fn:"lte",args:[arg0,arg1]}; }
Additive
= arg0:Multiplicative _? "+" _? arg1:Additive
@@ -111,11 +130,15 @@ Multiplicative
Primary
= Function
- / FieldName
+ / FieldRef
/ Value
FieldName
= Identifier
+
+FieldRef
+ = fn:Identifier
+ { return {field:fn}; }
Function
= fn:Identifier "(" el:ExprList ")"
View
@@ -15,4 +15,10 @@ function Row(fields, values) {
}
}
-util.inherits(Row, events.EventEmitter);
+util.inherits(Row, events.EventEmitter);
+
+Row.prototype.update = function(pairs) {
+ for (var i in pairs) {
+ this[pairs[i].field] = pairs[i].expr;
+ }
+}
View
@@ -5,7 +5,7 @@ var fs = require('fs');
var parser = peg.buildParser(fs.readFileSync('lib/grammar.pegjs', 'utf8'));
describe('grammar', function() {
it('select id, foo from bar', function() {
- assert.deepEqual({"select":{"exprs":["id","foo"],"from":["bar"]}}, parser.parse('select id, foo from bar'));
+ assert.deepEqual({"select":{"exprs":[{field:"id"},{field:"foo"}],"from":["bar"]}}, parser.parse('select id, foo from bar'));
});
it('create table bar (id integer primary key auto_increment, foo varchar)', function() {
assert.deepEqual({"createTable":{name:'bar',fields:[{"field":"id","type":"integer","pk":true,"ai":true},{"field":"foo","type":"varchar"}]}},
@@ -16,7 +16,7 @@ describe('grammar', function() {
parser.parse('insert into bar (id, foo) values (1, "hello")'));
});
it('update bar set foo="hello" where id = 1', function() {
- assert.deepEqual({"update":{"table":"bar","set":[{"field":"foo","expr":"hello"}],"where":{"expr":{"fn":"equal","args":["id",1]}}}},
+ assert.deepEqual({"update":{"table":"bar","set":[{"field":"foo","expr":"hello"}],"where":{"expr":{"fn":"equal","args":[{field:"id"},1]}}}},
parser.parse('update bar set foo="hello" where id=1'));
});
it('select 423', function() {
@@ -39,8 +39,25 @@ describe('grammar', function() {
assert.deepEqual({"select":{"exprs":[{"fn":"func","args":[2]}]}},
parser.parse('select func(2)'));
});
+/*
+
+This test takes 25s to run! All the others take 149ms in total.
+
it('select func(func(2+2, 6), 10, foo)', function() {
- assert.deepEqual({"select":{"exprs":[{"fn":"func","args":[{"fn":"func","args":[{"fn":"add","args":[2,2]},6]},10,"foo"]}]}},
+ assert.deepEqual({"select":{"exprs":[{"fn":"func","args":[{"fn":"func","args":[{"fn":"add","args":[2,2]},6]},10,{field:"foo"}]}]}},
parser.parse('select func(func(2+2, 6), 10, foo)'));
});
-});
+*/
+ it('select * from bar', function() {
+ assert.deepEqual({"selectStar":{"from":["bar"]}},
+ parser.parse('select * from bar'));
+ });
+ it('push * from bar', function() {
+ assert.deepEqual({"pushStar":{"from":["bar"]}},
+ parser.parse('push * from bar'));
+ });
+ it('select * from bar where x = 1 and y < now()', function() {
+ 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()'));
+ });
+});
View
@@ -23,8 +23,11 @@ describe('Session', function() {
var sess = new session.Session(db);
it('should event an error when executing nonsense', function(done) {
sess.exec('select three sheep from a field', function(err, res) {
- assert.equal(' ^\nSyntax error: Expected "*", "+", ",", "-", "/", "=" or "from" but "s" found.',
+ //console.log("!" + err + "!");
+ assert.equal(' ^\nSyntax error: Expected "*", "+", ",", "-", "/", "<", "<=", "=", ">", ">=" or "from" but "s" found.',
err);
+/*
+*/
done();
});
});

0 comments on commit 328bf64

Please sign in to comment.