Browse Source

Fix. Do not mutate the input raw Query

pull/1/head V0.0.3
Harish.K 9 years ago
parent
commit
e850edcdda
  1. 16
      index.js
  2. 10
      test.js

16
index.js

@ -6,6 +6,8 @@ var functionOperators = [
/* ---- */
'OR',
'AND',
'EQ',
'=',
/* ---- */
'OR_BETWEEN',
'OR_IN',
@ -20,6 +22,8 @@ var functionOperatorMap = {
/* ---- */
OR: 'orWhere',
AND: 'where',
EQ: 'where',
'=': 'where',
/* ---- */
OR_BETWEEN: 'orWhereBetween',
OR_IN: 'orWhereIn',
@ -33,19 +37,19 @@ var functionOperatorMap = {
function addCondition (q, field, val) {
if (Array.isArray(val[0])) {
return q.where(function () {
return val.forEach(addCondition.bind(null, this, field))
})
return val.forEach(addCondition.bind(null, this, field));
});
}
if (!Array.isArray(val)) {
val = ['AND', field, val ]
val = ['AND', field, val ];
} else if (functionOperators.indexOf(val[0]) !== -1) {
val.splice(1, 0, field)
val = [ val[0], field ].concat(val.slice(1));
} 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
test.js

@ -12,6 +12,16 @@ var conditions= [
input:{ f1: 10, f2: 20, 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',
input:{ f1: ['>', 10], f2: [ '<', 20 ], f3: 30 },

Loading…
Cancel
Save