Browse Source

Feature : between in where clause npm v0.3.2

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

1
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 %
```

2
lib/util/cmd.helper.js

@ -11,7 +11,7 @@ program.on('--help', () => {
})
program
.version('0.3.1')
.version('0.3.2')
.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')

34
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])
}

2
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": {

36
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();
//
// });
// });
});

Loading…
Cancel
Save