Browse Source

Fix#24 : Xjoin column names to have aliases derived from tablName-alias and columnName.

pull/27/head
o1lab 7 years ago
parent
commit
5a9b6f4a5f
  1. 18
      README.md
  2. 5
      lib/xapi.js
  3. 45
      lib/xsql.js
  4. 22
      tests/tests.js

18
README.md

@ -726,7 +726,7 @@ Sql join query:
```sql
SELECT *
SELECT pl.field1, pr.field2
FROM productlines as pl
JOIN products as pr
ON pl.productline = pr.productline
@ -735,14 +735,14 @@ FROM productlines as pl
Equivalent xjoin query API:
```
/api/xjoin?_join=pl.productlines,_j,pr.products&_on1=(pl.productline,eq,pr.productline)
/api/xjoin?_join=pl.productlines,_j,pr.products&_on1=(pl.productline,eq,pr.productline)&_fields=pl.field1,pr.field2
```
#### Multiple tables join
Sql join query:
```sql
SELECT *
SELECT pl.field1, pr.field2, ord.field3
FROM productlines as pl
JOIN products as pr
ON pl.productline = pr.productline
@ -753,7 +753,7 @@ FROM productlines as pl
Equivalent xjoin query API:
```
/api/xjoin?_join=pl.productlines,_j,pr.products,_j,ord.orderDetails&_on1=(pl.productline,eq,pr.productline)&_on2=(pr.productcode,eq,ord.productcode)
/api/xjoin?_join=pl.productlines,_j,pr.products,_j,ord.orderDetails&_on1=(pl.productline,eq,pr.productline)&_on2=(pr.productcode,eq,ord.productcode)&_fields=&_fields=pl.field1,pr.field2,ord.field3
```
@ -774,6 +774,16 @@ Example to use : _fields, _where, _p, _size in query params
/api/xjoin?_join=pl.productlines,_j,pr.products&_on1=(pl.productline,eq,pr.productline)&_fields=pl.productline,pr.productName&_size=2&_where=(productName,like,1972~)
```
Response:
```
[{"pl_productline":"Classic Cars","pr_productName":"1972 Alfa Romeo GTA"}]
```
Please note :
Xjoin response has aliases for fields like below aliasTableName + '_' + columnName.
eg: pl.productline in _fields query params - returns as pl_productline in response.
## Run dynamic queries
[:arrow_heading_up:](#api-overview)

5
lib/xapi.js

@ -304,9 +304,12 @@ class Xapi {
this.mysql.prepareJoinQuery(req, res, obj)
//console.log(obj);
if(obj.query.length){
let results = await this.mysql.exec(obj.query, obj.params)
res.status(200).json(results)
} else {
res.status(400).json({err: 'Invalid Xjoin request'});
}
}

45
lib/xsql.js

@ -139,6 +139,7 @@ class Xsql {
}
}
}
/**************** END : Cache functions ****************/
@ -670,7 +671,7 @@ class Xsql {
//select ? as ??, count(*) as _count from ?? where ?? between ? and ?
if (stepArray.length && stepArray.length >= 2 && stepArray.length%2 === 0) {
if (stepArray.length && stepArray.length >= 2 && stepArray.length % 2 === 0) {
for (let i = 0; i < stepArray.length && stepArray.length >= 2; i = i + 2) {
@ -954,27 +955,38 @@ class Xsql {
queryParamsObj.query = 'SELECT '
queryParamsObj.grammarErr = 0;
while (1) {
/**************** START : get fields ****************/
if (req.query._fields) {
let fields = req.query._fields.split(',')
// from _fields to - ??, ??, ?? [col1,col2,col3]
for (var i = 0; i < fields.length; ++i) {
for (var i = 0; i < fields.length && (!queryParamsObj.grammarErr); ++i) {
if (i) {
queryParamsObj.query += ','
}
queryParamsObj.query += ' ?? '
queryParamsObj.params.push(fields[i])
let aliases = fields[i].split('.');
if (aliases.length === 2) {
queryParamsObj.query += 'as ' + aliases[0] + '_' + aliases[1];
//console.log(queryParamsObj.query);
} else {
queryParamsObj.grammarErr = 1;
}
}
} else {
queryParamsObj.grammarErr = 1;
}
queryParamsObj.query += ' * '
queryParamsObj.query += ' from '
if(queryParamsObj.grammarErr){
break;
}
queryParamsObj.query += ' from '
/**************** END : get fields ****************/
@ -983,6 +995,7 @@ class Xsql {
if (joinTables.length < 3) {
//console.log('grammar error ', joinTables.length);
queryParamsObj.grammarErr = 1;
break;
}
//console.log('jointables.length', joinTables);
@ -1016,29 +1029,33 @@ class Xsql {
break;
}
//console.log('- - - - - - -');
if (i === 0) {
i = i + 1
}
//console.log('index after loop', i);
}
/**************** END : get join + on ****************/
if (queryParamsObj.grammarErr) {
queryParamsObj.query = ''
queryParamsObj.params = []
if(queryParamsObj.grammarErr){
break;
} else {
this.getWhereClause(req.query._where, ' ignore ', queryParamsObj, ' where ');
this._getGrpByHavingOrderBy(req, 'ignore', queryParamsObj, 5)
//console.log('after where',queryParamsObj);
}
this.getWhereClause(req.query._where, ' ignore ', queryParamsObj, ' where ');
break;
//console.log('after where',queryParamsObj);
}
this._getGrpByHavingOrderBy(req, 'ignore', queryParamsObj, 5)
return queryParamsObj;
if (queryParamsObj.grammarErr) {
queryParamsObj.query = ''
queryParamsObj.params = []
}
return queryParamsObj;
}
}

22
tests/tests.js

@ -1599,7 +1599,7 @@ describe('xmysql : tests', function () {
//post to an url with data
agent.get(apiPrefix + 'xjoin?_join=pl.productlines,_j,pr.products&_on1=(pl.productline,eq,pr.productline)') //enter url
.expect(200)//200 for success 4xx for failure
.expect(400)//200 for success 4xx for failure
.end(function (err, res) {
// Handle /api/v error
@ -1607,8 +1607,6 @@ describe('xmysql : tests', function () {
return done(err);
}
//validate response
Object.keys(res.body[0]).length.should.be.equals(12)
return done();
});
@ -1636,6 +1634,24 @@ describe('xmysql : tests', function () {
});
});
it('GET ' + apiPrefix + 'xjoin?_join=pl.productlines,_j,pr.products&_on1=(pl.productline,eq,pr.productline)&_fields=pl_productline,pr.productName should PASS', function (done) {
//post to an url with data
agent.get(apiPrefix + 'xjoin?_join=pl.productlines,_j,pr.products&_on1=(pl.productline,eq,pr.productline)&_fields=pl_productline,pr.productName') //enter url
.expect(400)//200 for success 4xx for failure
.end(function (err, res) {
// Handle /api/v error
if (err) {
return done(err);
}
return done();
});
});
it('GET ' + apiPrefix + 'xjoin?_join=pl.productlines,_j,pr.products&_on1=(pl.productline,eq,pr.productline)&_fields=pl.productline,pr.productName&_size=2 should PASS', function (done) {
//post to an url with data

Loading…
Cancel
Save