structured filter type => filterBehaviour

This commit is contained in:
Jan Prochazka
2024-08-19 10:26:18 +02:00
parent d0fa565704
commit 2a48e0c4a0
10 changed files with 65 additions and 67 deletions

View File

@@ -1,9 +1,7 @@
import P from 'parsimmon';
import moment from 'moment';
import { FilterType } from './types';
import { Condition } from 'dbgate-sqltree';
import type { TransformType } from 'dbgate-types';
import { interpretEscapes, token, word, whitespace } from './common';
import { token, word, whitespace } from './common';
const compoudCondition = conditionType => conditions => {
if (conditions.length == 1) return conditions[0];

View File

@@ -0,0 +1,12 @@
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,12 +0,0 @@
import { isTypeNumber, isTypeString, isTypeLogical, isTypeDateTime } from 'dbgate-tools';
import { StructuredFilterType } from 'dbgate-types';
import { DatetimeFilterType, LogicalFilterType, NumberFilterType, StringFilterType } from './filterTypes';
export function detectSqlFilterType(dataType: string): StructuredFilterType {
if (!dataType) return StringFilterType;
if (isTypeNumber(dataType)) return NumberFilterType;
if (isTypeString(dataType)) return StringFilterType;
if (isTypeLogical(dataType)) return LogicalFilterType;
if (isTypeDateTime(dataType)) return DatetimeFilterType;
return StringFilterType;
}

View File

@@ -1,6 +1,6 @@
import { StructuredFilterType } from 'dbgate-types';
import { FilterBehaviour } from 'dbgate-types';
export const NumberFilterType: StructuredFilterType = {
export const NumberFilterBehaviour: FilterBehaviour = {
compilerType: 'sqlTree',
supportEquals: true,
supportNumberLikeComparison: true,
@@ -10,7 +10,7 @@ export const NumberFilterType: StructuredFilterType = {
allowNumberToken: true,
};
export const StringFilterType: StructuredFilterType = {
export const StringFilterBehaviour: FilterBehaviour = {
compilerType: 'sqlTree',
supportEquals: true,
supportStringInclusion: true,
@@ -23,14 +23,14 @@ export const StringFilterType: StructuredFilterType = {
allowHexString: true,
};
export const LogicalFilterType: StructuredFilterType = {
export const LogicalFilterBehaviour: FilterBehaviour = {
compilerType: 'sqlTree',
supportBooleanValues: true,
supportNullTesting: true,
supportSqlCondition: true,
};
export const DatetimeFilterType: StructuredFilterType = {
export const DatetimeFilterBehaviour: FilterBehaviour = {
compilerType: 'sqlTree',
supportNullTesting: true,
supportSqlCondition: true,
@@ -38,7 +38,7 @@ export const DatetimeFilterType: StructuredFilterType = {
supportDatetimeComparison: true,
};
export const MongoFilterType: StructuredFilterType = {
export const MongoFilterBehaviour: FilterBehaviour = {
compilerType: 'mongoCondition',
supportEquals: true,
supportArrayTesting: true,
@@ -48,7 +48,7 @@ export const MongoFilterType: StructuredFilterType = {
supportExistsTesting: true,
};
export const EvalFilterType: StructuredFilterType = {
export const EvalFilterBehaviour: FilterBehaviour = {
compilerType: 'sqlTree',
supportEquals: true,
supportStringInclusion: true,

View File

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

View File

@@ -4,7 +4,7 @@ import { interpretEscapes, token, word, whitespace } from './common';
import { mongoParser } from './mongoParser';
import { datetimeParser } from './datetimeParser';
import { hexStringToArray } from 'dbgate-tools';
import { StructuredFilterType } from 'dbgate-types';
import { FilterBehaviour } from 'dbgate-types';
const binaryCondition = operator => value => ({
conditionType: 'binary',
@@ -77,7 +77,7 @@ const sqlTemplate = templateSql => {
};
};
const createParser = (structuredFilterType: StructuredFilterType) => {
const createParser = (filterBehaviour: FilterBehaviour) => {
const langDef = {
string1: () =>
token(P.regexp(/"((?:\\.|.)*?)"/, 1))
@@ -155,44 +155,44 @@ const createParser = (structuredFilterType: StructuredFilterType) => {
};
const allowedValues = []; // 'string1', 'string2', 'number', 'noQuotedString'];
if (structuredFilterType.allowStringToken) {
if (filterBehaviour.allowStringToken) {
allowedValues.push('string1', 'string2', 'noQuotedString');
}
if (structuredFilterType.allowNumberToken) {
if (filterBehaviour.allowNumberToken) {
allowedValues.push('string1Num', 'string2Num', 'number');
}
const allowedElements = [];
if (structuredFilterType.supportNullTesting) {
if (filterBehaviour.supportNullTesting) {
allowedElements.push('null', 'notNull');
}
if (structuredFilterType.supportEquals) {
if (filterBehaviour.supportEquals) {
allowedElements.push('eq', 'ne', 'ne2');
}
if (structuredFilterType.supportSqlCondition) {
if (filterBehaviour.supportSqlCondition) {
allowedElements.push('sql');
}
if (structuredFilterType.supportNumberLikeComparison || structuredFilterType.supportDatetimeComparison) {
if (filterBehaviour.supportNumberLikeComparison || filterBehaviour.supportDatetimeComparison) {
allowedElements.push('le', 'ge', 'lt', 'gt');
}
if (structuredFilterType.supportEmpty) {
if (filterBehaviour.supportEmpty) {
allowedElements.push('empty', 'notEmpty');
}
if (structuredFilterType.allowHexString) {
if (filterBehaviour.allowHexString) {
allowedElements.push('hexTestEq');
}
if (structuredFilterType.supportStringInclusion) {
if (filterBehaviour.supportStringInclusion) {
allowedElements.push('startsWith', 'endsWith', 'contains', 'startsWithNot', 'endsWithNot', 'containsNot');
}
if (structuredFilterType.supportBooleanValues) {
if (structuredFilterType.allowNumberToken || structuredFilterType.allowStringToken) {
if (filterBehaviour.supportBooleanValues) {
if (filterBehaviour.allowNumberToken || filterBehaviour.allowStringToken) {
allowedElements.push('true', 'false');
} else {
allowedElements.push('true', 'false', 'trueNum', 'falseNum');
@@ -200,7 +200,7 @@ const createParser = (structuredFilterType: StructuredFilterType) => {
}
// must be last
if (structuredFilterType.allowStringToken) {
if (filterBehaviour.allowStringToken) {
allowedElements.push('valueTestStr');
} else {
allowedElements.push('valueTestEq');
@@ -211,22 +211,22 @@ const createParser = (structuredFilterType: StructuredFilterType) => {
const cachedFilters: { [key: string]: P.Language } = {};
function getParser(structuredFilterType: StructuredFilterType) {
if (structuredFilterType.compilerType == 'mongoCondition') {
function getParser(filterBehaviour: FilterBehaviour) {
if (filterBehaviour.compilerType == 'mongoCondition') {
return mongoParser;
}
if (structuredFilterType.compilerType == 'datetime') {
if (filterBehaviour.compilerType == 'datetime') {
return datetimeParser;
}
const key = JSON.stringify(structuredFilterType);
const key = JSON.stringify(filterBehaviour);
if (!cachedFilters[key]) {
cachedFilters[key] = createParser(structuredFilterType);
cachedFilters[key] = createParser(filterBehaviour);
}
return cachedFilters[key];
}
export function parseFilter(value: string, structuredFilterType: StructuredFilterType): Condition {
const parser = getParser(structuredFilterType);
export function parseFilter(value: string, filterBehaviour: FilterBehaviour): Condition {
const parser = getParser(filterBehaviour);
const ast = parser.list.tryParse(value);
// console.log('AST', ast);
return ast;

View File

@@ -3,7 +3,7 @@ import { QueryResult } from './query';
import { SqlDialect } from './dialect';
import { SqlDumper } from './dumper';
import { DatabaseInfo, NamedObjectInfo, TableInfo, ViewInfo, ProcedureInfo, FunctionInfo, TriggerInfo } from './dbinfo';
import { StructuredFilterType } from './filter-type';
import { FilterBehaviour } from './filter-type';
export interface StreamOptions {
recordset: (columns) => void;
@@ -154,7 +154,7 @@ 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): StructuredFilterType;
getFilterType(dataType: string): FilterBehaviour;
analyserClass?: any;
dumperClass?: any;

View File

@@ -1,6 +1,6 @@
export type FilterParserCompilerType = 'sqlTree' | 'mongoCondition' | 'datetime';
export interface StructuredFilterType {
export interface FilterBehaviour {
compilerType: FilterParserCompilerType;
supportEquals?: boolean;

View File

@@ -20,7 +20,7 @@
export let isReadOnly = false;
export let filterType;
export let structuredFilterType;
export let filterBehaviour;
export let filter;
export let setFilter;
export let showResizeSplitter = false;
@@ -66,35 +66,35 @@
{ onClick: () => filterMultipleValues(), text: 'Filter multiple values' },
];
if (structuredFilterType.supportEquals) {
if (filterBehaviour.supportEquals) {
res.push(
{ onClick: () => openFilterWindow('='), text: 'Equals...' },
{ onClick: () => openFilterWindow('<>'), text: 'Does Not Equal...' }
);
}
if (structuredFilterType.supportExistsTesting) {
if (filterBehaviour.supportExistsTesting) {
res.push(
{ onClick: () => setFilter('EXISTS'), text: 'Field exists' },
{ onClick: () => setFilter('NOT EXISTS'), text: 'Field does not exist' }
);
}
if (structuredFilterType.supportArrayTesting) {
if (filterBehaviour.supportArrayTesting) {
res.push(
{ onClick: () => setFilter('NOT EMPTY ARRAY'), text: 'Array is not empty' },
{ onClick: () => setFilter('EMPTY ARRAY'), text: 'Array is empty' }
);
}
if (structuredFilterType.supportNullTesting) {
if (filterBehaviour.supportNullTesting) {
res.push(
{ onClick: () => setFilter('NULL'), text: 'Is Null' },
{ onClick: () => setFilter('NOT NULL'), text: 'Is Not Null' }
);
}
if (structuredFilterType.supportNumberLikeComparison) {
if (filterBehaviour.supportNumberLikeComparison) {
res.push(
{ onClick: () => openFilterWindow('>'), text: 'Greater Than...' },
{ onClick: () => openFilterWindow('>='), text: 'Greater Than Or Equal To...' },
@@ -103,7 +103,7 @@
);
}
if (structuredFilterType.supportStringInclusion) {
if (filterBehaviour.supportStringInclusion) {
res.push(
{ divider: true },
@@ -116,21 +116,21 @@
);
}
if (structuredFilterType.supportBooleanValues) {
if (filterBehaviour.supportBooleanValues) {
res.push(
{ onClick: () => setFilter('TRUE'), text: 'Is True' },
{ onClick: () => setFilter('FALSE'), text: 'Is False' }
);
}
if (structuredFilterType.supportBooleanValues && structuredFilterType.supportNullTesting) {
if (filterBehaviour.supportBooleanValues && filterBehaviour.supportNullTesting) {
res.push(
{ onClick: () => setFilter('TRUE, NULL'), text: 'Is True or NULL' },
{ onClick: () => setFilter('FALSE, NULL'), text: 'Is False or NULL' }
);
}
if (structuredFilterType.supportDatetimeSymbols) {
if (filterBehaviour.supportDatetimeSymbols) {
res.push(
{ divider: true },
@@ -158,7 +158,7 @@
);
}
if (structuredFilterType.supportDatetimeComparison) {
if (filterBehaviour.supportDatetimeComparison) {
res.push(
{ divider: true },
{ onClick: () => openFilterWindow('<='), text: 'Before...' },
@@ -167,7 +167,7 @@
);
}
if (structuredFilterType.supportSqlCondition) {
if (filterBehaviour.supportSqlCondition) {
res.push(
{ divider: true },
{ onClick: () => openFilterWindow('sql'), text: 'SQL condition ...' },

View File

@@ -3,15 +3,15 @@
export let name;
export let filterType;
export let structuredFilterType;
export let filterBehaviour;
function getOptions() {
const res = [];
if (structuredFilterType.supportEquals) {
if (filterBehaviour.supportEquals) {
res.push({ value: '=', label: 'equals' }, { value: '<>', label: 'does not equal' });
}
if (structuredFilterType.supportStringInclusion) {
if (filterBehaviour.supportStringInclusion) {
res.push(
{ value: '+', label: 'contains' },
{ value: '~', label: 'does not contain' },
@@ -22,7 +22,7 @@
);
}
if (structuredFilterType.supportNumberLikeComparison) {
if (filterBehaviour.supportNumberLikeComparison) {
res.push(
{ value: '<', label: 'is smaller' },
{ value: '>', label: 'is greater' },
@@ -31,7 +31,7 @@
);
}
if (structuredFilterType.supportDatetimeComparison) {
if (filterBehaviour.supportDatetimeComparison) {
res.push(
{ value: '<', label: 'is before' },
{ value: '>', label: 'is after' },
@@ -40,15 +40,15 @@
);
}
if (structuredFilterType.supportNullTesting) {
if (filterBehaviour.supportNullTesting) {
res.push({ value: 'NULL', label: 'is NULL' }, { value: 'NOT NULL', label: 'is not NULL' });
}
if (structuredFilterType.supportExistsTesting) {
if (filterBehaviour.supportExistsTesting) {
res.push({ value: 'EXISTS', label: 'field exists' }, { value: 'NOT EXISTS', label: 'field does not exist' });
}
if (structuredFilterType.supportSqlCondition) {
if (filterBehaviour.supportSqlCondition) {
res.push(
{ value: 'sql', label: 'SQL condition' },
{ value: 'sqlRight', label: 'SQL condition - right side only' }