Created
June 29, 2011 06:07
-
-
Save goatslacker/1053251 to your computer and use it in GitHub Desktop.
and/or sql builder
This file contains 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
<script type="text/javascript"> | |
var Search = { | |
opened: [], | |
closed: [], | |
or: [], | |
and: [], | |
DFS: function (tree) { | |
var branch = tree; | |
if (Object.prototype.toString.call(branch) === '[object Array]') { | |
for (var i = 0; i < branch.length; i = i + 1) { | |
this.DFS(branch[i]); | |
} | |
} else { | |
if ('and' in branch) { | |
//tmpArr.push(branch.and); | |
this.and = this.and.concat(branch.and); | |
this.DFS(branch.and); | |
} | |
if ('or' in branch) { | |
this.or = this.or.concat(branch.or); | |
this.DFS(branch.or); | |
} | |
if (!('or' in branch) && !('and' in branch)) { | |
this.closed.push(branch); | |
} | |
} | |
//} | |
// console.log(this.or); | |
// console.log(this.and); | |
} | |
} | |
setTimeout(function () { | |
//console.log(and(Search.closed)) // this is wrong... | |
}); | |
//WHERE (a = 1 and b = 2) or (c = 3 and d = 4); | |
console.log(or(and({ a: 1 }, { b: 2 }), and({ c: 3 }, { d: 4 }))); | |
// WHERE (a = 1 OR b = 2) | |
console.log(or({ a: 1 }, { b: 2 })); | |
// WHERE ((a = 1 AND b = 2) OR (c = 3 AND d = 4 AND e = 5) OR (a = 1 AND c = 3)) AND b = 2 | |
console.log(and(or(and({ a: 1 }, { b: 2 }), and({ c: 3 }, { d: 4 }, { e: 5 }), and({ a: 1 }, { c: 3 })), { b: 2})); | |
//WHERE ((suit = 'spades' or suit = 'clubs) and face = 'q') or ((suit = 'hearts' or suit = 'diamonds') and fade = 'j') | |
console.log(or(and(or({ suit: 'spades' }, { suit: 'clubs' }), { face: 'q' }), and(or({ suit: 'hearts' }, { suit: 'diamonds' }), { face: 'j' }))); | |
// ------------------------------------------ | |
// using objects | |
// ------------------------------------------ | |
/* | |
find ({ | |
or: [ { a: 1 }, { b: 2 } ], | |
and: [ { c: 3 }, { d: 4 } ], | |
{ b: 3 } | |
}); | |
*/ | |
// (a = 1 or b = 2) and (c = 3 and d = 4) and b = 3 // ??????? Ok. | |
// WHERE (a = 1 and b = 2) or (c = 3 and d = 4); | |
var testObj = { | |
or: [{ | |
and: [ | |
{ a: 1 }, | |
{ b: 2 }, | |
], | |
}, | |
{ | |
and: [ | |
{ c: 3 }, | |
{ d: 4 }, | |
] | |
}] | |
}; | |
//find(testObj); | |
function find () { | |
var obj = arguments[0]; | |
var dfs = []; | |
// what happens when there are two in the obj, it can only be one or the other, right? | |
// the and's get appended with and by the find method (hope this makes sense later) | |
if ("or" in obj) { | |
dfs = dfs.concat(obj.or); | |
// TODO check to see if it's an array or a single object | |
// for now we'll just assume its always an array and loop through it | |
// next step is to push this whole or into a dfs array | |
// ok now is the time for DFS! | |
// for (var i = 0; i < obj.or.length; i = i + 1) { | |
// console.log(obj.or[i]); | |
// } | |
} | |
if ("and" in obj) { | |
dfs = dfs.concat(obj.or); | |
// next step is to push this whole and into a dfs array | |
} | |
//Search.opened = dfs; | |
Search.DFS(obj); | |
// while loop goes here | |
// after while loop we have all our results. i think? | |
} | |
function or () { | |
var sql = []; | |
if (Object.prototype.toString.call(arguments[0]) === '[object Array]') { | |
arguments = arguments[0]; | |
} | |
for (var i = 0; i < arguments.length; i = i + 1) { | |
if (typeof(arguments[i]) === "object") { | |
// object | |
sql.push(parseObj(arguments[i])); | |
} else { | |
// string | |
sql.push(arguments[i]); | |
} | |
} | |
return "(" + sql.join(" OR ") + ")"; | |
} | |
function and () { | |
var sql = []; | |
if (Object.prototype.toString.call(arguments[0]) === '[object Array]') { | |
arguments = arguments[0]; | |
} | |
for (var i = 0; i < arguments.length; i = i + 1) { | |
if (typeof(arguments[i]) === "object") { | |
// object | |
sql.push(parseObj(arguments[i])); | |
} else { | |
// string | |
sql.push(arguments[i]); | |
} | |
} | |
return "(" + sql.join(" AND ") + ")"; | |
} | |
function parseObj (obj) { | |
for (var prop in obj) { | |
var item = prop + ' = ' + obj[prop]; | |
} | |
return item; | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment