filtering datetime values

This commit is contained in:
Jan Prochazka 2021-01-24 09:25:38 +01:00
parent ac59665be4
commit b3d436ddf9
3 changed files with 42 additions and 7 deletions

View File

@ -45,6 +45,7 @@ export class FormViewDisplay {
? cfg.addedColumns
: [...cfg.addedColumns, column.uniqueName],
}));
this.reload();
}
}

View File

@ -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}"`;
}

View File

@ -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',