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); this.mysql.getHavingClause(req.query._having, req.app.locals._tableName, queryParamsObj);
/**************** add order clause ****************/ /**************** 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 ****************/ /**************** add limit clause ****************/
queryParamsObj.query += ' limit ?,? ' queryParamsObj.query += ' limit ?,? '
@ -497,7 +497,7 @@ class Xapi {
/**************** where clause ****************/ /**************** where clause ****************/
this.mysql.getHavingClause(req.query._having, req.app.locals._tableName, queryParamsObj, ' having '); 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); //console.log(queryParamsObj.query, queryParamsObj.params);
var results = await this.mysql.exec(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) { getOrderByClause(queryparams, tableName, queryParamsObj) {
//defaults
let orderBy = '';
if (queryparams._sort) { if (queryparams._sort) {
orderBy += ' ORDER BY ' queryParamsObj.query += ' ORDER BY '
let orderByCols = queryparams._sort.split(',') let orderByCols = queryparams._sort.split(',')
for (let i = 0; i < orderByCols.length; ++i) { for (let i = 0; i < orderByCols.length; ++i) {
if (i) { if (i) {
orderBy = orderBy + ', ' queryParamsObj.query += ', '
} }
if (orderByCols[i][0] === '-') { if (orderByCols[i][0] === '-') {
let len = orderByCols[i].length; 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 { } else {
orderBy = orderBy + orderByCols[i] + ' ASC' queryParamsObj.query += ' ?? ASC'
queryParamsObj.params.push(orderByCols[i])
} }
} }
} }
return orderBy return orderBy

Loading…
Cancel
Save