1 Commits
Author SHA1 Message Date
harish2704 e850edcdda Fix. Do not mutate the input raw Query 2017-02-09 16:11:32 +05:30
2 changed files with 20 additions and 6 deletions
+10 -6
View File
@@ -6,6 +6,8 @@ var functionOperators = [
/* ---- */ /* ---- */
'OR', 'OR',
'AND', 'AND',
'EQ',
'=',
/* ---- */ /* ---- */
'OR_BETWEEN', 'OR_BETWEEN',
'OR_IN', 'OR_IN',
@@ -20,6 +22,8 @@ var functionOperatorMap = {
/* ---- */ /* ---- */
OR: 'orWhere', OR: 'orWhere',
AND: 'where', AND: 'where',
EQ: 'where',
'=': 'where',
/* ---- */ /* ---- */
OR_BETWEEN: 'orWhereBetween', OR_BETWEEN: 'orWhereBetween',
OR_IN: 'orWhereIn', OR_IN: 'orWhereIn',
@@ -33,19 +37,19 @@ var functionOperatorMap = {
function addCondition (q, field, val) { function addCondition (q, field, val) {
if (Array.isArray(val[0])) { if (Array.isArray(val[0])) {
return q.where(function () { return q.where(function () {
return val.forEach(addCondition.bind(null, this, field)) return val.forEach(addCondition.bind(null, this, field));
}) });
} }
if (!Array.isArray(val)) { if (!Array.isArray(val)) {
val = ['AND', field, val ] val = ['AND', field, val ];
} else if (functionOperators.indexOf(val[0]) !== -1) { } else if (functionOperators.indexOf(val[0]) !== -1) {
val.splice(1, 0, field) val = [ val[0], field ].concat(val.slice(1));
} else { } else {
val = [ 'AND', field ].concat(val) val = [ 'AND', field ].concat(val);
} }
return q[functionOperatorMap[val[0]]].apply(q, val.slice(1)) return q[functionOperatorMap[val[0]]].apply(q, val.slice(1));
} }
+10
View File
@@ -12,6 +12,16 @@ var conditions= [
input:{ f1: 10, f2: 20, f3: 30 }, input:{ f1: 10, f2: 20, f3: 30 },
output: 'select * where ("f1" = 10 and "f2" = 20 and "f3" = 30)' output: 'select * where ("f1" = 10 and "f2" = 20 and "f3" = 30)'
}, },
{
name: 'handle simple and conditions',
input:{"firstName":[["LIKE","H%"],["OR","hemanth"]]},
output: 'select * where (("firstName" LIKE \'H%\' or "firstName" = \'hemanth\'))'
},
{
name: 'handle simple and conditions',
input:{ f1: [[ 'A' ],['LIKE', 'B' ]], f2: 20, f3: 30 },
output: 'select * where (("f1" = \'A\' and "f1" LIKE \'B\') and "f2" = 20 and "f3" = 30)'
},
{ {
name: 'handle basic operators :: greater-than less-than', name: 'handle basic operators :: greater-than less-than',
input:{ f1: ['>', 10], f2: [ '<', 20 ], f3: 30 }, input:{ f1: ['>', 10], f2: [ '<', 20 ], f3: 30 },