mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-18 00:56:02 +00:00
structured filter type => filterBehaviour
This commit is contained in:
@@ -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];
|
||||
|
||||
12
packages/filterparser/src/detectSqlFilterBehaviour.ts
Normal file
12
packages/filterparser/src/detectSqlFilterBehaviour.ts
Normal 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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export * from './parseFilter';
|
||||
export * from './detectSqlFilterType';
|
||||
export * from './detectSqlFilterBehaviour';
|
||||
export * from './filterTool';
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user