mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-19 15:25:59 +00:00
support blob values #211
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import _isString from 'lodash/isString';
|
||||
import _isArray from 'lodash/isArray';
|
||||
import _isPlainObject from 'lodash/isPlainObject';
|
||||
|
||||
export function arrayToHexString(byteArray) {
|
||||
return byteArray.reduce((output, elem) => output + ('0' + elem.toString(16)).slice(-2), '');
|
||||
return byteArray.reduce((output, elem) => output + ('0' + elem.toString(16)).slice(-2), '').toUpperCase();
|
||||
}
|
||||
|
||||
export function hexStringToArray(inputString) {
|
||||
@@ -10,3 +14,33 @@ export function hexStringToArray(inputString) {
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
export function parseCellValue(value) {
|
||||
if (!_isString(value)) return value;
|
||||
|
||||
if (value == '(NULL)') return null;
|
||||
|
||||
const mHex = value.match(/^0x([0-9a-fA-F][0-9a-fA-F])+$/);
|
||||
if (mHex) {
|
||||
return {
|
||||
type: 'Buffer',
|
||||
data: hexStringToArray(value.substring(2)),
|
||||
};
|
||||
}
|
||||
|
||||
const mOid = value.match(/^ObjectId\("([0-9a-f]{24})"\)$/);
|
||||
if (mOid) {
|
||||
return { $oid: mOid[1] };
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
export function stringifyCellValue(value) {
|
||||
if (value === null) return '(NULL)';
|
||||
if (value === undefined) return '(NoField)';
|
||||
if (value?.type == 'Buffer' && _isArray(value.data)) return '0x' + arrayToHexString(value.data);
|
||||
if (value?.$oid) return `ObjectId("${value?.$oid}")`;
|
||||
if (_isPlainObject(value) || _isArray(value)) return JSON.stringify(value);
|
||||
return value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user