diff --git a/package.json b/package.json index b3b1280..2f7f32c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rsql-mongodb", - "version": "1.4.0", + "version": "1.5.0", "description": "Converting RSQL queries to MongoDB queries", "main": "rsql-mongodb.js", "typings": "rsql-mongodb.ts", diff --git a/rsql-mongodb.js b/rsql-mongodb.js index 718d530..a39fe7c 100644 --- a/rsql-mongodb.js +++ b/rsql-mongodb.js @@ -77,9 +77,10 @@ module.exports = function (input) { lastLogical = logicalsTab[logicalsTab.length - 1]; } - + // Push the character into 'logicalsTab' logicalsTab.push(character); + } } @@ -93,7 +94,13 @@ module.exports = function (input) { } // Else push the character into the 'logicalsTab' else{ + + // Push all operator presents in 'logicalsTab' into 'outputTab' + while(logicalsTab.length > 0) { + outputTab.push(logicalsTab.pop()); + } logicalsTab.push(character); + outputTab.push(character); } } @@ -120,6 +127,7 @@ module.exports = function (input) { // Remove the open parenthesis from 'logicalsTab' logicalsTab.pop(); + outputTab.push(character); } } // If the character is not an operator push it into the 'outputString' buffer @@ -148,7 +156,9 @@ module.exports = function (input) { // Define variables var mongoStack = []; var mongoQuery = []; - + var tmpPrecedence = []; + var lastLogical = ""; + var lastLogicalBeforePrecedence = "" for(var i = 0; i < outputTab.length; i++) { @@ -160,7 +170,7 @@ module.exports = function (input) { switch(outputTab[i]){ case ";": case ",": - if(i == (outputTab.length -1) || (mongoStack.length == 1 && mongoQuery.length == 1)){ + if(i == (outputTab.length -1) || (mongoQuery.length == 1)){ while(mongoQuery.length > 0) { tmpArray.push(mongoQuery.shift()) } @@ -169,10 +179,12 @@ module.exports = function (input) { tmpArray.push(mongoStack.shift()) } if(outputTab[i] == ";"){ - newValue['$and'] = tmpArray; + lastLogical = '$and'; + newValue[lastLogical] = tmpArray; } else{ - newValue['$or'] = tmpArray; + lastLogical = '$or'; + newValue[lastLogical] = tmpArray; } break; default: @@ -182,6 +194,19 @@ module.exports = function (input) { mongoQuery.push(newValue); } + else if( outputTab[i] == '('){ + tmpPrecedence = mongoQuery.shift(); + lastLogicalBeforePrecedence = lastLogical; + } + else if( outputTab[i] == ')'){ + if(tmpPrecedence){ + tmpPrecedence[lastLogicalBeforePrecedence].push(mongoQuery.shift()); + mongoQuery.push(tmpPrecedence); + + }else{ + + } + } else{ // Verify if the is no injections diff --git a/test.js b/test.js index 23a47e9..ebcace9 100644 --- a/test.js +++ b/test.js @@ -76,6 +76,8 @@ describe('rsql-mongodb', function () { it("Test other cases", function () { expect(rsqlMongoDB('firstName==john,firstName==janne,firstName==jim')).to.deep.include({$or:[{"firstName":"john"},{"firstName":"janne"},{"firstName":"jim"}]}); expect(rsqlMongoDB('firstName==john,firstName==janne,firstName==jim;lastName==doe')).to.deep.include({$and:[{$or:[{"firstName":"john"},{"firstName":"janne"},{"firstName":"jim"}]},{"lastName":"doe"}]}); + expect(rsqlMongoDB('a==1;(b==2,c==3)')).to.deep.include({"$and":[{"a":1},{"$or":[{"b":2},{"c":3}]}]}); + expect(rsqlMongoDB('(b==2,c==3);a==1')).to.deep.include({"$and":[{"$or":[{"b":2},{"c":3}]},{"a":1}]}); }); it("Test errors", function () { expect(function () { rsqlMongoDB('azerty') }).to.throw('Wrong RSQL query. No operator found.');