mirror of
https://github.com/harish2704/knex-json-query.git
synced 2026-09-10 12:51:08 +00:00
Merge pull request #2 from joselcvarela/master
Add raw statement for special handling like postgres operators
This commit is contained in:
@@ -25,6 +25,10 @@ var functionOperatorMap = {
|
||||
AND_NOTIN: 'andWhereNotIn',
|
||||
AND_ISNULL: 'andWhereNull',
|
||||
AND_NOTNULL: 'andWhereNotNull',
|
||||
/* ---- */
|
||||
RAW: 'whereRaw',
|
||||
OR_RAW: 'orWhereRaw',
|
||||
AND_RAW: 'whereRaw',
|
||||
};
|
||||
|
||||
var aliases = {
|
||||
@@ -57,12 +61,20 @@ function addCondition (q, field, val) {
|
||||
if (functionOperatorMap.hasOwnProperty( val[0] ) ) {
|
||||
// SQL operator
|
||||
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 {
|
||||
// other cases like ( '>', '10' ) Greater than 10
|
||||
val = [ 'AND', field ].concat(val);
|
||||
}
|
||||
}
|
||||
return q[functionOperatorMap[val[0]]].apply(q, val.slice(1));
|
||||
}
|
||||
var args = val[0].includes('RAW') ? [ '"'+val[1]+'" ' + val[2] ] : val.slice(1)
|
||||
return q[functionOperatorMap[val[0]]].apply(q, args);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -72,6 +72,11 @@ var conditions= [
|
||||
input:{ f1: ['NOTIN', [ 50, 60 ] ], f2: 20, f3: 30 },
|
||||
output: 'select * where ("f1" not in (50, 60) and "f2" = 20 and "f3" = 30)'
|
||||
},
|
||||
{
|
||||
name: 'handle simple and condition with raw statement',
|
||||
input: { f1: { $raw: '@> ANY(ARRAY(1,2,3))' }, f2: 20 },
|
||||
output: 'select * where ("f1" @> ANY(ARRAY(1,2,3)) and "f2" = 20)'
|
||||
},
|
||||
/* -----------Tests for or conditions---------- */
|
||||
{
|
||||
name: 'handle simple or condition',
|
||||
@@ -125,9 +130,19 @@ var conditions= [
|
||||
},
|
||||
{
|
||||
name: 'handle simple or condition with nin statement :MongoQuery',
|
||||
input:[ { f1: {$nin:[ 50, 60 ]} }, { f2: 20 }, { f3: 30 } ],
|
||||
input:[ { f1: { $nin: [ 50, 60 ] } }, { f2: 20 }, { f3: 30 } ],
|
||||
output: 'select * where (("f1" not in (50, 60)) or ("f2" = 20) or ("f3" = 30))'
|
||||
},
|
||||
{
|
||||
name: 'handle simple or condition with raw statement',
|
||||
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%\'))'
|
||||
}
|
||||
];
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user