diff --git a/README.md b/README.md index c84cb228ee..dcbb790905 100644 --- a/README.md +++ b/README.md @@ -200,13 +200,15 @@ eg: gets all fields in table row but not checkNumber #### Comparison operators ``` -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) +eq - '=' - (colName,eq,colValue) +ne - '!=' - (colName,ne,colValue) +gt - '>' - (colName,gt,colValue) +gte - '>=' - (colName,gte,colValue) +lt - '<' - (colName,lt,colValue) +lte - '<=' - (colName,lte,colValue) +in - 'in' - (colName,in,val1,val2,val3,val4) +like - 'like' - (colName,like,~name) note: use ~ in place of % +nlike - 'not like' - (colName,nlike,~name) note: use ~ in place of % ``` #### Use of comparison operators diff --git a/lib/util/cmd.helper.js b/lib/util/cmd.helper.js index 0e50ecf776..7084c17ccb 100644 --- a/lib/util/cmd.helper.js +++ b/lib/util/cmd.helper.js @@ -11,7 +11,7 @@ program.on('--help', () => { }) program - .version('0.2.8') + .version('0.2.9') .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 aeab4e7ad7..8f42af0dc7 100644 --- a/lib/util/whereClause.helper.js +++ b/lib/util/whereClause.helper.js @@ -37,6 +37,14 @@ function prepareInClauseParams(input) { } +function prepareLikeValue(value) { + + //return value.replace("~", "%"); + return value.replace(/~/g, '%'); + +} + + function replaceWhereParamsToQMarks(openParentheses, str, comparisonOperator) { let converted = '' @@ -111,13 +119,13 @@ function getComparisonOperator(operator) { // return ' is not NULL ' // break; - // case 'like': - // return ' like ' - // break; + case 'like': + return ' like ' + break; - // case 'nlike': - // return ' not like ' - // break; + case 'nlike': + return ' not like ' + break; case 'in': return ' in ' @@ -243,6 +251,8 @@ exports.getConditionClause = function (whereInQueryParams, condType = 'where') { let obj = prepareInClauseParams(variableValue[1]) whereQuery += obj.whereQuery whereParams = whereParams.concat(obj.whereParams) + } else if (comparisonOperator === ' like ' || comparisonOperator === ' not like ') { + whereParams.push(prepareLikeValue(variableValue[1])) } else { whereParams.push(variableValue[1]) } diff --git a/package.json b/package.json index bafe83ffdb..adee76481b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xmysql", - "version": "0.2.8", + "version": "0.2.9", "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 8e7125af7f..1cc2ab5442 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -783,6 +783,71 @@ describe('xmysql : tests', function () { }); }); + http://localhost:3000/api/offices?_where=(city,like,~on~) + + it('GET /api/offices?_where=(city,like,~on~) should PASS', function (done) { + + //post to an url with data + agent.get('/api/offices?_where=(city,like,~on~)') //enter url + .expect(200)//200 for success 4xx for failure + .end(function (err, res) { + // Handle /api/v error + if (err) { + return done(err); + } + + //validate response + res.body.length.should.be.equals(2) + + + return done(); + + }); + }); + + it('GET /api/offices?_where=(city,like,san~) should PASS', function (done) { + + //post to an url with data + agent.get('/api/offices?_where=(city,like,san~)') //enter url + .expect(200)//200 for success 4xx for failure + .end(function (err, res) { + // Handle /api/v error + if (err) { + return done(err); + } + + //validate response + res.body.length.should.be.equals(1) + + + + return done(); + + }); + }); + + it('GET /api/offices?_where=(country,nlike,us~) should PASS', function (done) { + + //post to an url with data + agent.get('/api/offices?_where=(country,nlike,us~)') //enter url + .expect(200)//200 for success 4xx for failure + .end(function (err, res) { + // Handle /api/v error + if (err) { + return done(err); + } + + //validate response + res.body.length.should.be.equals(4) + + + + return done(); + + }); + }); + + it('GET /api/payments?_where=(amount,gte,1000)&_sort=-amount should PASS', function (done) { //post to an url with data @@ -1710,6 +1775,33 @@ describe('xmysql : tests', function () { }); + it('where clause unit ?_where=(a,like,~1234) should PASS', function (done) { + + var err = whereClause.getConditionClause('(a,like,~1234)') + + err.err.should.be.equal(0) + err.query.should.be.equal('(?? like ?)') + err.params[0].should.be.equal('a') + err.params[1].should.be.equal('%1234') + + done() + + }); + + it('where clause unit ?_where=(a,like,~1234~) should PASS', function (done) { + + var err = whereClause.getConditionClause('(a,like,~1234~)') + + err.err.should.be.equal(0) + err.query.should.be.equal('(?? like ?)') + err.params[0].should.be.equal('a') + err.params[1].should.be.equal('%1234%') + + 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