Browse Source

Handle conditional arrays

pull/2/head
Jose Varela 7 years ago
committed by joselcvarela
parent
commit
4422e9e424
  1. 7
      index.js
  2. 5
      test.js

7
index.js

@ -61,11 +61,18 @@ function addCondition (q, field, val) {
if (functionOperatorMap.hasOwnProperty( val[0] ) ) { if (functionOperatorMap.hasOwnProperty( val[0] ) ) {
// SQL operator // SQL operator
val = [ val[0], field ].concat(val.slice(1)); val = [ val[0], field ].concat(val.slice(1));
} else {
// Cases when we have something like 'OR_ILIKE' or 'AND_@>'
var operators = /(\w+)_(\w+)/.exec(val[0])
var operatorsExist = operators && operators.constructor === Array && operators.length >= 3
if (operatorsExist) {
val = [operators[1], field].concat([operators[2]], val.slice(1));
} else { } else {
// other cases like ( '>', '10' ) Greater than 10 // other cases like ( '>', '10' ) Greater than 10
val = [ 'AND', field ].concat(val); val = [ 'AND', field ].concat(val);
} }
} }
}
var args = val[0].includes('RAW') ? [ '"'+val[1]+'" ' + val[2] ] : val.slice(1) var args = val[0].includes('RAW') ? [ '"'+val[1]+'" ' + val[2] ] : val.slice(1)
return q[functionOperatorMap[val[0]]].apply(q, args); return q[functionOperatorMap[val[0]]].apply(q, args);
} }

5
test.js

@ -138,6 +138,11 @@ var conditions= [
input: [ { f1: { $raw: '@> ANY(ARRAY(1,2,3))' } }, { f2: 20 } ], input: [ { f1: { $raw: '@> ANY(ARRAY(1,2,3))' } }, { f2: 20 } ],
output: 'select * where (("f1" @> ANY(ARRAY(1,2,3))) or ("f2" = 20))' output: 'select * where (("f1" @> ANY(ARRAY(1,2,3))) or ("f2" = 20))'
}, },
{
name: 'handle simple or condition with conditional array',
input: { f1: [['ILIKE', 'awesome'], ['OR_ILIKE', '%super%'] ] },
output: 'select * where (("f1" ILIKE \'awesome\' or "f1" ILIKE \'%super%\'))'
}
]; ];

Loading…
Cancel
Save