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。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
feat(import): add table import wizard
t8y24666f8a5f220
diff --git a/apps/desktop/src/components/import/TableImportDialog.vue b/apps/desktop/src/components/import/TableImportDialog.vueindex 257d12e798..0f05ccee26 100644--- a/apps/desktop/src/components/import/TableImportDialog.vue+++ b/apps/desktop/src/components/import/TableImportDialog.vue@@ -8,10 +8,10 @@ import { Button } from "@/components/ui/button";import { Input } from "@/components/ui/input";import { Label } from "@/components/ui/label";import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";-import { Check, FileUp, Loader2, Square, Upload, X } from "@lucide/vue";+import { AlertTriangle, ArrowLeft, ArrowRight, Check, CheckCircle2, FileJson, FileSpreadsheet, FileText, FileUp, Loader2, RefreshCw, Square, Upload, X } from "@lucide/vue";import { useConnectionStore } from "@/stores/connectionStore";import { useToast } from "@/composables/useToast";-import { autoMapImportColumns } from "@/lib/table/tableImport";+import { autoMapImportColumns, nextTableImportWizardStep, previousTableImportWizardStep, requiredImportTargetColumns, validateImportMappings, type TableImportWizardStep } from "@/lib/table/tableImport";import type { ColumnInfo } from "@/types/database";import * as api from "@/lib/backend/api";@@ -29,6 +29,8 @@ const props = defineProps<{const SKIP_VALUE = "__skip__";const targetColumns = ref<ColumnInfo[]>([]);+const selectedSource = ref<string | File | null>(null);+const sourceFormat = ref<api.TableImportSourceFormat>("csv");const preview = ref<api.TableImportPreview | null>(null);const columnMapping = ref<Record<string, string>>({});const loadingTarget = ref(false);@@ -40,7 +42,32 @@ const cancelling = ref(false);const importId = ref("");const progress = ref<api.TableImportProgress | null>(null);const errorMessage = ref("");+const wizardStep = ref<TableImportWizardStep>("source");const fileInput = ref<HTMLInputElement | null>(null);+const delimiter = ref(",");+const hasHeader = ref(true);+const trimValues = ref(false);+const emptyStringAsNull = ref(true);+const selectedSheet = ref("");+const jsonShape = ref<api.TableImportJsonShape>("auto");+const previewLimit = ref(50);+let previewReloadTimer: ReturnType<typeof setTimeout> | null = null;++const formatOptions: Array<{ value: api.TableImportSourceFormat; icon: any; labelKey: string; descriptionKey: string }> = [+ { value: "csv", icon: FileText, labelKey: "tableImport.formatCsv", descriptionKey: "tableImport.formatCsvDescription" },+ { value: "tsv", icon: FileText, labelKey: "tableImport.formatTsv", descriptionKey: "tableImport.formatTsvDescription" },+ { value: "delimited", icon: FileText, labelKey: "tableImport.formatDelimited", descriptionKey: "tableImport.formatDelimitedDescription" },+ { value: "json", icon: FileJson, labelKey: "tableImport.formatJson", descriptionKey: "tableImport.formatJsonDescription" },+ { value: "excel", icon: FileSpreadsheet, labelKey: "tableImport.formatExcel", descriptionKey: "tableImport.formatExcelDescription" },+];++const wizardSteps: Array<{ value: TableImportWizardStep; labelKey: string }> = [+ { value: "source", labelKey: "tableImport.stepSource" },+ { value: "options", labelKey: "tableImport.stepOptions" },+ { value: "mapping", labelKey: "tableImport.stepMapping" },+ { value: "review", labelKey: "tableImport.stepReview" },+ { value: "execution", labelKey: "tableImport.stepExecution" },+];const selectedConnection = computed(() => (props.prefillConnectionId ? store.getConfig(props.prefillConnectionId) : undefined));const targetColumnNames = computed(() => targetColumns.value.map((column) => column.name));@@ -55,7 +82,21 @@ const mappedColumns = computed<api.TableImportColumnMapping[]>(() => {.filter((mapping) => mapping.targetColumn);});const mappedCount = computed(() => mappedColumns.value.length);-const canImport = computed(() => !!preview.value && !!props.prefillConnectionId && !!props.prefillTable && mappedColumns.value.length > 0 && !running.value);+const mappingValidation = computed(() => validateImportMappings(mappedColumns.value));+const requiredUnmappedColumns = computed(() =>+ requiredImportTargetColumns(+ targetColumns.value,+ mappedColumns.value.map((mapping) => mapping.targetColumn),+ ),+);+const canImport = computed(() => !!preview.value && !!props.prefillConnectionId && !!props.prefillTable && mappingValidation.value.valid && !running.value);+const canGoBack = computed(() => wizardStep.value !== "source" && wizardStep.value !== "execution" && !running.value);+const canGoNext = computed(() => {+ if (wizardStep.value === "source") return !!selectedSource.value && !!sourceFormat.value;+ if (wizardStep.value === "options") return !!preview.value;+ if (wizardStep.value === "mapping") return mappingValidation.value.valid;+ return false;+});const progressPercent = computed(() => {const p = progress.value;if (!p || p.totalRows <= 0) return 0;@@ -65,9 +106,32 @@ const targetLabel = computed(() => {const pieces = [selectedConnection.value?.name, props.prefillDatabase, props.prefillSchema, props.prefillTable].filter(Boolean);return pieces.join(" / ");});+const selectedSourceName = computed(() => {+ const source = selectedSource.value;+ if (!source) return "";+ return typeof source === "string" ? source.split(/[\\/]/).pop() || source : source.name;+});+const parseOptions = computed<api.TableImportParseOptions>(() => ({+ delimiter: sourceFormat.value === "tsv" ? "\\t" : sourceFormat.value === "csv" ? "," : delimiter.value,+ hasHeader: hasHeader.value,+ trimValues: trimValues.value,+ emptyStringAsNull: emptyStringAsNull.value,+ sheetName: sourceFormat.value === "excel" ? selectedSheet.value || null : null,+ jsonShape: sourceFormat.value === "json" ? jsonShape.value : null,+}));+const terminalStatus = computed(() => progress.value?.status && ["done", "error", "cancelled"].includes(progress.value.status));function resetState() {targetColumns.value = [];+ selectedSource.value = null;+ sourceFormat.value = "csv";+ delimiter.value = ",";+ hasHeader.value = true;+ trimValues.value = false;+ emptyStringAsNull.value = true;+ selectedSheet.value = "";+ jsonShape.value = "auto";+ previewLimit.value = 50;preview.value = null;columnMapping.value = {};importMode.value = "append";@@ -77,6 +141,16 @@ function resetState() {importId.value = "";progress.value = null;errorMessage.value = "";+ wizardStep.value = "source";+}++function detectFormat(name: string): api.TableImportSourceFormat {+ const lower = name.toLowerCase();+ if (lower.endsWith(".tsv")) return "tsv";+ if (lower.endsWith(".txt")) return "delimited";+ if (lower.endsWith(".json")) return "json";+ if (lower.endsWith(".xls") || lower.endsWith(".xlsx") || lower.endsWith(".xlsm")) return "excel";+ return "csv";}function applyAutoMapping() {@@ -101,18 +175,23 @@ async function loadTargetColumns() {}async function previewSelectedImportFile(fileOrPath: string | File) {- if (isTauriRuntime()) {- return api.previewTableImportFile(fileOrPath as string);- }- const { previewTableImportFile } = await import("@/lib/backend/http");- return previewTableImportFile(fileOrPath as File);+ return api.previewTableImportFile(fileOrPath, {+ sourceFormat: sourceFormat.value,+ parseOptions: parseOptions.value,+ previewLimit: Math.max(1, Number(previewLimit.value) || 50),+ });}-async function loadPreview(fileOrPath: string | File) {+async function loadPreview(fileOrPath = selectedSource.value) {+ if (!fileOrPath) return;loadingPreview.value = true;errorMessage.value = "";try {- preview.value = await previewSelectedImportFile(fileOrPath);+ const nextPreview = await previewSelectedImportFile(fileOrPath);+ preview.value = nextPreview;+ if (sourceFormat.value === "excel" && !selectedSheet.value && nextPreview.sheets?.length) {+ selectedSheet.value = nextPreview.sheets[0];+ }applyAutoMapping();} catch (e: any) {preview.value = null;@@ -123,6 +202,19 @@ async function loadPreview(fileOrPath: string | File) {}}+function assignSelectedSource(source: string | File) {+ selectedSource.value = source;+ preview.value = null;+ columnMapping.value = {};+ progress.value = null;+ errorMessage.value = "";+ const name = typeof source === "string" ? source : source.name;+ sourceFormat.value = detectFormat(name);+ delimiter.value = sourceFormat.value === "tsv" ? "\\t" : ",";+ selectedSheet.value = "";+ wizardStep.value = "options";+}+async function selectFile() {if (!isTauriRuntime()) {fileInput.value?.click();@@ -132,15 +224,14 @@ async function selectFile() {const selected = await open({multiple: false,filters: [- { name: "Data files", extensions: ["csv", "tsv", "json", "xlsx", "xlsm", "xls"] },- { name: "CSV", extensions: ["csv", "tsv"] },+ { name: "Data files", extensions: ["csv", "tsv", "txt", "json", "xlsx", "xlsm", "xls"] },+ { name: "Text", extensions: ["csv", "tsv", "txt"] },{ name: "JSON", extensions: ["json"] },{ name: "Excel", extensions: ["xlsx", "xlsm", "xls"] },],});if (!selected || Array.isArray(selected)) return;-- await loadPreview(selected);+ assignSelectedSource(selected);}async function handleFileInputChange(event: Event) {@@ -148,7 +239,7 @@ async function handleFileInputChange(event: Event) {const file = input.files?.[0];input.value = "";if (!file || running.value) return;- await loadPreview(file);+ assignSelectedSource(file);}function updateMapping(sourceColumn: string, value: any) {@@ -165,12 +256,34 @@ function formatCell(value: unknown) {return String(value);}+function goBack() {+ wizardStep.value = previousTableImportWizardStep(wizardStep.value);+}++function canOpenStep(step: TableImportWizardStep) {+ if (running.value || step === "execution") return false;+ if (step === "source") return true;+ if (step === "options") return !!selectedSource.value;+ if (step === "mapping") return !!preview.value;+ if (step === "review") return !!preview.value && mappingValidation.value.valid;+ return false;+}++async function goNext() {+ if (wizardStep.value === "options" && !preview.value) {+ await loadPreview();+ if (!preview.value) return;+ }+ wizardStep.value = nextTableImportWizardStep(wizardStep.value);+}+async function startImport() {const currentPreview = preview.value;if (!canImport.value || !currentPreview || !props.prefillConnectionId || !props.prefillTable) return;running.value = true;cancelling.value = false;errorMessage.value = "";+ wizardStep.value = "execution";importId.value = uuid();progress.value = {importId: importId.value,@@ -188,6 +301,9 @@ async function startImport() {schema: props.prefillSchema || "",table: props.prefillTable,filePath: currentPreview.filePath,+ sourceRef: currentPreview.sourceRef || null,+ sourceFormat: sourceFormat.value,+ parseOptions: parseOptions.value,mappings: mappedColumns.value,mode: importMode.value,batchSize: Math.max(1, Number(batchSize.value) || 500),@@ -196,11 +312,19 @@ async function startImport() {progress.value = nextProgress;},);+ progress.value = { importId: summary.importId, status: "done", rowsImported: summary.rowsImported, totalRows: summary.totalRows };toast(t("tableImport.success", { count: summary.rowsImported }), 2500);store.invalidateMetadataCache(props.prefillConnectionId, props.prefillDatabase || "", props.prefillSchema || undefined, props.prefillTable);- open.value = false;} catch (e: any) {- errorMessage.value = String(e?.message || e);+ const message = String(e?.message || e);+ errorMessage.value = message;+ progress.value = {+ importId: importId.value,+ status: progress.value?.status === "cancelled" ? "cancelled" : "error",+ rowsImported: progress.value?.rowsImported ?? 0,+ totalRows: progress.value?.totalRows ?? currentPreview.totalRows,+ error: message,+ };} finally {running.value = false;cancelling.value = false;@@ -213,6 +337,14 @@ async function cancelImport() {await api.cancelTableImport(importId.value);}+function schedulePreviewReload() {+ if (!preview.value || !selectedSource.value || loadingPreview.value || running.value) return;+ if (previewReloadTimer) clearTimeout(previewReloadTimer);+ previewReloadTimer = setTimeout(() => {+ void loadPreview();+ }, 250);+}+watch(open,(value) => {@@ -223,13 +355,15 @@ watch(},{ immediate: true },);++watch([sourceFormat, delimiter, hasHeader, trimValues, emptyStringAsNull, selectedSheet, jsonShape, previewLimit], schedulePreviewReload);</script><template><Dialog v-model:open="open">- <DialogScrollContent class="sm:max-w-[760px]" :trap-focus="false" @interact-outside.prevent>+ <DialogScrollContent class="sm:max-w-[980px] pt-12" :trap-focus="false" @interact-outside.prevent><DialogHeader>- <DialogTitle class="flex items-center gap-2">+ <DialogTitle class="flex items-center gap-2 text-base"><FileUp class="h-4 w-4" />{{ t("tableImport.title") }}</DialogTitle>@@ -237,7 +371,7 @@ watch(<div class="space-y-4 py-2"><div class="grid grid-cols-[1fr_auto] gap-2">- <input ref="fileInput" type="file" accept=".csv,.tsv,.json,.xlsx,.xlsm,.xls" class="hidden" @change="handleFileInputChange" />+ <input ref="fileInput" type="file" accept=".csv,.tsv,.txt,.json,.xlsx,.xlsm,.xls" class="hidden" @change="handleFileInputChange" /><div class="min-w-0 rounded-md border bg-muted/20 px-3 py-2"><div class="truncate text-xs text-muted-foreground">{{ t("tableImport.target") }}</div><div class="truncate text-sm font-medium">@@ -247,96 +381,257 @@ watch(<Button variant="outline" size="sm" :disabled="running || loadingPreview" @click="selectFile"><Loader2 v-if="loadingPreview" class="mr-1.5 h-3.5 w-3.5 animate-spin" /><Upload v-else class="mr-1.5 h-3.5 w-3.5" />- {{ t("tableImport.selectFile") }}+ {{ selectedSource ? t("tableImport.changeFile") : t("tableImport.selectFile") }}</Button></div>- <div v-if="preview" class="grid grid-cols-3 gap-2 text-xs">- <div class="rounded-md border px-3 py-2">- <div class="text-muted-foreground">{{ t("tableImport.file") }}</div>- <div class="truncate font-medium">{{ preview.fileName }}</div>+ <div class="grid grid-cols-5 gap-1 rounded-md border bg-muted/20 p-1">+ <button v-for="step in wizardSteps" :key="step.value" type="button" class="h-8 rounded px-2 text-xs font-medium" :class="wizardStep === step.value ? 'bg-background text-foreground shadow-sm' : 'text-muted-foreground'" :disabled="!canOpenStep(step.value)" @click="wizardStep = step.value">+ {{ t(step.labelKey) }}+ </button>+ </div>++ <div v-if="wizardStep === 'source'" class="space-y-4">+ <div class="rounded-md border border-dashed p-6 text-center">+ <FileUp class="mx-auto mb-3 h-8 w-8 text-muted-foreground" />+ <div class="text-sm font-medium">{{ selectedSourceName || t("tableImport.noFileSelected") }}</div>+ <Button class="mt-4" size="sm" @click="selectFile">+ <Upload class="mr-1.5 h-3.5 w-3.5" />+ {{ t("tableImport.selectFile") }}+ </Button>+ </div>+ <div class="grid grid-cols-5 gap-2">+ <button v-for="format in formatOptions" :key="format.value" type="button" class="min-h-20 rounded-md border px-3 py-2 text-left" :class="sourceFormat === format.value ? 'border-primary bg-primary/5' : 'hover:bg-muted/30'" @click="sourceFormat = format.value">+ <component :is="format.icon" class="mb-2 h-4 w-4 text-muted-foreground" />+ <div class="text-xs font-medium">{{ t(format.labelKey) }}</div>+ <div class="mt-1 text-[11px] leading-snug text-muted-foreground">{{ t(format.descriptionKey) }}</div>+ </button>+ </div>+ </div>++ <div v-else-if="wizardStep === 'options'" class="space-y-4">+ <div class="grid grid-cols-3 gap-3">+ <div class="space-y-1.5">+ <Label class="text-xs">{{ t("tableImport.sourceFormat") }}</Label>+ <Select :model-value="sourceFormat" @update:model-value="(value: any) => (sourceFormat = value)">+ <SelectTrigger class="h-8 text-xs">+ <SelectValue />+ </SelectTrigger>+ <SelectContent>+ <SelectItem v-for="format in formatOptions" :key="format.value" :value="format.value">+ {{ t(format.labelKey) }}+ </SelectItem>+ </SelectContent>+ </Select>+ </div>+ <div class="space-y-1.5">+ <Label class="text-xs">{{ t("tableImport.previewRows") }}</Label>+ <Input v-model.number="previewLimit" type="number" min="1" max="500" class="h-8 text-xs" />+ </div>+ <div class="space-y-1.5">+ <Label class="text-xs">{{ t("tableImport.sourceFile") }}</Label>+ <div class="flex h-8 items-center rounded-md border px-2 text-xs">+ <span class="truncate">{{ selectedSourceName || t("tableImport.noFileSelected") }}</span>+ </div>+ </div>+ </div>++ <div v-if="sourceFormat === 'csv' || sourceFormat === 'tsv' || sourceFormat === 'delimited'" class="grid grid-cols-4 gap-3 rounded-md border p-3">+ <div class="space-y-1.5">+ <Label class="text-xs">{{ t("tableImport.delimiter") }}</Label>+ <Input v-model="delimiter" :disabled="sourceFormat !== 'delimited'" class="h-8 text-xs font-mono" />+ </div>+ <label class="flex items-center gap-2 text-xs">+ <input v-model="hasHeader" type="checkbox" class="h-3.5 w-3.5 accent-primary" />+ {{ t("tableImport.hasHeader") }}+ </label>+ <label class="flex items-center gap-2 text-xs">+ <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>++ <div v-else-if="sourceFormat === 'json'" class="grid grid-cols-2 gap-3 rounded-md border p-3">+ <div class="space-y-1.5">+ <Label class="text-xs">{{ t("tableImport.jsonShape") }}</Label>+ <Select :model-value="jsonShape" @update:model-value="(value: any) => (jsonShape = value)">+ <SelectTrigger class="h-8 text-xs">+ <SelectValue />+ </SelectTrigger>+ <SelectContent>+ <SelectItem value="auto">{{ t("tableImport.jsonShapeAuto") }}</SelectItem>+ <SelectItem value="objects">{{ t("tableImport.jsonShapeObjects") }}</SelectItem>+ <SelectItem value="arrays">{{ t("tableImport.jsonShapeArrays") }}</SelectItem>+ </SelectContent>+ </Select>+ </div></div>- <div class="rounded-md border px-3 py-2">- <div class="text-muted-foreground">{{ t("tableImport.rows") }}</div>- <div class="font-medium">{{ preview.totalRows.toLocaleString() }}</div>++ <div v-else-if="sourceFormat === 'excel'" class="grid grid-cols-3 gap-3 rounded-md border p-3">+ <div class="space-y-1.5">+ <Label class="text-xs">{{ t("tableImport.sheet") }}</Label>+ <Select :model-value="selectedSheet" :disabled="!preview?.sheets?.length" @update:model-value="(value: any) => (selectedSheet = value)">+ <SelectTrigger class="h-8 text-xs">+ <SelectValue :placeholder="t('tableImport.firstSheet')" />+ </SelectTrigger>+ <SelectContent>+ <SelectItem v-for="sheet in preview?.sheets || []" :key="sheet" :value="sheet">{{ sheet }}</SelectItem>+ </SelectContent>+ </Select>+ </div>+ <label class="flex items-center gap-2 text-xs">+ <input v-model="hasHeader" type="checkbox" class="h-3.5 w-3.5 accent-primary" />+ {{ t("tableImport.hasHeader") }}+ </label></div>- <div class="rounded-md border px-3 py-2">- <div class="text-muted-foreground">{{ t("tableImport.mapped") }}</div>- <div class="font-medium">{{ mappedCount }} / {{ preview.columns.length }}</div>++ <div class="flex items-center gap-2">+ <Button size="sm" :disabled="!selectedSource || loadingPreview" @click="loadPreview()">+ <Loader2 v-if="loadingPreview" class="mr-1.5 h-3.5 w-3.5 animate-spin" />+ <RefreshCw v-else class="mr-1.5 h-3.5 w-3.5" />+ {{ preview ? t("tableImport.reloadPreview") : t("tableImport.loadPreview") }}+ </Button>+ <span v-if="preview" class="text-xs text-muted-foreground">{{ t("tableImport.previewReady", { rows: preview.totalRows, columns: preview.columns.length }) }}</span></div></div>- <div v-if="preview" class="grid grid-cols-[minmax(220px,280px)_1fr] gap-3">- <div class="rounded-md border">- <div class="border-b px-3 py-2 text-xs font-medium">{{ t("tableImport.mapping") }}</div>- <div class="max-h-[280px] overflow-auto p-2">- <div v-for="sourceColumn in preview.columns" :key="sourceColumn" class="grid grid-cols-[1fr_1fr] items-center gap-2 py-1">- <div class="truncate font-mono text-xs" :title="sourceColumn">- {{ sourceColumn }}+ <div v-else-if="wizardStep === 'mapping'" class="space-y-3">+ <div v-if="preview" class="grid grid-cols-3 gap-2 text-xs">+ <div class="rounded-md border px-3 py-2">+ <div class="text-muted-foreground">{{ t("tableImport.file") }}</div>+ <div class="truncate font-medium">{{ preview.fileName }}</div>+ </div>+ <div class="rounded-md border px-3 py-2">+ <div class="text-muted-foreground">{{ t("tableImport.rows") }}</div>+ <div class="font-medium">{{ preview.totalRows.toLocaleString() }}</div>+ </div>+ <div class="rounded-md border px-3 py-2">+ <div class="text-muted-foreground">{{ t("tableImport.mapped") }}</div>+ <div class="font-medium">{{ mappedCount }} / {{ preview.columns.length }}</div>+ </div>+ </div>++ <div v-if="preview" class="grid grid-cols-[minmax(240px,300px)_1fr] gap-3">+ <div class="rounded-md border">+ <div class="border-b px-3 py-2 text-xs font-medium">{{ t("tableImport.mapping") }}</div>+ <div class="max-h-[320px] overflow-auto p-2">+ <div v-for="sourceColumn in preview.columns" :key="sourceColumn" class="grid grid-cols-[1fr_1fr] items-center gap-2 py-1">+ <div class="truncate font-mono text-xs" :title="sourceColumn">+ {{ sourceColumn }}+ </div>+ <Select :model-value="columnMapping[sourceColumn] || SKIP_VALUE" @update:model-value="(value: any) => updateMapping(sourceColumn, value)">+ <SelectTrigger class="h-7 text-xs">+ <SelectValue />+ </SelectTrigger>+ <SelectContent>+ <SelectItem :value="SKIP_VALUE">{{ t("tableImport.skipColumn") }}</SelectItem>+ <SelectItem v-for="column in targetColumns" :key="column.name" :value="column.name">+ {{ column.name }}+ </SelectItem>+ </SelectContent>+ </Select></div>- <Select :model-value="columnMapping[sourceColumn] || SKIP_VALUE" @update:model-value="(value: any) => updateMapping(sourceColumn, value)">- <SelectTrigger class="h-7 text-xs">- <SelectValue />- </SelectTrigger>- <SelectContent>- <SelectItem :value="SKIP_VALUE">{{ t("tableImport.skipColumn") }}</SelectItem>- <SelectItem v-for="column in targetColumns" :key="column.name" :value="column.name">- {{ column.name }}- </SelectItem>- </SelectContent>- </Select></div></div>- </div>- <div class="min-w-0 rounded-md border">- <div class="border-b px-3 py-2 text-xs font-medium">{{ t("tableImport.preview") }}</div>- <div class="max-h-[280px] overflow-auto">- <table class="min-w-full border-separate border-spacing-0 text-xs">- <thead class="sticky top-0 bg-background">- <tr>- <th v-for="column in preview.columns" :key="column" class="border-b border-r px-2 py-1.5 text-left font-medium">- <span class="block max-w-[140px] truncate">{{ column }}</span>- </th>- </tr>- </thead>- <tbody>- <tr v-for="(row, rowIndex) in preview.rows" :key="rowIndex">- <td v-for="(cell, colIndex) in row" :key="colIndex" class="max-w-[180px] border-b border-r px-2 py-1.5 font-mono" :class="{ 'text-muted-foreground': cell === null }">- <span class="block truncate">{{ formatCell(cell) }}</span>- </td>- </tr>- </tbody>- </table>+ <div class="min-w-0 rounded-md border">+ <div class="border-b px-3 py-2 text-xs font-medium">{{ t("tableImport.preview") }}</div>+ <div class="max-h-[320px] overflow-auto">+ <table class="min-w-full border-separate border-spacing-0 text-xs">+ <thead class="sticky top-0 bg-background">+ <tr>+ <th v-for="column in preview.columns" :key="column" class="border-b border-r px-2 py-1.5 text-left font-medium">+ <span class="block max-w-[140px] truncate">{{ column }}</span>+ </th>+ </tr>+ </thead>+ <tbody>+ <tr v-for="(row, rowIndex) in preview.rows" :key="rowIndex">+ <td v-for="(cell, colIndex) in row" :key="colIndex" class="max-w-[180px] border-b border-r px-2 py-1.5 font-mono" :class="{ 'text-muted-foreground': cell === null }">+ <span class="block truncate">{{ formatCell(cell) }}</span>+ </td>+ </tr>+ </tbody>+ </table>+ </div></div></div>++ <div v-if="mappingValidation.errors.length" class="rounded-md border border-destructive/30 bg-destructive/10 px-3 py-2 text-xs text-destructive">+ {{ mappingValidation.errors.join("; ") }}+ </div>+ <div v-else-if="requiredUnmappedColumns.length" class="flex items-start gap-2 rounded-md border border-amber-300 bg-amber-50 px-3 py-2 text-xs text-amber-800 dark:bg-amber-950/20 dark:text-amber-300">+ <AlertTriangle class="mt-0.5 h-3.5 w-3.5 shrink-0" />+ <span>{{ t("tableImport.requiredUnmapped", { columns: requiredUnmappedColumns.join(", ") }) }}</span>+ </div></div>- <div v-if="preview" class="grid grid-cols-3 gap-3">- <div class="space-y-1.5">- <Label class="text-xs">{{ t("tableImport.mode") }}</Label>- <Select :model-value="importMode" @update:model-value="(value: any) => (importMode = value)">- <SelectTrigger class="h-8 text-xs">- <SelectValue />- </SelectTrigger>- <SelectContent>- <SelectItem value="append">{{ t("tableImport.append") }}</SelectItem>- <SelectItem value="truncate">{{ t("tableImport.truncate") }}</SelectItem>- </SelectContent>- </Select>+ <div v-else-if="wizardStep === 'review'" class="space-y-3">+ <div class="grid grid-cols-2 gap-3 text-xs">+ <div class="rounded-md border px-3 py-2">+ <div class="text-muted-foreground">{{ t("tableImport.target") }}</div>+ <div class="truncate font-medium">{{ targetLabel }}</div>+ </div>+ <div class="rounded-md border px-3 py-2">+ <div class="text-muted-foreground">{{ t("tableImport.sourceFile") }}</div>+ <div class="truncate font-medium">{{ preview?.fileName }}</div>+ </div>+ <div class="rounded-md border px-3 py-2">+ <div class="text-muted-foreground">{{ t("tableImport.rows") }}</div>+ <div class="font-medium">{{ preview?.totalRows.toLocaleString() }}</div>+ </div>+ <div class="rounded-md border px-3 py-2">+ <div class="text-muted-foreground">{{ t("tableImport.mapped") }}</div>+ <div class="font-medium">{{ mappedCount }} / {{ preview?.columns.length || 0 }}</div>+ </div>+ </div>+ <div class="grid grid-cols-3 gap-3">+ <div class="space-y-1.5">+ <Label class="text-xs">{{ t("tableImport.mode") }}</Label>+ <Select :model-value="importMode" @update:model-value="(value: any) => (importMode = value)">+ <SelectTrigger class="h-8 text-xs">+ <SelectValue />+ </SelectTrigger>+ <SelectContent>+ <SelectItem value="append">{{ t("tableImport.append") }}</SelectItem>+ <SelectItem value="truncate">{{ t("tableImport.truncate") }}</SelectItem>+ </SelectContent>+ </Select>+ </div>+ <div class="space-y-1.5">+ <Label class="text-xs">{{ t("transfer.batchSize") }}</Label>+ <Input v-model.number="batchSize" type="number" min="1" class="h-8 text-xs" />+ </div></div>- <div class="space-y-1.5">- <Label class="text-xs">{{ t("transfer.batchSize") }}</Label>- <Input v-model.number="batchSize" type="number" min="1" class="h-8 text-xs" />+ <div v-if="importMode === 'truncate'" class="flex items-start gap-2 rounded-md border border-amber-300 bg-amber-50 px-3 py-2 text-xs text-amber-800 dark:bg-amber-950/20 dark:text-amber-300">+ <AlertTriangle class="mt-0.5 h-3.5 w-3.5 shrink-0" />+ <span>{{ t("tableImport.truncateWarning") }}</span></div>- <div v-if="running || progress" class="space-y-1.5">- <Label class="text-xs">{{ t("tableImport.progress") }}</Label>- <div class="h-8 rounded-md border px-2 text-xs flex items-center gap-2">- <Loader2 v-if="running && !cancelling" class="h-3.5 w-3.5 animate-spin text-primary" />- <Square v-else-if="cancelling" class="h-3.5 w-3.5 fill-current text-destructive" />- <Check v-else class="h-3.5 w-3.5 text-emerald-600" />- <span class="truncate"> {{ progress?.rowsImported ?? 0 }} / {{ progress?.totalRows ?? preview.totalRows }} · {{ progressPercent }}% </span>+ </div>++ <div v-else class="space-y-4">+ <div class="rounded-md border px-4 py-5">+ <div class="flex items-center gap-3">+ <Loader2 v-if="running && !cancelling" class="h-5 w-5 animate-spin text-primary" />+ <Square v-else-if="cancelling || progress?.status === 'cancelled'" class="h-5 w-5 fill-current text-destructive" />+ <CheckCircle2 v-else-if="progress?.status === 'done'" class="h-5 w-5 text-emerald-600" />+ <AlertTriangle v-else-if="progress?.status === 'error'" class="h-5 w-5 text-destructive" />+ <FileUp v-else class="h-5 w-5 text-muted-foreground" />+ <div class="min-w-0 flex-1">+ <div class="text-sm font-medium">{{ t(`tableImport.status_${progress?.status || "idle"}`) }}</div>+ <div class="mt-1 text-xs text-muted-foreground">{{ progress?.rowsImported ?? 0 }} / {{ progress?.totalRows ?? preview?.totalRows ?? 0 }} · {{ progressPercent }}%</div>+ </div></div>+ <div class="mt-4 h-2 overflow-hidden rounded bg-muted">+ <div class="h-full bg-primary transition-all" :style="{ width: `${progressPercent}%` }" />+ </div>+ </div>+ <div v-if="errorMessage || progress?.error" class="rounded-md border border-destructive/30 bg-destructive/10 px-3 py-2 text-xs text-destructive">+ {{ errorMessage || progress?.error }}</div></div>@@ -344,7 +639,7 @@ watch(<Loader2 class="h-3.5 w-3.5 animate-spin" />{{ t("common.loading") }}</div>- <div v-if="errorMessage" class="rounded-md border border-destructive/30 bg-destructive/10 px-3 py-2 text-xs text-destructive">+ <div v-if="errorMessage && wizardStep !== 'execution'" class="rounded-md border border-destructive/30 bg-destructive/10 px-3 py-2 text-xs text-destructive">{{ errorMessage }}</div></div>@@ -352,16 +647,28 @@ watch(<DialogFooter><Button variant="outline" :disabled="running" @click="open = false"><X class="mr-1.5 h-3.5 w-3.5" />- {{ t("dangerDialog.cancel") }}+ {{ terminalStatus ? t("common.close") : t("dangerDialog.cancel") }}+ </Button>+ <Button v-if="canGoBack" variant="outline" @click="goBack">+ <ArrowLeft class="mr-1.5 h-3.5 w-3.5" />+ {{ t("tableImport.back") }}</Button>- <Button v-if="running" variant="destructive" :disabled="cancelling" @click="cancelImport">+ <Button v-if="wizardStep === 'source' || wizardStep === 'options' || wizardStep === 'mapping'" :disabled="!canGoNext || loadingPreview" @click="goNext">+ <ArrowRight class="mr-1.5 h-3.5 w-3.5" />+ {{ t("tableImport.next") }}+ </Button>+ <Button v-else-if="wizardStep === 'review'" :disabled="!canImport" @click="startImport">+ <Upload class="mr-1.5 h-3.5 w-3.5" />+ {{ t("tableImport.start") }}+ </Button>+ <Button v-else-if="running" variant="destructive" :disabled="cancelling" @click="cancelImport"><Loader2 v-if="cancelling" class="mr-1.5 h-3.5 w-3.5 animate-spin" /><Square v-else class="mr-1.5 h-3.5 w-3.5 fill-current" />{{ t("sqlFile.cancel") }}</Button>- <Button v-else :disabled="!canImport" @click="startImport">- <Upload class="mr-1.5 h-3.5 w-3.5" />- {{ t("tableImport.start") }}+ <Button v-else-if="progress?.status === 'done'" @click="open = false">+ <Check class="mr-1.5 h-3.5 w-3.5" />+ {{ t("common.done") }}</Button></DialogFooter></DialogScrollContent>diff --git a/apps/desktop/src/i18n/locales/en.ts b/apps/desktop/src/i18n/locales/en.tsindex 80fd820154..f875562533 100644--- a/apps/desktop/src/i18n/locales/en.ts+++ b/apps/desktop/src/i18n/locales/en.ts@@ -2187,6 +2187,39 @@ export default {title: "Import Table Data",target: "Target table",selectFile: "Select File",+ changeFile: "Change File",+ noFileSelected: "No file selected",+ sourceFile: "Source file",+ sourceFormat: "Source format",+ stepSource: "Source",+ stepOptions: "Options",+ stepMapping: "Mapping",+ stepReview: "Review",+ stepExecution: "Run",+ formatCsv: "CSV",+ formatCsvDescription: "Comma-separated text",+ formatTsv: "TSV",+ formatTsvDescription: "Tab-separated text",+ formatDelimited: "Delimited",+ formatDelimitedDescription: "Custom text delimiter",+ formatJson: "JSON",+ formatJsonDescription: "Object or array rows",+ formatExcel: "Excel",+ formatExcelDescription: "XLS, XLSX, XLSM",+ previewRows: "Preview rows",+ delimiter: "Delimiter",+ hasHeader: "First row is header",+ trimValues: "Trim values",+ emptyStringAsNull: "Empty string as NULL",+ jsonShape: "JSON rows",+ jsonShapeAuto: "Auto-detect",+ jsonShapeObjects: "Objects",+ jsonShapeArrays: "Arrays",+ sheet: "Sheet",+ firstSheet: "First sheet",+ loadPreview: "Load Preview",+ reloadPreview: "Reload Preview",+ previewReady: "{rows} rows, {columns} columns",file: "File",rows: "Rows",mapped: "Mapped",@@ -2199,6 +2232,15 @@ export default {progress: "Progress",start: "Start Import",success: "Imported {count} rows",+ back: "Back",+ next: "Next",+ truncateWarning: "Existing rows in the target table will be removed before import starts.",+ requiredUnmapped: "Required target columns are not mapped: {columns}",+ status_idle: "Ready to import",+ status_running: "Importing data",+ status_done: "Import complete",+ status_error: "Import failed",+ status_cancelled: "Import cancelled",},dataGenerate: {title: "Data Generation",diff --git a/apps/desktop/src/i18n/locales/es.ts b/apps/desktop/src/i18n/locales/es.tsindex 60b9f59da1..155939ca74 100644--- a/apps/desktop/src/i18n/locales/es.ts+++ b/apps/desktop/src/i18n/locales/es.ts@@ -2131,6 +2131,39 @@ export default withEnglishFallback({title: "Importar datos a tabla",target: "Tabla destino",selectFile: "Seleccionar archivo",+ changeFile: "Change File",+ noFileSelected: "No file selected",+ sourceFile: "Source file",+ sourceFormat: "Source format",+ stepSource: "Source",+ stepOptions: "Options",+ stepMapping: "Mapping",+ stepReview: "Review",+ stepExecution: "Run",+ formatCsv: "CSV",+ formatCsvDescription: "Comma-separated text",+ formatTsv: "TSV",+ formatTsvDescription: "Tab-separated text",+ formatDelimited: "Delimited",+ formatDelimitedDescription: "Custom text delimiter",+ formatJson: "JSON",+ formatJsonDescription: "Object or array rows",+ formatExcel: "Excel",+ formatExcelDescription: "XLS, XLSX, XLSM",+ previewRows: "Preview rows",+ delimiter: "Delimiter",+ hasHeader: "First row is header",+ trimValues: "Trim values",+ emptyStringAsNull: "Empty string as NULL",+ jsonShape: "JSON rows",+ jsonShapeAuto: "Auto-detect",+ jsonShapeObjects: "Objects",+ jsonShapeArrays: "Arrays",+ sheet: "Sheet",+ firstSheet: "First sheet",+ loadPreview: "Load Preview",+ reloadPreview: "Reload Preview",+ previewReady: "{rows} rows, {columns} columns",file: "Archivo",rows: "Filas",mapped: "Mapeadas",@@ -2143,6 +2176,15 @@ export default withEnglishFallback({progress: "Progreso",start: "Iniciar importación",success: "Se importaron {count} filas",+ back: "Back",+ next: "Next",+ truncateWarning: "Existing rows in the target table will be removed before import starts.",+ requiredUnmapped: "Required target columns are not mapped: {columns}",+ status_idle: "Ready to import",+ status_running: "Importing data",+ status_done: "Import complete",+ status_error: "Import failed",+ status_cancelled: "Import cancelled",},dataGenerate: {title: "Generación de datos",diff --git a/apps/desktop/src/i18n/locales/it.ts b/apps/desktop/src/i18n/locales/it.tsindex 2a17a2a8d6..7ec8035330 100644--- a/apps/desktop/src/i18n/locales/it.ts+++ b/apps/desktop/src/i18n/locales/it.ts@@ -2129,6 +2129,39 @@ export default withEnglishFallback({title: "Importa Dati Tabella",target: "Tabella di destinazione",selectFile: "Seleziona File",+ changeFile: "Change File",+ noFileSelected: "No file selected",+ sourceFile: "Source file",+ sourceFormat: "Source format",+ stepSource: "Source",+ stepOptions: "Options",+ stepMapping: "Mapping",+ stepReview: "Review",+ stepExecution: "Run",+ formatCsv: "CSV",+ formatCsvDescription: "Comma-separated text",+ formatTsv: "TSV",+ formatTsvDescription: "Tab-separated text",+ formatDelimited: "Delimited",+ formatDelimitedDescription: "Custom text delimiter",+ formatJson: "JSON",+ formatJsonDescription: "Object or array rows",+ formatExcel: "Excel",+ formatExcelDescription: "XLS, XLSX, XLSM",+ previewRows: "Preview rows",+ delimiter: "Delimiter",+ hasHeader: "First row is header",+ trimValues: "Trim values",+ emptyStringAsNull: "Empty string as NULL",+ jsonShape: "JSON rows",+ jsonShapeAuto: "Auto-detect",+ jsonShapeObjects: "Objects",+ jsonShapeArrays: "Arrays",+ sheet: "Sheet",+ firstSheet: "First sheet",+ loadPreview: "Load Preview",+ reloadPreview: "Reload Preview",+ previewReady: "{rows} rows, {columns} columns",file: "File",rows: "Righe",mapped: "Mappata",@@ -2141,6 +2174,15 @@ export default withEnglishFallback({progress: "Progresso",start: "Avvia Importazione",success: "Importate {count} righe",+ back: "Back",+ next: "Next",+ truncateWarning: "Existing rows in the target table will be removed before import starts.",+ requiredUnmapped: "Required target columns are not mapped: {columns}",+ status_idle: "Ready to import",+ status_running: "Importing data",+ status_done: "Import complete",+ status_error: "Import failed",+ status_cancelled: "Import cancelled",},dataGenerate: {title: "Generazione dati",diff --git a/apps/desktop/src/i18n/locales/ja.ts b/apps/desktop/src/i18n/locales/ja.tsindex 5ccc31515e..91794199d5 100644--- a/apps/desktop/src/i18n/locales/ja.ts+++ b/apps/desktop/src/i18n/locales/ja.ts@@ -2129,6 +2129,39 @@ export default withEnglishFallback({title: "テーブルデータをインポート",target: "対象テーブル",selectFile: "ファイルを選択",+ changeFile: "Change File",+ noFileSelected: "No file selected",+ sourceFile: "Source file",+ sourceFormat: "Source format",+ stepSource: "Source",+ stepOptions: "Options",+ stepMapping: "Mapping",+ stepReview: "Review",+ stepExecution: "Run",+ formatCsv: "CSV",+ formatCsvDescription: "Comma-separated text",+ formatTsv: "TSV",+ formatTsvDescription: "Tab-separated text",+ formatDelimited: "Delimited",+ formatDelimitedDescription: "Custom text delimiter",+ formatJson: "JSON",+ formatJsonDescription: "Object or array rows",+ formatExcel: "Excel",+ formatExcelDescription: "XLS, XLSX, XLSM",+ previewRows: "Preview rows",+ delimiter: "Delimiter",+ hasHeader: "First row is header",+ trimValues: "Trim values",+ emptyStringAsNull: "Empty string as NULL",+ jsonShape: "JSON rows",+ jsonShapeAuto: "Auto-detect",+ jsonShapeObjects: "Objects",+ jsonShapeArrays: "Arrays",+ sheet: "Sheet",+ firstSheet: "First sheet",+ loadPreview: "Load Preview",+ reloadPreview: "Reload Preview",+ previewReady: "{rows} rows, {columns} columns",file: "ファイル",rows: "行数",mapped: "マッピング済み",@@ -2141,6 +2174,15 @@ export default withEnglishFallback({progress: "進捗",start: "インポート開始",success: "{count}行をインポートしました",+ back: "Back",+ next: "Next",+ truncateWarning: "Existing rows in the target table will be removed before import starts.",+ requiredUnmapped: "Required target columns are not mapped: {columns}",+ status_idle: "Ready to import",+ status_running: "Importing data",+ status_done: "Import complete",+ status_error: "Import failed",+ status_cancelled: "Import cancelled",},dataGenerate: {title: "データ生成",diff --git a/apps/desktop/src/i18n/locales/pt-BR.ts b/apps/desktop/src/i18n/locales/pt-BR.tsindex d3c4008953..888d23aaf0 100644--- a/apps/desktop/src/i18n/locales/pt-BR.ts+++ b/apps/desktop/src/i18n/locales/pt-BR.ts@@ -2130,6 +2130,39 @@ export default withEnglishFallback({title: "Importar Dados da Tabela",target: "Tabela de destino",selectFile: "Selecionar arquivo",+ changeFile: "Change File",+ noFileSelected: "No file selected",+ sourceFile: "Source file",+ sourceFormat: "Source format",+ stepSource: "Source",+ stepOptions: "Options",+ stepMapping: "Mapping",+ stepReview: "Review",+ stepExecution: "Run",+ formatCsv: "CSV",+ formatCsvDescription: "Comma-separated text",+ formatTsv: "TSV",+ formatTsvDescription: "Tab-separated text",+ formatDelimited: "Delimited",+ formatDelimitedDescription: "Custom text delimiter",+ formatJson: "JSON",+ formatJsonDescription: "Object or array rows",+ formatExcel: "Excel",+ formatExcelDescription: "XLS, XLSX, XLSM",+ previewRows: "Preview rows",+ delimiter: "Delimiter",+ hasHeader: "First row is header",+ trimValues: "Trim values",+ emptyStringAsNull: "Empty string as NULL",+ jsonShape: "JSON rows",+ jsonShapeAuto: "Auto-detect",+ jsonShapeObjects: "Objects",+ jsonShapeArrays: "Arrays",+ sheet: "Sheet",+ firstSheet: "First sheet",+ loadPreview: "Load Preview",+ reloadPreview: "Reload Preview",+ previewReady: "{rows} rows, {columns} columns",file: "Arquivo",rows: "Linhas",mapped: "Mapeado",@@ -2142,6 +2175,15 @@ export default withEnglishFallback({progress: "Progresso",start: "Iniciar importação",success: "{count} linhas importadas",+ back: "Back",+ next: "Next",+ truncateWarning: "Existing rows in the target table will be removed before import starts.",+ requiredUnmapped: "Required target columns are not mapped: {columns}",+ status_idle: "Ready to import",+ status_running: "Importing data",+ status_done: "Import complete",+ status_error: "Import failed",+ status_cancelled: "Import cancelled",},dataGenerate: {title: "Geração de dados",diff --git a/apps/desktop/src/i18n/locales/zh-CN.ts b/apps/desktop/src/i18n/locales/zh-CN.tsindex 6b9cc1be9a..753536fdc4 100644--- a/apps/desktop/src/i18n/locales/zh-CN.ts+++ b/apps/desktop/src/i18n/locales/zh-CN.ts@@ -2187,6 +2187,39 @@ export default withEnglishFallback({title: "导入表数据",target: "目标表",selectFile: "选择文件",+ changeFile: "更换文件",+ noFileSelected: "未选择文件",+ sourceFile: "源文件",+ sourceFormat: "源格式",+ stepSource: "来源",+ stepOptions: "选项",+ stepMapping: "映射",+ stepReview: "确认",+ stepExecution: "执行",+ formatCsv: "CSV",+ formatCsvDescription: "逗号分隔文本",+ formatTsv: "TSV",+ formatTsvDescription: "制表符分隔文本",+ formatDelimited: "分隔文本",+ formatDelimitedDescription: "自定义文本分隔符",+ formatJson: "JSON",+ formatJsonDescription: "对象行或数组行",+ formatExcel: "Excel",+ formatExcelDescription: "XLS、XLSX、XLSM",+ previewRows: "预览行数",+ delimiter: "分隔符",+ hasHeader: "首行为表头",+ trimValues: "裁剪空白",+ emptyStringAsNull: "空字符串作为 NULL",+ jsonShape: "JSON 行结构",+ jsonShapeAuto: "自动检测",+ jsonShapeObjects: "对象",+ jsonShapeArrays: "数组",+ sheet: "工作表",+ firstSheet: "第一个工作表",+ loadPreview: "加载预览",+ reloadPreview: "重新加载预览",+ previewReady: "{rows} 行,{columns} 列",file: "文件",rows: "行数",mapped: "已映射",@@ -2199,6 +2232,15 @@ export default withEnglishFallback({progress: "进度",start: "开始导入",success: "已导入 {count} 行",+ back: "上一步",+ next: "下一步",+ truncateWarning: "导入开始前会先删除目标表中的现有数据。",+ requiredUnmapped: "以下必填目标列未映射:{columns}",+ status_idle: "准备导入",+ status_running: "正在导入数据",+ status_done: "导入完成",+ status_error: "导入失败",+ status_cancelled: "导入已取消",},dataGenerate: {title: "数据生成",diff --git a/apps/desktop/src/i18n/locales/zh-TW.ts b/apps/desktop/src/i18n/locales/zh-TW.tsindex 194452b009..1874ac323a 100644--- a/apps/desktop/src/i18n/locales/zh-TW.ts+++ b/apps/desktop/src/i18n/locales/zh-TW.ts@@ -2033,6 +2033,39 @@ export default withEnglishFallback({title: "匯入資料表資料",target: "目標資料表",selectFile: "選擇檔案",+ changeFile: "更換檔案",+ noFileSelected: "未選擇檔案",+ sourceFile: "來源檔案",+ sourceFormat: "來源格式",+ stepSource: "來源",+ stepOptions: "選項",+ stepMapping: "映射",+ stepReview: "確認",+ stepExecution: "執行",+ formatCsv: "CSV",+ formatCsvDescription: "逗號分隔文字",+ formatTsv: "TSV",+ formatTsvDescription: "定位字元分隔文字",+ formatDelimited: "分隔文字",+ formatDelimitedDescription: "自訂文字分隔符",+ formatJson: "JSON",+ formatJsonDescription: "物件列或陣列列",+ formatExcel: "Excel",+ formatExcelDescription: "XLS、XLSX、XLSM",+ previewRows: "預覽列數",+ delimiter: "分隔符",+ hasHeader: "首列為標頭",+ trimValues: "裁剪空白",+ emptyStringAsNull: "空字串作為 NULL",+ jsonShape: "JSON 列結構",+ jsonShapeAuto: "自動偵測",+ jsonShapeObjects: "物件",+ jsonShapeArrays: "陣列",+ sheet: "工作表",+ firstSheet: "第一個工作表",+ loadPreview: "載入預覽",+ reloadPreview: "重新載入預覽",+ previewReady: "{rows} 列,{columns} 欄",file: "檔案",rows: "列數",mapped: "已映射",@@ -2045,6 +2078,15 @@ export default withEnglishFallback({progress: "進度",start: "開始匯入",success: "已匯入 {count} 行",+ back: "上一步",+ next: "下一步",+ truncateWarning: "匯入開始前會先刪除目標資料表中的現有資料。",+ requiredUnmapped: "以下必填目標欄位未映射:{columns}",+ status_idle: "準備匯入",+ status_running: "正在匯入資料",+ status_done: "匯入完成",+ status_error: "匯入失敗",+ status_cancelled: "匯入已取消",},dataGenerate: {title: "資料生成",diff --git a/apps/desktop/src/lib/__tests__/table/tableImport.spec.ts b/apps/desktop/src/lib/__tests__/table/tableImport.spec.tsnew file mode 100644index 0000000000..2099ee94f4--- /dev/null+++ b/apps/desktop/src/lib/__tests__/table/tableImport.spec.ts@@ -0,0 +1,49 @@+import { describe, expect, it } from "vitest";+import { autoMapImportColumns, nextTableImportWizardStep, previousTableImportWizardStep, requiredImportTargetColumns, validateImportMappings } from "@/lib/table/tableImport";++describe("tableImport", () => {+ it("auto maps exact and normalized column names", () => {+ expect(autoMapImportColumns(["id", "user name", "ignored"], ["id", "user_name"])).toEqual({+ id: "id",+ "user name": "user_name",+ ignored: "",+ });+ });++ it("rejects empty mappings and duplicate target columns", () => {+ expect(validateImportMappings([])).toEqual({+ valid: false,+ errors: ["No columns mapped for import"],+ duplicateTargets: [],+ });++ const result = validateImportMappings([+ { sourceColumn: "a", targetColumn: "name" },+ { sourceColumn: "b", targetColumn: "NAME" },+ ]);++ expect(result.valid).toBe(false);+ expect(result.duplicateTargets).toEqual(["NAME"]);+ expect(result.errors[0]).toContain("Target column mapped more than once");+ });++ it("detects unmapped required target columns", () => {+ expect(+ requiredImportTargetColumns(+ [+ { name: "id", is_nullable: false, column_default: null, extra: "auto_increment" },+ { name: "name", is_nullable: false, column_default: null },+ { name: "created_at", is_nullable: false, column_default: "CURRENT_TIMESTAMP" },+ ],+ ["id"],+ ),+ ).toEqual(["name"]);+ });++ it("moves through wizard steps with bounds", () => {+ expect(nextTableImportWizardStep("source")).toBe("options");+ expect(nextTableImportWizardStep("execution")).toBe("execution");+ expect(previousTableImportWizardStep("review")).toBe("mapping");+ expect(previousTableImportWizardStep("source")).toBe("source");+ });+});diff --git a/apps/desktop/src/lib/backend/api.ts b/apps/desktop/src/lib/backend/api.tsindex b33d6cbe03..a8b7c4f0a1 100644--- a/apps/desktop/src/lib/backend/api.ts+++ b/apps/desktop/src/lib/backend/api.ts@@ -501,7 +501,11 @@ export type {TransferTableNameCase,TableImportMode,TableImportStatus,+ TableImportSourceFormat,+ TableImportJsonShape,TableImportColumnMapping,+ TableImportParseOptions,+ TableImportPreviewRequest,TableImportPreview,TableImportRequest,TableImportSummary,diff --git a/apps/desktop/src/lib/backend/http.ts b/apps/desktop/src/lib/backend/http.tsindex da95f13637..2d16ee50bb 100644--- a/apps/desktop/src/lib/backend/http.ts+++ b/apps/desktop/src/lib/backend/http.ts@@ -74,6 +74,7 @@ import type {SqlFileProgress,TransferRequest,TransferProgress,+ TableImportPreviewRequest,TableImportPreview,TableImportRequest,TableImportSummary,@@ -1313,12 +1314,27 @@ export async function sortTablesByFkDependency(options: SortTablesByFkOptions):// Table File Import// ----------------------------------------------------------------------------export async function previewTableImportFile(fileOrPath: string | File): Promise<TableImportPreview> {+export async function previewTableImportFile(fileOrPath: string | File | TableImportPreviewRequest, options: Partial<TableImportPreviewRequest> = {}): Promise<TableImportPreview> {+ if (typeof fileOrPath === "object" && !(fileOrPath instanceof File)) {+ throw new Error("previewTableImportFile in web mode requires a File object for upload previews");+ }if (typeof fileOrPath === "string") {- throw new Error("previewTableImportFile in web mode requires a File object, not a file path");+ if (!options.sourceRef) {+ throw new Error("previewTableImportFile in web mode requires a File object for new uploads");+ }+ const res = await fetch(apiUrl("/api/import/preview"), {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ request: { ...options, filePath: fileOrPath } }),+ });+ if (!res.ok) throw new Error(await res.text());+ return res.json();}const formData = new FormData();formData.append("file", fileOrPath);+ if (options.sourceFormat) formData.append("sourceFormat", options.sourceFormat);+ if (options.parseOptions) formData.append("parseOptions", JSON.stringify(options.parseOptions));+ if (options.previewLimit != null) formData.append("previewLimit", String(options.previewLimit));const res = await fetch(apiUrl("/api/import/preview"), { method: "POST", body: formData });if (!res.ok) throw new Error(await res.text());return res.json();diff --git a/apps/desktop/src/lib/backend/tauri.ts b/apps/desktop/src/lib/backend/tauri.tsindex ad9499e389..55244be825 100644--- a/apps/desktop/src/lib/backend/tauri.ts+++ b/apps/desktop/src/lib/backend/tauri.ts@@ -1898,20 +1898,42 @@ export async function sortTablesByFkDependency(options: SortTablesByFkOptions):// --- Table File Import ---export type TableImportMode = "append" | "truncate";export type TableImportStatus = "running" | "done" | "error" | "cancelled";+export type TableImportSourceFormat = "csv" | "tsv" | "delimited" | "json" | "excel";+export type TableImportJsonShape = "auto" | "objects" | "arrays";export interface TableImportColumnMapping {sourceColumn: string;targetColumn: string;}+export interface TableImportParseOptions {+ delimiter?: string | null;+ hasHeader?: boolean | null;+ trimValues?: boolean | null;+ emptyStringAsNull?: boolean | null;+ sheetName?: string | null;+ sheetIndex?: number | null;+ jsonShape?: TableImportJsonShape | null;+}++export interface TableImportPreviewRequest {+ filePath: string;+ sourceRef?: string | null;+ sourceFormat?: TableImportSourceFormat | null;+ parseOptions?: TableImportParseOptions | null;+ previewLimit?: number | null;+}+export interface TableImportPreview {fileName: string;filePath: string;+ sourceRef?: string | null;fileType: string;sizeBytes: number;columns: string[];rows: unknown[][];totalRows: number;+ sheets?: string[];}export interface TableImportRequest {@@ -1921,6 +1943,9 @@ export interface TableImportRequest {schema: string;table: string;filePath: string;+ sourceRef?: string | null;+ sourceFormat?: TableImportSourceFormat | null;+ parseOptions?: TableImportParseOptions | null;mappings: TableImportColumnMapping[];mode: TableImportMode;batchSize: number;@@ -1940,8 +1965,12 @@ export interface TableImportProgress {error?: string | null;}-export async function previewTableImportFile(filePath: string): Promise<TableImportPreview> {- return invoke("preview_table_import_file", { filePath });+export async function previewTableImportFile(filePathOrRequest: string | File | TableImportPreviewRequest, options: Partial<TableImportPreviewRequest> = {}): Promise<TableImportPreview> {+ if (typeof filePathOrRequest !== "string" && !("filePath" in filePathOrRequest)) {+ throw new Error("previewTableImportFile in desktop mode requires a file path, not a File object");+ }+ const request: TableImportPreviewRequest = typeof filePathOrRequest === "string" ? { ...options, filePath: filePathOrRequest } : filePathOrRequest;+ return invoke("preview_table_import_file", { request });}export async function importTableFile(request: TableImportRequest, onProgress: (progress: TableImportProgress) => void): Promise<TableImportSummary> {diff --git a/apps/desktop/src/lib/table/tableImport.ts b/apps/desktop/src/lib/table/tableImport.tsindex 0edf0dba65..5b93d33176 100644--- a/apps/desktop/src/lib/table/tableImport.ts+++ b/apps/desktop/src/lib/table/tableImport.ts@@ -1,5 +1,28 @@export const IMPORT_SKIP_TARGET = "";+export interface ImportColumnMappingLike {+ sourceColumn: string;+ targetColumn: string;+}++export interface ImportMappingValidationResult {+ valid: boolean;+ errors: string[];+ duplicateTargets: string[];+}++export interface ImportTargetColumnLike {+ name: string;+ is_nullable?: boolean;+ column_default?: string | null;+ extra?: string | null;+ is_primary_key?: boolean;+}++export type TableImportWizardStep = "source" | "options" | "mapping" | "review" | "execution";++export const TABLE_IMPORT_WIZARD_STEPS: TableImportWizardStep[] = ["source", "options", "mapping", "review", "execution"];+export function normalizeImportColumnName(name: string): string {return name.trim().toLowerCase().replace(/[_-]+/g, " ").replace(/\s+/g, " ");}@@ -10,3 +33,51 @@ export function autoMapImportColumns(sourceColumns: string[], targetColumns: strreturn Object.fromEntries(sourceColumns.map((source) => [source, exactTargets.get(source) ?? normalizedTargets.get(normalizeImportColumnName(source)) ?? IMPORT_SKIP_TARGET]));}++export function validateImportMappings(mappings: ImportColumnMappingLike[]): ImportMappingValidationResult {+ const activeMappings = mappings.filter((mapping) => mapping.targetColumn.trim());+ const errors: string[] = [];+ const duplicateTargets: string[] = [];+ if (activeMappings.length === 0) {+ errors.push("No columns mapped for import");+ }++ const seen = new Set<string>();+ for (const mapping of activeMappings) {+ const key = mapping.targetColumn.trim().toLowerCase();+ if (seen.has(key) && !duplicateTargets.includes(mapping.targetColumn)) {+ duplicateTargets.push(mapping.targetColumn);+ }+ seen.add(key);+ }+ if (duplicateTargets.length) {+ errors.push(`Target column mapped more than once: ${duplicateTargets.join(", ")}`);+ }++ return { valid: errors.length === 0, errors, duplicateTargets };+}++export function requiredImportTargetColumns(columns: ImportTargetColumnLike[], mappedTargetColumns: string[]): string[] {+ const mapped = new Set(mappedTargetColumns.map((column) => column.toLowerCase()));+ return columns+ .filter((column) => !mapped.has(column.name.toLowerCase()))+ .filter(+ (column) =>+ column.is_nullable === false &&+ !column.column_default &&+ !String(column.extra || "")+ .toLowerCase()+ .includes("auto"),+ )+ .map((column) => column.name);+}++export function nextTableImportWizardStep(step: TableImportWizardStep): TableImportWizardStep {+ const index = TABLE_IMPORT_WIZARD_STEPS.indexOf(step);+ return TABLE_IMPORT_WIZARD_STEPS[Math.min(TABLE_IMPORT_WIZARD_STEPS.length - 1, Math.max(0, index) + 1)];+}++export function previousTableImportWizardStep(step: TableImportWizardStep): TableImportWizardStep {+ const index = TABLE_IMPORT_WIZARD_STEPS.indexOf(step);+ return TABLE_IMPORT_WIZARD_STEPS[Math.max(0, index - 1)];+}diff --git a/crates/dbx-core/src/table_import.rs b/crates/dbx-core/src/table_import.rsindex c5beac2867..680a6a6f17 100644--- a/crates/dbx-core/src/table_import.rs+++ b/crates/dbx-core/src/table_import.rs@@ -1,4 +1,5 @@use std::collections::HashSet;+use std::fs::File;use std::path::Path;use calamine::{open_workbook_auto, Data, Reader};@@ -10,6 +11,7 @@ use crate::transfer::{execute_on_pool, generate_insert_typed, get_columns_for_trpub const DEFAULT_PREVIEW_LIMIT: usize = 50;pub const DEFAULT_BATCH_SIZE: usize = 500;+pub const MAX_NON_STREAMING_IMPORT_BYTES: u64 = 100 * 1024 * 1024;#[derive(Debug, Clone)]pub struct ParsedImportFile {@@ -38,6 +40,80 @@ pub enum TableImportMode {Truncate,}+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+pub enum TableImportSourceFormat {+ Csv,+ Tsv,+ Delimited,+ Json,+ Excel,+}++impl TableImportSourceFormat {+ pub fn label(self) -> &'static str {+ match self {+ TableImportSourceFormat::Csv => "csv",+ TableImportSourceFormat::Tsv => "tsv",+ TableImportSourceFormat::Delimited => "txt",+ TableImportSourceFormat::Json => "json",+ TableImportSourceFormat::Excel => "excel",+ }+ }++ pub fn is_delimited(self) -> bool {+ matches!(self, TableImportSourceFormat::Csv | TableImportSourceFormat::Tsv | TableImportSourceFormat::Delimited)+ }+}++#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+pub enum TableImportJsonShape {+ Auto,+ Objects,+ Arrays,+}++#[derive(Debug, Clone, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct TableImportParseOptions {+ pub delimiter: Option<String>,+ pub has_header: Option<bool>,+ pub trim_values: Option<bool>,+ pub empty_string_as_null: Option<bool>,+ pub sheet_name: Option<String>,+ pub sheet_index: Option<usize>,+ pub json_shape: Option<TableImportJsonShape>,+}++impl Default for TableImportParseOptions {+ fn default() -> Self {+ Self {+ delimiter: None,+ has_header: None,+ trim_values: Some(false),+ empty_string_as_null: Some(true),+ sheet_name: None,+ sheet_index: None,+ json_shape: Some(TableImportJsonShape::Auto),+ }+ }+}++#[derive(Debug, Clone, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct TableImportPreviewRequest {+ pub file_path: String,+ #[serde(default)]+ pub source_ref: Option<String>,+ #[serde(default)]+ pub source_format: Option<TableImportSourceFormat>,+ #[serde(default)]+ pub parse_options: TableImportParseOptions,+ #[serde(default)]+ pub preview_limit: Option<usize>,+}+#[derive(Debug, Clone, Serialize, Deserialize)]#[serde(rename_all = "camelCase")]pub struct TableImportRequest {@@ -47,6 +123,12 @@ pub struct TableImportRequest {pub schema: String,pub table: String,pub file_path: String,+ #[serde(default)]+ pub source_ref: Option<String>,+ #[serde(default)]+ pub source_format: Option<TableImportSourceFormat>,+ #[serde(default)]+ pub parse_options: TableImportParseOptions,pub mappings: Vec<TableImportColumnMapping>,pub mode: TableImportMode,pub batch_size: usize,@@ -57,11 +139,15 @@ pub struct TableImportRequest {pub struct TableImportPreview {pub file_name: String,pub file_path: String,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub source_ref: Option<String>,pub file_type: String,pub size_bytes: u64,pub columns: Vec<String>,pub rows: Vec<Vec<serde_json::Value>>,pub total_rows: usize,+ #[serde(skip_serializing_if = "Vec::is_empty")]+ pub sheets: Vec<String>,}#[derive(Debug, Clone, Serialize)]@@ -95,6 +181,7 @@ pub enum TableImportStatus {pub enum ImportFileKind {Csv,Tsv,+ Txt,Json,Xlsx,}@@ -104,6 +191,7 @@ impl ImportFileKind {match self {ImportFileKind::Csv => "csv",ImportFileKind::Tsv => "tsv",+ ImportFileKind::Txt => "txt",ImportFileKind::Json => "json",ImportFileKind::Xlsx => "xlsx",}@@ -116,6 +204,8 @@ pub fn import_file_kind(path: &str) -> Result<ImportFileKind, String> {Ok(ImportFileKind::Csv)} else if lower.ends_with(".tsv") {Ok(ImportFileKind::Tsv)+ } else if lower.ends_with(".txt") {+ Ok(ImportFileKind::Txt)} else if lower.ends_with(".json") {Ok(ImportFileKind::Json)} else if lower.ends_with(".xlsx") || lower.ends_with(".xlsm") || lower.ends_with(".xls") {@@ -125,6 +215,25 @@ pub fn import_file_kind(path: &str) -> Result<ImportFileKind, String> {}}+pub fn source_format_for_path(path: &str) -> Result<TableImportSourceFormat, String> {+ Ok(match import_file_kind(path)? {+ ImportFileKind::Csv => TableImportSourceFormat::Csv,+ ImportFileKind::Tsv => TableImportSourceFormat::Tsv,+ ImportFileKind::Txt => TableImportSourceFormat::Delimited,+ ImportFileKind::Json => TableImportSourceFormat::Json,+ ImportFileKind::Xlsx => TableImportSourceFormat::Excel,+ })+}++pub fn effective_source_format(+ path: &str,+ source_format: Option<TableImportSourceFormat>,+) -> Result<TableImportSourceFormat, String> {+ source_format+ .or_else(|| source_format_for_path(path).ok())+ .ok_or_else(|| "Unsupported import file type".to_string())+}+pub fn normalize_header(value: &str, index: usize) -> String {let trimmed = value.trim();if trimmed.is_empty() {@@ -134,29 +243,127 @@ pub fn normalize_header(value: &str, index: usize) -> String {}}-pub fn csv_value(value: &str) -> serde_json::Value {- if value.is_empty() {+#[derive(Debug, Clone, Copy)]+pub struct DelimitedParseConfig {+ pub delimiter: u8,+ pub has_header: bool,+ pub trim_values: bool,+ pub empty_string_as_null: bool,+}++pub fn effective_delimited_config(+ source_format: TableImportSourceFormat,+ options: &TableImportParseOptions,+) -> Result<DelimitedParseConfig, String> {+ let default_delimiter = match source_format {+ TableImportSourceFormat::Tsv => b'\t',+ _ => b',',+ };+ let delimiter = match options.delimiter.as_deref() {+ None | Some("") => default_delimiter,+ Some("\\t") | Some("tab") | Some("TAB") => b'\t',+ Some(value) => {+ let bytes = value.as_bytes();+ if bytes.len() != 1 {+ return Err("Delimiter must be a single-byte character".to_string());+ }+ bytes[0]+ }+ };++ Ok(DelimitedParseConfig {+ delimiter,+ has_header: options.has_header.unwrap_or(true),+ trim_values: options.trim_values.unwrap_or(false),+ empty_string_as_null: options.empty_string_as_null.unwrap_or(true),+ })+}++pub fn csv_value_with_config(value: &str, config: DelimitedParseConfig) -> serde_json::Value {+ let value = if config.trim_values { value.trim() } else { value };+ if config.empty_string_as_null && value.is_empty() {serde_json::Value::Null} else {serde_json::Value::String(value.to_string())}}-pub fn parse_delimited_bytes(bytes: &[u8], delimiter: u8, preview_limit: usize) -> Result<ParsedImportFile, String> {- let mut reader = csv::ReaderBuilder::new().delimiter(delimiter).flexible(true).from_reader(bytes);- let columns = reader- .headers()- .map_err(|e| e.to_string())?- .iter()- .enumerate()- .map(|(index, header)| normalize_header(header, index))- .collect::<Vec<_>>();- if columns.is_empty() {- return Err("Import file has no columns".to_string());- }+pub fn csv_value(value: &str) -> serde_json::Value {+ csv_value_with_config(+ value,+ DelimitedParseConfig { delimiter: b',', has_header: true, trim_values: false, empty_string_as_null: true },+ )+}++pub fn parse_delimited_reader<R: std::io::Read>(+ reader: R,+ config: DelimitedParseConfig,+ preview_limit: usize,+) -> Result<ParsedImportFile, String> {+ let mut reader = csv::ReaderBuilder::new()+ .delimiter(config.delimiter)+ .has_headers(config.has_header)+ .flexible(true)+ .from_reader(reader);let mut rows = Vec::new();let mut total_rows = 0;+ let columns = if config.has_header {+ reader+ .headers()+ .map_err(|e| e.to_string())?+ .iter()+ .enumerate()+ .map(|(index, header)| normalize_header(header.trim_start_matches('\u{feff}'), index))+ .collect::<Vec<_>>()+ } else {+ Vec::new()+ };++ let mut columns = columns;+ if columns.is_empty() {+ let mut records = reader.records();+ let first_record = match records.next() {+ Some(record) => record.map_err(|e| e.to_string())?,+ None => return Err("Import file has no rows".to_string()),+ };+ columns = (0..first_record.len()).map(|index| format!("column_{}", index + 1)).collect();+ if columns.is_empty() {+ return Err("Import file has no columns".to_string());+ }+ total_rows += 1;+ if preview_limit > 0 {+ rows.push(+ (0..columns.len())+ .map(|index| {+ first_record+ .get(index)+ .map(|value| csv_value_with_config(value, config))+ .unwrap_or(serde_json::Value::Null)+ })+ .collect(),+ );+ }+ for record in records {+ let record = record.map_err(|e| e.to_string())?;+ total_rows += 1;+ if rows.len() >= preview_limit {+ continue;+ }+ let mut row = Vec::with_capacity(columns.len());+ for index in 0..columns.len() {+ row.push(+ record+ .get(index)+ .map(|value| csv_value_with_config(value, config))+ .unwrap_or(serde_json::Value::Null),+ );+ }+ rows.push(row);+ }+ return Ok(ParsedImportFile { columns, rows, total_rows });+ }+for record in reader.records() {let record = record.map_err(|e| e.to_string())?;total_rows += 1;@@ -165,7 +372,9 @@ pub fn parse_delimited_bytes(bytes: &[u8], delimiter: u8, preview_limit: usize)}let mut row = Vec::with_capacity(columns.len());for index in 0..columns.len() {- row.push(record.get(index).map(csv_value).unwrap_or(serde_json::Value::Null));+ row.push(+ record.get(index).map(|value| csv_value_with_config(value, config)).unwrap_or(serde_json::Value::Null),+ );}rows.push(row);}@@ -173,11 +382,51 @@ pub fn parse_delimited_bytes(bytes: &[u8], delimiter: u8, preview_limit: usize)Ok(ParsedImportFile { columns, rows, total_rows })}+pub fn parse_delimited_bytes_with_options(+ bytes: &[u8],+ source_format: TableImportSourceFormat,+ options: &TableImportParseOptions,+ preview_limit: usize,+) -> Result<ParsedImportFile, String> {+ parse_delimited_reader(+ bytes.strip_prefix(b"\xEF\xBB\xBF").unwrap_or(bytes),+ effective_delimited_config(source_format, options)?,+ preview_limit,+ )+}++pub fn parse_delimited_file_with_options(+ path: &str,+ source_format: TableImportSourceFormat,+ options: &TableImportParseOptions,+ preview_limit: usize,+) -> Result<ParsedImportFile, String> {+ let file = File::open(path).map_err(|e| e.to_string())?;+ parse_delimited_reader(file, effective_delimited_config(source_format, options)?, preview_limit)+}+pub fn parse_csv_bytes(bytes: &[u8], preview_limit: usize) -> Result<ParsedImportFile, String> {- parse_delimited_bytes(bytes, b',', preview_limit)+ parse_delimited_bytes_with_options(+ bytes,+ TableImportSourceFormat::Csv,+ &TableImportParseOptions::default(),+ preview_limit,+ )}-pub fn parse_json_bytes(bytes: &[u8], preview_limit: usize) -> Result<ParsedImportFile, String> {+pub fn parse_delimited_bytes(bytes: &[u8], delimiter: u8, preview_limit: usize) -> Result<ParsedImportFile, String> {+ let options = TableImportParseOptions {+ delimiter: Some(if delimiter == b'\t' { "\\t".to_string() } else { (delimiter as char).to_string() }),+ ..TableImportParseOptions::default()+ };+ parse_delimited_bytes_with_options(bytes, TableImportSourceFormat::Delimited, &options, preview_limit)+}++pub fn parse_json_bytes_with_options(+ bytes: &[u8],+ options: &TableImportParseOptions,+ preview_limit: usize,+) -> Result<ParsedImportFile, String> {let bytes = bytes.strip_prefix(b"\xEF\xBB\xBF").unwrap_or(bytes);let value: serde_json::Value = serde_json::from_slice(bytes).map_err(|e| e.to_string())?;let items = match value {@@ -189,7 +438,18 @@ pub fn parse_json_bytes(bytes: &[u8], preview_limit: usize) -> Result<ParsedImporeturn Err("Import file has no rows".to_string());}- if items.iter().all(|item| item.is_object()) {+ let shape = options.json_shape.unwrap_or(TableImportJsonShape::Auto);+ let all_objects = items.iter().all(|item| item.is_object());+ let all_arrays = items.iter().all(|item| item.is_array());++ if shape == TableImportJsonShape::Objects && !all_objects {+ return Err("JSON import is configured for object rows, but at least one row is not an object".to_string());+ }+ if shape == TableImportJsonShape::Arrays && !all_arrays {+ return Err("JSON import is configured for array rows, but at least one row is not an array".to_string());+ }++ if all_objects {let mut columns = Vec::new();for item in &items {if let Some(obj) = item.as_object() {@@ -217,7 +477,7 @@ pub fn parse_json_bytes(bytes: &[u8], preview_limit: usize) -> Result<ParsedImporeturn Ok(ParsedImportFile { columns, rows, total_rows: items.len() });}- if items.iter().all(|item| item.is_array()) {+ if all_arrays {let max_cols = items.iter().filter_map(|item| item.as_array().map(|row| row.len())).max().unwrap_or(0);if max_cols == 0 {return Err("Import file has no columns".to_string());@@ -236,7 +496,11 @@ pub fn parse_json_bytes(bytes: &[u8], preview_limit: usize) -> Result<ParsedImporeturn Ok(ParsedImportFile { columns, rows, total_rows: items.len() });}- Err("JSON rows must all be objects or all be arrays".to_string())+ Err("JSON rows must all be objects or all be arrays; mixed row shapes are not supported".to_string())+}++pub fn parse_json_bytes(bytes: &[u8], preview_limit: usize) -> Result<ParsedImportFile, String> {+ parse_json_bytes_with_options(bytes, &TableImportParseOptions::default(), preview_limit)}pub fn xlsx_cell_value(cell: &Data) -> serde_json::Value {@@ -269,17 +533,63 @@ pub fn xlsx_cell_label(cell: &Data) -> String {}}-pub fn parse_xlsx_file(path: &str, preview_limit: usize) -> Result<ParsedImportFile, String> {+pub fn xlsx_sheet_names(path: &str) -> Result<Vec<String>, String> {+ let workbook = open_workbook_auto(path).map_err(|e| e.to_string())?;+ Ok(workbook.sheet_names().to_vec())+}++pub fn parse_xlsx_file_with_options(+ path: &str,+ options: &TableImportParseOptions,+ preview_limit: usize,+) -> Result<ParsedImportFile, String> {let mut workbook = open_workbook_auto(path).map_err(|e| e.to_string())?;- let sheet_name = workbook.sheet_names().first().cloned().ok_or_else(|| "Workbook has no sheets".to_string())?;+ let sheet_names = workbook.sheet_names().to_vec();+ let sheet_name = if let Some(name) = options.sheet_name.as_ref().filter(|name| !name.trim().is_empty()) {+ if !sheet_names.iter().any(|sheet| sheet == name) {+ return Err(format!("Workbook sheet not found: {name}"));+ }+ name.clone()+ } else if let Some(index) = options.sheet_index {+ sheet_names.get(index).cloned().ok_or_else(|| format!("Workbook sheet index out of range: {index}"))?+ } else {+ sheet_names.first().cloned().ok_or_else(|| "Workbook has no sheets".to_string())?+ };let range = workbook.worksheet_range(&sheet_name).map_err(|e| e.to_string())?;let mut rows_iter = range.rows();- let header = rows_iter.next().ok_or_else(|| "Import file has no rows".to_string())?;- let columns = header- .iter()- .enumerate()- .map(|(index, cell)| normalize_header(&xlsx_cell_label(cell), index))- .collect::<Vec<_>>();+ let has_header = options.has_header.unwrap_or(true);+ let columns = if has_header {+ let header = rows_iter.next().ok_or_else(|| "Import file has no rows".to_string())?;+ header+ .iter()+ .enumerate()+ .map(|(index, cell)| normalize_header(&xlsx_cell_label(cell), index))+ .collect::<Vec<_>>()+ } else {+ let first_row = rows_iter.next().ok_or_else(|| "Import file has no rows".to_string())?;+ let columns = (0..first_row.len()).map(|index| format!("column_{}", index + 1)).collect::<Vec<_>>();+ let mut rows = Vec::new();+ if preview_limit > 0 {+ rows.push(+ (0..columns.len())+ .map(|index| first_row.get(index).map(xlsx_cell_value).unwrap_or(serde_json::Value::Null))+ .collect::<Vec<_>>(),+ );+ }+ let mut total_rows = 1;+ for source_row in rows_iter {+ total_rows += 1;+ if rows.len() >= preview_limit {+ continue;+ }+ let mut row = Vec::with_capacity(columns.len());+ for index in 0..columns.len() {+ row.push(source_row.get(index).map(xlsx_cell_value).unwrap_or(serde_json::Value::Null));+ }+ rows.push(row);+ }+ return Ok(ParsedImportFile { columns, rows, total_rows });+ };if columns.is_empty() {return Err("Import file has no columns".to_string());}@@ -301,32 +611,72 @@ pub fn parse_xlsx_file(path: &str, preview_limit: usize) -> Result<ParsedImportFOk(ParsedImportFile { columns, rows, total_rows })}-pub async fn parse_import_file(path: &str, preview_limit: usize) -> Result<ParsedImportFile, String> {- match import_file_kind(path)? {- ImportFileKind::Csv => {- let bytes = tokio::fs::read(path).await.map_err(|e| e.to_string())?;- parse_csv_bytes(&bytes, preview_limit)- }- ImportFileKind::Tsv => {- let bytes = tokio::fs::read(path).await.map_err(|e| e.to_string())?;- parse_delimited_bytes(&bytes, b'\t', preview_limit)+pub fn parse_xlsx_file(path: &str, preview_limit: usize) -> Result<ParsedImportFile, String> {+ parse_xlsx_file_with_options(path, &TableImportParseOptions::default(), preview_limit)+}++fn ensure_non_streaming_file_size(path: &str, format: TableImportSourceFormat) -> Result<(), String> {+ if format.is_delimited() {+ return Ok(());+ }+ let metadata = std::fs::metadata(path).map_err(|e| e.to_string())?;+ if metadata.len() > MAX_NON_STREAMING_IMPORT_BYTES {+ return Err(format!(+ "File too large for {} import: {} bytes (max {} bytes)",+ format.label(),+ metadata.len(),+ MAX_NON_STREAMING_IMPORT_BYTES+ ));+ }+ Ok(())+}++pub async fn parse_import_file_with_options(+ path: &str,+ source_format: Option<TableImportSourceFormat>,+ options: &TableImportParseOptions,+ preview_limit: usize,+) -> Result<ParsedImportFile, String> {+ let format = effective_source_format(path, source_format)?;+ ensure_non_streaming_file_size(path, format)?;+ match format {+ TableImportSourceFormat::Csv | TableImportSourceFormat::Tsv | TableImportSourceFormat::Delimited => {+ let path = path.to_string();+ let options = options.clone();+ tokio::task::spawn_blocking(move || {+ parse_delimited_file_with_options(&path, format, &options, preview_limit)+ })+ .await+ .map_err(|e| e.to_string())?}- ImportFileKind::Json => {+ TableImportSourceFormat::Json => {let bytes = tokio::fs::read(path).await.map_err(|e| e.to_string())?;- parse_json_bytes(&bytes, preview_limit)+ parse_json_bytes_with_options(&bytes, options, preview_limit)}- ImportFileKind::Xlsx => {+ TableImportSourceFormat::Excel => {let path = path.to_string();- tokio::task::spawn_blocking(move || parse_xlsx_file(&path, preview_limit))+ let options = options.clone();+ tokio::task::spawn_blocking(move || parse_xlsx_file_with_options(&path, &options, preview_limit)).await.map_err(|e| e.to_string())?}}}+pub async fn parse_import_file(path: &str, preview_limit: usize) -> Result<ParsedImportFile, String> {+ parse_import_file_with_options(path, None, &TableImportParseOptions::default(), preview_limit).await+}+pub fn mapping_indexes(data: &ParsedImportFile,mappings: &[TableImportColumnMapping],+) -> Result<Vec<(usize, String)>, String> {+ mapping_indexes_for_columns(&data.columns, mappings)+}++pub fn mapping_indexes_for_columns(+ columns: &[String],+ mappings: &[TableImportColumnMapping],) -> Result<Vec<(usize, String)>, String> {if mappings.is_empty() {return Err("No columns mapped for import".to_string());@@ -334,8 +684,7 @@ pub fn mapping_indexes(let mut mapped = Vec::new();let mut target_seen = HashSet::new();for mapping in mappings {- let source_index = data- .columns+ let source_index = columns.iter().position(|column| column == &mapping.source_column).ok_or_else(|| format!("Source column not found: {}", mapping.source_column))?;@@ -350,6 +699,42 @@ pub fn mapping_indexes(Ok(mapped)}+pub fn build_import_insert_batch_from_rows(+ rows: &[Vec<serde_json::Value>],+ columns: &[String],+ mappings: &[TableImportColumnMapping],+ target_column_types: &[(String, String)],+ table: &str,+ schema: &str,+ db_type: &DatabaseType,+) -> Result<Option<ImportSqlBatch>, String> {+ if rows.is_empty() {+ return Ok(None);+ }+ let mapped = mapping_indexes_for_columns(columns, mappings)?;+ let target_columns = mapped.iter().map(|(_, target)| target.clone()).collect::<Vec<_>>();+ let column_types = target_columns+ .iter()+ .map(|column| {+ target_column_types+ .iter()+ .find(|(name, _)| name.eq_ignore_ascii_case(column))+ .map(|(_, data_type)| data_type.clone())+ })+ .collect::<Vec<_>>();+ let mapped_rows = rows+ .iter()+ .map(|row| {+ mapped+ .iter()+ .map(|(source_index, _)| row.get(*source_index).cloned().unwrap_or(serde_json::Value::Null))+ .collect::<Vec<_>>()+ })+ .collect::<Vec<_>>();+ let sql = generate_insert_typed(&target_columns, &column_types, &mapped_rows, table, schema, db_type);+ Ok((!sql.trim().is_empty()).then_some(ImportSqlBatch { sql, row_count: rows.len() }))+}+pub fn build_import_insert_batches(data: &ParsedImportFile,mappings: &[TableImportColumnMapping],@@ -405,23 +790,122 @@ pub fn truncate_sql(table: &str, schema: &str, db_type: &DatabaseType) -> String}}-pub async fn preview_table_import_file_core(file_path: &str) -> Result<TableImportPreview, String> {- let kind = import_file_kind(file_path)?;- let parsed = parse_import_file(file_path, DEFAULT_PREVIEW_LIMIT).await?;- let metadata = tokio::fs::metadata(file_path).await.map_err(|e| e.to_string())?;- let file_name = Path::new(file_path).file_name().and_then(|name| name.to_str()).unwrap_or(file_path).to_string();+fn import_error_message(request: &TableImportRequest, rows_imported: usize, error: impl AsRef<str>) -> String {+ format!("Import into table '{}' failed after {} imported rows: {}", request.table, rows_imported, error.as_ref())+}++fn emit_import_error<F>(+ progress_callback: &mut F,+ request: &TableImportRequest,+ rows_imported: usize,+ total_rows: usize,+ error: impl AsRef<str>,+) -> String+where+ F: FnMut(TableImportProgress),+{+ let message = import_error_message(request, rows_imported, error);+ progress_callback(TableImportProgress {+ import_id: request.import_id.clone(),+ status: TableImportStatus::Error,+ rows_imported,+ total_rows,+ error: Some(message.clone()),+ });+ message+}++fn delimited_record_to_row(+ record: &csv::StringRecord,+ columns_len: usize,+ config: DelimitedParseConfig,+) -> Vec<serde_json::Value> {+ (0..columns_len)+ .map(|index| {+ record.get(index).map(|value| csv_value_with_config(value, config)).unwrap_or(serde_json::Value::Null)+ })+ .collect()+}++fn delimited_columns_and_first_record<R: std::io::Read>(+ reader: &mut csv::Reader<R>,+ config: DelimitedParseConfig,+) -> Result<(Vec<String>, Option<csv::StringRecord>), String> {+ if config.has_header {+ let columns = reader+ .headers()+ .map_err(|e| e.to_string())?+ .iter()+ .enumerate()+ .map(|(index, header)| normalize_header(header.trim_start_matches('\u{feff}'), index))+ .collect::<Vec<_>>();+ if columns.is_empty() {+ return Err("Import file has no columns".to_string());+ }+ return Ok((columns, None));+ }++ let mut records = reader.records();+ let first_record =+ records.next().transpose().map_err(|e| e.to_string())?.ok_or_else(|| "Import file has no rows".to_string())?;+ let columns = (0..first_record.len()).map(|index| format!("column_{}", index + 1)).collect::<Vec<_>>();+ if columns.is_empty() {+ return Err("Import file has no columns".to_string());+ }+ Ok((columns, Some(first_record)))+}++pub async fn preview_table_import_file_with_request(+ request: TableImportPreviewRequest,+) -> Result<TableImportPreview, String> {+ let format = effective_source_format(&request.file_path, request.source_format)?;+ let parsed = parse_import_file_with_options(+ &request.file_path,+ Some(format),+ &request.parse_options,+ request.preview_limit.unwrap_or(DEFAULT_PREVIEW_LIMIT),+ )+ .await?;+ let metadata = tokio::fs::metadata(&request.file_path).await.map_err(|e| e.to_string())?;+ let file_name = Path::new(&request.file_path)+ .file_name()+ .and_then(|name| name.to_str())+ .unwrap_or(&request.file_path)+ .to_string();+ let sheets = if matches!(format, TableImportSourceFormat::Excel) {+ let file_path = request.file_path.clone();+ tokio::task::spawn_blocking(move || xlsx_sheet_names(&file_path))+ .await+ .map_err(|e| e.to_string())?+ .unwrap_or_default()+ } else {+ Vec::new()+ };Ok(TableImportPreview {file_name,- file_path: file_path.to_string(),- file_type: kind.label().to_string(),+ file_path: request.file_path,+ source_ref: request.source_ref,+ file_type: format.label().to_string(),size_bytes: metadata.len(),columns: parsed.columns,rows: parsed.rows,total_rows: parsed.total_rows,+ sheets,})}+pub async fn preview_table_import_file_core(file_path: &str) -> Result<TableImportPreview, String> {+ preview_table_import_file_with_request(TableImportPreviewRequest {+ file_path: file_path.to_string(),+ source_ref: None,+ source_format: None,+ parse_options: TableImportParseOptions::default(),+ preview_limit: Some(DEFAULT_PREVIEW_LIMIT),+ })+ .await+}+/// Core import logic. Returns (rows_imported, total_rows)./// `progress_callback` is invoked for progress updates.pub async fn import_table_file_core<F>(@@ -436,22 +920,232 @@ whereF: FnMut(TableImportProgress),{let batch_size = if request.batch_size == 0 { DEFAULT_BATCH_SIZE } else { request.batch_size };+ let source_format = match effective_source_format(&request.file_path, request.source_format) {+ Ok(format) => format,+ Err(error) => {+ return Err(emit_import_error(&mut progress_callback, request, 0, 0, error));+ }+ };++ if let Err(error) = tokio::fs::metadata(&request.file_path).await {+ return Err(emit_import_error(+ &mut progress_callback,+ request,+ 0,+ 0,+ format!("Import source is no longer available: {error}"),+ ));+ }++ if source_format.is_delimited() {+ let parsed =+ match parse_import_file_with_options(&request.file_path, Some(source_format), &request.parse_options, 0)+ .await+ {+ Ok(parsed) => parsed,+ Err(error) => {+ return Err(emit_import_error(&mut progress_callback, request, 0, 0, error));+ }+ };+ let total_rows = parsed.total_rows;+ if let Err(error) = mapping_indexes_for_columns(&parsed.columns, &request.mappings) {+ return Err(emit_import_error(&mut progress_callback, request, 0, total_rows, error));+ }++ progress_callback(TableImportProgress {+ import_id: request.import_id.clone(),+ status: TableImportStatus::Running,+ rows_imported: 0,+ total_rows,+ error: None,+ });++ let target_column_types = get_columns_for_transfer(+ state,+ pool_key,+ &request.connection_id,+ &request.database,+ &request.schema,+ &request.table,+ )+ .await+ .unwrap_or_default()+ .into_iter()+ .map(|column| (column.name, column.data_type))+ .collect::<Vec<_>>();++ if matches!(request.mode, TableImportMode::Truncate) {+ let sql = truncate_sql(&request.table, &request.schema, db_type);+ if let Err(error) = execute_on_pool(state, pool_key, &sql).await {+ return Err(emit_import_error(&mut progress_callback, request, 0, total_rows, error));+ }+ }++ let config = match effective_delimited_config(source_format, &request.parse_options) {+ Ok(config) => config,+ Err(error) => return Err(emit_import_error(&mut progress_callback, request, 0, total_rows, error)),+ };+ let file = match File::open(&request.file_path) {+ Ok(file) => file,+ Err(error) => {+ return Err(emit_import_error(+ &mut progress_callback,+ request,+ 0,+ total_rows,+ format!("Import source is no longer available: {error}"),+ ));+ }+ };+ let mut reader = csv::ReaderBuilder::new()+ .delimiter(config.delimiter)+ .has_headers(config.has_header)+ .flexible(true)+ .from_reader(file);+ let (columns, first_record) = match delimited_columns_and_first_record(&mut reader, config) {+ Ok(result) => result,+ Err(error) => return Err(emit_import_error(&mut progress_callback, request, 0, total_rows, error)),+ };+ let effective_batch_size = match db_type {+ DatabaseType::Oracle | DatabaseType::OceanbaseOracle => 1,+ _ => batch_size.max(1),+ };+ let mut rows_imported = 0;+ let mut pending_rows: Vec<Vec<serde_json::Value>> = Vec::with_capacity(effective_batch_size);++ if let Some(record) = first_record {+ pending_rows.push(delimited_record_to_row(&record, columns.len(), config));+ }++ for record in reader.records() {+ if is_cancelled(&request.import_id).await {+ progress_callback(TableImportProgress {+ import_id: request.import_id.clone(),+ status: TableImportStatus::Cancelled,+ rows_imported,+ total_rows,+ error: None,+ });+ return Err("Import cancelled".to_string());+ }++ let record = match record {+ Ok(record) => record,+ Err(error) => {+ return Err(emit_import_error(+ &mut progress_callback,+ request,+ rows_imported,+ total_rows,+ error.to_string(),+ ))+ }+ };+ pending_rows.push(delimited_record_to_row(&record, columns.len(), config));++ if pending_rows.len() >= effective_batch_size {+ let batch = match build_import_insert_batch_from_rows(+ &pending_rows,+ &columns,+ &request.mappings,+ &target_column_types,+ &request.table,+ &request.schema,+ db_type,+ ) {+ Ok(Some(batch)) => batch,+ Ok(None) => {+ pending_rows.clear();+ continue;+ }+ Err(error) => {+ return Err(emit_import_error(+ &mut progress_callback,+ request,+ rows_imported,+ total_rows,+ error,+ ))+ }+ };+ if let Err(error) = execute_on_pool(state, pool_key, &batch.sql).await {+ return Err(emit_import_error(&mut progress_callback, request, rows_imported, total_rows, error));+ }+ rows_imported = (rows_imported + batch.row_count).min(total_rows);+ pending_rows.clear();+ progress_callback(TableImportProgress {+ import_id: request.import_id.clone(),+ status: TableImportStatus::Running,+ rows_imported,+ total_rows,+ error: None,+ });+ }+ }++ if !pending_rows.is_empty() {+ if is_cancelled(&request.import_id).await {+ progress_callback(TableImportProgress {+ import_id: request.import_id.clone(),+ status: TableImportStatus::Cancelled,+ rows_imported,+ total_rows,+ error: None,+ });+ return Err("Import cancelled".to_string());+ }+ let batch = match build_import_insert_batch_from_rows(+ &pending_rows,+ &columns,+ &request.mappings,+ &target_column_types,+ &request.table,+ &request.schema,+ db_type,+ ) {+ Ok(Some(batch)) => batch,+ Ok(None) => ImportSqlBatch { sql: String::new(), row_count: 0 },+ Err(error) => {+ return Err(emit_import_error(&mut progress_callback, request, rows_imported, total_rows, error))+ }+ };+ if !batch.sql.is_empty() {+ if let Err(error) = execute_on_pool(state, pool_key, &batch.sql).await {+ return Err(emit_import_error(&mut progress_callback, request, rows_imported, total_rows, error));+ }+ rows_imported = (rows_imported + batch.row_count).min(total_rows);+ }+ }++ progress_callback(TableImportProgress {+ import_id: request.import_id.clone(),+ status: TableImportStatus::Done,+ rows_imported,+ total_rows,+ error: None,+ });- let parsed = match parse_import_file(&request.file_path, usize::MAX).await {+ return Ok(TableImportSummary { import_id: request.import_id.clone(), rows_imported, total_rows });+ }++ let parsed = match parse_import_file_with_options(+ &request.file_path,+ Some(source_format),+ &request.parse_options,+ usize::MAX,+ )+ .await+ {Ok(parsed) => parsed,Err(error) => {- progress_callback(TableImportProgress {- import_id: request.import_id.clone(),- status: TableImportStatus::Error,- rows_imported: 0,- total_rows: 0,- error: Some(error.clone()),- });- return Err(error);+ return Err(emit_import_error(&mut progress_callback, request, 0, 0, error));}};let total_rows = parsed.total_rows;+ if let Err(error) = mapping_indexes(&parsed, &request.mappings) {+ return Err(emit_import_error(&mut progress_callback, request, 0, total_rows, error));+ }progress_callback(TableImportProgress {import_id: request.import_id.clone(),status: TableImportStatus::Running,@@ -485,28 +1179,14 @@ where) {Ok(batches) => batches,Err(error) => {- progress_callback(TableImportProgress {- import_id: request.import_id.clone(),- status: TableImportStatus::Error,- rows_imported: 0,- total_rows,- error: Some(error.clone()),- });- return Err(error);+ return Err(emit_import_error(&mut progress_callback, request, 0, total_rows, error));}};if matches!(request.mode, TableImportMode::Truncate) {let sql = truncate_sql(&request.table, &request.schema, db_type);if let Err(error) = execute_on_pool(state, pool_key, &sql).await {- progress_callback(TableImportProgress {- import_id: request.import_id.clone(),- status: TableImportStatus::Error,- rows_imported: 0,- total_rows,- error: Some(error.clone()),- });- return Err(error);+ return Err(emit_import_error(&mut progress_callback, request, 0, total_rows, error));}}@@ -524,14 +1204,7 @@ where}if let Err(error) = execute_on_pool(state, pool_key, &batch.sql).await {- progress_callback(TableImportProgress {- import_id: request.import_id.clone(),- status: TableImportStatus::Error,- rows_imported,- total_rows,- error: Some(error.clone()),- });- return Err(error);+ return Err(emit_import_error(&mut progress_callback, request, rows_imported, total_rows, error));}rows_imported = (rows_imported + batch.row_count).min(total_rows);progress_callback(TableImportProgress {@@ -558,6 +1231,7 @@ wheremod tests {use super::*;use crate::models::connection::DatabaseType;+ use crate::xlsx_export::{build_xlsx_workbook_multi, XlsxWorksheetData};#[test]fn parses_csv_headers_and_preview_rows() {@@ -595,6 +1269,29 @@ mod tests {);}+ #[test]+ fn parses_delimited_text_without_header_and_trims_values() {+ let options = TableImportParseOptions {+ delimiter: Some("|".to_string()),+ has_header: Some(false),+ trim_values: Some(true),+ empty_string_as_null: Some(true),+ ..TableImportParseOptions::default()+ };+ let parsed = parse_delimited_bytes_with_options(+ b" 1 | Ada \n 2 | \n",+ TableImportSourceFormat::Delimited,+ &options,+ 10,+ )+ .unwrap();++ assert_eq!(parsed.columns, vec!["column_1", "column_2"]);+ assert_eq!(parsed.total_rows, 2);+ assert_eq!(parsed.rows[0], vec![serde_json::json!("1"), serde_json::json!("Ada")]);+ assert_eq!(parsed.rows[1], vec![serde_json::json!("2"), serde_json::Value::Null]);+ }+#[test]fn parses_json_array_objects_with_union_columns() {let parsed = parse_json_bytes(br#"[{"id":1,"name":"Ada"},{"id":2,"active":true}]"#, 10).unwrap();@@ -614,6 +1311,45 @@ mod tests {assert_eq!(parsed.rows[0], vec![serde_json::json!(1), serde_json::json!("Ada")]);}+ #[test]+ fn json_shape_option_rejects_wrong_row_shape() {+ let options = TableImportParseOptions {+ json_shape: Some(TableImportJsonShape::Objects),+ ..TableImportParseOptions::default()+ };+ let error = parse_json_bytes_with_options(br#"[["id","name"],[1,"Ada"]]"#, &options, 10).unwrap_err();++ assert!(error.contains("configured for object rows"));+ }++ #[test]+ fn parses_selected_excel_sheet() {+ let path = std::env::temp_dir().join(format!("dbx-table-import-{}.xlsx", uuid::Uuid::new_v4()));+ let workbook = build_xlsx_workbook_multi(&[+ XlsxWorksheetData {+ sheet_name: Some("First".to_string()),+ columns: vec!["id".to_string()],+ rows: vec![vec![serde_json::json!(1)]],+ },+ XlsxWorksheetData {+ sheet_name: Some("Second".to_string()),+ columns: vec!["name".to_string()],+ rows: vec![vec![serde_json::json!("Ada")]],+ },+ ])+ .unwrap();+ std::fs::write(&path, workbook).unwrap();++ let options =+ TableImportParseOptions { sheet_name: Some("Second".to_string()), ..TableImportParseOptions::default() };+ let parsed = parse_xlsx_file_with_options(&path.to_string_lossy(), &options, 10).unwrap();++ assert_eq!(xlsx_sheet_names(&path.to_string_lossy()).unwrap(), vec!["First", "Second"]);+ assert_eq!(parsed.columns, vec!["name"]);+ assert_eq!(parsed.rows, vec![vec![serde_json::json!("Ada")]]);+ let _ = std::fs::remove_file(path);+ }+#[test]fn builds_import_insert_batches_from_mapped_columns() {let mappings = vec![@@ -645,6 +1381,60 @@ mod tests {]);}+ #[test]+ fn duplicate_mapping_is_rejected_before_sql_generation() {+ let columns = vec!["id".to_string(), "name".to_string()];+ let mappings = vec![+ TableImportColumnMapping { source_column: "id".to_string(), target_column: "target".to_string() },+ TableImportColumnMapping { source_column: "name".to_string(), target_column: "target".to_string() },+ ];++ let error = mapping_indexes_for_columns(&columns, &mappings).unwrap_err();++ assert!(error.contains("mapped more than once"));+ }++ #[test]+ fn builds_single_streaming_import_batch_from_rows() {+ let columns = vec!["id".to_string(), "name".to_string()];+ let mappings = vec![+ TableImportColumnMapping { source_column: "id".to_string(), target_column: "id".to_string() },+ TableImportColumnMapping { source_column: "name".to_string(), target_column: "name".to_string() },+ ];+ let rows = vec![vec![serde_json::json!(1), serde_json::json!("Ada")]];++ let batch = build_import_insert_batch_from_rows(+ &rows,+ &columns,+ &mappings,+ &[],+ "users",+ "public",+ &DatabaseType::Postgres,+ )+ .unwrap()+ .unwrap();++ assert_eq!(batch.sql, "INSERT INTO \"public\".\"users\" (\"id\", \"name\") VALUES\n(1, 'Ada')");+ assert_eq!(batch.row_count, 1);+ }++ #[tokio::test]+ async fn preview_missing_source_fails_before_parsing() {+ let path = std::env::temp_dir().join(format!("dbx-missing-import-{}.csv", uuid::Uuid::new_v4()));+ let error = preview_table_import_file_with_request(TableImportPreviewRequest {+ file_path: path.to_string_lossy().to_string(),+ source_ref: Some("missing".to_string()),+ source_format: Some(TableImportSourceFormat::Csv),+ parse_options: TableImportParseOptions::default(),+ preview_limit: Some(10),+ })+ .await+ .unwrap_err();++ assert!(error.contains("No such file") || error.contains("os error"));+ }+#[test]fn oracle_import_insert_batches_use_single_row_statements() {let mappings = vec![diff --git a/crates/dbx-web/src/routes/table_import.rs b/crates/dbx-web/src/routes/table_import.rsindex ddb95180c3..d7f9d1ae1c 100644--- a/crates/dbx-web/src/routes/table_import.rs+++ b/crates/dbx-web/src/routes/table_import.rs@@ -1,9 +1,14 @@+use std::path::{Path as StdPath, PathBuf};use std::sync::Arc;+use std::time::{Duration, SystemTime};+use axum::body::Bytes;use axum::extract::{Multipart, Path, State};use axum::response::sse::{Event, Sse};use axum::Json;-use dbx_core::table_import::{self, TableImportRequest};+use dbx_core::table_import::{+ self, TableImportParseOptions, TableImportPreviewRequest, TableImportRequest, TableImportSourceFormat,+};use dbx_core::transfer;use futures::stream::Stream;use serde::Deserialize;@@ -27,23 +32,59 @@ pub async fn preview_import(State(state): State<Arc<WebState>>,mut multipart: Multipart,) -> Result<Json<serde_json::Value>, AppError> {- let tmp_dir = state.data_dir.join("tmp");+ let tmp_dir = import_upload_dir(&state.data_dir);std::fs::create_dir_all(&tmp_dir).map_err(|e| AppError(e.to_string()))?;+ cleanup_expired_import_uploads(&tmp_dir, Duration::from_secs(24 * 60 * 60));- if let Some(field) = multipart.next_field().await.map_err(|e| AppError(e.to_string()))? {- let file_name = field.file_name().unwrap_or("upload.csv").to_string();- let data = field.bytes().await.map_err(|e| AppError(e.to_string()))?;+ let mut uploaded_file: Option<(String, Bytes)> = None;+ let mut source_format: Option<TableImportSourceFormat> = None;+ let mut parse_options = TableImportParseOptions::default();+ let mut preview_limit: Option<usize> = None;+ while let Some(field) = multipart.next_field().await.map_err(|e| AppError(e.to_string()))? {+ let name = field.name().unwrap_or_default().to_string();+ if name == "file" {+ let file_name = field.file_name().unwrap_or("upload.csv").to_string();+ let data = field.bytes().await.map_err(|e| AppError(e.to_string()))?;+ uploaded_file = Some((file_name, data));+ } else {+ let value = field.text().await.map_err(|e| AppError(e.to_string()))?;+ match name.as_str() {+ "sourceFormat" => {+ source_format = Some(+ serde_json::from_value(serde_json::Value::String(value))+ .map_err(|e| AppError(e.to_string()))?,+ );+ }+ "parseOptions" => {+ parse_options = serde_json::from_str(&value).map_err(|e| AppError(e.to_string()))?;+ }+ "previewLimit" => {+ preview_limit = value.parse::<usize>().ok();+ }+ _ => {}+ }+ }+ }++ if let Some((file_name, data)) = uploaded_file {if data.len() > 100 * 1024 * 1024 {return Err(AppError(format!("File too large: {} bytes (max {} bytes)", data.len(), 100 * 1024 * 1024)));}- let file_path = tmp_dir.join(&file_name);+ let source_ref = uuid::Uuid::new_v4().to_string();+ let file_path = safe_uploaded_import_path(&tmp_dir, &file_name, &source_ref)?;std::fs::write(&file_path, &data).map_err(|e| AppError(e.to_string()))?;let file_path_str = file_path.to_string_lossy().to_string();- let preview = table_import::preview_table_import_file_core(&file_path_str).await;- let _ = tokio::fs::remove_file(&file_path).await;+ let preview = table_import::preview_table_import_file_with_request(TableImportPreviewRequest {+ file_path: file_path_str,+ source_ref: Some(source_ref),+ source_format,+ parse_options,+ preview_limit,+ })+ .await;let preview = preview.map_err(AppError)?;return Ok(Json(serde_json::to_value(preview).map_err(|e| AppError(e.to_string()))?));}@@ -55,10 +96,13 @@ pub async fn execute_import(State(state): State<Arc<WebState>>,Json(body): Json<ExecuteImportWrapper>,) -> Result<Json<serde_json::Value>, AppError> {- let req = body.request;+ let mut req = body.request;+ let file_path = validated_uploaded_import_path(&state.data_dir, &req.file_path)?;+ req.file_path = file_path.to_string_lossy().to_string();// Reject import early if the connection is read-onlyif let Some(name) = dbx_core::query::connection_readonly_name(&state.app, &req.connection_id).await {+ cleanup_uploaded_import_source(&req.file_path).await;return Err(AppError(format!("Read-only mode: connection '{}' has read-only protection enabled. Import blocked.",name@@ -79,12 +123,16 @@ pub async fn execute_import(Err(e) => {let _ = tx.send(serde_json::json!({- "importId": req.import_id,+ "importId": req.import_id.clone(),"status": "error",+ "rowsImported": 0,+ "totalRows": 0,"error": e}).to_string(),);+ cleanup_uploaded_import_source(&req.file_path).await;+ state_clone.sse_channels.write().await.remove(&req.import_id);return;}};@@ -94,12 +142,16 @@ pub async fn execute_import(Err(e) => {let _ = tx.send(serde_json::json!({- "importId": req.import_id,+ "importId": req.import_id.clone(),"status": "error",+ "rowsImported": 0,+ "totalRows": 0,"error": e}).to_string(),);+ cleanup_uploaded_import_source(&req.file_path).await;+ state_clone.sse_channels.write().await.remove(&req.import_id);return;}};@@ -134,6 +186,8 @@ pub async fn execute_import(serde_json::json!({"importId": import_id_for_cancel,"status": "error",+ "rowsImported": 0,+ "totalRows": 0,"error": e}).to_string(),@@ -141,6 +195,7 @@ pub async fn execute_import(}}+ cleanup_uploaded_import_source(&req.file_path).await;state_clone.sse_channels.write().await.remove(&req.import_id);});@@ -165,3 +220,52 @@ pub async fn cancel_import(transfer::set_cancelled(&req.import_id).await;Json(serde_json::json!({ "cancelled": true }))}++fn import_upload_dir(data_dir: &StdPath) -> PathBuf {+ data_dir.join("tmp").join("table_import")+}++fn safe_uploaded_import_path(tmp_dir: &StdPath, file_name: &str, source_ref: &str) -> Result<PathBuf, AppError> {+ let base_name = file_name.rsplit(['/', '\\']).find(|part| !part.is_empty()).unwrap_or("upload.csv").trim();+ if base_name.is_empty() || base_name == "." || base_name == ".." {+ return Err(AppError("Invalid import file name".to_string()));+ }+ Ok(tmp_dir.join(format!("{source_ref}-{base_name}")))+}++fn validated_uploaded_import_path(data_dir: &StdPath, file_path: &str) -> Result<PathBuf, AppError> {+ let path = PathBuf::from(file_path);+ if !path.is_absolute() {+ return Err(AppError("Import source path must be absolute".to_string()));+ }++ let tmp_dir = import_upload_dir(data_dir).canonicalize().map_err(|e| AppError(e.to_string()))?;+ let canonical_path =+ path.canonicalize().map_err(|e| AppError(format!("Import source is no longer available: {e}")))?;+ if !canonical_path.starts_with(&tmp_dir) {+ return Err(AppError("Import source must be inside the uploaded import directory".to_string()));+ }+ Ok(canonical_path)+}++fn cleanup_expired_import_uploads(tmp_dir: &StdPath, max_age: Duration) {+ let Ok(entries) = std::fs::read_dir(tmp_dir) else {+ return;+ };+ let now = SystemTime::now();+ for entry in entries.flatten() {+ let Ok(metadata) = entry.metadata() else {+ continue;+ };+ let Ok(modified) = metadata.modified() else {+ continue;+ };+ if now.duration_since(modified).map(|age| age > max_age).unwrap_or(false) {+ let _ = std::fs::remove_file(entry.path());+ }+ }+}++async fn cleanup_uploaded_import_source(file_path: &str) {+ let _ = tokio::fs::remove_file(file_path).await;+}diff --git a/src-tauri/src/commands/table_import.rs b/src-tauri/src/commands/table_import.rsindex eb871a5dce..5b6678f196 100644--- a/src-tauri/src/commands/table_import.rs+++ b/src-tauri/src/commands/table_import.rs@@ -8,7 +8,9 @@ use crate::commands::connection::{ensure_connection_writable, AppState};use crate::commands::transfer::get_db_type;// Re-export types for backward compatibility-pub use dbx_core::table_import::{TableImportPreview, TableImportProgress, TableImportRequest, TableImportSummary};+pub use dbx_core::table_import::{+ TableImportPreview, TableImportPreviewRequest, TableImportProgress, TableImportRequest, TableImportSummary,+};static CANCELLED_IMPORTS: OnceLock<RwLock<HashSet<String>>> = OnceLock::new();@@ -29,8 +31,8 @@ async fn clear_cancelled(import_id: &str) {}#[tauri::command]-pub async fn preview_table_import_file(file_path: String) -> Result<TableImportPreview, String> {- dbx_core::table_import::preview_table_import_file_core(&file_path).await+pub async fn preview_table_import_file(request: TableImportPreviewRequest) -> Result<TableImportPreview, String> {+ dbx_core::table_import::preview_table_import_file_with_request(request).await}#[tauri::command]