Browse Source

Refactor: keeping order by as params in query

pull/13/head
oof1lab 7 years ago
parent
commit
b913cf2d88
  1. 4
      lib/xapi.js
  2. 17
      lib/xsql.js

4
lib/xapi.js

@ -284,7 +284,7 @@ class Xapi {
this.mysql.getHavingClause(req.query._having, req.app.locals._tableName, queryParamsObj);
/**************** add order clause ****************/
queryParamsObj.query += this.mysql.getOrderByClause(req.query, req.app.locals._tableName);
this.mysql.getOrderByClause(req.query, req.app.locals._tableName, queryParamsObj);
/**************** add limit clause ****************/
queryParamsObj.query += ' limit ?,? '
@ -497,7 +497,7 @@ class Xapi {
/**************** where clause ****************/
this.mysql.getHavingClause(req.query._having, req.app.locals._tableName, queryParamsObj, ' having ');
queryParamsObj.query = queryParamsObj.query + this.mysql.getOrderByClause(req.query, tableName);
this.mysql.getOrderByClause(req.query, tableName, queryParamsObj);
//console.log(queryParamsObj.query, queryParamsObj.params);
var results = await this.mysql.exec(queryParamsObj.query, queryParamsObj.params);

17
lib/xsql.js

@ -215,30 +215,27 @@ class Xsql {
}
getOrderByClause(queryparams, tableName) {
//defaults
let orderBy = '';
getOrderByClause(queryparams, tableName, queryParamsObj) {
if (queryparams._sort) {
orderBy += ' ORDER BY '
queryParamsObj.query += ' ORDER BY '
let orderByCols = queryparams._sort.split(',')
for (let i = 0; i < orderByCols.length; ++i) {
if (i) {
orderBy = orderBy + ', '
queryParamsObj.query += ', '
}
if (orderByCols[i][0] === '-') {
let len = orderByCols[i].length;
orderBy = orderBy + orderByCols[i].substring(1, len) + ' DESC'
queryParamsObj.query += ' ?? DESC'
queryParamsObj.params.push(orderByCols[i].substring(1, len) )
} else {
orderBy = orderBy + orderByCols[i] + ' ASC'
queryParamsObj.query += ' ?? ASC'
queryParamsObj.params.push(orderByCols[i])
}
}
}
return orderBy

Loading…
Cancel
Save