From a29e82787baf165b0827d0388eede508b13d7326 Mon Sep 17 00:00:00 2001 From: oof1lab Date: Sat, 18 Nov 2017 13:01:50 +0530 Subject: [PATCH] refactor : move prepare methods to from api to sql class --- lib/xapi.js | 274 ++------------------------------------------------- lib/xsql.js | 275 +++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 269 insertions(+), 280 deletions(-) diff --git a/lib/xapi.js b/lib/xapi.js index 2b716e1298..a2b652ae7d 100644 --- a/lib/xapi.js +++ b/lib/xapi.js @@ -283,275 +283,13 @@ class Xapi { } - _getGrpByHavingOrderBy(req, tableName, queryParamsObj, listType) { - - /**************** add group by ****************/ - this.mysql.getGroupByClause(req.query._groupby, req.app.locals._tableName, queryParamsObj); - - /**************** add having ****************/ - this.mysql.getHavingClause(req.query._having, req.app.locals._tableName, queryParamsObj); - - /**************** add order clause ****************/ - this.mysql.getOrderByClause(req.query, req.app.locals._tableName, queryParamsObj); - - /**************** add limit clause ****************/ - if (listType === 2) { //nested - queryParamsObj.query += ' limit 1 ' - } else { - queryParamsObj.query += ' limit ?,? ' - queryParamsObj.params = queryParamsObj.params.concat(this.mysql.getLimitClause(req.query)); - } - - } - - - /** - * - * @param req - * @param res - * @param queryParamsObj : {query, params} - * @param listType : 0:list, 1:nested, 2:findOne, 3:bulkRead, 4:distinct, 5:xjoin - * - * Updates query, params for query of type listType - */ - prepareListQuery(req, res, queryParamsObj, listType = 0) { - - queryParamsObj.query = 'select '; - queryParamsObj.params = []; - - if (listType === 4) { //list type distinct - queryParamsObj.query += ' distinct ' - } - - /**************** select columns ****************/ - 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); - } - - /**************** add tableName ****************/ - queryParamsObj.query += ' from ?? '; - - if (listType === 1) { //nested list - - 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 if (listType === 3) { //bulkRead - - // select * from table where pk in (ids) and whereConditions - queryParamsObj.params.push(req.app.locals._tableName); - queryParamsObj.query += ' where ?? in '; - queryParamsObj.params.push(this.mysql.getPrimaryKeyName(req.app.locals._tableName)); - - queryParamsObj.query += '(' - - if (req.query && req.query._ids) { - let ids = req.query._ids.split(',') - for (var i = 0; i < ids.length; ++i) { - if (i) { - queryParamsObj.query += ',' - } - queryParamsObj.query += '?' - queryParamsObj.params.push(ids[i]) - } - } - queryParamsObj.query += ') ' - 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 '); - - } - - this._getGrpByHavingOrderBy(req, req.app.locals._tableName, queryParamsObj) - - - //console.log(queryParamsObj.query, queryParamsObj.params); - } - - - _joinTableNames(isSecondJoin, joinTables, index, queryParamsObj) { - - if (isSecondJoin) { - - /** - * in second join - there will be ONE table and an ON condition - * this if clause deals with this - * - */ - - // add : join / left join / right join / full join / inner join - queryParamsObj.query += this.mysql.getJoinType(joinTables[index]) - queryParamsObj.query += ' ?? as ?? ' - - // eg: tbl.tableName - let tableNameAndAs = joinTables[index + 1].split('.') - - if (tableNameAndAs.length === 2 && !(tableNameAndAs[1] in this.config.ignoreTables)) { - queryParamsObj.params.push(tableNameAndAs[1]) - queryParamsObj.params.push(tableNameAndAs[0]) - } else { - queryParamsObj.grammarErr = 1 - console.log('there was no dot for tableName ', joinTables[index + 1]); - } - - } else { - - /** - * in first join - there will be TWO tables and an ON condition - * this else clause deals with this - */ - - - // first table - queryParamsObj.query += ' ?? as ?? ' - // add : join / left join / right join / full join / inner join - queryParamsObj.query += this.mysql.getJoinType(joinTables[index + 1]) - // second table - queryParamsObj.query += ' ?? as ?? ' - - let tableNameAndAs = joinTables[index].split('.') - if (tableNameAndAs.length === 2 && !(tableNameAndAs[1] in this.config.ignoreTables)) { - queryParamsObj.params.push(tableNameAndAs[1]) - queryParamsObj.params.push(tableNameAndAs[0]) - } else { - queryParamsObj.grammarErr = 1 - console.log('there was no dot for tableName ', joinTables[index]); - } - - tableNameAndAs = [] - tableNameAndAs = joinTables[index + 2].split('.') - if (tableNameAndAs.length === 2 && !(tableNameAndAs[1] in this.config.ignoreTables)) { - queryParamsObj.params.push(tableNameAndAs[1]) - queryParamsObj.params.push(tableNameAndAs[0]) - } else { - queryParamsObj.grammarErr = 1 - console.log('there was no dot for tableName ', joinTables[index]); - } - } - - } - - prepareJoinQuery(req, res, queryParamsObj) { - - queryParamsObj.query = 'SELECT ' - queryParamsObj.grammarErr = 0; - - /**************** 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) { - if (i) { - queryParamsObj.query += ',' - } - queryParamsObj.query += ' ?? ' - queryParamsObj.params.push(fields[i]) - } - - } else { - - queryParamsObj.query += ' * ' - - } - - queryParamsObj.query += ' from ' - /**************** END : get fields ****************/ - - - /**************** START : get join + on ****************/ - let joinTables = req.query._join.split(',') - if (joinTables.length < 3) { - //console.log('grammar error ', joinTables.length); - queryParamsObj.grammarErr = 1; - } - - //console.log('jointables.length', joinTables); - - let onCondnCount = 0; - - for (let i = 0; i < joinTables.length - 1 && queryParamsObj.grammarErr === 0; i = i + 2) { - - onCondnCount++; - - this._joinTableNames(i, joinTables, i, queryParamsObj) - - if (queryParamsObj.grammarErr) { - console.log('failed at _joinTableNames', queryParamsObj); - break; - } - - //console.log('after join tables', queryParamsObj); - - let onCondn = '_on' + (onCondnCount) - let onCondnObj = {} - if (onCondn in req.query) { - //console.log(onCondn, req.query[onCondn]); - onCondnObj = whrHelp.getConditionClause(req.query[onCondn], ' on ') - //console.log('onCondnObj', onCondnObj); - queryParamsObj.query += ' on ' + onCondnObj.query - queryParamsObj.params = queryParamsObj.params.concat(onCondnObj.params) - } else { - queryParamsObj.grammarErr = 1; - //console.log('No on condition: ', onCondn); - 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 = [] - } - - this.mysql.getWhereClause(req.query._where, ' ignore ', queryParamsObj, ' where '); - - //console.log('after where',queryParamsObj); - - this._getGrpByHavingOrderBy(req, 'ignore', queryParamsObj, 5) - - return queryParamsObj; - } - - async list(req, res) { let queryParamsObj = {} queryParamsObj.query = '' queryParamsObj.params = [] - this.prepareListQuery(req, res, queryParamsObj, 0); + this.mysql.prepareListQuery(req, res, queryParamsObj, 0); let results = await this.mysql.exec(queryParamsObj.query, queryParamsObj.params); res.status(200).json(results); @@ -566,7 +304,7 @@ class Xapi { obj.query = ''; obj.params = []; - this.prepareJoinQuery(req, res, obj) + this.mysql.prepareJoinQuery(req, res, obj) //console.log(obj); @@ -583,7 +321,7 @@ class Xapi { queryParamsObj.query = '' queryParamsObj.params = [] - this.prepareListQuery(req, res, queryParamsObj, 4); + this.mysql.prepareListQuery(req, res, queryParamsObj, 4); let results = await this.mysql.exec(queryParamsObj.query, queryParamsObj.params); res.status(200).json(results); @@ -596,7 +334,7 @@ class Xapi { queryParamsObj.query = ''; queryParamsObj.params = []; - this.prepareListQuery(req, res, queryParamsObj, 1) + this.mysql.prepareListQuery(req, res, queryParamsObj, 1) let results = await this.mysql.exec(queryParamsObj.query, queryParamsObj.params); res.status(200).json(results); @@ -609,7 +347,7 @@ class Xapi { queryParamsObj.query = '' queryParamsObj.params = [] - this.prepareListQuery(req, res, queryParamsObj, 2); + this.mysql.prepareListQuery(req, res, queryParamsObj, 2); let results = await this.mysql.exec(queryParamsObj.query, queryParamsObj.params); res.status(200).json(results); @@ -794,7 +532,7 @@ class Xapi { queryParamsObj.query = '' queryParamsObj.params = [] - this.prepareListQuery(req, res, queryParamsObj, 3); + this.mysql.prepareListQuery(req, res, queryParamsObj, 3); //console.log(queryParamsObj.query, queryParamsObj.params); diff --git a/lib/xsql.js b/lib/xsql.js index 0dc8d81919..46b64e7d76 100644 --- a/lib/xsql.js +++ b/lib/xsql.js @@ -635,18 +635,6 @@ class Xsql { } - getJoinTables(req) { - - } - - getJoinOnConditions() { - - } - - getJoinQuery() { - - } - globalRoutesPrint(apiPrefix) { @@ -746,6 +734,269 @@ class Xsql { } + _getGrpByHavingOrderBy(req, tableName, queryParamsObj, listType) { + + /**************** add group by ****************/ + this.getGroupByClause(req.query._groupby, req.app.locals._tableName, queryParamsObj); + + /**************** add having ****************/ + this.getHavingClause(req.query._having, req.app.locals._tableName, queryParamsObj); + + /**************** add order clause ****************/ + this.getOrderByClause(req.query, req.app.locals._tableName, queryParamsObj); + + /**************** add limit clause ****************/ + if (listType === 2) { //nested + queryParamsObj.query += ' limit 1 ' + } else { + queryParamsObj.query += ' limit ?,? ' + queryParamsObj.params = queryParamsObj.params.concat(this.getLimitClause(req.query)); + } + + } + + + /** + * + * @param req + * @param res + * @param queryParamsObj : {query, params} + * @param listType : 0:list, 1:nested, 2:findOne, 3:bulkRead, 4:distinct, 5:xjoin + * + * Updates query, params for query of type listType + */ + prepareListQuery(req, res, queryParamsObj, listType = 0) { + + queryParamsObj.query = 'select '; + queryParamsObj.params = []; + + if (listType === 4) { //list type distinct + queryParamsObj.query += ' distinct ' + } + + /**************** select columns ****************/ + if (req.query._groupby) { + this.getColumnsForSelectStmtWithGrpBy(req.query, req.app.locals._tableName, queryParamsObj); + } else { + this.getColumnsForSelectStmt(req.app.locals._tableName, req.query, queryParamsObj); + } + + /**************** add tableName ****************/ + queryParamsObj.query += ' from ?? '; + + if (listType === 1) { //nested list + + 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.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.getWhereClause(req.query._where, req.app.locals._tableName, queryParamsObj, ' and '); + + } else if (listType === 3) { //bulkRead + + // select * from table where pk in (ids) and whereConditions + queryParamsObj.params.push(req.app.locals._tableName); + queryParamsObj.query += ' where ?? in '; + queryParamsObj.params.push(this.getPrimaryKeyName(req.app.locals._tableName)); + + queryParamsObj.query += '(' + + if (req.query && req.query._ids) { + let ids = req.query._ids.split(',') + for (var i = 0; i < ids.length; ++i) { + if (i) { + queryParamsObj.query += ',' + } + queryParamsObj.query += '?' + queryParamsObj.params.push(ids[i]) + } + } + queryParamsObj.query += ') ' + this.getWhereClause(req.query._where, req.app.locals._tableName, queryParamsObj, ' and '); + + } else { + + queryParamsObj.params.push(req.app.locals._tableName); + + /**************** add where clause ****************/ + this.getWhereClause(req.query._where, req.app.locals._tableName, queryParamsObj, ' where '); + + } + + this._getGrpByHavingOrderBy(req, req.app.locals._tableName, queryParamsObj) + + + //console.log(queryParamsObj.query, queryParamsObj.params); + } + + + _joinTableNames(isSecondJoin, joinTables, index, queryParamsObj) { + + if (isSecondJoin) { + + /** + * in second join - there will be ONE table and an ON condition + * this if clause deals with this + * + */ + + // add : join / left join / right join / full join / inner join + queryParamsObj.query += this.getJoinType(joinTables[index]) + queryParamsObj.query += ' ?? as ?? ' + + // eg: tbl.tableName + let tableNameAndAs = joinTables[index + 1].split('.') + + if (tableNameAndAs.length === 2 && !(tableNameAndAs[1] in this.sqlConfig.ignoreTables)) { + queryParamsObj.params.push(tableNameAndAs[1]) + queryParamsObj.params.push(tableNameAndAs[0]) + } else { + queryParamsObj.grammarErr = 1 + console.log('there was no dot for tableName ', joinTables[index + 1]); + } + + } else { + + /** + * in first join - there will be TWO tables and an ON condition + * this else clause deals with this + */ + + + // first table + queryParamsObj.query += ' ?? as ?? ' + // add : join / left join / right join / full join / inner join + queryParamsObj.query += this.getJoinType(joinTables[index + 1]) + // second table + queryParamsObj.query += ' ?? as ?? ' + + let tableNameAndAs = joinTables[index].split('.') + if (tableNameAndAs.length === 2 && !(tableNameAndAs[1] in this.sqlConfig.ignoreTables)) { + queryParamsObj.params.push(tableNameAndAs[1]) + queryParamsObj.params.push(tableNameAndAs[0]) + } else { + queryParamsObj.grammarErr = 1 + console.log('there was no dot for tableName ', joinTables[index]); + } + + tableNameAndAs = [] + tableNameAndAs = joinTables[index + 2].split('.') + if (tableNameAndAs.length === 2 && !(tableNameAndAs[1] in this.sqlConfig.ignoreTables)) { + queryParamsObj.params.push(tableNameAndAs[1]) + queryParamsObj.params.push(tableNameAndAs[0]) + } else { + queryParamsObj.grammarErr = 1 + console.log('there was no dot for tableName ', joinTables[index]); + } + } + + } + + prepareJoinQuery(req, res, queryParamsObj) { + + queryParamsObj.query = 'SELECT ' + queryParamsObj.grammarErr = 0; + + /**************** 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) { + if (i) { + queryParamsObj.query += ',' + } + queryParamsObj.query += ' ?? ' + queryParamsObj.params.push(fields[i]) + } + + } else { + + queryParamsObj.query += ' * ' + + } + + queryParamsObj.query += ' from ' + /**************** END : get fields ****************/ + + + /**************** START : get join + on ****************/ + let joinTables = req.query._join.split(',') + if (joinTables.length < 3) { + //console.log('grammar error ', joinTables.length); + queryParamsObj.grammarErr = 1; + } + + //console.log('jointables.length', joinTables); + + let onCondnCount = 0; + + for (let i = 0; i < joinTables.length - 1 && queryParamsObj.grammarErr === 0; i = i + 2) { + + onCondnCount++; + + this._joinTableNames(i, joinTables, i, queryParamsObj) + + if (queryParamsObj.grammarErr) { + console.log('failed at _joinTableNames', queryParamsObj); + break; + } + + //console.log('after join tables', queryParamsObj); + + let onCondn = '_on' + (onCondnCount) + let onCondnObj = {} + if (onCondn in req.query) { + //console.log(onCondn, req.query[onCondn]); + onCondnObj = whereHelp.getConditionClause(req.query[onCondn], ' on ') + //console.log('onCondnObj', onCondnObj); + queryParamsObj.query += ' on ' + onCondnObj.query + queryParamsObj.params = queryParamsObj.params.concat(onCondnObj.params) + } else { + queryParamsObj.grammarErr = 1; + //console.log('No on condition: ', onCondn); + 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 = [] + } + + this.getWhereClause(req.query._where, ' ignore ', queryParamsObj, ' where '); + + //console.log('after where',queryParamsObj); + + this._getGrpByHavingOrderBy(req, 'ignore', queryParamsObj, 5) + + return queryParamsObj; + } + + + }