|
|
@ -348,13 +348,16 @@ class Xsql { |
|
|
|
if (i) { |
|
|
|
if (i) { |
|
|
|
queryParamsObj.query += ", "; |
|
|
|
queryParamsObj.query += ", "; |
|
|
|
} |
|
|
|
} |
|
|
|
if (orderByCols[i][0] === "-") { |
|
|
|
const aggregationFunction = this.getAggregationFunction(orderByCols[i]); |
|
|
|
let len = orderByCols[i].length; |
|
|
|
const columnName = this.getColumnNameWithoutAggregationFunctions(orderByCols[i]); |
|
|
|
queryParamsObj.query += " ?? DESC"; |
|
|
|
const orderByDirection = orderByCols[i][0] === "-" ? 'DESC' : 'ASC'; |
|
|
|
queryParamsObj.params.push(orderByCols[i].substring(1, len)); |
|
|
|
|
|
|
|
|
|
|
|
if (aggregationFunction) { |
|
|
|
|
|
|
|
queryParamsObj.query += `${aggregationFunction}(??) ${orderByDirection}`; |
|
|
|
|
|
|
|
queryParamsObj.params.push(columnName); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
queryParamsObj.query += " ?? ASC"; |
|
|
|
queryParamsObj.query += `?? ${orderByDirection}`; |
|
|
|
queryParamsObj.params.push(orderByCols[i]); |
|
|
|
queryParamsObj.params.push(columnName); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -376,7 +379,7 @@ class Xsql { |
|
|
|
|
|
|
|
|
|
|
|
getColumnsForGroupBy(tableName, reqQueryParams, queryParamsObj) { |
|
|
|
getColumnsForGroupBy(tableName, reqQueryParams, queryParamsObj) { |
|
|
|
const updatedQueryParams = Object.assign({}, reqQueryParams); |
|
|
|
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
|
|
|
|
// allows you to group by different fields than you have in the select
|
|
|
|
updatedQueryParams['_fields'] = updatedQueryParams['_groupbyfields']; |
|
|
|
updatedQueryParams['_fields'] = updatedQueryParams['_groupbyfields']; |
|
|
|
} |
|
|
|
} |
|
|
@ -424,21 +427,39 @@ class Xsql { |
|
|
|
if (i) { |
|
|
|
if (i) { |
|
|
|
queryParamsObj.query += ","; |
|
|
|
queryParamsObj.query += ","; |
|
|
|
} |
|
|
|
} |
|
|
|
queryParamsObj.query += "??"; |
|
|
|
const aggregationFunction = this.getAggregationFunction(cols[i]); |
|
|
|
queryParamsObj.params.push(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(","); |
|
|
|
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) { |
|
|
|
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); |
|
|
|
const aggFuncMatch = rawColumnName.match(AGGREGATION_FUNCTION_REGEX); |
|
|
|
if (aggFuncMatch && aggFuncMatch.length === 3) { |
|
|
|
if (aggFuncMatch && aggFuncMatch.length === 3) { |
|
|
|
// match will look like (3) ["AVG(timestamp)", "AVG", "timestamp", index: 0, input: "AVG(timestamp)", groups: undefined]
|
|
|
|
// match will look like (3) ["AVG(timestamp)", "AVG", "timestamp", index: 0, input: "AVG(timestamp)", groups: undefined]
|
|
|
|
return aggFuncMatch[2]; |
|
|
|
return aggFuncMatch[2]; |
|
|
|
} |
|
|
|
} |
|
|
|
return rawColumnName; |
|
|
|
return rawColumnName.replace(/-/, ''); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
removeUnknownColumns(inputColumns, tableName) { |
|
|
|
removeUnknownColumns(inputColumns, tableName) { |
|
|
|