filter behaviour WIP

This commit is contained in:
Jan Prochazka 2024-08-19 12:49:26 +02:00
parent 2a48e0c4a0
commit c3609e8c7b
24 changed files with 105 additions and 76 deletions

View File

@ -2,6 +2,7 @@ import _ from 'lodash';
import { GridDisplay, ChangeCacheFunc, ChangeConfigFunc, DisplayColumn } from './GridDisplay';
import type { EngineDriver, ViewInfo, ColumnInfo, CollectionInfo } from 'dbgate-types';
import { GridConfig, GridCache } from './GridConfig';
import { mongoFilterBehaviour, standardFilterBehaviours } from 'dbgate-tools';
function getObjectKeys(obj) {
if (_.isArray(obj)) {
@ -52,7 +53,7 @@ function getColumnsForObject(basePath, obj, res: any[], display) {
}
}
function getDisplayColumn(basePath, columnName, display) {
function getDisplayColumn(basePath, columnName, display: CollectionGridDisplay) {
const uniquePath = [...basePath, columnName];
const uniqueName = uniquePath.join('.');
return {
@ -62,7 +63,7 @@ function getDisplayColumn(basePath, columnName, display) {
uniquePath,
isStructured: true,
parentHeaderText: createHeaderText(basePath),
filterType: 'mongo',
filterBehaviour: display?.driver?.getFilterBehaviour(null, standardFilterBehaviours) ?? mongoFilterBehaviour,
pureName: display.collection?.pureName,
schemaName: display.collection?.schemaName,
};

View File

@ -10,12 +10,13 @@ import type {
CollectionInfo,
SqlDialect,
ViewInfo,
FilterBehaviour,
} from 'dbgate-types';
import { parseFilter, getFilterType } from 'dbgate-filterparser';
import { parseFilter } from 'dbgate-filterparser';
import { filterName } from 'dbgate-tools';
import { ChangeSetFieldDefinition, ChangeSetRowDefinition } from './ChangeSet';
import { Expression, Select, treeToSql, dumpSqlSelect, Condition, CompoudCondition } from 'dbgate-sqltree';
import { isTypeLogical } from 'dbgate-tools';
import { isTypeLogical, standardFilterBehaviours, detectSqlFilterBehaviour, stringFilterBehaviour } from 'dbgate-tools';
export interface DisplayColumn {
schemaName: string;
@ -33,7 +34,7 @@ export interface DisplayColumn {
isChecked?: boolean;
hintColumnNames?: string[];
dataType?: string;
filterType?: boolean;
filterBehaviour?: FilterBehaviour;
isStructured?: boolean;
}
@ -92,7 +93,7 @@ export abstract class GridDisplay {
isLoadedCorrectly = true;
supportsReload = false;
isDynamicStructure = false;
filterTypeOverride = null;
filterBehaviourOverride = null;
setColumnVisibility(uniquePath: string[], isVisible: boolean) {
const uniqueName = uniquePath.join('.');
@ -192,7 +193,11 @@ export abstract class GridDisplay {
const column = displayedColumnInfo[uniqueName];
if (!column) continue;
try {
const condition = parseFilter(filter, getFilterType(column.dataType));
const condition = parseFilter(
filter,
this.driver?.getFilterBehaviour(column.dataType, standardFilterBehaviours) ??
detectSqlFilterBehaviour(column.dataType)
);
if (condition) {
conditions.push(
_.cloneDeepWith(condition, (expr: Expression) => {
@ -220,7 +225,7 @@ export abstract class GridDisplay {
};
for (const column of this.baseTableOrView.columns) {
try {
const condition = parseFilter(this.config.multiColumnFilter, getFilterType(column.dataType));
const condition = parseFilter(this.config.multiColumnFilter, detectSqlFilterBehaviour(column.dataType));
if (condition) {
orCondition.conditions.push(
_.cloneDeepWith(condition, (expr: Expression) => {
@ -748,10 +753,12 @@ export abstract class GridDisplay {
for (const name in filters) {
const column = this.isDynamicStructure ? null : this.columns.find(x => x.columnName == name);
if (!this.isDynamicStructure && !column) continue;
const filterType =
this.filterTypeOverride ?? (this.isDynamicStructure ? 'mongo' : getFilterType(column.dataType));
const filterBehaviour =
this.filterBehaviourOverride ??
this.driver?.getFilterBehaviour(column.dataType, standardFilterBehaviours) ??
detectSqlFilterBehaviour(column.dataType);
try {
const condition = parseFilter(filters[name], filterType);
const condition = parseFilter(filters[name], filterBehaviour);
const replaced = _.cloneDeepWith(condition, (expr: Expression) => {
if (expr.exprType == 'placeholder')
return {
@ -766,7 +773,7 @@ export abstract class GridDisplay {
}
if (this.config.multiColumnFilter) {
const placeholderCondition = parseFilter(this.config.multiColumnFilter, 'string');
const placeholderCondition = parseFilter(this.config.multiColumnFilter, stringFilterBehaviour);
if (placeholderCondition) {
conditions.push({
conditionType: 'anyColumnPass',

View File

@ -2,6 +2,7 @@ import _ from 'lodash';
import { GridDisplay, ChangeCacheFunc, ChangeConfigFunc } from './GridDisplay';
import { GridConfig, GridCache } from './GridConfig';
import { analyseCollectionDisplayColumns } from './CollectionGridDisplay';
import { evalFilterBehaviour } from 'dbgate-tools';
export class JslGridDisplay extends GridDisplay {
constructor(
@ -22,7 +23,7 @@ export class JslGridDisplay extends GridDisplay {
this.sortable = true;
this.supportsReload = supportsReload;
this.isDynamicStructure = isDynamicStructure;
this.filterTypeOverride = 'eval';
this.filterBehaviourOverride = evalFilterBehaviour;
this.editable = editable;
this.editableStructure = editable ? structure : null;

View File

@ -1,12 +0,0 @@
import { isTypeNumber, isTypeString, isTypeLogical, isTypeDateTime } from 'dbgate-tools';
import { FilterBehaviour } from 'dbgate-types';
import { DatetimeFilterBehaviour, LogicalFilterBehaviour, NumberFilterBehaviour, StringFilterBehaviour } from './filterTypes';
export function detectSqlFilterType(dataType: string): FilterBehaviour {
if (!dataType) return StringFilterBehaviour;
if (isTypeNumber(dataType)) return NumberFilterBehaviour;
if (isTypeString(dataType)) return StringFilterBehaviour;
if (isTypeLogical(dataType)) return LogicalFilterBehaviour;
if (isTypeDateTime(dataType)) return DatetimeFilterBehaviour;
return StringFilterBehaviour;
}

View File

@ -1,3 +1,2 @@
export * from './parseFilter';
export * from './detectSqlFilterBehaviour';
export * from './filterTool';

View File

@ -1,8 +1,8 @@
const { parseFilter } = require('./parseFilter');
const { StringFilterType } = require('./filterTypes');
const { stringFilterBehaviour } = require('dbgate-tools');
test('parse string', () => {
const ast = parseFilter('"123"', StringFilterType);
const ast = parseFilter('"123"', stringFilterBehaviour);
console.log(JSON.stringify(ast));
expect(ast).toEqual({
conditionType: 'like',

View File

@ -0,0 +1,17 @@
import { FilterBehaviour } from 'dbgate-types';
import {
datetimeFilterBehaviour,
logicalFilterBehaviour,
numberFilterBehaviour,
stringFilterBehaviour,
} from './filterBehaviours';
import { isTypeDateTime, isTypeLogical, isTypeNumber, isTypeString } from './commonTypeParser';
export function detectSqlFilterBehaviour(dataType: string): FilterBehaviour {
if (!dataType) return stringFilterBehaviour;
if (isTypeNumber(dataType)) return numberFilterBehaviour;
if (isTypeString(dataType)) return stringFilterBehaviour;
if (isTypeLogical(dataType)) return logicalFilterBehaviour;
if (isTypeDateTime(dataType)) return datetimeFilterBehaviour;
return stringFilterBehaviour;
}

View File

@ -3,6 +3,7 @@ import { SqlDumper } from './SqlDumper';
import { splitQuery } from 'dbgate-query-splitter';
import { dumpSqlSelect } from 'dbgate-sqltree';
import { EngineDriver, QueryResult, RunScriptOptions } from 'dbgate-types';
import { detectSqlFilterBehaviour } from './detectSqlFilterBehaviour';
const dialect = {
limitSelect: true,
@ -150,4 +151,7 @@ export const driverBase = {
showConnectionField: (field, values) => false,
showConnectionTab: field => true,
getAccessTokenFromAuth: async (connection, req) => null,
getFilterBehaviour(dataType: string, standardFilterBehaviours) {
return detectSqlFilterBehaviour(dataType);
},
};

View File

@ -1,6 +1,6 @@
import { FilterBehaviour } from 'dbgate-types';
export const NumberFilterBehaviour: FilterBehaviour = {
export const numberFilterBehaviour: FilterBehaviour = {
compilerType: 'sqlTree',
supportEquals: true,
supportNumberLikeComparison: true,
@ -10,7 +10,7 @@ export const NumberFilterBehaviour: FilterBehaviour = {
allowNumberToken: true,
};
export const StringFilterBehaviour: FilterBehaviour = {
export const stringFilterBehaviour: FilterBehaviour = {
compilerType: 'sqlTree',
supportEquals: true,
supportStringInclusion: true,
@ -23,14 +23,14 @@ export const StringFilterBehaviour: FilterBehaviour = {
allowHexString: true,
};
export const LogicalFilterBehaviour: FilterBehaviour = {
export const logicalFilterBehaviour: FilterBehaviour = {
compilerType: 'sqlTree',
supportBooleanValues: true,
supportNullTesting: true,
supportSqlCondition: true,
};
export const DatetimeFilterBehaviour: FilterBehaviour = {
export const datetimeFilterBehaviour: FilterBehaviour = {
compilerType: 'sqlTree',
supportNullTesting: true,
supportSqlCondition: true,
@ -38,7 +38,7 @@ export const DatetimeFilterBehaviour: FilterBehaviour = {
supportDatetimeComparison: true,
};
export const MongoFilterBehaviour: FilterBehaviour = {
export const mongoFilterBehaviour: FilterBehaviour = {
compilerType: 'mongoCondition',
supportEquals: true,
supportArrayTesting: true,
@ -48,7 +48,7 @@ export const MongoFilterBehaviour: FilterBehaviour = {
supportExistsTesting: true,
};
export const EvalFilterBehaviour: FilterBehaviour = {
export const evalFilterBehaviour: FilterBehaviour = {
compilerType: 'sqlTree',
supportEquals: true,
supportStringInclusion: true,
@ -58,3 +58,12 @@ export const EvalFilterBehaviour: FilterBehaviour = {
allowStringToken: true,
};
export const standardFilterBehaviours = {
numberFilterBehaviour,
stringFilterBehaviour,
logicalFilterBehaviour,
datetimeFilterBehaviour,
mongoFilterBehaviour,
evalFilterBehaviour,
};

View File

@ -21,3 +21,5 @@ export * from './preloadedRowsTools';
export * from './ScriptWriter';
export * from './getLogger';
export * from './getConnectionLabel';
export * from './detectSqlFilterBehaviour';
export * from './filterBehaviours';

View File

@ -72,7 +72,11 @@ export interface ServerSummary {
databases: ServerSummaryDatabase[];
}
export interface EngineDriver {
export interface FilterBehaviourProvider {
getFilterBehaviour(dataType: string, standardFilterBehaviours: { string: FilterBehaviour }): FilterBehaviour;
}
export interface EngineDriver extends FilterBehaviourProvider {
engine: string;
title: string;
defaultPort?: number;
@ -154,7 +158,6 @@ export interface EngineDriver {
getRedirectAuthUrl(connection, options): Promise<{ url: string; sid: string }>;
getAuthTokenFromCode(connection, options): Promise<string>;
getAccessTokenFromAuth(connection, req): Promise<string | null>;
getFilterType(dataType: string): FilterBehaviour;
analyserClass?: any;
dumperClass?: any;

View File

@ -19,7 +19,6 @@
import ValueLookupModal from '../modals/ValueLookupModal.svelte';
export let isReadOnly = false;
export let filterType;
export let filterBehaviour;
export let filter;
export let setFilter;
@ -51,7 +50,7 @@
$: if (onGetReference && domInput) onGetReference(domInput);
function openFilterWindow(condition1) {
showModal(SetFilterModal, { condition1, filterType, onFilter: setFilter });
showModal(SetFilterModal, { condition1, filterBehaviour, onFilter: setFilter });
}
const filterMultipleValues = () => {
@ -246,7 +245,7 @@
isOk = false;
isError = false;
if (value) {
parseFilter(value, filterType);
parseFilter(value, filterBehaviour);
isOk = true;
}
} catch (err) {

View File

@ -78,7 +78,6 @@
export let display;
export let changeSetState;
export let dispatchChangeSet;
export let useEvalFilters = false;
export let isDetailView = false;
export let showReferences = false;
@ -184,7 +183,6 @@
{...$$props}
{managerSize}
{isDynamicStructure}
{useEvalFilters}
{isFormView}
{hasMultiColumnFilter}
driver={display?.driver}

View File

@ -343,13 +343,13 @@
<script lang="ts">
import { GridDisplay } from 'dbgate-datalib';
import { driverBase, parseCellValue } from 'dbgate-tools';
import { driverBase, parseCellValue, detectSqlFilterBehaviour } from 'dbgate-tools';
import { getContext, onDestroy } from 'svelte';
import _, { map } from 'lodash';
import registerCommand from '../commands/registerCommand';
import ColumnHeaderControl from './ColumnHeaderControl.svelte';
import DataGridRow from './DataGridRow.svelte';
import { getFilterType, getFilterValueExpression } from 'dbgate-filterparser';
import { getFilterValueExpression } from 'dbgate-filterparser';
import stableStringify from 'json-stable-stringify';
import contextMenu, { getContextMenu, registerMenu } from '../utility/contextMenu';
import { tick } from 'svelte';
@ -435,7 +435,6 @@
export let tabControlHiddenTab = false;
export let onCustomGridRefresh = null;
export let onOpenQuery = null;
export let useEvalFilters = false;
export let jslid;
// export let generalAllowSave = false;
@ -1901,7 +1900,9 @@
{jslid}
{formatterFunction}
driver={display?.driver}
filterType={useEvalFilters ? 'eval' : col.filterType || getFilterType(col.dataType)}
filterBehaviour={display?.filterBehaviourOverride ??
col.filterBehaviour ??
detectSqlFilterBehaviour(col.dataType)}
filter={display.getFilter(col.uniqueName)}
setFilter={value => display.setFilter(col.uniqueName, value)}
showResizeSplitter

View File

@ -87,7 +87,6 @@
formViewComponent={JslFormView}
setLoadedRows={handleSetLoadedRows}
isDynamicStructure={!!infoUsed?.__isDynamicStructure}
useEvalFilters
showMacros={!!dispatchChangeSet}
expandMacros={!!dispatchChangeSet}
onRunMacro={handleRunMacro}

View File

@ -10,7 +10,7 @@ import {
referenceIsConnecting,
mergeSelectsFromDesigner,
findQuerySource,
findDesignerFilterType,
findDesignerFilterBehaviour,
} from './designerTools';
import { parseFilter } from 'dbgate-filterparser';
@ -83,7 +83,7 @@ export class DesignerQueryDumper {
}
try {
const condition = parseFilter(column[filterField], findDesignerFilterType(column, this.designer));
const condition = parseFilter(column[filterField], findDesignerFilterBehaviour(column, this.designer));
if (condition) {
conditions.push(
_.cloneDeepWith(condition, expr => {

View File

@ -1,6 +1,6 @@
<script lang="ts">
import { presetDarkPalettes, presetPalettes } from '@ant-design/colors';
import { filterName } from 'dbgate-tools';
import { filterName, stringFilterBehaviour } from 'dbgate-tools';
import { tick } from 'svelte';
import { createDatabaseObjectMenu } from '../appobj/DatabaseObjectAppObject.svelte';
@ -303,7 +303,7 @@
</div>
{#if settings?.getMutliColumnFilter && settings?.setMutliColumnFilter}
<DataFilterControl
filterType="string"
filterBehaviour={stringFilterBehaviour}
filter={settings?.getMutliColumnFilter(designerId)}
setFilter={value => settings?.setMutliColumnFilter(designerId, value)}
placeholder="Data filter"

View File

@ -5,7 +5,7 @@ import type { EngineDriver } from 'dbgate-types';
import type { DesignerInfo, DesignerTableInfo, DesignerReferenceInfo, DesignerJoinType } from './types';
import { DesignerComponentCreator } from './DesignerComponentCreator';
import { DesignerQueryDumper } from './DesignerQueryDumper';
import { getFilterType } from 'dbgate-filterparser';
import { detectSqlFilterBehaviour } from 'dbgate-tools';
export function referenceIsConnecting(
reference: DesignerReferenceInfo,
@ -133,13 +133,13 @@ export function isConnectedByReference(
return array1 == array2;
}
export function findDesignerFilterType({ designerId, columnName }, designer) {
export function findDesignerFilterBehaviour({ designerId, columnName }, designer) {
const table = (designer.tables || []).find(x => x.designerId == designerId);
if (table) {
const column = (table.columns || []).find(x => x.columnName == columnName);
if (column) {
const { dataType } = column;
return getFilterType(dataType);
return detectSqlFilterBehaviour(dataType);
}
}
return 'string';

View File

@ -8,7 +8,7 @@
<script>
import DataFilterControl from '../datagrid/DataFilterControl.svelte';
import { findDesignerFilterType } from '../designer/designerTools';
import { findDesignerFilterBehaviour } from '../designer/designerTools';
import CheckboxField from '../forms/CheckboxField.svelte';
import SelectField from '../forms/SelectField.svelte';
import TextField from '../forms/TextField.svelte';
@ -238,7 +238,7 @@
</svelte:fragment>
<svelte:fragment slot="5" let:row let:filterField>
<DataFilterControl
filterType={findDesignerFilterType(row, value)}
filterBehaviour={findDesignerFilterBehaviour(row, value)}
filter={row[filterField]}
setFilter={filter => {
changeColumn({ ...row, [filterField]: filter });

View File

@ -1,5 +1,5 @@
<script lang="ts">
import { getFilterType } from 'dbgate-filterparser';
import { detectSqlFilterBehaviour, standardFilterBehaviours, mongoFilterBehaviour } from 'dbgate-tools';
import DataFilterControl from '../datagrid/DataFilterControl.svelte';
@ -17,22 +17,20 @@
export let schemaName;
export let pureName;
export let useEvalFilters;
export let isDynamicStructure;
export let isFormView;
$: column = isFormView
? display.formColumns?.find(x => x.uniqueName == uniqueName)
: display?.findColumn(uniqueName);
$: column = isFormView ? display.formColumns?.find(x => x.uniqueName == uniqueName) : display?.findColumn(uniqueName);
function computeFilterType(column, isDynamicStructure, useEvalFilters) {
if (useEvalFilters) return 'eval';
if (isDynamicStructure) return 'mongo';
if (column) {
return column.filterType || getFilterType(column.dataType);
function computeFilterBehavoir(column, display, isDynamicStructure) {
if (display?.filterBehaviourOverride) {
return display?.filterBehaviourOverride;
}
return 'string';
const fromDriver = display?.driver?.getFilterBehaviour(column.dataType, standardFilterBehaviours);
if (fromDriver) return fromDriver;
if (isDynamicStructure) return mongoFilterBehaviour;
return detectSqlFilterBehaviour(column.dataType);
}
</script>
@ -51,7 +49,7 @@
</InlineButton>
</div>
<DataFilterControl
filterType={computeFilterType(column, isDynamicStructure, useEvalFilters)}
filterBehaviour={computeFilterBehavoir(column, display, isDynamicStructure)}
filter={filters[uniqueName]}
setFilter={value => display.setFilter(uniqueName, value)}
{driver}

View File

@ -20,7 +20,6 @@
export let pureName;
export let isDynamicStructure;
export let useEvalFilters;
export let isFormView;
export let hasMultiColumnFilter;
@ -93,7 +92,6 @@
{#each allFilterNames as uniqueName}
<FormViewFilterColumn
{isDynamicStructure}
{useEvalFilters}
{isFormView}
{uniqueName}
{display}

View File

@ -13,7 +13,7 @@
export let condition1;
export let onFilter;
export let filterType;
export let filterBehaviour;
const hasOperand = condition => {
return condition != 'NULL' && condition != 'NOT NULL' && condition != 'EXISTS' && condition != 'NOT EXISTS';
@ -24,7 +24,9 @@
if (!value) return null;
if (condition == 'sql') return `{${value}}`;
if (condition == 'sqlRight') return `{$$ ${value}}`;
if (filterType == 'string') return `${condition}"${value}"`;
if (filterBehaviour.allowStringToken) {
return `${condition}"${value}"`;
}
return `${condition}${value}`;
};
@ -48,7 +50,7 @@
<div class="row">Show rows where</div>
<div class="row">
<div class="col-6 mr-1">
<SetFilterModal_Select {filterType} name="condition1" />
<SetFilterModal_Select {filterBehaviour} name="condition1" />
</div>
<div class="col-6 mr-1">
<FormValues let:values>
@ -66,7 +68,7 @@
<div class="row">
<div class="col-6 mr-1">
<SetFilterModal_Select {filterType} name="condition2" />
<SetFilterModal_Select {filterBehaviour} name="condition2" />
</div>
<div class="col-6 mr-1">
<FormValues let:values>

View File

@ -2,7 +2,6 @@
import FormSelectFieldRaw from '../forms/FormSelectFieldRaw.svelte';
export let name;
export let filterType;
export let filterBehaviour;
function getOptions() {

View File

@ -93,6 +93,10 @@ const driver = {
}
return res;
},
getFilterBehaviour(dataType, standardFilterBehaviours) {
return standardFilterBehaviours.mongoFilterBehaviour;
},
};
module.exports = driver;