Skip to content

Instantly share code, notes, and snippets.

@jesslilly
Created February 5, 2014 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesslilly/8827206 to your computer and use it in GitHub Desktop.
Save jesslilly/8827206 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
var util = require('util');
util.puts("Convert tree to query!");
var tree = {
type : "and",
left : {
type : "eq",
left : {
type : "property",
name : "City"
},
right : {
type : "literal",
value : "Seattle"
}
},
right : {
type : "and",
left : {
type : "eq",
left : {
type : "property",
name : "State"
},
right : {
type : "literal",
value : "Washington"
}
},
right : {
type : "and",
left : {
type : "eq",
left : {
type : "property",
name : "FirstName"
},
right : {
type : "literal",
value : "John"
}
},
right : {
type : "eq",
left : {
type : "property",
name : "LastName"
},
right : {
type : "literal",
value : "Doe"
}
}
}
}
};
var readNode = function(node) {
if (node.type === "eq") {
return "(" + node.left.name + " = '" + node.right.value + "')";
}
if (node.type === "and") {
return readNode(node.left) + " and " + readNode(node.right);
}
};
util.puts(readNode(tree));
var filter = function(querySchema) {
if (querySchema.type == 'and') {
// standard binary tree traversal recursion:
return filter(querySchema.left) + " and " + filter(querySchema.right);
} else if (querySchema.type == 'eq') {
return " " + querySchema.left.name + " = " + querySchema.right.value + " ";
}
}
util.puts(filter(tree));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment