Browse Source

Handle conditional arrays

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

11
index.js

@ -62,8 +62,15 @@ function addCondition (q, field, val) {
// SQL operator
val = [ val[0], field ].concat(val.slice(1));
} else {
// other cases like ( '>', '10' ) Greater than 10
val = [ 'AND', field ].concat(val);
// 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 {
// other cases like ( '>', '10' ) Greater than 10
val = [ 'AND', field ].concat(val);
}
}
}
var args = val[0].includes('RAW') ? [ '"'+val[1]+'" ' + val[2] ] : val.slice(1)

5
test.js

@ -138,6 +138,11 @@ var conditions= [
input: [ { f1: { $raw: '@> ANY(ARRAY(1,2,3))' } }, { 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