Skip to content
Browse files

Compiler fixes for deps and insert

  • Loading branch information...
1 parent 181af20 commit 9e8cbf99683d5abf59e84a21591494520a069d6e @s3u s3u committed
Showing with 76 additions and 44 deletions.
  1. +43 −44 modules/compiler/lib/compiler.js
  2. +33 −0 modules/compiler/test/insert-test.js
View
87 modules/compiler/lib/compiler.js
@@ -53,9 +53,10 @@ function plan(compiled) {
var ret, single, count = 0;
var comments = [];
var creates = {};
+ var maxid = 0;
for(i = 0; i < compiled.length; i++) {
line = compiled[i];
-
+ maxid = line.id > maxid ? line.id : maxid;
// Collect the statements and assign them to the following non-comment.
if(line.type === 'comment') {
comments.push(line);
@@ -98,7 +99,7 @@ function plan(compiled) {
ret = {
type: 'return',
line: single.line,
- id: single.id,
+ id: maxid + 1,
rhs: single
}
}
@@ -107,7 +108,7 @@ function plan(compiled) {
ret = {
type: 'return',
line: 1,
- id: 0,
+ id: maxid + 1,
rhs: {
object: {},
type: 'define',
@@ -117,8 +118,7 @@ function plan(compiled) {
};
}
}
-// var util = require('util');
-// console.log(util.inspect(compiled, false, 10));
+ var util = require('util');
// Start with the return statement and create the plan.
walk(ret, symbols, creates);
@@ -150,11 +150,7 @@ function walk(line, symbols, creates) {
else if(line.rhs.ref) {
dependency = symbols[line.rhs.ref];
if(dependency) {
- line.dependsOn.push(dependency);
- if(dependency.type === 'create') {
- delete creates[dependency.id.toString()];
- }
- walk(dependency, symbols, creates);
+ addDep(line.dependsOn, dependency, symbols, creates);
}
}
else if(line.rhs) {
@@ -168,6 +164,12 @@ function walk(line, symbols, creates) {
walk(line.fallback, symbols, creates);
}
break;
+ case 'insert':
+ introspectFrom(line, [line.source], symbols, creates);
+ if(line.fallback) {
+ walk(line.fallback, symbols, creates);
+ }
+ break;
case 'select':
introspectFrom(line, line.fromClause, symbols, creates);
if(line.joiner) {
@@ -178,8 +180,23 @@ function walk(line, symbols, creates) {
walk(line.fallback, symbols, creates);
}
break;
- case 'describe':
+ }
+}
+function addDep(dependsOn, dependency, symbols, creates) {
+ var contains = false;
+ for(var i = 0; i < dependsOn.length; i++) {
+ contains = _.isEqual(dependsOn[i], dependency);
+ if(contains) {
+ break;
+ }
+ }
+ if(!contains) {
+ dependsOn.push(dependency);
+ if(dependency.type === 'create') {
+ delete creates[dependency.id.toString()];
+ }
+ walk(dependency, symbols, creates);
}
}
@@ -204,11 +221,12 @@ function introspectString(v, symbols, creates, dependsOn) {
}
}
if(!contains) {
- dependsOn.push(dependency);
- if(dependency.type === 'create') {
- delete creates[dependency.id.toString()];
- }
- walk(dependency, symbols, creates);
+ addDep(dependsOn, dependency, symbols, creates);
+// dependsOn.push(dependency);
+// if(dependency.type === 'create') {
+// delete creates[dependency.id.toString()];
+// }
+// walk(dependency, symbols, creates);
}
}
});
@@ -259,15 +277,11 @@ function introspectFrom(line, froms, symbols, creates, parent) {
}
else {
if(parent) {
- parent.dependsOn.push(dependency);
+ addDep(parent.dependsOn, dependency, symbols, creates);
}
else {
- line.dependsOn.push(dependency);
- }
- if(dependency.type === 'create') {
- delete creates[dependency.id.toString()];
+ addDep(line.dependsOn, dependency, symbols, creates);
}
- walk(dependency, symbols, creates);
}
}
}
@@ -280,15 +294,11 @@ function introspectFrom(line, froms, symbols, creates, parent) {
}
else {
if(parent) {
- parent.dependsOn.push(dependency);
+ addDep(parent.dependsOn, dependency, symbols, creates);
}
else {
- line.dependsOn.push(dependency);
+ addDep(line.dependsOn, dependency, symbols, creates);
}
- if(dependency.type === 'create') {
- delete creates[dependency.id.toString()];
- }
- walk(dependency, symbols, creates);
}
}
}
@@ -316,11 +326,7 @@ function introspectWhere(line, symbols, creates) {
throw new this.SyntaxError('Circular reference ' + line.assign);
}
else {
- line.dependsOn.push(dependency);
- if(dependency.type === 'create') {
- delete creates[dependency.id.toString()];
- }
- walk(dependency, symbols, creates);
+ addDep(line.dependsOn, dependency, symbols, creates);
}
}
}
@@ -343,11 +349,7 @@ function introspectWhere(line, symbols, creates) {
throw new this.SyntaxError('Circular reference ' + line.assign);
}
else {
- line.dependsOn.push(dependency);
- if(dependency.type === 'create') {
- delete creates[dependency.id.toString()];
- }
- walk(dependency, symbols, creates);
+ addDep(line.dependsOn, dependency, symbols, creates);
}
}
}
@@ -364,16 +366,13 @@ function introspectWhere(line, symbols, creates) {
throw new this.SyntaxError('Circular reference ' + line.assign);
}
else {
- line.dependsOn.push(dependency);
- if(dependency.type === 'create') {
- delete creates[dependency.id.toString()];
- }
- walk(dependency, symbols, creates);
+ addDep(line.dependsOn, dependency, symbols, creates);
}
}
else {
- throw new this.SyntaxError('User defined function ' + where.name + ' not resolved');
+ throw new this.SyntaxError('UDF ' + where.name + ' not resolved')
}
+ break;
}
}
}
View
33 modules/compiler/test/insert-test.js
@@ -162,3 +162,36 @@ exports['insert-timeout'] = function(test) {
test.done();
};
+
+exports['insert-obj'] = function(test) {
+ var q = 'obj = {\n\
+ "p3" : "v3",\n\
+ "p4" : "v4"\n\
+ };\n\
+ updated = insert into obj (p5, p6) values ("v5", "v6");\n\
+ return updated;'
+
+ var plan = compiler.compile(q);
+ test.deepEqual(plan, { type: 'return',
+ line: 6,
+ id: 2,
+ rhs: { ref: 'updated' },
+ dependsOn:
+ [ { type: 'insert',
+ source: { name: '{obj}' },
+ values: [ 'v5', 'v6' ],
+ line: 5,
+ columns:
+ [ { type: 'column', name: 'p5' },
+ { type: 'column', name: 'p6' } ],
+ assign: 'updated',
+ id: 1,
+ dependsOn:
+ [ { object: { p3: 'v3', p4: 'v4' },
+ type: 'define',
+ line: 1,
+ assign: 'obj',
+ id: 0,
+ dependsOn: [] } ] } ] });
+ test.done();
+}

0 comments on commit 9e8cbf9

Please sign in to comment.
Something went wrong with that request. Please try again.