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

@@ -101,6 +101,7 @@ import {
type TabSortMode,
type UpdateDownloadSource,
type CsvQuoteMode,
type CsvNullMode,
type CustomThemeColors,
type CustomTheme,
type McpConnectionPolicy,
@@ -854,6 +855,7 @@ const editSidebarIndent = ref(settingsStore.editorSettings.sidebarIndent);
const editSidebarFontSize = ref(settingsStore.editorSettings.sidebarFontSize);
const editExportBatchSize = ref(settingsStore.editorSettings.exportBatchSize);
const editCsvQuoteMode = ref<CsvQuoteMode>(settingsStore.editorSettings.csvQuoteMode);
const editCsvNullMode = ref<CsvNullMode>(settingsStore.editorSettings.csvNullMode);
const editGlobalDateTimeDisplayFormat = ref(settingsStore.editorSettings.globalDateTimeDisplayFormat);
const editGlobalDateTimeExportFormat = ref(settingsStore.editorSettings.globalDateTimeExportFormat);
const editGlobalDateTimeImportFormat = ref(settingsStore.editorSettings.globalDateTimeImportFormat);
@@ -1117,6 +1119,7 @@ function currentEditorSettingsDraft(): EditorSettingsDraft {
redisDatabaseDisplayLimit: editRedisDatabaseDisplayLimit.value,
exportBatchSize: editExportBatchSize.value,
csvQuoteMode: editCsvQuoteMode.value,
csvNullMode: editCsvNullMode.value,
globalDateTimeDisplayFormat: editGlobalDateTimeDisplayFormat.value,
globalDateTimeExportFormat: editGlobalDateTimeExportFormat.value,
globalDateTimeImportFormat: editGlobalDateTimeImportFormat.value,
@@ -1779,6 +1782,7 @@ function syncEditorSettingsDraftFromStore() {
editSidebarFontSize.value = settingsStore.editorSettings.sidebarFontSize;
editExportBatchSize.value = settingsStore.editorSettings.exportBatchSize;
editCsvQuoteMode.value = settingsStore.editorSettings.csvQuoteMode;
editCsvNullMode.value = settingsStore.editorSettings.csvNullMode;
editGlobalDateTimeDisplayFormat.value = settingsStore.editorSettings.globalDateTimeDisplayFormat;
editGlobalDateTimeExportFormat.value = settingsStore.editorSettings.globalDateTimeExportFormat;
editGlobalDateTimeImportFormat.value = settingsStore.editorSettings.globalDateTimeImportFormat;
@@ -1915,6 +1919,7 @@ const editorSettingsDraftRefs: EditorSettingsDraftRefMap = {
redisDatabaseDisplayLimit: editRedisDatabaseDisplayLimit,
exportBatchSize: editExportBatchSize,
csvQuoteMode: editCsvQuoteMode,
csvNullMode: editCsvNullMode,
exportRowLimitEnabled: editExportRowLimitEnabled,
exportRowLimit: editExportRowLimit,
queryExportKeysetOptimizationEnabled: editQueryExportKeysetOptimizationEnabled,
@@ -2428,6 +2433,7 @@ function resetDefaultsForTab(tab: SettingsCategory) {
editRedisDatabaseDisplayLimit.value = DEFAULT_EDITOR_SETTINGS.redisDatabaseDisplayLimit;
editExportBatchSize.value = DEFAULT_EDITOR_SETTINGS.exportBatchSize;
editCsvQuoteMode.value = DEFAULT_EDITOR_SETTINGS.csvQuoteMode;
editCsvNullMode.value = DEFAULT_EDITOR_SETTINGS.csvNullMode;
editGlobalDateTimeDisplayFormat.value = DEFAULT_EDITOR_SETTINGS.globalDateTimeDisplayFormat;
editGlobalDateTimeExportFormat.value = DEFAULT_EDITOR_SETTINGS.globalDateTimeExportFormat;
editGlobalDateTimeImportFormat.value = DEFAULT_EDITOR_SETTINGS.globalDateTimeImportFormat;
@@ -2572,6 +2578,7 @@ function resetAllDefaults() {
editRedisDatabaseDisplayLimit.value = DEFAULT_EDITOR_SETTINGS.redisDatabaseDisplayLimit;
editExportBatchSize.value = DEFAULT_EDITOR_SETTINGS.exportBatchSize;
editCsvQuoteMode.value = DEFAULT_EDITOR_SETTINGS.csvQuoteMode;
editCsvNullMode.value = DEFAULT_EDITOR_SETTINGS.csvNullMode;
editGlobalDateTimeDisplayFormat.value = DEFAULT_EDITOR_SETTINGS.globalDateTimeDisplayFormat;
editGlobalDateTimeExportFormat.value = DEFAULT_EDITOR_SETTINGS.globalDateTimeExportFormat;
editGlobalDateTimeImportFormat.value = DEFAULT_EDITOR_SETTINGS.globalDateTimeImportFormat;
@@ -8550,6 +8557,23 @@ onUnmounted(() => {
</SelectContent>
</Select>
</div>
<div class="flex items-start justify-between gap-4">
<div class="min-w-0 space-y-0.5">
<Label for="csv-null-mode">{{ t("settings.csvNullMode") }}</Label>
<p class="text-xs text-muted-foreground">
{{ t("settings.csvNullModeDescription") }}
</p>
</div>
<Select v-model="editCsvNullMode">
<SelectTrigger id="csv-null-mode" class="h-8 w-44 shrink-0">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="marker">{{ t("settings.csvNullModeMarker") }}</SelectItem>
<SelectItem value="empty">{{ t("settings.csvNullModeEmpty") }}</SelectItem>
</SelectContent>
</Select>
</div>
<div class="space-y-2">
<Label>{{ t("settings.exportBatchSize") }}</Label>
<div class="flex items-center gap-3">
@@ -769,7 +769,7 @@ async function prepareBatchSources(sources: ImportSource[]) {
loadingPreview.value = false;
return;
}
emptyStringAsNull.value = formats.length && formats.every((format) => format === formats[0]) ? defaultTableImportEmptyStringAsNull(formats[0]!) : true;
emptyStringAsNull.value = formats.length && formats.every((format) => format === formats[0]) ? defaultTableImportEmptyStringAsNull(formats[0]!) : defaultTableImportEmptyStringAsNull(formats[0] ?? "csv");
try {
for (const [index, source] of sources.entries()) {
const format = formats[index]!;
@@ -1471,10 +1471,13 @@ watch(rawProgressPercent, (percent) => {
<input v-model="trimValues" type="checkbox" class="h-3.5 w-3.5 accent-primary" />
{{ t("tableImport.trimValues") }}
</label>
<label class="flex items-center gap-2 text-xs">
<input v-model="emptyStringAsNull" type="checkbox" class="h-3.5 w-3.5 accent-primary" />
{{ t("tableImport.emptyStringAsNull") }}
</label>
<div class="space-y-1">
<label class="flex items-center gap-2 text-xs">
<input v-model="emptyStringAsNull" type="checkbox" class="h-3.5 w-3.5 accent-primary" />
{{ t("tableImport.emptyStringAsNull") }}
</label>
<p class="text-[11px] text-muted-foreground">{{ t("tableImport.emptyStringAsNullHint") }}</p>
</div>
</div>
<div v-else-if="sourceFormat === 'sql'" class="grid grid-cols-5 gap-3 rounded-md border p-3">
@@ -102,6 +102,7 @@ import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
import { generateDatabaseExportId } from "@/lib/export/databaseExport";
import { buildXlsxHeaderOverrides, hasXlsxHeaderComments, type XlsxExportOptions, type XlsxHeaderMode } from "@/lib/export/xlsxHeader";
import { showSqlInsertModeDialog, type SqlInsertDialect, type SqlInsertMode } from "@/lib/export/sqlInsertMode";
import { csvNullLiteralForMode } from "@/lib/export/csvNullMode";
import { copyToClipboard, eventTargetAllowsAppClipboardShortcut } from "@/lib/common/clipboard";
import {
defaultPasteTableMode,
@@ -2423,7 +2424,7 @@ async function exportTableData(row: ObjectBrowserRow, format: "csv" | "xlsx" | "
executePage: (sql) => api.executeQuery(props.connection.id, props.database, sql),
});
if (format === "csv") {
await api.exportQueryResultCsv(filePath, result.columns, forceCsvTextForTemporalColumns(result.rows, result.column_types ?? []), settingsStore.editorSettings.csvQuoteMode);
await api.exportQueryResultCsv(filePath, result.columns, forceCsvTextForTemporalColumns(result.rows, result.column_types ?? []), settingsStore.editorSettings.csvQuoteMode, csvNullLiteralForMode(settingsStore.editorSettings.csvNullMode));
} else {
const comments = result.columns.map((name) => columnInfos?.find((column) => column.name.toLocaleLowerCase() === name.toLocaleLowerCase())?.comment);
const headerOverrides = buildXlsxHeaderOverrides(result.columns, comments, headerMode);
@@ -2463,6 +2464,7 @@ async function exportTableData(row: ObjectBrowserRow, format: "csv" | "xlsx" | "
format,
...(format === "sql" ? { insertMode, insertDialect, splitMaxMb } : {}),
csvQuoteMode: settingsStore.editorSettings.csvQuoteMode,
nullLiteral: csvNullLiteralForMode(settingsStore.editorSettings.csvNullMode),
columns,
columnComments: format === "xlsx" ? columnComments : undefined,
autoFilter: format === "xlsx" ? autoFilter : undefined,
@@ -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,
+5
View File
@@ -6291,6 +6291,7 @@ export default withEnglishFallback({
lastDataRow: "Son məlumat sətri (sona qədər üçün 0)",
trimValues: "Dəyərlərin əvvəlindəki və sonundakı boşluqları sil",
emptyStringAsNull: "Boş sətri NULL kimi qəbul et",
emptyStringAsNullHint: "İşarələndikdə boş sahələr NULL kimi idxal olunur və \\N adi mətn olur; əks halda boş sahələr boş sətir qalır və \\N NULL kimi idxal olunur.",
jsonShape: "JSON sətirləri",
jsonShapeAuto: "Avtomatik müəyyən et",
jsonShapeObjects: "Obyektlər",
@@ -7822,6 +7823,10 @@ export default withEnglishFallback({
csvQuoteModeDescription: "CSV sahələrində qoşa dırnaqların necə istifadə olunacağını seçin.",
csvQuoteModeAll: "Bütün sahələr",
csvQuoteModeNecessary: "Yalnız lazım olduqda",
csvNullMode: "CSV NULL təsviri",
csvNullModeDescription: "CSV ixracında NULL-un yazılma üsulu. «\\N» NULL ilə boş sətri fərqləndirir, yenidən idxalda boş sətirlər NULL olmur; «Boş» köhnə davranışı saxlayır, lakin onları fərqləndirmir.",
csvNullModeMarker: "\\N",
csvNullModeEmpty: "Boş",
performanceSection: "Məhsuldarlıq",
shortcutExecuteSql: "SQL-i icra et",
shortcutExecuteSqlInNewResultTab: "SQL-i yeni nəticə vərəqində icra et",
+5
View File
@@ -6899,6 +6899,7 @@ export default {
lastDataRow: "Last data row (0 for end)",
trimValues: "Trim values",
emptyStringAsNull: "Empty string as NULL",
emptyStringAsNullHint: "When checked, empty fields import as NULL and \\N becomes plain text; otherwise empty fields stay as empty strings and \\N imports as NULL.",
conflictPolicy: "On primary-key conflict",
conflictError: "Stop with an error",
skipDuplicateRows: "Skip duplicate rows",
@@ -8550,6 +8551,10 @@ export default {
csvQuoteModeDescription: "Choose how CSV fields use double quotes.",
csvQuoteModeAll: "All fields",
csvQuoteModeNecessary: "Only when needed",
csvNullMode: "CSV NULL representation",
csvNullModeDescription: 'How NULL is written when exporting CSV. "\\N" keeps NULL distinguishable from an empty string, so re-importing does not turn empty strings into NULL; "Empty" matches older versions but leaves the two indistinguishable.',
csvNullModeMarker: "\\N",
csvNullModeEmpty: "Empty",
performanceSection: "Performance",
shortcutExecuteSql: "Execute SQL",
shortcutExecuteSqlInNewResultTab: "Execute SQL in new result tab",
+5
View File
@@ -6326,6 +6326,7 @@ export default withEnglishFallback({
hasHeader: "First row is header",
trimValues: "Trim values",
emptyStringAsNull: "Empty string as NULL",
emptyStringAsNullHint: "Si se activa, los campos vacíos se importan como NULL y \\N pasa a ser texto; si no, los campos vacíos se mantienen como cadenas vacías y \\N se importa como NULL.",
jsonShape: "JSON rows",
jsonShapeAuto: "Auto-detect",
jsonShapeObjects: "Objects",
@@ -7853,6 +7854,10 @@ export default withEnglishFallback({
csvQuoteModeDescription: "Elige cómo usar comillas dobles en los campos CSV.",
csvQuoteModeAll: "Todos los campos",
csvQuoteModeNecessary: "Solo cuando sea necesario",
csvNullMode: "Representación de NULL en CSV",
csvNullModeDescription: "Cómo se escribe NULL al exportar CSV. «\\N» distingue NULL de la cadena vacía, de modo que al reimportar las cadenas vacías no se convierten en NULL; «Vacío» mantiene el comportamiento anterior, pero no los distingue.",
csvNullModeMarker: "\\N",
csvNullModeEmpty: "Vacío",
performanceSection: "Rendimiento",
shortcutExecuteSql: "Ejecutar SQL",
shortcutExecuteSqlInNewResultTab: "Ejecutar SQL en una nueva pestaña de resultados",
+5
View File
@@ -6248,6 +6248,7 @@ export default withEnglishFallback({
hasHeader: "First row is header",
trimValues: "Trim values",
emptyStringAsNull: "Empty string as NULL",
emptyStringAsNullHint: "Se attivo, i campi vuoti vengono importati come NULL e \\N diventa testo normale; altrimenti restano stringhe vuote e \\N viene importato come NULL.",
jsonShape: "JSON rows",
jsonShapeAuto: "Auto-detect",
jsonShapeObjects: "Objects",
@@ -7777,6 +7778,10 @@ export default withEnglishFallback({
csvQuoteModeDescription: "Scegli come usare le virgolette doppie nei campi CSV.",
csvQuoteModeAll: "Tutti i campi",
csvQuoteModeNecessary: "Solo quando necessario",
csvNullMode: "Rappresentazione di NULL nel CSV",
csvNullModeDescription: "Come viene scritto NULL durante l'esportazione CSV. «\\N» distingue NULL dalla stringa vuota, così al reimport le stringhe vuote non diventano NULL; «Vuoto» mantiene il comportamento precedente, ma non li distingue.",
csvNullModeMarker: "\\N",
csvNullModeEmpty: "Vuoto",
performanceSection: "Prestazioni",
shortcutExecuteSql: "Esegui SQL",
shortcutExecuteSqlInNewResultTab: "Esegui SQL in una nuova scheda dei risultati",
+5
View File
@@ -6267,6 +6267,7 @@ export default withEnglishFallback({
hasHeader: "First row is header",
trimValues: "Trim values",
emptyStringAsNull: "Empty string as NULL",
emptyStringAsNullHint: "チェックすると空フィールドを NULL として取り込み、\\N は通常のテキストになります。チェックしない場合は空フィールドは空文字列のままとなり、\\N を NULL として取り込みます。",
jsonShape: "JSON rows",
jsonShapeAuto: "Auto-detect",
jsonShapeObjects: "Objects",
@@ -7776,6 +7777,10 @@ export default withEnglishFallback({
csvQuoteModeDescription: "CSV フィールドを二重引用符で囲む方法を選択します。",
csvQuoteModeAll: "すべてのフィールド",
csvQuoteModeNecessary: "必要な場合のみ",
csvNullMode: "CSV の NULL 表記",
csvNullModeDescription: "CSV エクスポート時に NULL を書く方式です。「\\N」は NULL と空文字列を区別でき、再インポートしても空文字列が NULL になりません。「空」は以前の動作と同じですが区別できません。",
csvNullModeMarker: "\\N",
csvNullModeEmpty: "空",
performanceSection: "パフォーマンス",
shortcutExecuteSql: "SQLを実行",
shortcutExecuteSqlInNewResultTab: "新しい結果タブでSQLを実行",
+5
View File
@@ -5981,6 +5981,7 @@ export default withEnglishFallback({
lastDataRow: "마지막 데이터 행 (끝까지면 0)",
trimValues: "값 양끝 공백 제거",
emptyStringAsNull: "빈 문자열을 NULL로",
emptyStringAsNullHint: "체크하면 빈 필드를 NULL로 가져오고 \\N은 일반 텍스트가 됩니다. 해제하면 빈 필드는 빈 문자열로 유지되고 \\N은 NULL로 가져옵니다.",
jsonShape: "JSON 행",
jsonShapeAuto: "자동 감지",
jsonShapeObjects: "객체",
@@ -8083,6 +8084,10 @@ export default withEnglishFallback({
csvQuoteModeDescription: "CSV 내보내기 시 필드에 큰따옴표를 사용하는 방식을 선택합니다.",
csvQuoteModeAll: "모든 필드",
csvQuoteModeNecessary: "필수 필드만",
csvNullMode: "CSV NULL 표기",
csvNullModeDescription: 'CSV 내보내기 시 NULL을 쓰는 방식입니다. "\\N"은 NULL과 빈 문자열을 구분하므로 다시 가져올 때 빈 문자열이 NULL이 되지 않습니다. "비움"은 이전 동작과 같지만 구분할 수 없습니다.',
csvNullModeMarker: "\\N",
csvNullModeEmpty: "비움",
shortcutConvertNamingStyle: "네이밍 스타일 전환",
dataGridKeepFilterEditorExpanded: "필터 편집기 기본 펼치기",
dataGridKeepFilterEditorExpandedDescription: "조건 및 텍스트 보기에서 편집기를 처음에 펼칩니다. 수동으로 접을 수도 있습니다.",
+5
View File
@@ -6250,6 +6250,7 @@ export default withEnglishFallback({
hasHeader: "First row is header",
trimValues: "Trim values",
emptyStringAsNull: "Empty string as NULL",
emptyStringAsNullHint: "Quando marcado, campos vazios são importados como NULL e \\N passa a ser texto; caso contrário, permanecem como strings vazias e \\N é importado como NULL.",
jsonShape: "JSON rows",
jsonShapeAuto: "Auto-detect",
jsonShapeObjects: "Objects",
@@ -7779,6 +7780,10 @@ export default withEnglishFallback({
csvQuoteModeDescription: "Escolha como usar aspas duplas nos campos CSV.",
csvQuoteModeAll: "Todos os campos",
csvQuoteModeNecessary: "Somente quando necessário",
csvNullMode: "Representação de NULL no CSV",
csvNullModeDescription: "Como NULL é escrito ao exportar CSV. «\\N» distingue NULL da string vazia, então ao reimportar as strings vazias não viram NULL; «Vazio» mantém o comportamento antigo, mas não os distingue.",
csvNullModeMarker: "\\N",
csvNullModeEmpty: "Vazio",
performanceSection: "Desempenho",
shortcutExecuteSql: "Executar SQL",
shortcutExecuteSqlInNewResultTab: "Executar SQL em uma nova aba de resultados",
+5
View File
@@ -7418,6 +7418,7 @@ export default withEnglishFallback({
lastDataRow: "Последняя строка данных (0 — до конца)",
trimValues: "Обрезать значения",
emptyStringAsNull: "Пустая строка как NULL",
emptyStringAsNullHint: "Если включено, пустые поля импортируются как NULL, а \\N считается обычным текстом; иначе пустые поля остаются пустыми строками, а \\N импортируется как NULL.",
jsonShape: "Строки JSON",
jsonShapeAuto: "Автоопределение",
jsonShapeObjects: "Объекты",
@@ -9049,6 +9050,10 @@ export default withEnglishFallback({
csvQuoteModeDescription: "Выберите, как поля CSV используют двойные кавычки.",
csvQuoteModeAll: "Все поля",
csvQuoteModeNecessary: "Только когда нужно",
csvNullMode: "Представление NULL в CSV",
csvNullModeDescription: "Как записывается NULL при экспорте CSV. «\\N» отличает NULL от пустой строки, поэтому при повторном импорте пустые строки не станут NULL; «Пусто» сохраняет прежнее поведение, но не различает их.",
csvNullModeMarker: "\\N",
csvNullModeEmpty: "Пусто",
performanceSection: "Производительность",
shortcutExecuteSql: "Выполнить SQL",
shortcutExecuteSqlInNewResultTab: "Выполнить SQL в новой вкладке результатов",
+5
View File
@@ -6173,6 +6173,7 @@ export default withEnglishFallback({
lastDataRow: "Son veri satırı (sona kadar için 0)",
trimValues: "Değerleri kırp",
emptyStringAsNull: "Boş dizeyi NULL say",
emptyStringAsNullHint: "İşaretlendiğinde boş alanlar NULL olarak içe aktarılır ve \\N düz metin olur; aksi halde boş alanlar boş dize kalır ve \\N NULL olarak içe aktarılır.",
jsonShape: "JSON satırları",
jsonShapeAuto: "Otomatik algıla",
jsonShapeObjects: "Nesneler",
@@ -7721,6 +7722,10 @@ export default withEnglishFallback({
csvQuoteModeDescription: "CSV alanlarının çift tırnağı nasıl kullanacağını seçin.",
csvQuoteModeAll: "Tüm alanlar",
csvQuoteModeNecessary: "Yalnızca gerektiğinde",
csvNullMode: "CSV NULL gösterimi",
csvNullModeDescription: "CSV dışa aktarılırken NULL nasıl yazılır. «\\N» NULL ile boş dizeyi ayırır, böylece yeniden içe aktarımda boş dizeler NULL olmaz; «Boş» eski davranışı korur ancak ikisini ayırmaz.",
csvNullModeMarker: "\\N",
csvNullModeEmpty: "Boş",
performanceSection: "Başarım",
shortcutExecuteSql: "SQL'i çalıştır",
shortcutExecuteSqlInNewResultTab: "SQL'i yeni sonuç sekmesinde çalıştır",
+5
View File
@@ -6866,6 +6866,7 @@ export default withEnglishFallback({
lastDataRow: "最后数据行(0 表示末尾)",
trimValues: "裁剪空白",
emptyStringAsNull: "空字符串作为 NULL",
emptyStringAsNullHint: "勾选后空字段一律按 NULL 导入(此时文件里的 \\N 按普通文本导入);不勾选则空字段保留为空字符串,并把 \\N 按 NULL 导入。",
conflictPolicy: "主键冲突时",
conflictError: "停止并报错",
skipDuplicateRows: "跳过重复行",
@@ -8513,6 +8514,10 @@ export default withEnglishFallback({
csvQuoteModeDescription: "选择 CSV 导出时字段使用双引号的方式。",
csvQuoteModeAll: "全部字段",
csvQuoteModeNecessary: "仅必要字段",
csvNullMode: "CSV NULL 表示",
csvNullModeDescription: "导出 CSV 时 NULL 的写法。选「\\N」可区分 NULL 与空字符串,重新导入时不会把空字符串写成 NULL;选「留空」与旧版本一致,但两者在文件里无法区分。",
csvNullModeMarker: "\\N",
csvNullModeEmpty: "留空",
performanceSection: "性能",
shortcutExecuteSql: "执行 SQL",
shortcutExecuteSqlInNewResultTab: "在新结果标签页中执行 SQL",
+5
View File
@@ -5557,6 +5557,7 @@ export default withEnglishFallback({
hasHeader: "首列為標頭",
trimValues: "裁剪空白",
emptyStringAsNull: "空字串作為 NULL",
emptyStringAsNullHint: "勾選後空欄位一律以 NULL 匯入(此時檔案裡的 \\N 以一般文字匯入);不勾選則空欄位保留為空字串,並把 \\N 以 NULL 匯入。",
conflictPolicy: "主鍵衝突時",
conflictError: "停止並回報錯誤",
skipDuplicateRows: "略過重複列",
@@ -7610,6 +7611,10 @@ export default withEnglishFallback({
csvQuoteModeDescription: "選擇 CSV 匯出時欄位使用雙引號的方式。",
csvQuoteModeAll: "全部欄位",
csvQuoteModeNecessary: "僅必要欄位",
csvNullMode: "CSV NULL 表示",
csvNullModeDescription: "匯出 CSV 時 NULL 的寫法。選「\\N」可區分 NULL 與空字串,重新匯入時不會把空字串寫成 NULL;選「留空」與舊版本一致,但兩者在檔案裡無法區分。",
csvNullModeMarker: "\\N",
csvNullModeEmpty: "留空",
performanceSection: "效能",
exportBatchSize: "匯出批次大小",
dateTimeSection: "日期與時間",
+27 -5
View File
@@ -15,12 +15,34 @@ import {
} from "@/lib/table/tableImport";
describe("tableImport", () => {
it("preserves explicit empty strings by default for Excel only", () => {
it("keeps empty fields as empty strings for every source format by default", () => {
// 导出端默认用 `\N` 表示 NULL,空字段代表空字符串;导入端默认必须与之一致,
// 否则 DBX 导出的 CSV 再导回时会把 '' 变成 NULL。
expect(defaultTableImportEmptyStringAsNull("excel")).toBe(false);
expect(defaultTableImportEmptyStringAsNull("csv")).toBe(true);
expect(defaultTableImportEmptyStringAsNull("tsv")).toBe(true);
expect(defaultTableImportEmptyStringAsNull("delimited")).toBe(true);
expect(defaultTableImportEmptyStringAsNull("json")).toBe(true);
expect(defaultTableImportEmptyStringAsNull("csv")).toBe(false);
expect(defaultTableImportEmptyStringAsNull("tsv")).toBe(false);
expect(defaultTableImportEmptyStringAsNull("delimited")).toBe(false);
expect(defaultTableImportEmptyStringAsNull("json")).toBe(false);
});
it("only disables the shared NULL literal when empty fields are treated as NULL", () => {
const baseSettings = {
delimiter: ",",
textEncoding: "auto" as const,
titleRow: 1,
dataStartRow: 2,
lastDataRow: 0,
trimValues: false,
jsonShape: "auto" as const,
};
const sharedLiteral = buildTableImportParseOptions({ ...baseSettings, format: "csv", emptyStringAsNull: false });
expect(sharedLiteral.nullLiteral).toBeUndefined();
expect(sharedLiteral.emptyStringAsNull).toBe(false);
const legacyEmptyField = buildTableImportParseOptions({ ...baseSettings, format: "csv", emptyStringAsNull: true });
expect(legacyEmptyField.nullLiteral).toBe("");
expect(legacyEmptyField.emptyStringAsNull).toBe(true);
});
it("formats import elapsed time for progress and terminal summaries", () => {
+2 -2
View File
@@ -3388,9 +3388,9 @@ export async function cancelQueryResultExport(exportId: string, executionId?: st
});
}
export async function exportQueryResultCsv(filePath: string, columns: string[], rows: readonly (readonly XlsxCellValue[])[], csvQuoteMode: CsvQuoteMode = "all"): Promise<void> {
export async function exportQueryResultCsv(filePath: string, columns: string[], rows: readonly (readonly XlsxCellValue[])[], csvQuoteMode: CsvQuoteMode = "all", nullLiteral?: string): Promise<void> {
const { formatCsv } = await import("@/lib/export/exportFormats");
const content = formatCsv(columns, rows as (string | number | boolean | null)[][], csvQuoteMode);
const content = formatCsv(columns, rows as (string | number | boolean | null)[][], csvQuoteMode, nullLiteral);
const fileName = filePath.split(/[\\/]/).pop() || "export.csv";
const blob = new Blob(["\uFEFF", content], {
type: "text/csv;charset=utf-8",
+10 -1
View File
@@ -5466,6 +5466,8 @@ export interface TableImportParseOptions {
lastDataRow?: number | null;
trimValues?: boolean | null;
emptyStringAsNull?: boolean | null;
/** 分隔文本里代表 NULL 的字面量。缺省表示用后端默认值 `\N`;空串表示关闭字面量,退回「空字段即 NULL」。 */
nullLiteral?: string | null;
sheetName?: string | null;
sheetIndex?: number | null;
jsonShape?: TableImportJsonShape | null;
@@ -5861,6 +5863,8 @@ export interface TableExportRequest {
insertMode?: SqlInsertMode;
insertDialect?: SqlInsertDialect;
csvQuoteMode?: CsvQuoteMode;
/** CSV 里 NULL 写成什么。缺省表示用后端默认值 `\N`;空串表示关闭字面量。 */
nullLiteral?: string;
columns?: string[];
selectedColumns?: SqlExportColumnSelection[];
columnTypes?: Array<string | null | undefined>;
@@ -5891,6 +5895,8 @@ export interface TableCsvExportOptions {
pageSize?: number;
timeoutSecs?: number;
csvQuoteMode?: CsvQuoteMode;
/** CSV 里 NULL 写成什么。缺省表示用后端默认值 `\N`;空串表示关闭字面量。 */
nullLiteral?: string;
}
export interface TableExportProgress {
@@ -5917,6 +5923,8 @@ export interface QueryResultExportRequest {
format: "csv" | "xlsx" | "json" | "txt" | "sql";
insertMode?: SqlInsertMode;
csvQuoteMode?: CsvQuoteMode;
/** CSV 里 NULL 写成什么。缺省表示用后端默认值 `\N`;空串表示关闭字面量。 */
nullLiteral?: string;
includeSqlSheet?: boolean;
pageSize: number;
rowLimit?: number | null;
@@ -6074,13 +6082,14 @@ export async function recordDatabaseExportDestination(directory: string): Promis
await invoke("record_database_export_destination", { directory });
}
export async function exportQueryResultCsv(filePath: string, columns: string[], rows: readonly (readonly XlsxCellValue[])[], csvQuoteMode: CsvQuoteMode = "all"): Promise<void> {
export async function exportQueryResultCsv(filePath: string, columns: string[], rows: readonly (readonly XlsxCellValue[])[], csvQuoteMode: CsvQuoteMode = "all", nullLiteral?: string): Promise<void> {
return invoke("export_query_result_csv", {
request: {
filePath,
columns,
rows,
csvQuoteMode,
...(nullLiteral === undefined ? {} : { nullLiteral }),
},
});
}
@@ -0,0 +1,32 @@
import { describe, expect, it } from "vitest";
import { csvNullLiteralForMode, DEFAULT_CSV_NULL_LITERAL, DEFAULT_CSV_NULL_MODE, normalizeCsvNullMode } from "@/lib/export/csvNullMode";
describe("normalizeCsvNullMode", () => {
it("defaults to writing the NULL marker", () => {
expect(DEFAULT_CSV_NULL_MODE).toBe("marker");
expect(normalizeCsvNullMode(undefined)).toBe("marker");
expect(normalizeCsvNullMode(null)).toBe("marker");
expect(normalizeCsvNullMode("marker")).toBe("marker");
});
it("keeps an explicit request for the legacy empty field", () => {
expect(normalizeCsvNullMode("empty")).toBe("empty");
});
it("falls back to the default for unknown values", () => {
expect(normalizeCsvNullMode("")).toBe("marker");
expect(normalizeCsvNullMode("\\N")).toBe("marker");
expect(normalizeCsvNullMode(0)).toBe("marker");
});
});
describe("csvNullLiteralForMode", () => {
it("writes the MySQL-style NULL marker by default", () => {
expect(csvNullLiteralForMode("marker")).toBe(DEFAULT_CSV_NULL_LITERAL);
expect(csvNullLiteralForMode("marker")).toBe("\\N");
});
it("disables the literal with an empty string to restore the old export", () => {
expect(csvNullLiteralForMode("empty")).toBe("");
});
});
+68 -3
View File
@@ -1,5 +1,58 @@
import { describe, expect, it } from "vitest";
import { formatTsv } from "@/lib/export/exportFormats";
import { formatCsv, formatTsv } from "@/lib/export/exportFormats";
describe("formatCsv NULL handling", () => {
it("writes NULL as an unquoted marker and an empty string as an empty quoted field", () => {
// 即使 quote mode = all,NULL 字面量也必须裸写:PostgreSQL/ClickHouse 的 CSV 读取
// 不把带引号的 `"\N"` 当作 NULL,裸 `\N` 才是它们和 MySQL 共同识别的写法。
expect(
formatCsv(
["id", "name"],
[
[1, ""],
[2, null],
],
"all",
"\\N",
),
).toBe('"id","name"\n"1",""\n"2",\\N');
});
it("keeps the marker unquoted when only necessary fields are quoted", () => {
expect(
formatCsv(
["id", "name"],
[
[1, ""],
[2, null],
],
"necessary",
"\\N",
),
).toBe("id,name\n1,\n2,\\N");
});
it("quotes a marker that itself needs quoting", () => {
// 自定义字面量含分隔符时无法裸写,只能退化成带引号的普通字段
expect(formatCsv(["id", "name"], [[1, null]], "all", "a,b")).toBe('"id","name"\n"1","a,b"');
});
it("restores the legacy empty field when the marker is disabled", () => {
// 关闭字面量时 NULL 是完全空字段、空字符串是 `""`:CSV 引号语义上仍有区别,
// 但 DBX 的导入端比较的是去引号后的值,因此旧格式在往返时依赖不了这个区别。
expect(
formatCsv(
["id", "name"],
[
[1, ""],
[2, null],
],
"all",
"",
),
).toBe('"id","name"\n"1",""\n"2",');
});
});
describe("formatTsv", () => {
it("copies a value that only contains double quotes verbatim (#10087)", () => {
@@ -14,8 +67,20 @@ describe("formatTsv", () => {
expect(output).toBe('v\n"a\tb"\n"c\nd"');
});
it("renders NULL as an empty field without adding quotes", () => {
const output = formatTsv(["id", "name"], [[1, null]]);
it("renders NULL as the null literal and an empty string as an empty field", () => {
const output = formatTsv(
["id", "name"],
[
[1, null],
[2, ""],
],
);
expect(output).toBe("id\tname\n1\t\\N\n2\t");
});
it("falls back to an empty NULL field when the literal is disabled", () => {
const output = formatTsv(["id", "name"], [[1, null]], "");
expect(output).toBe("id\tname\n1\t");
});
@@ -0,0 +1,23 @@
/**
* 导出 CSV 时 NULL 的写法。与 Rust `dbx_formats::csv_export` 保持一致:
* `marker` 把 NULL 写成 `\N`(MySQL `SELECT ... INTO OUTFILE`、ClickHouse
* `format_csv_null_representation` 的既定写法),空字符串才写成空字段,两者在文件里可区分,
* 导回 NOT NULL DEFAULT '' 的列时不会被还原成 NULL;`empty` 是旧行为:NULL 与空字符串都写成空字段。
*/
export const DEFAULT_CSV_NULL_LITERAL = "\\N";
export const CSV_NULL_MODE_MARKER = "marker";
export const CSV_NULL_MODE_EMPTY = "empty";
export type CsvNullMode = typeof CSV_NULL_MODE_MARKER | typeof CSV_NULL_MODE_EMPTY;
export const DEFAULT_CSV_NULL_MODE: CsvNullMode = CSV_NULL_MODE_MARKER;
export function normalizeCsvNullMode(value: unknown): CsvNullMode {
return value === CSV_NULL_MODE_EMPTY ? CSV_NULL_MODE_EMPTY : DEFAULT_CSV_NULL_MODE;
}
/** 导出请求里的 NULL 字面量;空串表示关闭该字面量(旧行为)。 */
export function csvNullLiteralForMode(mode: CsvNullMode): string {
return mode === CSV_NULL_MODE_EMPTY ? "" : DEFAULT_CSV_NULL_LITERAL;
}
+19 -10
View File
@@ -1,26 +1,35 @@
import type { DatabaseType, QueryResult } from "@/types/database";
import * as api from "@/lib/backend/api";
import { escapeCsvField, type CsvQuoteMode } from "@/lib/export/csvQuoteMode";
import { DEFAULT_CSV_NULL_LITERAL } from "@/lib/export/csvNullMode";
import type { SqlInsertDialect, SqlInsertMode } from "@/lib/export/sqlInsertMode";
export type ExportCellValue = string | number | boolean | null;
export function formatCsv(columns: string[], rows: ExportCellValue[][], quoteMode: CsvQuoteMode = "all"): string {
export function formatCsv(columns: string[], rows: ExportCellValue[][], quoteMode: CsvQuoteMode = "all", nullLiteral: string = DEFAULT_CSV_NULL_LITERAL): string {
const header = columns.map((column) => escapeCsvField(column, quoteMode)).join(",");
const body = rows.map((row) => row.map((cell) => (cell === null ? "" : escapeCsvField(String(cell), quoteMode))).join(",")).join("\n");
// NULL 写成独立字面量、空字符串写成空字段,否则导入端无法把两者区分开
// (写回 NOT NULL DEFAULT '' 的列时会把空字符串还原成 NULL)。
//
// 字面量裸写:PostgreSQL 的 COPY ... FORMAT csv 不认带引号的 NULL(`"\N"` 是普通字符串,
// 整数列直接报错),ClickHouse 的 CSV 读取在引号内遇到 `\N` 会解析失败;只有裸 `\N` 是
// 它们与 MySQL 共同识别的 NULL。字面量自身含分隔符/引号/换行时才加引号。
const nullField = nullLiteral ? escapeCsvField(nullLiteral, "necessary") : "";
const body = rows.map((row) => row.map((cell) => (cell === null ? nullField : escapeCsvField(String(cell), quoteMode))).join(",")).join("\n");
return `${header}\n${body}`;
}
// Tab-separated values with a header row, mirroring Navicat's "Text File (*.txt)"
// export: fields are joined by a tab and NULL becomes empty. A field is wrapped
// in double quotes only when it contains a tab or a line break - the characters
// that would otherwise corrupt the TSV row/column shape. A field that merely
// contains a double quote is emitted verbatim: TSV is parsed by splitting on the
// tab alone (there is no quote state machine on paste-back), so quoting such a
// value would corrupt it (e.g. a value of `"abc"` must not become `"""abc"""`).
export function formatTsv(columns: string[], rows: ExportCellValue[][]): string {
// export: fields are joined by a tab and NULL becomes the null literal. A field
// is wrapped in double quotes only when it contains a tab or a line break - the
// characters that would otherwise corrupt the TSV row/column shape. A field that
// merely contains a double quote is emitted verbatim: TSV is parsed by splitting
// on the tab alone (there is no quote state machine on paste-back), so quoting
// such a value would corrupt it (e.g. a value of `"abc"` must not become `"""abc"""`).
export function formatTsv(columns: string[], rows: ExportCellValue[][], nullLiteral: string = DEFAULT_CSV_NULL_LITERAL): string {
const esc = (value: ExportCellValue) => {
const text = value === null ? "" : String(value);
// 与 CSV 一致:NULL 写字面量、空字符串写空字段,导出→导入才能无损往返
const text = value === null ? nullLiteral || "" : String(value);
if (text.includes("\t") || text.includes("\n") || text.includes("\r")) {
return `"${text.replace(/"/g, '""')}"`;
}
@@ -236,6 +236,11 @@ describe("editorSettingsDraftFromSettings", () => {
expect(draft.completionTriggerMode).toBe("positional");
});
it("maps csvNullMode and normalizes an unknown value back to the marker", () => {
expect(editorSettingsDraftFromSettings(makeSettings({ csvNullMode: "empty" })).csvNullMode).toBe("empty");
expect(editorSettingsDraftFromSettings(makeSettings({ csvNullMode: "bogus" as unknown } as Partial<EditorSettings>)).csvNullMode).toBe("marker");
});
it("maps and normalizes tableCompletionSchemaQualification", () => {
expect(editorSettingsDraftFromSettings(makeSettings({ tableCompletionSchemaQualification: "always" })).tableCompletionSchemaQualification).toBe("always");
expect(editorSettingsDraftFromSettings(makeSettings({ tableCompletionSchemaQualification: "invalid" } as any)).tableCompletionSchemaQualification).toBe("collision");
@@ -413,4 +413,9 @@ describe("settingsTransfer", () => {
expect(transferCategoryForKey("csvQuoteMode")).toBe("data");
expect(collectTransferCategories(["csvQuoteMode", "pageSize"])).toEqual(["data"]);
});
it("maps csvNullMode into the data category", () => {
expect(transferCategoryForKey("csvNullMode")).toBe("data");
expect(collectTransferCategories(["csvNullMode", "csvQuoteMode"])).toEqual(["data"]);
});
});
@@ -7,6 +7,7 @@ import { normalizeCompletionTriggerMode } from "@/lib/sql/sqlCompletionTriggerPo
import { normalizeSqlTableCompletionSchemaQualification } from "@/lib/sql/sqlCompletionSchemaQualification";
import { normalizeTableHoverLookupMode } from "@/lib/editor/hoverTableLookup";
import { normalizeRedisKeyTemplates } from "@/lib/redis/redisKeyTemplates";
import { normalizeCsvNullMode } from "@/lib/export/csvNullMode";
export const EDITOR_SETTINGS_DRAFT_KEYS = [
"fontFamily",
@@ -120,6 +121,7 @@ export const EDITOR_SETTINGS_DRAFT_KEYS = [
"redisDatabaseDisplayLimit",
"exportBatchSize",
"csvQuoteMode",
"csvNullMode",
"exportRowLimitEnabled",
"exportRowLimit",
"queryExportKeysetOptimizationEnabled",
@@ -164,6 +166,7 @@ function normalizedDraftValue(key: EditorSettingsDraftKey, value: unknown): unkn
if (key === "tableCompletionSchemaQualification") return normalizeSqlTableCompletionSchemaQualification(value);
if (key === "tableHoverLookupMode") return normalizeTableHoverLookupMode(value);
if (key === "redisKeyTemplates") return normalizeRedisKeyTemplates(value);
if (key === "csvNullMode") return normalizeCsvNullMode(value);
if (key === "backgroundImage") return normalizeBackgroundImageSettings(value);
return value;
}
@@ -178,6 +178,7 @@ const SETTINGS_TRANSFER_CATEGORY_KEYS: Record<SettingsTransferCategoryId, readon
"redisDatabaseDisplayLimit",
"exportBatchSize",
"csvQuoteMode",
"csvNullMode",
"exportRowLimitEnabled",
"exportRowLimit",
"queryExportKeysetOptimizationEnabled",
+12 -2
View File
@@ -97,8 +97,15 @@ export interface TableImportParseSettings {
databaseType?: DatabaseType | null;
}
export function defaultTableImportEmptyStringAsNull(format: TableImportSourceFormat): boolean {
return format !== "excel";
/**
* 导入时分隔文本的「空字段即 NULL」默认值。
*
* 导出端默认把 NULL 写成 `\N` 字面量、空字符串才写成空字段,所以导入端默认必须
* 保留空字段(不当作 NULL),否则 DBX 自己导出的 CSV 在导入时会把空字符串还原成
* NULL,写回 `NOT NULL DEFAULT ''` 的列就会报「不允许为 null」。
*/
export function defaultTableImportEmptyStringAsNull(_format: TableImportSourceFormat): boolean {
return false;
}
export function buildTableImportParseOptions(settings: TableImportParseSettings): TableImportParseOptions {
@@ -113,6 +120,9 @@ export function buildTableImportParseOptions(settings: TableImportParseSettings)
lastDataRow: settings.lastDataRow,
trimValues: settings.trimValues,
emptyStringAsNull: settings.emptyStringAsNull,
// 勾选「空字符串作为 NULL」= 退回旧行为:关闭 NULL 字面量,空字段一律当 NULL。
// 不勾选时留空,由后端使用与导出端一致的默认字面量 `\N`。
nullLiteral: settings.emptyStringAsNull ? "" : undefined,
sheetName: settings.format === "excel" ? settings.sheetName || null : null,
jsonShape: settings.format === "json" ? settings.jsonShape : null,
// SQL 脚本需要按源方言解析字符串转义与标识符大小写(取目标连接的数据库类型)
+3
View File
@@ -69,6 +69,7 @@ import { TABLE_DATA_EXPORT_PAGE_SIZE } from "@/lib/table/tableDataExport";
import { tableMetaForDataTab } from "@/lib/table/tableDataTabMeta";
import { isDataTabMetadataLifecycleStale } from "@/lib/sidebar/dataTabOpenPolicy";
import type { SqlInsertMode } from "@/lib/export/sqlInsertMode";
import { csvNullLiteralForMode } from "@/lib/export/csvNullMode";
import { tableOpenPageLimit } from "@/lib/table/tableOpenPageLimit";
import { getCachedTableMetadata, loadTableColumns, loadTableIndexes, loadTableMetadata, tableMetadataToDataTabMeta, updateCachedTableMetadataType, type TableMetadataRequest } from "@/lib/metadata/tableMetadataCache";
import { loadObjectDdl } from "@/lib/metadata/objectDdlCache";
@@ -9237,6 +9238,7 @@ export const useQueryStore = defineStore("query", () => {
keysetOptimizationEnabled: settings.queryExportKeysetOptimizationEnabled,
csvQuoteMode: settings.csvQuoteMode,
clientSessionId,
nullLiteral: csvNullLiteralForMode(settings.csvNullMode),
executionId: uuid(),
exportTableName: options.exportTableName,
exportColumnTypes: options.exportColumnTypes,
@@ -9277,6 +9279,7 @@ export const useQueryStore = defineStore("query", () => {
keysetOptimizationEnabled: settings.queryExportKeysetOptimizationEnabled,
csvQuoteMode: settings.csvQuoteMode,
clientSessionId: `${tabClientSessionId(tab, "export")}:${exportId}`,
nullLiteral: csvNullLiteralForMode(settings.csvNullMode),
executionId: uuid(),
numericColumnRightAlign: settings.numericColumnRightAlign,
columnComments,
+6
View File
@@ -36,6 +36,7 @@ import { normalizeTableHoverLookupMode, type TableHoverLookupMode } from "@/lib/
import { normalizeCompletionTriggerMode, type SqlCompletionTriggerMode } from "@/lib/sql/sqlCompletionTriggerPolicy";
import { DEFAULT_SQL_TABLE_COMPLETION_SCHEMA_QUALIFICATION, normalizeSqlTableCompletionSchemaQualification, type SqlTableCompletionSchemaQualification } from "@/lib/sql/sqlCompletionSchemaQualification";
import { DEFAULT_CSV_QUOTE_MODE, normalizeCsvQuoteMode, type CsvQuoteMode } from "@/lib/export/csvQuoteMode";
import { DEFAULT_CSV_NULL_MODE, normalizeCsvNullMode, type CsvNullMode } from "@/lib/export/csvNullMode";
import { configureMetadataRuntimeCache, METADATA_CACHE_DEFAULT_MEMORY_MB, normalizeMetadataCacheMemoryMb } from "@/lib/metadata/metadataRuntimeCache";
import type { AiApiStyle, AiAssistantMode, AiAuthMethod, AiChatSelectionState, AiConfig, AiConfigItem, AiConfiguredModel, AiEffortLevel, AiEffortSelection, AiModelEffortPreference, AiProvider, AiReasoningLevel, AiTestConnectionResult } from "@/types/ai";
import type { SqlShortcutAction, SqlSnippet, TableInfoTab } from "@/types/database";
@@ -53,6 +54,7 @@ export type {
AiReasoningLevel,
AiTestConnectionResult,
CsvQuoteMode,
CsvNullMode,
DataTabReuseMode,
SavedSqlOpenTargetMode,
SqlCompletionTriggerMode,
@@ -998,6 +1000,7 @@ export interface EditorSettings {
tableColumnTemplateFields: string[];
exportBatchSize: number;
csvQuoteMode: CsvQuoteMode;
csvNullMode: CsvNullMode;
/** Global Redis key-search templates; overridden by non-empty connection templates. */
redisKeyTemplates: string[];
/** Sidebar database-list cap for Redis connections; the rest are revealed via "load more". */
@@ -1279,6 +1282,7 @@ export const DEFAULT_EDITOR_SETTINGS: EditorSettings = {
tableColumnTemplateFields: [...DEFAULT_TABLE_COLUMN_TEMPLATE_FIELDS],
exportBatchSize: 2000,
csvQuoteMode: DEFAULT_CSV_QUOTE_MODE,
csvNullMode: DEFAULT_CSV_NULL_MODE,
redisKeyTemplates: [],
redisDatabaseDisplayLimit: REDIS_DATABASE_DISPLAY_LIMIT_DEFAULT,
exportRowLimitEnabled: false,
@@ -1896,6 +1900,7 @@ export function normalizeEditorSettings(settings: Partial<EditorSettings>, exist
tableColumnTemplateFields: normalizeTableColumnTemplateFields(settings.tableColumnTemplateFields),
exportBatchSize: typeof settings.exportBatchSize === "number" && settings.exportBatchSize >= 100 && settings.exportBatchSize <= 100000 ? Math.round(settings.exportBatchSize) : DEFAULT_EDITOR_SETTINGS.exportBatchSize,
csvQuoteMode: normalizeCsvQuoteMode(settings.csvQuoteMode),
csvNullMode: normalizeCsvNullMode(settings.csvNullMode),
redisKeyTemplates: normalizeRedisKeyTemplates(settings.redisKeyTemplates),
redisDatabaseDisplayLimit:
typeof settings.redisDatabaseDisplayLimit === "number" && settings.redisDatabaseDisplayLimit >= REDIS_DATABASE_DISPLAY_LIMIT_MIN && settings.redisDatabaseDisplayLimit <= REDIS_DATABASE_DISPLAY_LIMIT_MAX
@@ -2703,6 +2708,7 @@ export const useSettingsStore = defineStore("settings", () => {
if (partial.tableColumnTemplateFields !== undefined) editorSettings.value.tableColumnTemplateFields = normalizeTableColumnTemplateFields(partial.tableColumnTemplateFields);
if (partial.exportBatchSize !== undefined) editorSettings.value.exportBatchSize = Math.min(100000, Math.max(100, Math.round(partial.exportBatchSize)));
if (partial.csvQuoteMode !== undefined) editorSettings.value.csvQuoteMode = normalizeCsvQuoteMode(partial.csvQuoteMode);
if (partial.csvNullMode !== undefined) editorSettings.value.csvNullMode = normalizeCsvNullMode(partial.csvNullMode);
if (partial.redisKeyTemplates !== undefined) editorSettings.value.redisKeyTemplates = normalizeRedisKeyTemplates(partial.redisKeyTemplates);
if (partial.redisDatabaseDisplayLimit !== undefined) editorSettings.value.redisDatabaseDisplayLimit = Math.min(REDIS_DATABASE_DISPLAY_LIMIT_MAX, Math.max(REDIS_DATABASE_DISPLAY_LIMIT_MIN, Math.round(partial.redisDatabaseDisplayLimit)));
if (partial.exportRowLimitEnabled !== undefined) editorSettings.value.exportRowLimitEnabled = partial.exportRowLimitEnabled;