fix(export): mark NULL as \N in CSV and TSV exports

This commit is contained in:
zipg authored and GitHub committed 2026-09-28 22:35:43 +08:00
1 parent 02f27dc294
commit 1aaa5ca717
49 files changed
+1056 -130

No files matched your search

@@ -13,6 +13,7 @@ import { DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS } from "@/lib/dataGrid/dataGridCopy
import { clearDataGridClipboardCopy, parseDataGridClipboard } from "@/lib/dataGrid/dataGridClipboard";
import { MONGO_DOCUMENT_GRID_NULL, mongoDocumentGridExternalValue } from "@/lib/mongo/mongoDocumentValues";
import { saveTextFile } from "@/lib/export/saveTextFile";
import { useSettingsStore } from "@/stores/settingsStore";
const toast = vi.fn();
@@ -780,8 +781,8 @@ describe("useDataGridExport prepared row statements", () => {
expect(copyToClipboard).toHaveBeenCalledWith("2020-12-02 15:18:29");
});
it("copies all rows with empty fields for NULL cells", async () => {
const text = "id\tname\n1\t\n2\tAda";
it("copies all rows with the null literal for NULL cells", async () => {
const text = "id\tname\n1\t\\N\n2\tAda";
const state = createExportState(editableTable, ["id", "name"], undefined, undefined, undefined, [
[1, null],
[2, "Ada"],
@@ -790,6 +791,7 @@ describe("useDataGridExport prepared row statements", () => {
await state.copyAll();
expect(copyToClipboard).toHaveBeenCalledWith(text);
// 内部剪贴板按文本全等匹配保留逻辑矩阵:粘贴回网格时 NULL 语义无损还原
expect(parseDataGridClipboard(text)).toEqual([
["id", "name"],
["1", null],
@@ -1672,7 +1674,7 @@ describe("useDataGridExport prepared row statements", () => {
});
await state.copyAll();
expect(copyToClipboard).toHaveBeenCalledWith("_id\tnullable\n1\t");
expect(copyToClipboard).toHaveBeenCalledWith("_id\tnullable\n1\t\\N");
vi.mocked(extractDataGridSelection).mockResolvedValueOnce({ text: "", mimeType: "text/tab-separated-values", fileExtension: "tsv", rowCount: 1, columnCount: 1 });
await expect(state.copyWithExtractor("tsv")).resolves.toBe(true);
@@ -1680,7 +1682,7 @@ describe("useDataGridExport prepared row statements", () => {
setActivePinia(createPinia());
await state.exportCurrentPageCsv();
expect(exportQueryResultCsv).toHaveBeenCalledWith(expect.any(String), ["_id", "nullable"], [["1", null]], expect.anything());
expect(exportQueryResultCsv).toHaveBeenCalledWith(expect.any(String), ["_id", "nullable"], [["1", null]], expect.anything(), expect.anything());
const reservedString = MONGO_DOCUMENT_GRID_NULL;
const fullExportState = createMongoExportState({
@@ -1699,7 +1701,7 @@ describe("useDataGridExport prepared row statements", () => {
});
await fullExportState.exportCsv();
expect(exportQueryResultCsv).toHaveBeenLastCalledWith(expect.any(String), ["_id", "value"], [["1", reservedString]], expect.anything());
expect(exportQueryResultCsv).toHaveBeenLastCalledWith(expect.any(String), ["_id", "value"], [["1", reservedString]], expect.anything(), expect.anything());
});
it("exports only visible Mongo columns from the full result set", async () => {
@@ -1722,7 +1724,7 @@ describe("useDataGridExport prepared row statements", () => {
await state.exportCsv();
expect(exportQueryResultCsv).toHaveBeenLastCalledWith(expect.any(String), ["name"], [["Visible"], ["Other"]], expect.anything());
expect(exportQueryResultCsv).toHaveBeenLastCalledWith(expect.any(String), ["name"], [["Visible"], ["Other"]], expect.anything(), expect.anything());
});
it("applies visible Mongo columns when the complete result is already local", async () => {
@@ -1751,7 +1753,23 @@ describe("useDataGridExport prepared row statements", () => {
await state.exportCsv();
expect(exportQueryResultCsv).toHaveBeenLastCalledWith(expect.any(String), ["name"], [["Visible"], ["Other"]], expect.anything());
expect(exportQueryResultCsv).toHaveBeenLastCalledWith(expect.any(String), ["name"], [["Visible"], ["Other"]], expect.anything(), expect.anything());
});
it("passes the CSV NULL setting to the export call", async () => {
setActivePinia(createPinia());
const state = createMongoExportState({
columns: ["name"],
item: { ...row(["Visible"]), sourceIndex: 0 },
mongoDocuments: [{ name: "Visible" }],
});
await state.exportCsv();
expect(exportQueryResultCsv).toHaveBeenLastCalledWith(expect.any(String), ["name"], [["Visible"]], expect.anything(), "\\N");
useSettingsStore().editorSettings.csvNullMode = "empty";
await state.exportCsv();
expect(exportQueryResultCsv).toHaveBeenLastCalledWith(expect.any(String), ["name"], [["Visible"]], expect.anything(), "");
});
it("exports missing Mongo fields as null while retaining explicit empty strings", async () => {
@@ -21,6 +21,7 @@ import { summarizeExportRows } from "@/lib/export/exportDiagnostics";
import { appendDebugLog, appendNativeProcessMemoryLog, getBrowserMemorySnapshot, isDebugLoggingEnabled } from "@/lib/backend/debugLog";
import { uuid } from "@/lib/common/utils";
import { useSettingsStore } from "@/stores/settingsStore";
import { csvNullLiteralForMode } from "@/lib/export/csvNullMode";
import { expandNestedJsonStringsForCopy } from "@/lib/common/jsonCopyValue";
import { buildMongoCopyDocumentFromOriginal, buildMongoCopyInsertDocument, buildMongoCopyUpdateDocument, formatMongoShellLiteral, type MongoInputValue } from "@/lib/mongo/mongoDocumentValues";
import { formatMongoShellText } from "@/lib/mongo/mongoFormatter";
@@ -912,7 +913,7 @@ export function useDataGridExport(options: UseDataGridExportOptions) {
}
outputPath = path as string;
}
await api.exportQueryResultCsv(outputPath, result.columns, rows, useSettingsStore().editorSettings.csvQuoteMode);
await api.exportQueryResultCsv(outputPath, result.columns, rows, useSettingsStore().editorSettings.csvQuoteMode, csvNullLiteralForMode(useSettingsStore().editorSettings.csvNullMode));
if (needsFullExport && exportProgressState) {
exportProgressState.value = {
...exportProgressState.value,
@@ -952,7 +953,7 @@ export function useDataGridExport(options: UseDataGridExportOptions) {
}
const result = await resultToExport(undefined, undefined, false);
const rows = forceCsvTextForTemporalColumns(result.rows, result.columnTypes);
await api.exportQueryResultCsv(outputPath, result.columns, rows, useSettingsStore().editorSettings.csvQuoteMode);
await api.exportQueryResultCsv(outputPath, result.columns, rows, useSettingsStore().editorSettings.csvQuoteMode, csvNullLiteralForMode(useSettingsStore().editorSettings.csvNullMode));
toast(t("grid.exported"));
} catch (e: any) {
toast(t("grid.exportFailed", { message: translateBackendError(t, e) }), 5000);
@@ -1389,6 +1390,7 @@ export function useDataGridExport(options: UseDataGridExportOptions) {
format,
...(format === "sql" && sqlExportOptions ? { insertMode: sqlExportOptions.insertMode, splitMaxMb: sqlExportOptions.splitMaxMb, selectedColumns: sqlExportOptions.selectedColumns } : {}),
csvQuoteMode: editorSettings.csvQuoteMode,
nullLiteral: csvNullLiteralForMode(editorSettings.csvNullMode),
columns: format === "sql" ? effectiveColumns(sourceColumns.value, columns.value).map((column, index) => column ?? columns.value[index]!) : columns.value,
columnTypes: columnTypes.value,
...(format === "sql" ? { columnExtras: sqlExportColumnExtras(effectiveColumns(sourceColumns.value, columns.value).map((column, index) => column ?? columns.value[index]!)) } : {}),
@@ -1470,6 +1472,7 @@ export function useDataGridExport(options: UseDataGridExportOptions) {
...baseRequest,
...(format === "sql" ? { selectedColumns: sqlExportOptions?.selectedColumns } : {}),
csvQuoteMode: useSettingsStore().editorSettings.csvQuoteMode,
nullLiteral: csvNullLiteralForMode(useSettingsStore().editorSettings.csvNullMode),
...sqlExportPrimaryKeyOptions(),
dateTimeFormat: useSettingsStore().editorSettings.globalDateTimeExportFormat || undefined,
numericColumnRightAlign: useSettingsStore().editorSettings.numericColumnRightAlign ?? true,
@@ -32,6 +32,7 @@ import {
structurePreviewDdlStorageType,
} from "@/components/sidebar/sidebarTreeDialogState";
import type { CsvQuoteMode } from "@/lib/export/csvQuoteMode";
import { csvNullLiteralForMode } from "@/lib/export/csvNullMode";
import type { SqlInsertDialect } from "@/lib/export/sqlInsertMode";
import { uuid } from "@/lib/common/utils";
@@ -59,6 +60,8 @@ interface SidebarTableExportTarget {
batchSize: number;
rowLimit: number | null;
csvQuoteMode: CsvQuoteMode;
/** 导出 CSV 时 NULL 的字面量;空串表示关闭(旧行为)。 */
nullLiteral: string;
fileNameBase?: string;
}
@@ -437,6 +440,7 @@ export function useSidebarTreeExportRuntime(options: SidebarTreeExportRuntimeOpt
batchSize: editorSettings.exportBatchSize,
rowLimit: editorSettings.exportRowLimitEnabled ? editorSettings.exportRowLimit : null,
csvQuoteMode: editorSettings.csvQuoteMode,
nullLiteral: csvNullLiteralForMode(editorSettings.csvNullMode),
};
}
@@ -472,7 +476,7 @@ export function useSidebarTreeExportRuntime(options: SidebarTreeExportRuntimeOpt
executePage: (sql) => api.executeQuery(connectionId, database, sql),
});
if (format === "csv") {
await api.exportQueryResultCsv(outputPath, result.columns, forceCsvTextForTemporalColumns(result.rows, result.column_types ?? []), target.csvQuoteMode);
await api.exportQueryResultCsv(outputPath, result.columns, forceCsvTextForTemporalColumns(result.rows, result.column_types ?? []), target.csvQuoteMode, target.nullLiteral);
} else {
const comments = result.columns.map((name) => exportColumnInfos?.find((column) => column.name.toLocaleLowerCase() === name.toLocaleLowerCase())?.comment);
const headerOverrides = buildXlsxHeaderOverrides(result.columns, comments, headerMode);
@@ -503,6 +507,7 @@ export function useSidebarTreeExportRuntime(options: SidebarTreeExportRuntimeOpt
format,
...(format === "sql" ? { insertDialect } : {}),
csvQuoteMode: target.csvQuoteMode,
nullLiteral: target.nullLiteral,
columns: queryColumns,
columnComments,
autoFilter: format === "xlsx" ? autoFilter : undefined,