mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-05-03 00:13:57 +00:00
OR in group filter
This commit is contained in:
@@ -79,26 +79,27 @@ export class DesignerQueryDumper {
|
|||||||
return select;
|
return select;
|
||||||
}
|
}
|
||||||
|
|
||||||
buildConditionFromFilterField(tables: DesignerTableInfo[], filterField: string): Condition {
|
buildConditionFromFilterField(tables: DesignerTableInfo[], filterField: string, getExpression?: Function): Condition {
|
||||||
const conditions = [];
|
const conditions = [];
|
||||||
|
|
||||||
for (const column of this.designer.columns || []) {
|
for (const column of this.designer.columns || []) {
|
||||||
if (!column[filterField]) continue;
|
if (!column[filterField]) continue;
|
||||||
const table = (this.designer.tables || []).find(x => x.designerId == column.designerId);
|
|
||||||
if (!table) continue;
|
if (!column.isCustomExpression) {
|
||||||
if (!tables.find(x => x.designerId == table.designerId)) continue;
|
const table = (this.designer.tables || []).find(x => x.designerId == column.designerId);
|
||||||
|
if (!table) continue;
|
||||||
|
if (!tables.find(x => x.designerId == table.designerId)) continue;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const condition = parseFilter(column[filterField], findDesignerFilterType(column, this.designer));
|
const condition = parseFilter(column[filterField], findDesignerFilterType(column, this.designer));
|
||||||
if (condition) {
|
if (condition) {
|
||||||
conditions.push(
|
conditions.push(
|
||||||
_.cloneDeepWith(condition, expr => {
|
_.cloneDeepWith(condition, expr => {
|
||||||
if (expr.exprType == 'placeholder')
|
if (expr.exprType == 'placeholder') {
|
||||||
return {
|
if (getExpression) return getExpression(column);
|
||||||
exprType: 'column',
|
return this.getColumnExpression(column);
|
||||||
columnName: column.columnName,
|
}
|
||||||
source: findQuerySource(this.designer, column.designerId),
|
|
||||||
};
|
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -122,45 +123,39 @@ export class DesignerQueryDumper {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
addConditions(select: Select, tables: DesignerTableInfo[]) {
|
addConditionsCore(select: Select, tables: DesignerTableInfo[], filterFields, selectField, getExpression?) {
|
||||||
const additionalFilterCount = this.designer.settings?.additionalFilterCount || 0;
|
const conditions: Condition[] = _.compact(
|
||||||
|
filterFields.map(field => this.buildConditionFromFilterField(tables, field, getExpression))
|
||||||
const filterFields = ['filter', ..._.range(additionalFilterCount).map(index => `additionalFilter${index + 1}`)];
|
);
|
||||||
|
|
||||||
const conditions = _.compact(filterFields.map(field => this.buildConditionFromFilterField(tables, field)));
|
|
||||||
|
|
||||||
if (conditions.length == 0) {
|
if (conditions.length == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (conditions.length == 0) {
|
if (conditions.length == 0) {
|
||||||
select.where = mergeConditions(select.where, conditions[0]);
|
select[selectField] = mergeConditions(select[selectField], conditions[0]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
select.where = mergeConditions(select.where, {
|
select[selectField] = mergeConditions(select[selectField], {
|
||||||
conditionType: 'or',
|
conditionType: 'or',
|
||||||
conditions,
|
conditions,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
addGroupConditions(select: Select, tables: DesignerTableInfo[], selectIsGrouped: boolean) {
|
addConditions(select: Select, tables: DesignerTableInfo[]) {
|
||||||
for (const column of this.designer.columns || []) {
|
const additionalFilterCount = this.designer.settings?.additionalFilterCount || 0;
|
||||||
if (!column.groupFilter) continue;
|
const filterFields = ['filter', ..._.range(additionalFilterCount).map(index => `additionalFilter${index + 1}`)];
|
||||||
const table = (this.designer.tables || []).find(x => x.designerId == column.designerId);
|
this.addConditionsCore(select, tables, filterFields, 'where');
|
||||||
if (!table) continue;
|
}
|
||||||
if (!tables.find(x => x.designerId == table.designerId)) continue;
|
|
||||||
|
|
||||||
const condition = parseFilter(column.groupFilter, findDesignerFilterType(column, this.designer));
|
addGroupConditions(select: Select, tables: DesignerTableInfo[], selectIsGrouped: boolean) {
|
||||||
if (condition) {
|
const additionalGroupFilterCount = this.designer.settings?.additionalGroupFilterCount || 0;
|
||||||
select.having = mergeConditions(
|
const filterFields = [
|
||||||
select.having,
|
'groupFilter',
|
||||||
_.cloneDeepWith(condition, expr => {
|
..._.range(additionalGroupFilterCount).map(index => `additionalGroupFilter${index + 1}`),
|
||||||
if (expr.exprType == 'placeholder') {
|
];
|
||||||
return this.getColumnResultField(column, selectIsGrouped);
|
this.addConditionsCore(select, tables, filterFields, 'having', column =>
|
||||||
}
|
this.getColumnResultField(column, selectIsGrouped)
|
||||||
})
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getColumnExpression(col): Expression {
|
getColumnExpression(col): Expression {
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ export type DesignerColumnInfo = {
|
|||||||
export type DesignerSettings = {
|
export type DesignerSettings = {
|
||||||
isDistinct?: boolean;
|
isDistinct?: boolean;
|
||||||
additionalFilterCount?: number;
|
additionalFilterCount?: number;
|
||||||
|
additionalGroupFilterCount?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type DesignerInfo = {
|
export type DesignerInfo = {
|
||||||
|
|||||||
@@ -65,6 +65,26 @@
|
|||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const addGroupOrCondition = () => {
|
||||||
|
onChange(current => ({
|
||||||
|
...current,
|
||||||
|
settings: {
|
||||||
|
...current?.settings,
|
||||||
|
additionalGroupFilterCount: (current?.settings?.additionalGroupFilterCount ?? 0) + 1,
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
const removeGroupOrCondition = () => {
|
||||||
|
onChange(current => ({
|
||||||
|
...current,
|
||||||
|
settings: {
|
||||||
|
...current?.settings,
|
||||||
|
additionalGroupFilterCount: (current?.settings?.additionalGroupFilterCount ?? 1) - 1,
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
$: columns = value?.columns;
|
$: columns = value?.columns;
|
||||||
$: tables = value?.tables;
|
$: tables = value?.tables;
|
||||||
$: settings = value?.settings;
|
$: settings = value?.settings;
|
||||||
@@ -89,7 +109,18 @@
|
|||||||
slot: 5,
|
slot: 5,
|
||||||
props: { filterField: `additionalFilter${index + 1}` },
|
props: { filterField: `additionalFilter${index + 1}` },
|
||||||
})),
|
})),
|
||||||
hasGroupedColumn && { fieldName: 'groupFilter', header: 'Group filter', slot: 6 },
|
hasGroupedColumn && {
|
||||||
|
fieldName: 'groupFilter',
|
||||||
|
header: 'Group filter',
|
||||||
|
slot: 5,
|
||||||
|
props: { filterField: 'groupFilter' },
|
||||||
|
},
|
||||||
|
..._.range(hasGroupedColumn ? settings?.additionalGroupFilterCount || 0 : 0).map(index => ({
|
||||||
|
fieldName: `additionalGroupFilter${index + 1}`,
|
||||||
|
header: `OR group filter ${index + 2}`,
|
||||||
|
slot: 5,
|
||||||
|
props: { filterField: `additionalGroupFilter${index + 1}` },
|
||||||
|
})),
|
||||||
{ fieldName: 'actions', header: '', slot: 7 },
|
{ fieldName: 'actions', header: '', slot: 7 },
|
||||||
]}
|
]}
|
||||||
>
|
>
|
||||||
@@ -175,15 +206,6 @@
|
|||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</svelte:fragment>
|
</svelte:fragment>
|
||||||
<svelte:fragment slot="6" let:row>
|
|
||||||
<DataFilterControl
|
|
||||||
filterType={findDesignerFilterType(row, value)}
|
|
||||||
filter={row.groupFilter}
|
|
||||||
setFilter={groupFilter => {
|
|
||||||
changeColumn({ ...row, groupFilter });
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</svelte:fragment>
|
|
||||||
<svelte:fragment slot="7" let:row>
|
<svelte:fragment slot="7" let:row>
|
||||||
<InlineButton on:click={() => removeColumn(row)}>Remove</InlineButton>
|
<InlineButton on:click={() => removeColumn(row)}>Remove</InlineButton>
|
||||||
</svelte:fragment>
|
</svelte:fragment>
|
||||||
@@ -193,6 +215,12 @@
|
|||||||
{#if settings?.additionalFilterCount > 0}
|
{#if settings?.additionalFilterCount > 0}
|
||||||
<FormStyledButton value="Remove OR condition" on:click={removeOrCondition} style="width:200px" />
|
<FormStyledButton value="Remove OR condition" on:click={removeOrCondition} style="width:200px" />
|
||||||
{/if}
|
{/if}
|
||||||
|
{#if hasGroupedColumn}
|
||||||
|
<FormStyledButton value="Add group OR condition" on:click={addGroupOrCondition} style="width:200px" />
|
||||||
|
{/if}
|
||||||
|
{#if hasGroupedColumn && settings?.additionalGroupFilterCount > 0}
|
||||||
|
<FormStyledButton value="Remove group OR condition" on:click={removeGroupOrCondition} style="width:200px" />
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
|
|||||||
Reference in New Issue
Block a user