Browse Source

Feature : 'like' and 'not like' operators in where clause npm v0.2.9

pull/13/head
oof1lab 7 years ago
parent
commit
a33ae8595d
  1. 16
      README.md
  2. 2
      lib/util/cmd.helper.js
  3. 22
      lib/util/whereClause.helper.js
  4. 2
      package.json
  5. 92
      tests/tests.js

16
README.md

@ -200,13 +200,15 @@ eg: gets all fields in table row but not checkNumber
#### Comparison operators #### Comparison operators
``` ```
eq - '=' - (colName,eq,colValue) eq - '=' - (colName,eq,colValue)
ne - '!=' - (colName,ne,colValue) ne - '!=' - (colName,ne,colValue)
gt - '>' - (colName,ne,colValue) gt - '>' - (colName,gt,colValue)
gte - '>=' - (colName,ne,colValue) gte - '>=' - (colName,gte,colValue)
lt - '<' - (colName,ne,colValue) lt - '<' - (colName,lt,colValue)
lte - '<=' - (colName,ne,colValue) lte - '<=' - (colName,lte,colValue)
in - 'in' - (colName,ne,val1,val2,val3,val4) 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 #### Use of comparison operators

2
lib/util/cmd.helper.js

@ -11,7 +11,7 @@ program.on('--help', () => {
}) })
program program
.version('0.2.8') .version('0.2.9')
.option('-h, --host <n>', 'hostname / localhost by default') .option('-h, --host <n>', 'hostname / localhost by default')
.option('-u, --user <n>', 'username of database / root by default') .option('-u, --user <n>', 'username of database / root by default')
.option('-p, --password <n>', 'password of database / empty by default') .option('-p, --password <n>', 'password of database / empty by default')

22
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) { function replaceWhereParamsToQMarks(openParentheses, str, comparisonOperator) {
let converted = '' let converted = ''
@ -111,13 +119,13 @@ function getComparisonOperator(operator) {
// return ' is not NULL ' // return ' is not NULL '
// break; // break;
// case 'like': case 'like':
// return ' like ' return ' like '
// break; break;
// case 'nlike': case 'nlike':
// return ' not like ' return ' not like '
// break; break;
case 'in': case 'in':
return ' in ' return ' in '
@ -243,6 +251,8 @@ exports.getConditionClause = function (whereInQueryParams, condType = 'where') {
let obj = prepareInClauseParams(variableValue[1]) let obj = prepareInClauseParams(variableValue[1])
whereQuery += obj.whereQuery whereQuery += obj.whereQuery
whereParams = whereParams.concat(obj.whereParams) whereParams = whereParams.concat(obj.whereParams)
} else if (comparisonOperator === ' like ' || comparisonOperator === ' not like ') {
whereParams.push(prepareLikeValue(variableValue[1]))
} else { } else {
whereParams.push(variableValue[1]) whereParams.push(variableValue[1])
} }

2
package.json

@ -1,6 +1,6 @@
{ {
"name": "xmysql", "name": "xmysql",
"version": "0.2.8", "version": "0.2.9",
"description": "One command to generate REST APIs for any MySql database", "description": "One command to generate REST APIs for any MySql database",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

92
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) { it('GET /api/payments?_where=(amount,gte,1000)&_sort=-amount should PASS', function (done) {
//post to an url with data //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) { // 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 // //post to an url with data

Loading…
Cancel
Save