|
|
|
@ -348,13 +348,16 @@ class Xsql {
|
|
|
|
|
if (i) { |
|
|
|
|
queryParamsObj.query += ", "; |
|
|
|
|
} |
|
|
|
|
if (orderByCols[i][0] === "-") { |
|
|
|
|
let len = orderByCols[i].length; |
|
|
|
|
queryParamsObj.query += " ?? DESC"; |
|
|
|
|
queryParamsObj.params.push(orderByCols[i].substring(1, len)); |
|
|
|
|
const aggregationFunction = this.getAggregationFunction(orderByCols[i]); |
|
|
|
|
const columnName = this.getColumnNameWithoutAggregationFunctions(orderByCols[i]); |
|
|
|
|
const orderByDirection = orderByCols[i][0] === "-" ? 'DESC' : 'ASC'; |
|
|
|
|
|
|
|
|
|
if (aggregationFunction) { |
|
|
|
|
queryParamsObj.query += `${aggregationFunction}(??) ${orderByDirection}`; |
|
|
|
|
queryParamsObj.params.push(columnName); |
|
|
|
|
} else { |
|
|
|
|
queryParamsObj.query += " ?? ASC"; |
|
|
|
|
queryParamsObj.params.push(orderByCols[i]); |
|
|
|
|
queryParamsObj.query += `?? ${orderByDirection}`; |
|
|
|
|
queryParamsObj.params.push(columnName); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -376,7 +379,7 @@ class Xsql {
|
|
|
|
|
|
|
|
|
|
getColumnsForGroupBy(tableName, reqQueryParams, queryParamsObj) { |
|
|
|
|
const updatedQueryParams = Object.assign({}, reqQueryParams); |
|
|
|
|
if ("_groupbyfields" in updatedQueryParams) { |
|
|
|
|
if ('_groupbyfields' in updatedQueryParams) { |
|
|
|
|
// allows you to group by different fields than you have in the select
|
|
|
|
|
updatedQueryParams['_fields'] = updatedQueryParams['_groupbyfields']; |
|
|
|
|
} |
|
|
|
@ -424,21 +427,39 @@ class Xsql {
|
|
|
|
|
if (i) { |
|
|
|
|
queryParamsObj.query += ","; |
|
|
|
|
} |
|
|
|
|
queryParamsObj.query += "??"; |
|
|
|
|
queryParamsObj.params.push(cols[i]); |
|
|
|
|
const aggregationFunction = this.getAggregationFunction(cols[i]); |
|
|
|
|
|
|
|
|
|
if (aggregationFunction) { |
|
|
|
|
queryParamsObj.query += `${aggregationFunction}(??)`; |
|
|
|
|
const columnName = this.getColumnNameWithoutAggregationFunctions(cols[i]); |
|
|
|
|
queryParamsObj.params.push(columnName); |
|
|
|
|
} else { |
|
|
|
|
queryParamsObj.query += "??"; |
|
|
|
|
queryParamsObj.params.push(cols[i]); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return cols.join(","); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getAggregationFunction(rawColumnName) { |
|
|
|
|
const AGGREGATION_FUNCTION_REGEX = /^[-]?(AVG|BIT_AND|BIT_OR|BIT_XOR|COUNT|COUNTDISTINCT|GROUP_CONCAT|JSON_ARRAYAGG|JSON_OBJECTAGG|MAX|MIN|STD|STDDEV|STDDEV_POP|STDDEV_SAMP|SUM|VAR_POP|VAR_SAMP|VARIANCE)\((.*)\)$/i; |
|
|
|
|
const aggFuncMatch = rawColumnName.match(AGGREGATION_FUNCTION_REGEX); |
|
|
|
|
if (aggFuncMatch && aggFuncMatch.length === 3) { |
|
|
|
|
// match will look like (3) ["AVG(timestamp)", "AVG", "timestamp", index: 0, input: "AVG(timestamp)", groups: undefined]
|
|
|
|
|
return aggFuncMatch[1]; |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getColumnNameWithoutAggregationFunctions(rawColumnName) { |
|
|
|
|
const AGGREGATION_FUNCTION_REGEX = /^(AVG|BIT_AND|BIT_OR|BIT_XOR|COUNT|COUNTDISTINCT|GROUP_CONCAT|JSON_ARRAYAGG|JSON_OBJECTAGG|MAX|MIN|STD|STDDEV|STDDEV_POP|STDDEV_SAMP|SUM|VAR_POP|VAR_SAMP|VARIANCE)\((.*)\)$/; |
|
|
|
|
const AGGREGATION_FUNCTION_REGEX = /^[-]?(AVG|BIT_AND|BIT_OR|BIT_XOR|COUNT|COUNTDISTINCT|GROUP_CONCAT|JSON_ARRAYAGG|JSON_OBJECTAGG|MAX|MIN|STD|STDDEV|STDDEV_POP|STDDEV_SAMP|SUM|VAR_POP|VAR_SAMP|VARIANCE)\((.*)\)$/i; |
|
|
|
|
const aggFuncMatch = rawColumnName.match(AGGREGATION_FUNCTION_REGEX); |
|
|
|
|
if (aggFuncMatch && aggFuncMatch.length === 3) { |
|
|
|
|
// match will look like (3) ["AVG(timestamp)", "AVG", "timestamp", index: 0, input: "AVG(timestamp)", groups: undefined]
|
|
|
|
|
return aggFuncMatch[2]; |
|
|
|
|
} |
|
|
|
|
return rawColumnName; |
|
|
|
|
return rawColumnName.replace(/-/, ''); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
removeUnknownColumns(inputColumns, tableName) { |
|
|
|
|