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. 28
      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) lte - '<=' - (colName,lte,colValue)
is - 'is' - (colName,is,true/false/null) is - 'is' - (colName,is,true/false/null)
in - 'in' - (colName,in,val1,val2,val3,val4) in - 'in' - (colName,in,val1,val2,val3,val4)
bw - 'between' - (colName,bw,val1,val2)
like - 'like' - (colName,like,~name) note: use ~ in place of % like - 'like' - (colName,like,~name) note: use ~ in place of %
nlike - 'not like' - (colName,nlike,~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 program
.version('0.3.1') .version('0.3.2')
.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')

28
lib/util/whereClause.helper.js

@ -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) { function replaceWhereParamsToQMarks(openParentheses, str, comparisonOperator) {
@ -76,7 +93,7 @@ function replaceWhereParamsToQMarks(openParentheses, str, comparisonOperator) {
} }
} else { } else {
if (comparisonOperator !== ' in ') if (comparisonOperator !== ' in ' && comparisonOperator !== ' between ')
converted = '?' converted = '?'
for (var i = str.length - 1; i >= 0; --i) { for (var i = str.length - 1; i >= 0; --i) {
@ -147,6 +164,10 @@ function getComparisonOperator(operator) {
return ' in ' return ' in '
break; break;
case 'bw':
return ' between '
break;
default: default:
return null return null
break; break;
@ -271,6 +292,11 @@ exports.getConditionClause = function (whereInQueryParams, condType = 'where') {
whereParams.push(prepareLikeValue(variableValue[1])) whereParams.push(prepareLikeValue(variableValue[1]))
} else if (comparisonOperator === ' is ') { } else if (comparisonOperator === ' is ') {
whereParams.push(prepareIsValue(variableValue[1])) 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 { } else {
whereParams.push(variableValue[1]) whereParams.push(variableValue[1])
} }

2
package.json

@ -1,6 +1,6 @@
{ {
"name": "xmysql", "name": "xmysql",
"version": "0.3.1", "version": "0.3.2",
"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": {

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) { 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 //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