mirror of
https://github.com/nocodb/nocodb.git
synced 2026-05-03 08:07:33 +00:00
refactor : move prepare methods to from api to sql class
This commit is contained in:
275
lib/xsql.js
275
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user