Created
February 5, 2014 16:15
-
-
Save jesslilly/8827206 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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