From b3d436ddf9e680137b15c96d5beb6e054ee8331d Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Sun, 24 Jan 2021 09:25:38 +0100 Subject: [PATCH] filtering datetime values --- packages/datalib/src/FormViewDisplay.ts | 1 + packages/filterparser/src/filterTool.ts | 3 +- packages/filterparser/src/parseFilter.ts | 45 ++++++++++++++++++++---- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/packages/datalib/src/FormViewDisplay.ts b/packages/datalib/src/FormViewDisplay.ts index 5d8aa5d8..406fc583 100644 --- a/packages/datalib/src/FormViewDisplay.ts +++ b/packages/datalib/src/FormViewDisplay.ts @@ -45,6 +45,7 @@ export class FormViewDisplay { ? cfg.addedColumns : [...cfg.addedColumns, column.uniqueName], })); + this.reload(); } } diff --git a/packages/filterparser/src/filterTool.ts b/packages/filterparser/src/filterTool.ts index fad9cec1..af91fefc 100644 --- a/packages/filterparser/src/filterTool.ts +++ b/packages/filterparser/src/filterTool.ts @@ -1,10 +1,11 @@ import { isTypeDateTime } from 'dbgate-tools'; +import moment from 'moment'; export type FilterMultipleValuesMode = 'is' | 'is_not' | 'contains' | 'begins' | 'ends'; export function getFilterValueExpression(value, dataType) { if (value == null) return 'NULL'; - if (isTypeDateTime(dataType)) return value; + if (isTypeDateTime(dataType)) return moment(value).format('YYYY-MM-DD HH:mm:ss'); return `="${value}"`; } diff --git a/packages/filterparser/src/parseFilter.ts b/packages/filterparser/src/parseFilter.ts index 53151c6e..947ade58 100644 --- a/packages/filterparser/src/parseFilter.ts +++ b/packages/filterparser/src/parseFilter.ts @@ -140,7 +140,7 @@ const yearMonthDayCondition = () => value => { }; }; -const fixedIntervalCondition = (start, end) => () => { +const createIntervalCondition = (start, end) => { return { conditionType: 'and', conditions: [ @@ -157,7 +157,7 @@ const fixedIntervalCondition = (start, end) => () => { }, { conditionType: 'binary', - operator: '<', + operator: '<=', left: { exprType: 'placeholder', }, @@ -170,13 +170,42 @@ const fixedIntervalCondition = (start, end) => () => { }; }; -const fixedMomentIntervalCondition = (intervalType, diff) => { - return fixedIntervalCondition( - moment().add(intervalType, diff).startOf(intervalType).toISOString(), - moment().add(intervalType, diff).endOf(intervalType).toISOString() +const createDateIntervalCondition = (start, end) => { + return createIntervalCondition(start.format('YYYY-MM-DDTHH:mm:ss.SSS'), end.format('YYYY-MM-DDTHH:mm:ss.SSS')); +}; + +const fixedMomentIntervalCondition = (intervalType, diff) => () => { + return createDateIntervalCondition( + moment().add(intervalType, diff).startOf(intervalType), + moment().add(intervalType, diff).endOf(intervalType) ); }; +const yearMonthDayMinuteCondition = () => value => { + const m = value.match(/(\d\d\d\d)-(\d\d?)-(\d\d?)\s+(\d\d?):(\d\d?)/); + const year = m[1]; + const month = m[2]; + const day = m[3]; + const hour = m[4]; + const minute = m[5]; + const dateObject = new Date(year, month - 1, day, hour, minute); + + return createDateIntervalCondition(moment(dateObject).startOf('minute'), moment(dateObject).endOf('minute')); +}; + +const yearMonthDaySecondCondition = () => value => { + const m = value.match(/(\d\d\d\d)-(\d\d?)-(\d\d?)(T|\s+)(\d\d?):(\d\d?):(\d\d?)/); + const year = m[1]; + const month = m[2]; + const day = m[3]; + const hour = m[5]; + const minute = m[6]; + const second = m[7]; + const dateObject = new Date(year, month - 1, day, hour, minute, second); + + return createDateIntervalCondition(moment(dateObject).startOf('second'), moment(dateObject).endOf('second')); +}; + const createParser = (filterType: FilterType) => { const langDef = { string1: () => @@ -209,6 +238,8 @@ const createParser = (filterType: FilterType) => { yearNum: () => P.regexp(/\d\d\d\d/).map(yearCondition()), yearMonthNum: () => P.regexp(/\d\d\d\d-\d\d?/).map(yearMonthCondition()), yearMonthDayNum: () => P.regexp(/\d\d\d\d-\d\d?-\d\d?/).map(yearMonthDayCondition()), + yearMonthDayMinute: () => P.regexp(/\d\d\d\d-\d\d?-\d\d?\s+\d\d?:\d\d?/).map(yearMonthDayMinuteCondition()), + yearMonthDaySecond: () => P.regexp(/\d\d\d\d-\d\d?-\d\d?(\s+|T)\d\d?:\d\d?:\d\d?/).map(yearMonthDaySecondCondition()), value: r => P.alt(...allowedValues.map(x => r[x])), valueTestEq: r => r.value.map(binaryCondition('=')), @@ -286,6 +317,8 @@ const createParser = (filterType: FilterType) => { if (filterType == 'logical') allowedElements.push('true', 'false', 'trueNum', 'falseNum'); if (filterType == 'datetime') allowedElements.push( + 'yearMonthDaySecond', + 'yearMonthDayMinute', 'yearMonthDayNum', 'yearMonthNum', 'yearNum',