From 087ce3b2e4710200b2235ea683acfe1b7e87a455 Mon Sep 17 00:00:00 2001 From: oof1lab Date: Tue, 14 Nov 2017 17:52:55 +0530 Subject: [PATCH] Feature : 'in' comparison operator in where clause npm v0.2.8 --- README.md | 14 ++++---- lib/util/cmd.helper.js | 2 +- lib/util/whereClause.helper.js | 65 +++++++++++++++++++++++++++++----- package.json | 2 +- tests/tests.js | 24 ++++++------- 5 files changed, 78 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 042095ca3f..c84cb228ee 100644 --- a/README.md +++ b/README.md @@ -200,13 +200,15 @@ eg: gets all fields in table row but not checkNumber #### Comparison operators ``` -eq - '=' -ne - '!=' -gt - '>' -gte - '>=' -lt - '<' -lte - '<=' +eq - '=' - (colName,eq,colValue) +ne - '!=' - (colName,ne,colValue) +gt - '>' - (colName,ne,colValue) +gte - '>=' - (colName,ne,colValue) +lt - '<' - (colName,ne,colValue) +lte - '<=' - (colName,ne,colValue) +in - 'in' - (colName,ne,val1,val2,val3,val4) ``` + #### Use of comparison operators ``` /api/payments?_where=(checkNumber,eq,JM555205) diff --git a/lib/util/cmd.helper.js b/lib/util/cmd.helper.js index c13b8cf5c5..0e50ecf776 100644 --- a/lib/util/cmd.helper.js +++ b/lib/util/cmd.helper.js @@ -11,7 +11,7 @@ program.on('--help', () => { }) program - .version('0.2.7') + .version('0.2.8') .option('-h, --host ', 'hostname / localhost by default') .option('-u, --user ', 'username of database / root by default') .option('-p, --password ', 'password of database / empty by default') diff --git a/lib/util/whereClause.helper.js b/lib/util/whereClause.helper.js index 0adc034615..aeab4e7ad7 100644 --- a/lib/util/whereClause.helper.js +++ b/lib/util/whereClause.helper.js @@ -1,6 +1,43 @@ 'use strict'; -function replaceWhereParamsToQMarks(openParentheses, str) { +/** + * + * @param input : 1,2,3,4 + * @returns obj.query = (?,?,?,?) obj.params = [1,2,3,4] + */ +function prepareInClauseParams(input) { + + let inElems = input.split(',') + let obj = {} + obj.whereQuery = '' + obj.whereParams = [] + + for (var j = 0; j < inElems.length; ++j) { + + if (j === 0) { + //enclose with open parenthesis + obj.whereQuery += '(' + } + if (j) { + obj.whereQuery += ',' + } + // add q mark and push the variable + obj.whereQuery += '?' + obj.whereParams.push(inElems[j]) + + if (j === inElems.length - 1) { + //enclose with closing parenthesis + obj.whereQuery += ')' + } + } + + //console.log(obj); + + return obj + +} + +function replaceWhereParamsToQMarks(openParentheses, str, comparisonOperator) { let converted = '' @@ -14,7 +51,10 @@ function replaceWhereParamsToQMarks(openParentheses, str) { } } } else { - converted = '?' + + if (comparisonOperator !== ' in ') + converted = '?' + for (var i = str.length - 1; i >= 0; --i) { if (str[i] === ')') { converted += ')' @@ -22,6 +62,7 @@ function replaceWhereParamsToQMarks(openParentheses, str) { break; } } + } return converted; } @@ -78,9 +119,9 @@ function getComparisonOperator(operator) { // return ' not like ' // break; - // case 'in': - // return ' in ' - // break; + case 'in': + return ' in ' + break; default: return null @@ -191,22 +232,30 @@ exports.getConditionClause = function (whereInQueryParams, condType = 'where') { } whereQuery += comparisonOperator - // get the variableValue and push + // get the variableValue and push to params variableValue = result[2].match(/(.*?)\)/) if (!variableValue || variableValue.length !== 2) { grammarErr = 1; break; } - whereParams.push(variableValue[1]) + + if (comparisonOperator === ' in ') { + let obj = prepareInClauseParams(variableValue[1]) + whereQuery += obj.whereQuery + whereParams = whereParams.concat(obj.whereParams) + } else { + whereParams.push(variableValue[1]) + } // then replace the variableValue with ? - temp = replaceWhereParamsToQMarks(false, result[2]) + temp = replaceWhereParamsToQMarks(false, result[2], comparisonOperator) if (!temp) { grammarErr = 1; break; } whereQuery += temp + // only if (numOfConditions.length !== -1 && i !== numOfConditions.length - 1) { //console.log('finding logical operator for',logicalOperatorsInClause[i]); diff --git a/package.json b/package.json index bd518fef5a..bafe83ffdb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xmysql", - "version": "0.2.7", + "version": "0.2.8", "description": "One command to generate REST APIs for any MySql database", "main": "index.js", "scripts": { diff --git a/tests/tests.js b/tests/tests.js index 0a351c6130..8e7125af7f 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -17,7 +17,6 @@ var api = {} var mysqlPool = {} - //desribe group of tests done describe('xmysql : tests', function () { @@ -90,7 +89,6 @@ describe('xmysql : tests', function () { }); - it('GET /api/payments/count should PASS', function (done) { //http get an url @@ -336,11 +334,10 @@ describe('xmysql : tests', function () { }); - - it('GET /api/customers/findOne?_where=(customerNumber,eq,119) should PASS', function (done) { + it('GET /api/offices?_where=(((officeCode,in,1,2))~and(city,eq,boston)) should PASS', function (done) { //http get an url - agent.get('/api/customers/findOne?_where=(customerNumber,eq,119)') // api url + agent.get('/api/offices?_where=(((officeCode,in,1,2))~and(city,eq,boston))') // api url .expect(200) // 2xx for success and 4xx for failure .end(function (err, res) { // Handle /api/offices/1/employees error @@ -350,7 +347,7 @@ describe('xmysql : tests', function () { // tate is an invalid column but still it should query right number of columns res.body.length.should.be.equal(1) - res.body[0]['customerNumber'].should.be.equal(119) + res.body[0]['city'].should.be.equal('Boston') return done(); @@ -674,7 +671,6 @@ describe('xmysql : tests', function () { }); - it('PATCH /api/productlines/Hyperloop should PASS', function (done) { var obj = {}; @@ -748,7 +744,6 @@ describe('xmysql : tests', function () { }); - it('GET /api/offices/1/employees?_where=(jobTitle,eq,Sales%20Rep) should PASS', function (done) { //post to an url with data @@ -769,7 +764,6 @@ describe('xmysql : tests', function () { }); - it('GET /api/payments?_where=(amount,gte,1000)~and(customerNumber,lte,120) should PASS', function (done) { //post to an url with data @@ -1705,10 +1699,17 @@ describe('xmysql : tests', function () { }); + it('where clause unit ?_where=(a,in,1,2,3) should PASS', function (done) { + + var err = whereClause.getConditionClause('(a,in,1,2,3)') + err.err.should.be.equal(0) + err.query.should.be.equal('(?? in (?,?,?))') + done() + + }); - // // it('GET http://localhost:3000/api/customers/groupby?_fields=city,country&_having=(customerNumber,lt,110) should PASS', function (done) { // // //post to an url with data @@ -1726,7 +1727,4 @@ describe('xmysql : tests', function () { // }); - - - });