Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(grid): support blob downloads

t8y2175d1def0d76
diff --git a/apps/desktop/src/components/grid/DataGrid.vue b/apps/desktop/src/components/grid/DataGrid.vue
index 53d0210c1e..c9bc9116d7 100644
--- a/apps/desktop/src/components/grid/DataGrid.vue
+++ b/apps/desktop/src/components/grid/DataGrid.vue
@@ -109,6 +109,7 @@ import { matchesRowStatusFilter, type RowStatus, type RowStatusFilter } from "@/
import { displayCellValue, type CellValue } from "@/lib/cellValue";
import {
BINARY_CELL_DOWNLOAD_MODES,
+ binaryCellDisplayText,
binaryCellDownloadFileName,
binaryCellDownloadPayload,
canDownloadBinaryCellValue,
@@ -2976,6 +2977,11 @@ function primitiveCellFormatKey(value: CellValue, columnIndex?: number): string
function formatCell(value: CellValue, columnIndex?: number): string {
const formatter = columnIndex === undefined ? undefined : resolvedColumnFormatters.value[columnIndex];
+ const columnName = columnIndex === undefined ? undefined : props.result.columns[columnIndex];
+ const binaryDisplay = formatter
+ ? null
+ : binaryCellDisplayText(value, columnName ? columnTypeMap.value.get(columnName) : undefined);
+ if (binaryDisplay) return binaryDisplay;
const s = applyColumnFormatter(value, formatter);
return s.length > CELL_DISPLAY_MAX_LENGTH ? s.slice(0, CELL_DISPLAY_MAX_LENGTH) : s;
}
@@ -4019,7 +4025,7 @@ function canDownloadDetailBinaryValue(detail: DataGridCellDetail | null): boolea
async function downloadDetailBinaryValue(detail: DataGridCellDetail | null, mode: BinaryCellDownloadMode) {
if (!detail || !canDownloadDetailBinaryValue(detail)) return;
try {
- const payload = binaryCellDownloadPayload(detail.value, mode);
+ const payload = binaryCellDownloadPayload(detail.value, mode, detail.type);
const fileName = binaryCellDownloadFileName({
column: detail.column,
rowNumber: detail.rowNumber,
diff --git a/apps/desktop/src/lib/binaryCellDownload.ts b/apps/desktop/src/lib/binaryCellDownload.ts
index c7ee149cda..e6a6e6b132 100644
--- a/apps/desktop/src/lib/binaryCellDownload.ts
+++ b/apps/desktop/src/lib/binaryCellDownload.ts
@@ -18,6 +18,8 @@ export interface BinaryCellDownloadResult {
export const BINARY_CELL_DOWNLOAD_MODES: BinaryCellDownloadMode[] = ["binary", "utf8", "gbk"];
const HEX_VALUE_RE = /^(?:0[xX]|\\x)([0-9a-fA-F\s]+)$/;
+const BARE_HEX_RE = /^[0-9a-fA-F\s]+$/;
+const HEX_ESCAPE_RE = /^(?:\\x[0-9a-fA-F]{2}|\s)+$/;
const BINARY_TYPE_RE =
/^(?:blob|tinyblob|mediumblob|longblob|bytea|bytes|binary|varbinary|image|raw|long\s+raw)(?:\b|\()/i;
@@ -30,8 +32,12 @@ export function parseBinaryCellHexValue(value: CellValue): Uint8Array | null {
const match = value.trim().match(HEX_VALUE_RE);
if (!match) return null;
- const hex = match[1].replace(/\s+/g, "");
- if (!hex || hex.length % 2 !== 0) return null;
+ return bytesFromHex(match[1]);
+}
+
+function bytesFromHex(value: string): Uint8Array | null {
+ const hex = value.replace(/\s+/g, "");
+ if (!hex || hex.length % 2 !== 0 || !/^[0-9a-fA-F]+$/.test(hex)) return null;
const bytes = new Uint8Array(hex.length / 2);
for (let i = 0; i < bytes.length; i++) {
@@ -42,20 +48,80 @@ export function parseBinaryCellHexValue(value: CellValue): Uint8Array | null {
return bytes;
}
+function bytesFromByteArray(value: unknown): Uint8Array | null {
+ if (!Array.isArray(value)) return null;
+ const bytes = new Uint8Array(value.length);
+ for (let i = 0; i < value.length; i++) {
+ const item = value[i];
+ if (!Number.isInteger(item) || item < 0 || item > 255) return null;
+ bytes[i] = item;
+ }
+ return bytes;
+}
+
+function bytesFromBufferLikeObject(value: unknown): Uint8Array | null {
+ if (!value || typeof value !== "object") return null;
+ const data = (value as { data?: unknown }).data;
+ return bytesFromByteArray(data);
+}
+
+export function parseBinaryCellBytes(value: unknown, columnType?: string): Uint8Array | null {
+ if (typeof value === "string") {
+ const prefixed = parseBinaryCellHexValue(value);
+ if (prefixed) return prefixed;
+
+ const trimmed = value.trim();
+ if (HEX_ESCAPE_RE.test(trimmed)) {
+ return bytesFromHex(trimmed.replace(/\\x/gi, ""));
+ }
+
+ if (isBinaryCellColumnType(columnType) && BARE_HEX_RE.test(trimmed)) {
+ return bytesFromHex(trimmed);
+ }
+ }
+
+ return bytesFromByteArray(value) ?? bytesFromBufferLikeObject(value);
+}
+
export function isBinaryCellColumnType(columnType?: string): boolean {
const type = (columnType ?? "").trim();
return !!type && BINARY_TYPE_RE.test(type);
}
-export function canDownloadBinaryCellValue(value: CellValue, columnType?: string): boolean {
- if (!parseBinaryCellHexValue(value)) return false;
- return isBinaryCellColumnType(columnType) || typeof value === "string";
+export function canDownloadBinaryCellValue(value: unknown, columnType?: string): boolean {
+ return !!parseBinaryCellBytes(value, columnType);
+}
+
+export function binaryCellDisplayText(value: unknown, columnType?: string): string | null {
+ const bytes = parseBinaryCellBytes(value, columnType);
+ if (!bytes || !isBinaryCellColumnType(columnType)) return null;
+ return `${binaryCellDisplayLabel(columnType)} [${formatBinaryCellByteSize(bytes.length)}]`;
+}
+
+function binaryCellDisplayLabel(columnType?: string): string {
+ const base = (columnType ?? "")
+ .trim()
+ .split(/[(:\s]/)[0]
+ .toUpperCase();
+ if (!base) return "BLOB";
+ if (base.includes("BLOB")) return "BLOB";
+ return base;
+}
+
+function formatBinaryCellByteSize(bytes: number): string {
+ if (bytes < 1024) return `${bytes} bytes`;
+ if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(bytes < 10 * 1024 ? 1 : 0)} KB`;
+ return `${(bytes / (1024 * 1024)).toFixed(bytes < 10 * 1024 * 1024 ? 1 : 0)} MB`;
}
-export function binaryCellDownloadPayload(value: CellValue, mode: BinaryCellDownloadMode): BinaryCellDownloadPayload {
- const bytes = parseBinaryCellHexValue(value);
+export function binaryCellDownloadPayload(
+ value: unknown,
+ mode: BinaryCellDownloadMode,
+ columnType?: string,
+): BinaryCellDownloadPayload {
+ const bytes = parseBinaryCellBytes(value, columnType);
if (!bytes) {
- throw new Error("Cell value is not a hexadecimal binary value.");
+ throw new Error("Cell value is not a downloadable binary value.");
}
if (mode === "binary") {
diff --git a/crates/dbx-core/src/db/mysql.rs b/crates/dbx-core/src/db/mysql.rs
index 2a17413fe1..404e05dca6 100644
--- a/crates/dbx-core/src/db/mysql.rs
+++ b/crates/dbx-core/src/db/mysql.rs
@@ -145,6 +145,9 @@ fn mysql_printable_binary_preview(bytes: &[u8]) -> Option<String> {
}
fn mysql_blob_preview(bytes: &[u8], label: &str) -> serde_json::Value {
+ if label == "BLOB" {
+ return super::binary_value_to_json(bytes);
+ }
serde_json::Value::String(format!("({label}) {} bytes", bytes.len()))
}
@@ -1400,10 +1403,7 @@ mod tests {
let blob_column = mysql_test_column(ColumnType::MYSQL_TYPE_BLOB, 63, ColumnFlags::BLOB_FLAG, 65_535);
assert_eq!(mysql_bytes_to_json(b"hello".to_vec(), &text_column), serde_json::json!("hello"));
- assert_eq!(
- mysql_bytes_to_json(vec![0x00, 0x01, 0xab, 0xff], &blob_column),
- serde_json::json!("(BLOB) 4 bytes")
- );
+ assert_eq!(mysql_bytes_to_json(vec![0x00, 0x01, 0xab, 0xff], &blob_column), serde_json::json!("0x0001abff"));
}
#[test]
diff --git a/packages/app-tests/binaryCellDownload.test.ts b/packages/app-tests/binaryCellDownload.test.ts
index cfc80bc925..365afd52a7 100644
--- a/packages/app-tests/binaryCellDownload.test.ts
+++ b/packages/app-tests/binaryCellDownload.test.ts
@@ -2,10 +2,12 @@ import assert from "node:assert/strict";
import test from "node:test";
import {
+ binaryCellDisplayText,
binaryCellDownloadFileName,
binaryCellDownloadPayload,
canDownloadBinaryCellValue,
isBinaryCellColumnType,
+ parseBinaryCellBytes,
parseBinaryCellHexValue,
} from "../../apps/desktop/src/lib/binaryCellDownload.ts";
@@ -20,6 +22,15 @@ test("parseBinaryCellHexValue rejects non-hex and odd-length payloads", () => {
assert.equal(parseBinaryCellHexValue(null), null);
});
+test("parseBinaryCellBytes accepts common driver binary shapes", () => {
+ assert.deepEqual(Array.from(parseBinaryCellBytes("89504e47", "BLOB") ?? []), [137, 80, 78, 71]);
+ assert.deepEqual(Array.from(parseBinaryCellBytes("\\x89\\x50\\x4e\\x47") ?? []), [137, 80, 78, 71]);
+ assert.deepEqual(Array.from(parseBinaryCellBytes([0, 1, 171, 255]) ?? []), [0, 1, 171, 255]);
+ assert.deepEqual(Array.from(parseBinaryCellBytes({ type: "Buffer", data: [222, 173, 190, 239] }) ?? []), [
+ 222, 173, 190, 239,
+ ]);
+});
+
test("binary cell download detects common blob column types", () => {
assert.equal(isBinaryCellColumnType("BLOB"), true);
assert.equal(isBinaryCellColumnType("RAW(2000)"), true);
@@ -30,7 +41,14 @@ test("binary cell download detects common blob column types", () => {
test("canDownloadBinaryCellValue allows displayed binary hex strings", () => {
assert.equal(canDownloadBinaryCellValue("0x89504e47", "BLOB"), true);
assert.equal(canDownloadBinaryCellValue("0x89504e47"), true);
- assert.equal(canDownloadBinaryCellValue("89504e47", "BLOB"), false);
+ assert.equal(canDownloadBinaryCellValue("89504e47", "BLOB"), true);
+ assert.equal(canDownloadBinaryCellValue("89504e47"), false);
+});
+
+test("binaryCellDisplayText summarizes binary values for grid display", () => {
+ assert.equal(binaryCellDisplayText("0x89504e47", "BLOB"), "BLOB [4 bytes]");
+ assert.equal(binaryCellDisplayText(`0x${"00".repeat(2048)}`, "VARBINARY(2048)"), "VARBINARY [2.0 KB]");
+ assert.equal(binaryCellDisplayText("0x89504e47"), null);
});
test("binaryCellDownloadPayload builds raw and decoded payloads", () => {