From 719b8b2042df944dbc45ffb998cea23091376205 Mon Sep 17 00:00:00 2001 From: oof1lab Date: Thu, 16 Nov 2017 11:50:18 +0530 Subject: [PATCH] Feature : between in where clause npm v0.3.2 --- README.md | 1 + lib/util/cmd.helper.js | 2 +- lib/util/whereClause.helper.js | 34 ++++++++++++++++++++++++++++---- package.json | 2 +- tests/tests.js | 36 +++++++++++++++++++--------------- 5 files changed, 53 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 224b2eb2e3..f50a4ff897 100644 --- a/README.md +++ b/README.md @@ -208,6 +208,7 @@ lt - '<' - (colName,lt,colValue) lte - '<=' - (colName,lte,colValue) is - 'is' - (colName,is,true/false/null) in - 'in' - (colName,in,val1,val2,val3,val4) +bw - 'between' - (colName,bw,val1,val2) like - 'like' - (colName,like,~name) note: use ~ in place of % nlike - 'not like' - (colName,nlike,~name) note: use ~ in place of % ``` diff --git a/lib/util/cmd.helper.js b/lib/util/cmd.helper.js index 349f328e59..d128be3424 100644 --- a/lib/util/cmd.helper.js +++ b/lib/util/cmd.helper.js @@ -11,7 +11,7 @@ program.on('--help', () => { }) program - .version('0.3.1') + .version('0.3.2') .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 6570342487..7893a775a2 100644 --- a/lib/util/whereClause.helper.js +++ b/lib/util/whereClause.helper.js @@ -47,11 +47,11 @@ function prepareLikeValue(value) { function prepareIsValue(value) { //return value.replace("~", "%"); - if(value === 'null'){ + if (value === 'null') { return null; - } else if (value === 'true'){ + } else if (value === 'true') { return true; - } else if (value === 'false'){ + } else if (value === 'false') { return false; } else { return null @@ -59,6 +59,23 @@ function prepareIsValue(value) { } +function prepareBetweenValue(value) { + + let values = value.split(',') + let obj = {} + obj.whereQuery = '' + obj.whereParams = [] + + if (values.length === 2) { + obj.whereQuery = ' ? and ? ' + obj.whereParams.push(values[0]) + obj.whereParams.push(values[1]) + } + + //console.log('prepareBetweenValue', obj); + + return obj; +} function replaceWhereParamsToQMarks(openParentheses, str, comparisonOperator) { @@ -76,7 +93,7 @@ function replaceWhereParamsToQMarks(openParentheses, str, comparisonOperator) { } } else { - if (comparisonOperator !== ' in ') + if (comparisonOperator !== ' in ' && comparisonOperator !== ' between ') converted = '?' for (var i = str.length - 1; i >= 0; --i) { @@ -147,6 +164,10 @@ function getComparisonOperator(operator) { return ' in ' break; + case 'bw': + return ' between ' + break; + default: return null break; @@ -271,6 +292,11 @@ exports.getConditionClause = function (whereInQueryParams, condType = 'where') { whereParams.push(prepareLikeValue(variableValue[1])) } else if (comparisonOperator === ' is ') { whereParams.push(prepareIsValue(variableValue[1])) + } else if (comparisonOperator === ' between ') { + let obj = prepareBetweenValue(variableValue[1]) + whereQuery += obj.whereQuery + whereParams = whereParams.concat(obj.whereParams) + //console.log(whereQuery, whereParams); } else { whereParams.push(variableValue[1]) } diff --git a/package.json b/package.json index 302046ed94..ee0083712d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xmysql", - "version": "0.3.1", + "version": "0.3.2", "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 a98d36e6f4..f0af8a7294 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -1204,6 +1204,26 @@ describe('xmysql : tests', function () { }); }) + + it('GET /api/payments?_where=(amount,bw,1000,5000) should PASS', function (done) { + + //post to an url with data + agent.get('/api/payments?_where=(amount,bw,1000,5000)') //enter url + .expect(200)//200 for success 4xx for failure + .end(function (err, res) { + // Handle /api/v error + if (err) { + return done(err); + } + + res.body.length.should.be.equals(19) + + return done(); + + }); + }) + + it('GET /api/payments/chart?_fields=amount&min=0&max=131000&step=25000&range=1 should PASS', function (done) { //post to an url with data @@ -1897,21 +1917,5 @@ describe('xmysql : tests', function () { }); - // 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 - // agent.get('http://localhost:3000/api/customers/groupby?_fields=city,country&_having=(customerNumber,lt,110)') //enter url - // .expect(200)//200 for success 4xx for failure - // .end(function (err, res) { - // // Handle /api/v error - // if (err) { - // return done(err); - // } - // - // return done(); - // - // }); - // }); - });