From 7c03d31b843259f051318bdccde46093b0d55046 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Sun, 26 Feb 2023 15:44:29 +0100 Subject: [PATCH] mutli column condition for JSL data --- packages/datalib/src/GridDisplay.ts | 13 ++++++++++++- packages/sqltree/src/evaluateCondition.ts | 18 +++++++++++++++--- packages/sqltree/src/types.ts | 8 +++++++- packages/web/src/datagrid/JslDataGrid.svelte | 1 + .../web/src/datagrid/JslDataGridCore.svelte | 2 +- packages/web/src/formview/JslFormView.svelte | 2 +- 6 files changed, 37 insertions(+), 7 deletions(-) diff --git a/packages/datalib/src/GridDisplay.ts b/packages/datalib/src/GridDisplay.ts index 61630901..70656e76 100644 --- a/packages/datalib/src/GridDisplay.ts +++ b/packages/datalib/src/GridDisplay.ts @@ -738,7 +738,7 @@ export abstract class GridDisplay { // return sql; } - compileFilters(): Condition { + compileJslFilters(): Condition { const filters = this.config && this.config.filters; if (!filters) return null; const conditions = []; @@ -761,6 +761,17 @@ export abstract class GridDisplay { // filter parse error - ignore filter } } + + if (this.config.multiColumnFilter) { + const placeholderCondition = parseFilter(this.config.multiColumnFilter, 'string'); + if (placeholderCondition) { + conditions.push({ + conditionType: 'anyColumnPass', + placeholderCondition, + }); + } + } + if (conditions.length == 0) return null; return { conditionType: 'and', diff --git a/packages/sqltree/src/evaluateCondition.ts b/packages/sqltree/src/evaluateCondition.ts index f615af4e..795fe496 100644 --- a/packages/sqltree/src/evaluateCondition.ts +++ b/packages/sqltree/src/evaluateCondition.ts @@ -1,5 +1,6 @@ -import _ from 'lodash'; -import { Condition, BinaryCondition } from './types'; +import _cloneDeepWith from 'lodash/cloneDeepWith'; +import _escapeRegExp from 'lodash/escapeRegExp'; +import { Condition, Expression } from './types'; import { evaluateExpression } from './evaluateExpression'; function isEmpty(value) { @@ -10,7 +11,7 @@ function isEmpty(value) { function isLike(value, test) { if (!value) return false; if (!test) return false; - const regex = new RegExp(`^${_.escapeRegExp(test).replace(/%/g, '.*')}$`, 'i'); + const regex = new RegExp(`^${_escapeRegExp(test).replace(/%/g, '.*')}$`, 'i'); const res = !!value.toString().match(regex); return res; } @@ -55,5 +56,16 @@ export function evaluateCondition(condition: Condition, values) { return !isLike(evaluateExpression(condition.left, values), evaluateExpression(condition.right, values)); case 'not': return !evaluateCondition(condition.condition, values); + case 'anyColumnPass': + return Object.keys(values).some(columnName => { + const replaced = _cloneDeepWith(condition.placeholderCondition, (expr: Expression) => { + if (expr.exprType == 'placeholder') + return { + exprType: 'column', + columnName, + }; + }); + return evaluateCondition(replaced, values); + }); } } diff --git a/packages/sqltree/src/types.ts b/packages/sqltree/src/types.ts index 3e3e45a1..df9802ce 100644 --- a/packages/sqltree/src/types.ts +++ b/packages/sqltree/src/types.ts @@ -111,6 +111,11 @@ export interface RawTemplateCondition { expr: Expression; } +export interface AnyColumnPassEvalOnlyCondition { + conditionType: 'anyColumnPass'; + placeholderCondition: Condition; +} + export type Condition = | BinaryCondition | NotCondition @@ -121,7 +126,8 @@ export type Condition = | NotExistsCondition | BetweenCondition | InCondition - | RawTemplateCondition; + | RawTemplateCondition + | AnyColumnPassEvalOnlyCondition; export interface Source { name?: NamedObjectInfo; diff --git a/packages/web/src/datagrid/JslDataGrid.svelte b/packages/web/src/datagrid/JslDataGrid.svelte index e9f34fe9..8a98594c 100644 --- a/packages/web/src/datagrid/JslDataGrid.svelte +++ b/packages/web/src/datagrid/JslDataGrid.svelte @@ -88,6 +88,7 @@ expandMacros={!!dispatchChangeSet} onRunMacro={handleRunMacro} macroCondition={infoUsed?.__isDynamicStructure ? null : macro => macro.type == 'transformValue'} + hasMultiColumnFilter {changeSetState} {changeSetStore} {dispatchChangeSet} diff --git a/packages/web/src/datagrid/JslDataGridCore.svelte b/packages/web/src/datagrid/JslDataGridCore.svelte index edb302d2..7764d501 100644 --- a/packages/web/src/datagrid/JslDataGridCore.svelte +++ b/packages/web/src/datagrid/JslDataGridCore.svelte @@ -19,7 +19,7 @@ offset, limit, formatterFunction, - filters: display ? display.compileFilters() : null, + filters: display ? display.compileJslFilters() : null, sort: display.config.sort, }); diff --git a/packages/web/src/formview/JslFormView.svelte b/packages/web/src/formview/JslFormView.svelte index 19a1aac5..a34ced70 100644 --- a/packages/web/src/formview/JslFormView.svelte +++ b/packages/web/src/formview/JslFormView.svelte @@ -7,7 +7,7 @@ offset: index, limit: 1, formatterFunction, - filters: display ? display.compileFilters() : null, + filters: display ? display.compileJslFilters() : null, }); if (response.errorMessage) return response;