designer - aggregate, group by, order by

This commit is contained in:
Jan Prochazka 2020-12-29 15:07:32 +01:00
parent 9d10068799
commit ed11b9e5a1
4 changed files with 73 additions and 20 deletions

View File

@ -29,7 +29,8 @@ export function dumpSqlExpression(dmp: SqlDumper, expr: Expression) {
break;
case 'call':
dmp.put('%s(%s', expr.func, expr.argsPrefix);
dmp.put('%s(', expr.func);
if (expr.argsPrefix) dmp.put('%s ', expr.argsPrefix);
dmp.putCollection(',', expr.args, (x) => dumpSqlExpression(dmp, x));
dmp.put(')');
break;

View File

@ -88,14 +88,62 @@ export class DesignerQueryDumper {
const topLevelColumns = this.designer.columns.filter((col) =>
topLevelTables.find((tbl) => tbl.designerId == col.designerId)
);
const selectIsGrouped = !!topLevelColumns.find((x) => x.isGrouped || (x.aggregate && x.aggregate != '---'));
const outputColumns = topLevelColumns.filter((x) => x.isOutput);
if (outputColumns.length == 0) {
res.selectAll = true;
} else {
res.columns = outputColumns.map((col) => ({
res.columns = outputColumns.map((col) => {
const source = findQuerySource(this.designer, col.designerId);
const { columnName } = col;
let { alias } = col;
if (selectIsGrouped && !col.isGrouped) {
// use aggregate
const aggregate = col.aggregate == null || col.aggregate == '---' ? 'MAX' : col.aggregate;
if (!alias) alias = `${aggregate}(${columnName})`;
return {
exprType: 'call',
func: aggregate == 'COUNT DISTINCT' ? 'COUNT' : aggregate,
argsPrefix: aggregate == 'COUNT DISTINCT' ? 'DISTINCT' : null,
alias,
args: [
{
exprType: 'column',
columnName,
source,
},
],
};
} else {
return {
exprType: 'column',
columnName,
alias,
source,
};
}
});
}
const groupedColumns = topLevelColumns.filter((x) => x.isGrouped);
if (groupedColumns.length > 0) {
res.groupBy = groupedColumns.map((col) => ({
exprType: 'column',
columnName: col.columnName,
alias: col.alias,
source: findQuerySource(this.designer, col.designerId),
}));
}
const orderColumns = _.sortBy(
topLevelColumns.filter((x) => x.sortOrder),
(x) => Math.abs(x.sortOrder)
);
if (orderColumns.length > 0) {
res.orderBy = orderColumns.map((col) => ({
exprType: 'column',
direction: col.sortOrder < 0 ? 'DESC' : 'ASC',
columnName: col.columnName,
source: findQuerySource(this.designer, col.designerId),
}));
}

View File

@ -71,22 +71,24 @@ export default function QueryDesignColumns({ value, onChange }) {
<TableColumn
fieldName="aggregate"
header="Aggregate"
formatter={(row) => (
<SelectField
value={row.aggregate}
onChange={(e) => {
changeColumn({ ...row, aggregate: e.target.value });
}}
>
<option value="---">---</option>
<option value="MIN">MIN</option>
<option value="MAX">MAX</option>
<option value="COUNT">COUNT</option>
<option value="COUNT DISTINCT">COUNT DISTINCT</option>
<option value="SUM">SUM</option>
<option value="AVG">AVG</option>
</SelectField>
)}
formatter={(row) =>
!row.isGrouped && (
<SelectField
value={row.aggregate}
onChange={(e) => {
changeColumn({ ...row, aggregate: e.target.value });
}}
>
<option value="---">---</option>
<option value="MIN">MIN</option>
<option value="MAX">MAX</option>
<option value="COUNT">COUNT</option>
<option value="COUNT DISTINCT">COUNT DISTINCT</option>
<option value="SUM">SUM</option>
<option value="AVG">AVG</option>
</SelectField>
)
}
/>
<TableColumn
fieldName="sortOrder"
@ -95,7 +97,7 @@ export default function QueryDesignColumns({ value, onChange }) {
<SelectField
value={row.sortOrder}
onChange={(e) => {
changeColumn({ ...row, sortOrder: e.target.value });
changeColumn({ ...row, sortOrder: parseInt(e.target.value) });
}}
>
<option value="0">---</option>

View File

@ -26,7 +26,9 @@ export type DesignerColumnInfo = {
columnName: string;
alias?: string;
isGrouped?: boolean;
aggregate?: string;
isOutput?: boolean;
sortOrder?: number;
filter: string;
};