Browse Source

Feature : 'is' operator in where clause npm v0.3.0

pull/13/head
oof1lab 7 years ago
parent
commit
a165dcb779
  1. 1
      README.md
  2. 2
      lib/util/cmd.helper.js
  3. 30
      lib/util/whereClause.helper.js
  4. 2
      package.json
  5. 34
      tests/tests.js

1
README.md

@ -206,6 +206,7 @@ gt - '>' - (colName,gt,colValue)
gte - '>=' - (colName,gte,colValue)
lt - '<' - (colName,lt,colValue)
lte - '<=' - (colName,lte,colValue)
is - 'is' - (colName,is,true/false/null)
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 %

2
lib/util/cmd.helper.js

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

30
lib/util/whereClause.helper.js

@ -44,6 +44,22 @@ function prepareLikeValue(value) {
}
function prepareIsValue(value) {
//return value.replace("~", "%");
if(value === 'null'){
return null;
} else if (value === 'true'){
return true;
} else if (value === 'false'){
return false;
} else {
return null
}
}
function replaceWhereParamsToQMarks(openParentheses, str, comparisonOperator) {
@ -103,13 +119,13 @@ function getComparisonOperator(operator) {
return '>='
break;
// case 'is':
// return ' is '
// break;
case 'is':
return ' is '
break;
// case 'isnot':
// return ' is not '
// break;
case 'isnot':
return ' is not '
break;
// case 'isnull':
// return ' is NULL '
@ -253,6 +269,8 @@ exports.getConditionClause = function (whereInQueryParams, condType = 'where') {
whereParams = whereParams.concat(obj.whereParams)
} else if (comparisonOperator === ' like ' || comparisonOperator === ' not like ') {
whereParams.push(prepareLikeValue(variableValue[1]))
} else if (comparisonOperator === ' is ') {
whereParams.push(prepareIsValue(variableValue[1]))
} else {
whereParams.push(variableValue[1])
}

2
package.json

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

34
tests/tests.js

@ -783,7 +783,24 @@ describe('xmysql : tests', function () {
});
});
http://localhost:3000/api/offices?_where=(city,like,~on~)
it('GET /api/productlines?_where=(htmlDescription,is,null) should PASS', function (done) {
//post to an url with data
agent.get('/api/productlines?_where=(htmlDescription,is,null)') //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(6)
return done();
});
});
it('GET /api/offices?_where=(city,like,~on~) should PASS', function (done) {
@ -1801,6 +1818,21 @@ describe('xmysql : tests', function () {
});
it('where clause unit ?_where=(a,is,null)~and(b,is,true)~and(c,is,false) should PASS', function (done) {
var err = whereClause.getConditionClause('(a,is,null)~and(b,is,true)~and(c,is,false)')
//console.log(err.params[1],err);
err.err.should.be.equal(0)
err.query.should.be.equal('(?? is ?)and(?? is ?)and(?? is ?)')
//err.params[1].should.be.equal(null)
err.params[3].should.be.equal(true)
err.params[5].should.be.equal(false)
done()
});
// it('GET http://localhost:3000/api/customers/groupby?_fields=city,country&_having=(customerNumber,lt,110) should PASS', function (done) {
//

Loading…
Cancel
Save