Browse Source

Refactor: keeping list funcs dry.

pull/13/head
oof1lab 7 years ago
parent
commit
4235d39e0e
  1. 97
      lib/xapi.js

97
lib/xapi.js

@ -231,88 +231,89 @@ class Xapi {
} }
async list(req, res) { prepareListQuery(req, res, queryParamsObj, nested = false) {
let queryParamsObj = {}
queryParamsObj.query = 'select '; queryParamsObj.query = 'select ';
queryParamsObj.params = []; queryParamsObj.params = [];
/**************** select columns ****************/
if (req.query._groupby) { if (req.query._groupby) {
this.mysql.getColumnsForSelectStmtWithGrpBy(req.query, req.app.locals._tableName, queryParamsObj); this.mysql.getColumnsForSelectStmtWithGrpBy(req.query, req.app.locals._tableName, queryParamsObj);
} else { } else {
this.mysql.getColumnsForSelectStmt(req.app.locals._tableName, req.query, queryParamsObj); this.mysql.getColumnsForSelectStmt(req.app.locals._tableName, req.query, queryParamsObj);
} }
/**************** add tableName ****************/
/**************** tableName ****************/
queryParamsObj.query += ' from ?? '; queryParamsObj.query += ' from ?? ';
queryParamsObj.params.push(req.app.locals._tableName);
/**************** where clause ****************/ if (nested) {
this.mysql.getWhereClause(req.query._where, req.app.locals._tableName, queryParamsObj, ' where ');
req.app.locals._tableName = req.app.locals._childTable;
queryParamsObj.params.push(req.app.locals._childTable);
queryParamsObj.query += ' where ';
/**************** add where foreign key ****************/
let whereClause = this.mysql.getForeignKeyWhereClause(req.app.locals._parentTable,
req.params.id,
req.app.locals._childTable);
if (!whereClause) {
return res.status(400).send({
error: "Table is made of composite primary keys - all keys were not in input"
})
}
queryParamsObj.query += whereClause;
this.mysql.getWhereClause(req.query._where, req.app.locals._tableName, queryParamsObj, ' and ');
} else {
queryParamsObj.params.push(req.app.locals._tableName);
/**************** add where clause ****************/
this.mysql.getWhereClause(req.query._where, req.app.locals._tableName, queryParamsObj, ' where ');
/**************** group by ****************/ }
/**************** add group by ****************/
this.mysql.getGroupByClause(req.query._groupby, req.app.locals._tableName, queryParamsObj); this.mysql.getGroupByClause(req.query._groupby, req.app.locals._tableName, queryParamsObj);
/**************** having ****************/ /**************** add having ****************/
this.mysql.getHavingClause(req.query._having, req.app.locals._tableName, queryParamsObj); this.mysql.getHavingClause(req.query._having, req.app.locals._tableName, queryParamsObj);
/**************** order clause ****************/ /**************** add order clause ****************/
queryParamsObj.query += this.mysql.getOrderByClause(req.query, req.app.locals._tableName); queryParamsObj.query += this.mysql.getOrderByClause(req.query, req.app.locals._tableName);
/**************** limit clause ****************/ /**************** add limit clause ****************/
queryParamsObj.query += ' limit ?,? ' queryParamsObj.query += ' limit ?,? '
queryParamsObj.params = queryParamsObj.params.concat(this.mysql.getLimitClause(req.query)); queryParamsObj.params = queryParamsObj.params.concat(this.mysql.getLimitClause(req.query));
//console.log(queryParamsObj.query, queryParamsObj.params); //console.log(queryParamsObj.query, queryParamsObj.params);
let results = await this.mysql.exec(queryParamsObj.query, queryParamsObj.params);
res.status(200).json(results);
} }
async nestedList(req, res) { async list(req, res) {
let queryParamsObj = {} let queryParamsObj = {}
queryParamsObj.query = 'select '; queryParamsObj.query = ''
queryParamsObj.params = []; queryParamsObj.params = []
/**************** tableName ****************/ this.prepareListQuery(req, res, queryParamsObj, false);
if (req.query._groupby) {
this.mysql.getColumnsForSelectStmtWithGrpBy(req.query, req.app.locals._tableName, queryParamsObj);
} else {
this.mysql.getColumnsForSelectStmt(req.app.locals._tableName, req.query, queryParamsObj);
}
queryParamsObj.query += ' from ?? where ';
queryParamsObj.params.push(req.app.locals._childTable);
/**************** where foreign key ****************/ let results = await this.mysql.exec(queryParamsObj.query, queryParamsObj.params);
let whereClause = this.mysql.getForeignKeyWhereClause(req.app.locals._parentTable, res.status(200).json(results);
req.params.id,
req.app.locals._childTable);
if (!whereClause) {
return res.status(400).send({
error: "Table is made of composite primary keys - all keys were not in input"
})
}
queryParamsObj.query += whereClause;
/**************** where conditions in query ****************/
this.mysql.getWhereClause(req.query._where, req.app.locals._tableName, queryParamsObj, ' and ');
/**************** group by ****************/ }
this.mysql.getGroupByClause(req.query._groupby, req.app.locals._tableName, queryParamsObj);
/**************** having ****************/ async nestedList(req, res) {
this.mysql.getHavingClause(req.query._having, req.app.locals._tableName, queryParamsObj);
/**************** order clause ****************/ let queryParamsObj = {}
queryParamsObj.query = queryParamsObj.query + this.mysql.getOrderByClause(req.query, req.app.locals._parentTable); queryParamsObj.query = '';
queryParamsObj.params = [];
/**************** limit clause ****************/ this.prepareListQuery(req, res, queryParamsObj, true)
queryParamsObj.query = queryParamsObj.query + ' limit ?,? '
queryParamsObj.params = queryParamsObj.params.concat(this.mysql.getLimitClause(req.query));
let results = await this.mysql.exec(queryParamsObj.query, queryParamsObj.params); let results = await this.mysql.exec(queryParamsObj.query, queryParamsObj.params);
res.status(200).json(results); res.status(200).json(results);

Loading…
Cancel
Save