Browse Source

Feature : 'in' comparison operator in where clause npm v0.2.8

pull/13/head
oof1lab 7 years ago
parent
commit
087ce3b2e4
  1. 14
      README.md
  2. 2
      lib/util/cmd.helper.js
  3. 65
      lib/util/whereClause.helper.js
  4. 2
      package.json
  5. 24
      tests/tests.js

14
README.md

@ -200,13 +200,15 @@ eg: gets all fields in table row but not checkNumber
#### Comparison operators
```
eq - '='
ne - '!='
gt - '>'
gte - '>='
lt - '<'
lte - '<='
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)
```
#### Use of comparison operators
```
/api/payments?_where=(checkNumber,eq,JM555205)

2
lib/util/cmd.helper.js

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

65
lib/util/whereClause.helper.js

@ -1,6 +1,43 @@
'use strict';
function replaceWhereParamsToQMarks(openParentheses, str) {
/**
*
* @param input : 1,2,3,4
* @returns obj.query = (?,?,?,?) obj.params = [1,2,3,4]
*/
function prepareInClauseParams(input) {
let inElems = input.split(',')
let obj = {}
obj.whereQuery = ''
obj.whereParams = []
for (var j = 0; j < inElems.length; ++j) {
if (j === 0) {
//enclose with open parenthesis
obj.whereQuery += '('
}
if (j) {
obj.whereQuery += ','
}
// add q mark and push the variable
obj.whereQuery += '?'
obj.whereParams.push(inElems[j])
if (j === inElems.length - 1) {
//enclose with closing parenthesis
obj.whereQuery += ')'
}
}
//console.log(obj);
return obj
}
function replaceWhereParamsToQMarks(openParentheses, str, comparisonOperator) {
let converted = ''
@ -14,7 +51,10 @@ function replaceWhereParamsToQMarks(openParentheses, str) {
}
}
} else {
converted = '?'
if (comparisonOperator !== ' in ')
converted = '?'
for (var i = str.length - 1; i >= 0; --i) {
if (str[i] === ')') {
converted += ')'
@ -22,6 +62,7 @@ function replaceWhereParamsToQMarks(openParentheses, str) {
break;
}
}
}
return converted;
}
@ -78,9 +119,9 @@ function getComparisonOperator(operator) {
// return ' not like '
// break;
// case 'in':
// return ' in '
// break;
case 'in':
return ' in '
break;
default:
return null
@ -191,22 +232,30 @@ exports.getConditionClause = function (whereInQueryParams, condType = 'where') {
}
whereQuery += comparisonOperator
// get the variableValue and push
// get the variableValue and push to params
variableValue = result[2].match(/(.*?)\)/)
if (!variableValue || variableValue.length !== 2) {
grammarErr = 1;
break;
}
whereParams.push(variableValue[1])
if (comparisonOperator === ' in ') {
let obj = prepareInClauseParams(variableValue[1])
whereQuery += obj.whereQuery
whereParams = whereParams.concat(obj.whereParams)
} else {
whereParams.push(variableValue[1])
}
// then replace the variableValue with ?
temp = replaceWhereParamsToQMarks(false, result[2])
temp = replaceWhereParamsToQMarks(false, result[2], comparisonOperator)
if (!temp) {
grammarErr = 1;
break;
}
whereQuery += temp
// only
if (numOfConditions.length !== -1 && i !== numOfConditions.length - 1) {
//console.log('finding logical operator for',logicalOperatorsInClause[i]);

2
package.json

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

24
tests/tests.js

@ -17,7 +17,6 @@ var api = {}
var mysqlPool = {}
//desribe group of tests done
describe('xmysql : tests', function () {
@ -90,7 +89,6 @@ describe('xmysql : tests', function () {
});
it('GET /api/payments/count should PASS', function (done) {
//http get an url
@ -336,11 +334,10 @@ describe('xmysql : tests', function () {
});
it('GET /api/customers/findOne?_where=(customerNumber,eq,119) should PASS', function (done) {
it('GET /api/offices?_where=(((officeCode,in,1,2))~and(city,eq,boston)) should PASS', function (done) {
//http get an url
agent.get('/api/customers/findOne?_where=(customerNumber,eq,119)') // api url
agent.get('/api/offices?_where=(((officeCode,in,1,2))~and(city,eq,boston))') // api url
.expect(200) // 2xx for success and 4xx for failure
.end(function (err, res) {
// Handle /api/offices/1/employees error
@ -350,7 +347,7 @@ describe('xmysql : tests', function () {
// tate is an invalid column but still it should query right number of columns
res.body.length.should.be.equal(1)
res.body[0]['customerNumber'].should.be.equal(119)
res.body[0]['city'].should.be.equal('Boston')
return done();
@ -674,7 +671,6 @@ describe('xmysql : tests', function () {
});
it('PATCH /api/productlines/Hyperloop should PASS', function (done) {
var obj = {};
@ -748,7 +744,6 @@ describe('xmysql : tests', function () {
});
it('GET /api/offices/1/employees?_where=(jobTitle,eq,Sales%20Rep) should PASS', function (done) {
//post to an url with data
@ -769,7 +764,6 @@ describe('xmysql : tests', function () {
});
it('GET /api/payments?_where=(amount,gte,1000)~and(customerNumber,lte,120) should PASS', function (done) {
//post to an url with data
@ -1705,10 +1699,17 @@ describe('xmysql : tests', function () {
});
it('where clause unit ?_where=(a,in,1,2,3) should PASS', function (done) {
var err = whereClause.getConditionClause('(a,in,1,2,3)')
err.err.should.be.equal(0)
err.query.should.be.equal('(?? in (?,?,?))')
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
@ -1726,7 +1727,4 @@ describe('xmysql : tests', function () {
// });
});

Loading…
Cancel
Save