fix(grid): preview GBK-stored text in MySQL varbinary cells
This commit is contained in:
2 files changed
+89
-13
No files matched your search
@@ -208,7 +208,7 @@ export function binaryCellDisplayText(value: unknown, columnType?: string, origi
|
||||
const bytes = parseBinaryCellBytes(value, columnType, databaseType);
|
||||
if (!bytes || !isBinaryCellColumnType(columnType)) return null;
|
||||
if (isBinaryCellTextPreviewColumn(columnType, databaseType)) {
|
||||
const text = binaryCellUtf8TextBytes(bytes, columnType);
|
||||
const text = binaryCellTextPreviewBytes(bytes, columnType, databaseType);
|
||||
if (text !== null) return text;
|
||||
}
|
||||
return `${binaryCellDisplayLabel(columnType)} [${formatBinaryCellByteSize(bytes.length)}]`;
|
||||
@@ -223,9 +223,11 @@ export function binaryCellUtf8Text(value: unknown, columnType?: string, database
|
||||
|
||||
// 复制到剪贴板时,把「文本型」MySQL VARBINARY 单元格还原成其原始字符串。
|
||||
// DBX 后端为保留任意 bytes,把该值统一序列化成 `0x<hex>`;前端只有在严格 UTF-8 解码、
|
||||
// 无控制字符且重新编码后与原 bytes 完全一致时,才把 payload(如 token)复制为文本。
|
||||
// 其余情况返回 null,让调用方沿用无损 hex。范围严格限定 MySQL VARBINARY,避免改变
|
||||
// MySQL BINARY/BLOB、SQL Server VARBINARY、PostgreSQL BYTEA、Oracle RAW 等现有语义。
|
||||
// 无控制字符且重新编码后与原 bytes 完全一致时,才把 payload(如 token)复制为文本;
|
||||
// UTF-8 解码失败时再按网格预览同一闸门尝试 GBK,保持「显示成 GBK 文本的单元格复制时
|
||||
// 还原成原字符串」。其余情况返回 null,让调用方沿用无损 hex。范围严格限定 MySQL
|
||||
// VARBINARY,避免改变 MySQL BINARY/BLOB、SQL Server VARBINARY、PostgreSQL BYTEA、
|
||||
// Oracle RAW 等现有语义。
|
||||
export function binaryCellClipboardText(value: unknown, columnType?: string, databaseType?: DatabaseType): string | null {
|
||||
if (databaseType !== "mysql" || !VARBINARY_TYPE_RE.test((columnType ?? "").trim())) return null;
|
||||
// MySQL QueryResult 的 binary canonical form 是 `0x<hex>`;不要把普通的偶数长度
|
||||
@@ -233,9 +235,13 @@ export function binaryCellClipboardText(value: unknown, columnType?: string, dat
|
||||
const bytes = typeof value === "string" ? parseBinaryCellHexValue(value) : null;
|
||||
if (!bytes) return null;
|
||||
const text = binaryCellUtf8TextBytes(bytes, columnType);
|
||||
if (text === null) return null;
|
||||
const encoded = new TextEncoder().encode(text);
|
||||
return encoded.length === bytes.length && encoded.every((byte, index) => byte === bytes[index]) ? text : null;
|
||||
if (text !== null) {
|
||||
const encoded = new TextEncoder().encode(text);
|
||||
// UTF-8 可解码但无法 byte-for-byte 回编码(如解码会吞掉的 BOM)时保持 null,
|
||||
// 与 GBK 回退无关,沿用 hex。
|
||||
return encoded.length === bytes.length && encoded.every((byte, index) => byte === bytes[index]) ? text : null;
|
||||
}
|
||||
return printableGbkText(binaryPreviewBytes(bytes, columnType));
|
||||
}
|
||||
|
||||
// MySQL BLOB 的文本预览必须与编辑写回路径(coerceMysqlBlobTextValue,仅 mysql)走同一闸门:
|
||||
@@ -247,9 +253,26 @@ function isBinaryCellTextPreviewColumn(columnType: string | undefined, databaseT
|
||||
return isBlobCellColumnType(columnType) && databaseType === "mysql";
|
||||
}
|
||||
|
||||
// 显示/复制路径的解码链:先严格 UTF-8,仅 MySQL 连接的 binary/varbinary 再回退严格
|
||||
// GBK——中文存量库里 varbinary 常见 GBK 写入(Navicat 等工具按连接字符集直接显示),
|
||||
// UTF-8 优先保证 ASCII/UTF-8 数据永远不受影响。两处刻意不覆盖:
|
||||
// 1) 编辑写回路径(dataGridCellEditorText/coerceMysqlBlobTextValue)保持纯 UTF-8,
|
||||
// 编辑保存会把文本重新按 UTF-8 编码,放 GBK 文本进去会静默翻转该单元格的字节编码;
|
||||
// 2) MySQL BLOB 列,其文本预览与编辑路径共享同一闸门(见 isBinaryCellTextPreviewColumn),
|
||||
// GBK 回退会把该闸门凿开。varbinary 的「显示文本、编辑 hex」不对称是既有行为,不受影响。
|
||||
function binaryCellTextPreviewBytes(bytes: Uint8Array, columnType?: string, databaseType?: DatabaseType): string | null {
|
||||
const utf8Text = binaryCellUtf8TextBytes(bytes, columnType);
|
||||
if (utf8Text !== null) return utf8Text;
|
||||
if (databaseType !== "mysql" || isBlobCellColumnType(columnType)) return null;
|
||||
return printableGbkText(binaryPreviewBytes(bytes, columnType));
|
||||
}
|
||||
|
||||
function binaryCellUtf8TextBytes(bytes: Uint8Array, columnType?: string): string | null {
|
||||
const previewBytes = FIXED_BINARY_TYPE_RE.test((columnType ?? "").trim()) ? trimTrailingNullBytes(bytes) : bytes;
|
||||
return printableUtf8Text(previewBytes);
|
||||
return printableUtf8Text(binaryPreviewBytes(bytes, columnType));
|
||||
}
|
||||
|
||||
function binaryPreviewBytes(bytes: Uint8Array, columnType?: string): Uint8Array {
|
||||
return FIXED_BINARY_TYPE_RE.test((columnType ?? "").trim()) ? trimTrailingNullBytes(bytes) : bytes;
|
||||
}
|
||||
|
||||
function trimTrailingNullBytes(bytes: Uint8Array): Uint8Array {
|
||||
@@ -265,12 +288,37 @@ function printableUtf8Text(bytes: Uint8Array): string | null {
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
return printableText(text) ? text : null;
|
||||
}
|
||||
|
||||
// GBK 回退的解码闸门:fatal 解码拒绝非法序列,可打印过滤剔除控制字符,另加拒绝
|
||||
// Unicode 私用区(真实文本不含 PUA,而 0xFF 等字节的高段 GBK 序列常落在 PUA,是随机
|
||||
// 二进制的强信号)。TextEncoder 只有 UTF-8,GBK 没有 encode 侧、做不了 UTF-8 分支
|
||||
// 那样的往返校验;但 fatal 解码对合法序列是双射,随机二进制仍可能恰好全部组成合法
|
||||
// GBK 序列(如 0xdeadbeef → 两个 URO 汉字),此时显示/复制与网格预览一致地输出解码
|
||||
// 文本,原始 bytes 无损保留在 0x<hex> 值中。
|
||||
function printableGbkText(bytes: Uint8Array): string | null {
|
||||
let text: string;
|
||||
try {
|
||||
text = new TextDecoder("gbk", { fatal: true }).decode(bytes);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
if (!printableText(text)) return null;
|
||||
for (const char of text) {
|
||||
const codePoint = char.codePointAt(0) ?? 0;
|
||||
if (codePoint >= 0xe000 && codePoint <= 0xf8ff) return null;
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
function printableText(text: string): boolean {
|
||||
for (const char of text) {
|
||||
const codePoint = char.codePointAt(0) ?? 0;
|
||||
const allowedWhitespace = codePoint === 9 || codePoint === 10 || codePoint === 13;
|
||||
if (!allowedWhitespace && (codePoint <= 31 || (codePoint >= 127 && codePoint <= 159))) return null;
|
||||
if (!allowedWhitespace && (codePoint <= 31 || (codePoint >= 127 && codePoint <= 159))) return false;
|
||||
}
|
||||
return text;
|
||||
return true;
|
||||
}
|
||||
|
||||
function binaryCellDisplayLabel(columnType?: string): string {
|
||||
|
||||
Reference in new issue
Block a user