support date type

This commit is contained in:
Jan Prochazka
2024-08-26 11:33:29 +02:00
parent 3bfa7d54d0
commit 8e17516d54
3 changed files with 92 additions and 13 deletions

View File

@@ -5,9 +5,22 @@ import _isPlainObject from 'lodash/isPlainObject';
import _pad from 'lodash/pad'; import _pad from 'lodash/pad';
import { DataEditorTypesBehaviour } from 'dbgate-types'; import { DataEditorTypesBehaviour } from 'dbgate-types';
const dateTimeRegex = export type EditorDataType =
| 'null'
| 'objectid'
| 'string'
| 'number'
| 'object'
| 'date'
| 'array'
| 'boolean'
| 'unknown';
const dateTimeStorageRegex =
/^([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|()|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))$/; /^([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|()|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))$/;
const dateTimeParseRegex = /^(\d{4})-(\d{2})-(\d{2})[Tt ](\d{2}):(\d{2}):(\d{2})(\.[0-9]+)?(([Zz])|()|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))$/;
export function arrayToHexString(byteArray) { export function arrayToHexString(byteArray) {
return byteArray.reduce((output, elem) => output + ('0' + elem.toString(16)).slice(-2), '').toUpperCase(); return byteArray.reduce((output, elem) => output + ('0' + elem.toString(16)).slice(-2), '').toUpperCase();
} }
@@ -45,6 +58,15 @@ export function parseCellValue(value, editorTypes?: DataEditorTypesBehaviour) {
} }
} }
if (editorTypes?.parseDateAsDollar) {
const m = value.match(dateTimeParseRegex);
if (m) {
return {
$date: `${m[1]}-${m[2]}-${m[3]}T${m[4]}:${m[5]}:${m[6]}Z`,
};
}
}
if (editorTypes?.parseJsonNull) { if (editorTypes?.parseJsonNull) {
if (value == 'null') return null; if (value == 'null') return null;
} }
@@ -69,7 +91,7 @@ export function parseCellValue(value, editorTypes?: DataEditorTypesBehaviour) {
return value; return value;
} }
function parseObjectIdAsDollar(value) { function parseFunc_ObjectIdAsDollar(value) {
if (value?.$oid) return value; if (value?.$oid) return value;
if (_isString(value)) { if (_isString(value)) {
if (value.match(/^[0-9a-f]{24}$/)) return { $oid: value }; if (value.match(/^[0-9a-f]{24}$/)) return { $oid: value };
@@ -81,6 +103,17 @@ function parseObjectIdAsDollar(value) {
return value; return value;
} }
function parseFunc_DateAsDollar(value) {
if (value?.$date) return value;
if (_isString(value)) {
const m = value.match(dateTimeParseRegex);
if (m) {
return { $date: `${m[1]}-${m[2]}-${m[3]}T${m[4]}:${m[5]}:${m[6]}Z` };
}
}
return value;
}
function makeBulletString(value) { function makeBulletString(value) {
return _pad('', value.length, '•'); return _pad('', value.length, '•');
} }
@@ -168,6 +201,23 @@ export function stringifyCellValue(
} }
} }
if (editorTypes?.parseDateAsDollar) {
if (value?.$date) {
switch (intent) {
case 'exportIntent':
case 'stringConversionIntent':
return { value: value.$date };
default:
const m = value.$date.match(dateTimeStorageRegex);
if (m) {
return { value: `${m[1]}-${m[2]}-${m[3]} ${m[4]}:${m[5]}:${m[6]}`, gridStyle: 'valueCellStyle' };
} else {
return { value: value.$date.replaCE('T', ' '), gridStyle: 'valueCellStyle' };
}
}
}
}
if (_isArray(value)) { if (_isArray(value)) {
switch (intent) { switch (intent) {
case 'gridCellIntent': case 'gridCellIntent':
@@ -213,7 +263,7 @@ export function stringifyCellValue(
} else { } else {
if (!editorTypes?.explicitDataType) { if (!editorTypes?.explicitDataType) {
// reformat datetime for implicit date types // reformat datetime for implicit date types
const m = value.match(dateTimeRegex); const m = value.match(dateTimeStorageRegex);
if (m) { if (m) {
return { return {
value: `${m[1]}-${m[2]}-${m[3]} ${m[4]}:${m[5]}:${m[6]}`, value: `${m[1]}-${m[2]}-${m[3]} ${m[4]}:${m[5]}:${m[6]}`,
@@ -350,15 +400,39 @@ export function parseSqlDefaultValue(value: string) {
return undefined; return undefined;
} }
export function detectCellDataType(value): EditorDataType {
if (value === null) return 'null';
if (value?.$oid) return 'objectid';
if (value?.$date) return 'date';
if (_isString(value)) return 'string';
if (_isNumber(value)) return 'number';
if (_isPlainObject(value)) return 'object';
if (_isArray(value)) return 'array';
if (value === true || value === false) return 'boolean';
return 'unknown';
}
export function detectTypeIcon(value) { export function detectTypeIcon(value) {
if (value === null) return 'icon type-null'; switch (detectCellDataType(value)) {
if (value?.$oid) return 'icon type-objectid'; case 'null':
if (_isString(value)) return 'icon type-string'; return 'icon type-null';
if (_isNumber(value)) return 'icon type-number'; case 'objectid':
if (_isPlainObject(value)) return 'icon type-object'; return 'icon type-objectid';
if (_isArray(value)) return 'icon type-array'; case 'date':
if (value === true || value === false) return 'icon type-boolean'; return 'icon type-date';
return 'icon type-unknown'; case 'string':
return 'icon type-string';
case 'number':
return 'icon type-number';
case 'object':
return 'icon type-object';
case 'array':
return 'icon type-array';
case 'boolean':
return 'icon type-boolean';
default:
return 'icon type-unknown';
}
} }
export function getConvertValueMenu(value, onSetValue, editorTypes?: DataEditorTypesBehaviour) { export function getConvertValueMenu(value, onSetValue, editorTypes?: DataEditorTypesBehaviour) {
@@ -373,13 +447,16 @@ export function getConvertValueMenu(value, onSetValue, editorTypes?: DataEditorT
text: 'Boolean', text: 'Boolean',
onClick: () => onSetValue(value?.toString()?.toLowerCase() == 'true' || value == '1'), onClick: () => onSetValue(value?.toString()?.toLowerCase() == 'true' || value == '1'),
}, },
editorTypes?.supportObjectIdType && { text: 'ObjectId', onClick: () => onSetValue(parseObjectIdAsDollar(value)) }, editorTypes?.supportObjectIdType && {
text: 'ObjectId',
onClick: () => onSetValue(parseFunc_ObjectIdAsDollar(value)),
},
editorTypes?.supportDateType && { text: 'Date', onClick: () => onSetValue(parseFunc_DateAsDollar(value)) },
editorTypes?.supportJsonType && { editorTypes?.supportJsonType && {
text: 'JSON', text: 'JSON',
onClick: () => { onClick: () => {
const jsonValue = safeJsonParse(value); const jsonValue = safeJsonParse(value);
if (jsonValue != null) { if (jsonValue != null) {
console.log('**** ON SET VALUE', jsonValue);
onSetValue(jsonValue); onSetValue(jsonValue);
} }
}, },

View File

@@ -102,6 +102,7 @@ export interface DataEditorTypesBehaviour {
parseJsonObject?: boolean; parseJsonObject?: boolean;
parseHexAsBuffer?: boolean; parseHexAsBuffer?: boolean;
parseObjectIdAsDollar?: boolean; parseObjectIdAsDollar?: boolean;
parseDateAsDollar?: boolean;
explicitDataType?: boolean; explicitDataType?: boolean;
supportNumberType?: boolean; supportNumberType?: boolean;

View File

@@ -130,6 +130,7 @@ const driver = {
parseJsonArray: true, parseJsonArray: true,
parseJsonObject: true, parseJsonObject: true,
parseObjectIdAsDollar: true, parseObjectIdAsDollar: true,
parseDateAsDollar: true,
explicitDataType: true, explicitDataType: true,
supportNumberType: true, supportNumberType: true,