export { }; const { whereGenerator } = require('./utils'); const { cloneDeep } = require('lodash'); import { IWhere } from './interface'; const selectBuilder = (params: any, config: any) => { let query = `SELECT *, count(*) over () as ${config.totatRowsKey} FROM ${config.tableName} ${buildWhere(params.where, params.search, config)} ${orderCreation(params.orderByList)} ${limitAndOffset(params.limit, params.offset)}` return query } // const buildWhere = (whereObject: IWhere = {}, searchArray: Array = [], config: any) => { // let whereArray = (whereObject || {}).dimensionNameValueList || []; // if (whereArray.length == 0 && searchArray.length == 0) { // return '' // } // let whereStr = ' where '; // if (whereArray.length > 0) { // whereStr += ' ( '; // for (let i = 0; i < whereArray.length; i++) { // let where = whereArray[i]; // whereStr += whereGenerator(where.operator || 'EQUAL_TO', where.dimensionName, where.dimensionValue, config); // if (i + 1 < whereArray.length) { // whereStr += ' AND ' // } // } // whereStr += ' ) '; // } // return whereStr // } const convertObjectToArray: any = (obj: any) => { let result = Object.keys(obj).map(function (key) { return { 'key': key, 'value': obj[key] }; }); return result; } const buildWhere = (whereObject: IWhere = {}, searchArray: Array = [], config: any) => { let whereArray = (whereObject || {}).dimensionNameValueList || []; if (whereArray.length == 0 && searchArray.length == 0 && !whereObject.date) { return '' } let searchIndex = whereArray.findIndex((item) => { return item.dimensionName === 'search' }) let searchObj: any = null; if (searchIndex !== -1) { searchObj = cloneDeep(whereArray[searchIndex]); whereArray.splice(searchIndex, 1); } let whereObj: any = {}; let _whereArray = cloneDeep(whereArray); for (let i = 0; i < _whereArray.length; i++) { let row = _whereArray[i]; if (!whereObj[row.dimensionName]) { whereObj[row.dimensionName] = []; } whereObj[row.dimensionName].push(row) } let convertedArray = convertObjectToArray(whereObj); let whereStr = ' where '; if (whereObject.date) { let key = config.DATE_KEY; whereStr += ` ${key} BETWEEN '${whereObject.date.from}' AND '${whereObject.date.to}' `; if (whereArray.length > 0) { whereStr += ' AND '; } } for (let j = 0; j < convertedArray.length; j++) { let arr = convertedArray[j].value; let str = ' ( '; for (let i = 0; i < arr.length; i++) { let where = arr[i]; str += whereGenerator(where.operator || 'EQUAL_TO', where.dimensionName, where.dimensionValue, config); if (i + 1 < arr.length) { str += ' OR ' } } str += ' ) ' whereStr += str; if (j + 1 < convertedArray.length) { whereStr += ' AND ' } } if (searchObj && config.SEARCHCONFIG) { let column = config.SEARCHCONFIG.columns; let str = ' AND ( ' for (let i = 0; i < column.length; i++) { str += whereGenerator(searchObj.operator || 'EQUAL_TO', column[i], searchObj.dimensionValue, config); if (i + 1 < column.length) { str += ' OR ' } } str += ' ) '; whereStr += str } return whereStr } const limitAndOffset = (limit: number = 10, offset: number) => { let str = ''; if (limit) { str += `LIMIT ${limit} ` } if (offset) { str += `OFFSET ${offset}` } return str } const orderCreation = (orderArray: Array = []) => { if (orderArray.length == 0) { return '' } let str = 'ORDER BY '; for (let i = 0; i < orderArray.length; i++) { let order = orderArray[i]; str += `${order.dimension} ${order.direction} `; if (i + 1 < orderArray.length) { str += ',' } } return str; } exports.selectBuilder = selectBuilder;