Skip to content

armgit / dbx

Public Imported from GitHub
0

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

feat(sql-file): support selecting file encoding for execution

二丫讲梵c0c4a470ab29
diff --git a/apps/desktop/src/components/sql-file/SqlFileExecutionDialog.retry.spec.ts b/apps/desktop/src/components/sql-file/SqlFileExecutionDialog.retry.spec.ts
index 60e2c000db..017fd2ce2b 100644
--- a/apps/desktop/src/components/sql-file/SqlFileExecutionDialog.retry.spec.ts
+++ b/apps/desktop/src/components/sql-file/SqlFileExecutionDialog.retry.spec.ts
@@ -21,6 +21,7 @@ const mocks = vi.hoisted(() => ({
listenSqlFileProgress: vi.fn(),
openFileDialog: vi.fn(),
previewSqlFile: vi.fn(),
+ refreshSqlFilePreview: vi.fn(),
inspectSqlFileTables: vi.fn(),
progressHandler: undefined as undefined | ((progress: Record<string, unknown>) => void),
changeDialogOpen: undefined as undefined | ((open: boolean) => void),
@@ -108,6 +109,7 @@ vi.mock("@/lib/backend/api", () => ({
executeSqlFiles: mocks.executeSqlFiles,
listenSqlFileProgress: mocks.listenSqlFileProgress,
previewSqlFile: mocks.previewSqlFile,
+ refreshSqlFilePreview: mocks.refreshSqlFilePreview,
releaseSqlFilePreview: mocks.releaseSqlFilePreview,
inspectSqlFileTables: mocks.inspectSqlFileTables,
}));
@@ -342,6 +344,30 @@ describe("SqlFileExecutionDialog retries", () => {
return preview;
}
+ it("refreshes a prepared file with the selected encoding without releasing or uploading it", async () => {
+ const preview = await mountRestoredPreview();
+ mocks.refreshSqlFilePreview.mockResolvedValue({ preview: "SELECT '中文';", canExecuteWithoutSelectedDatabase: true });
+ root!.querySelector<HTMLElement>('[data-select-item="gbk"]')!.click();
+ await vi.waitFor(() => expect(root!.textContent).toContain("SELECT '中文';"));
+ expect(mocks.refreshSqlFilePreview).toHaveBeenCalledWith(preview.filePath, "gbk");
+ expect(mocks.previewWebSqlFile).not.toHaveBeenCalled();
+ expect(mocks.releaseSqlFilePreview).not.toHaveBeenCalled();
+ findButton("sqlFile.execute").click();
+ await vi.waitFor(() => expect(mocks.executeSqlFiles).toHaveBeenCalledWith(expect.objectContaining({ encoding: "gbk" }), [preview.filePath]));
+ });
+
+ it("blocks execution after a failed encoding refresh and allows retry with another encoding", async () => {
+ await mountRestoredPreview();
+ mocks.refreshSqlFilePreview.mockRejectedValueOnce(new Error("Invalid encoding"));
+ root!.querySelector<HTMLElement>('[data-select-item="utf8"]')!.click();
+ await vi.waitFor(() => expect(root!.textContent).toContain("Invalid encoding"));
+ expect(findButton("sqlFile.execute").disabled).toBe(true);
+ mocks.refreshSqlFilePreview.mockResolvedValue({ preview: "SELECT '中文';", canExecuteWithoutSelectedDatabase: true });
+ root!.querySelector<HTMLElement>('[data-select-item="gbk"]')!.click();
+ await vi.waitFor(() => expect(findButton("sqlFile.execute").disabled).toBe(false));
+ expect(root!.textContent).not.toContain("Invalid encoding");
+ });
+
it("renders a prepared Web preview without uploading a server path and releases it on unmount", async () => {
await mountRestoredPreview();
app!.unmount();
@@ -368,7 +394,7 @@ describe("SqlFileExecutionDialog retries", () => {
const input = root!.querySelector<HTMLInputElement>('input[type="file"]')!;
Object.defineProperty(input, "files", { value: [file] });
input.dispatchEvent(new Event("change", { bubbles: true }));
- await vi.waitFor(() => expect(mocks.previewWebSqlFile).toHaveBeenCalledWith(file));
+ await vi.waitFor(() => expect(mocks.previewWebSqlFile).toHaveBeenCalledWith(file, "auto"));
expect(mocks.releaseSqlFilePreview).toHaveBeenCalledWith("restore-token");
});
@@ -661,7 +687,7 @@ describe("SqlFileExecutionDialog retries", () => {
app = createApp(SqlFileExecutionDialog, { open: true, prefillFilePath: "/tmp/unassociated.sql" });
app.mount(root);
- await vi.waitFor(() => expect(mocks.previewSqlFile).toHaveBeenCalledWith("/tmp/unassociated.sql"));
+ await vi.waitFor(() => expect(mocks.previewSqlFile).toHaveBeenCalledWith("/tmp/unassociated.sql", "auto"));
expect(mocks.ensureConnected).not.toHaveBeenCalled();
expect(mocks.fetchSqlFileTargetOptions).not.toHaveBeenCalled();
expect(findButton("sqlFile.execute").disabled).toBe(true);
@@ -888,7 +914,7 @@ describe("SqlFileExecutionDialog retries", () => {
describe("SqlFileExecutionDialog connection order", () => {
function connectionOptionIds(): string[] {
- return Array.from(root!.querySelectorAll<HTMLElement>("[data-select-item]"), (item) => item.dataset.selectItem!);
+ return Array.from(root!.querySelectorAll<HTMLElement>("[data-sql-file-connection] [data-select-item]"), (item) => item.dataset.selectItem!);
}
it("tracks nested sidebar order and sort mode while keeping missing SQL connections and the selection", async () => {
@@ -938,7 +964,7 @@ describe("SqlFileExecutionDialog selected-table restore", () => {
it("scans the backup and sends only checked table identities", async () => {
await mountBackup();
await vi.waitFor(() => expect(root!.textContent).toContain("archive.users"));
- expect(mocks.inspectSqlFileTables).toHaveBeenCalledWith("/tmp/backup.sql.gz");
+ expect(mocks.inspectSqlFileTables).toHaveBeenCalledWith("/tmp/backup.sql.gz", "auto");
expect(findButton("sqlFile.execute").disabled).toBe(true);
const label = Array.from(root!.querySelectorAll("label")).find((label) => label.textContent === "app.users")!;
label.querySelector<HTMLInputElement>("input")!.click();
@@ -1026,7 +1052,7 @@ describe("SqlFileExecutionDialog selected-table restore", () => {
input.dispatchEvent(new Event("input"));
input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
- await vi.waitFor(() => expect(mocks.previewSqlFile).toHaveBeenCalledWith("/tmp/pasted.sql"));
+ await vi.waitFor(() => expect(mocks.previewSqlFile).toHaveBeenCalledWith("/tmp/pasted.sql", "auto"));
expect(mocks.previewSqlFile).toHaveBeenCalledTimes(1);
});
@@ -1067,7 +1093,7 @@ describe("SqlFileExecutionDialog target preservation (#4844)", () => {
expect(root.querySelector('[data-select-value="target_db"]')).not.toBeNull();
findButton("sqlFile.browse").click();
- await vi.waitFor(() => expect(mocks.previewSqlFile).toHaveBeenCalledWith("/tmp/unassociated.sql"));
+ await vi.waitFor(() => expect(mocks.previewSqlFile).toHaveBeenCalledWith("/tmp/unassociated.sql", "auto"));
// Target database MUST NOT have been overwritten by activeTab database ("first_db")
expect(root.querySelector('[data-select-value="target_db"]')).not.toBeNull();
@@ -1100,7 +1126,7 @@ describe("SqlFileExecutionDialog target preservation (#4844)", () => {
await vi.waitFor(() => expect(mocks.fetchSqlFileTargetOptions).toHaveBeenCalledWith("mysql-1", expect.anything()));
findButton("sqlFile.browse").click();
- await vi.waitFor(() => expect(mocks.previewSqlFile).toHaveBeenCalledWith("/tmp/saved_other.sql"));
+ await vi.waitFor(() => expect(mocks.previewSqlFile).toHaveBeenCalledWith("/tmp/saved_other.sql", "auto"));
expect(root.querySelector('[data-select-value="mysql-1"]')).not.toBeNull();
expect(root.querySelector('[data-select-value="explicit_prefill_db"]')).not.toBeNull();
@@ -1128,7 +1154,7 @@ describe("SqlFileExecutionDialog target preservation (#4844)", () => {
expect(root.querySelector('[data-select-value="chosen_db"]')).not.toBeNull();
findButton("sqlFile.browse").click();
- await vi.waitFor(() => expect(mocks.previewSqlFile).toHaveBeenCalledWith("/tmp/brand_new.sql"));
+ await vi.waitFor(() => expect(mocks.previewSqlFile).toHaveBeenCalledWith("/tmp/brand_new.sql", "auto"));
// Selection MUST be preserved, not overwritten with "first_db" from activeTab
expect(root.querySelector('[data-select-value="chosen_db"]')).not.toBeNull();
diff --git a/apps/desktop/src/components/sql-file/SqlFileExecutionDialog.vue b/apps/desktop/src/components/sql-file/SqlFileExecutionDialog.vue
index 7c12177595..87e77705de 100644
--- a/apps/desktop/src/components/sql-file/SqlFileExecutionDialog.vue
+++ b/apps/desktop/src/components/sql-file/SqlFileExecutionDialog.vue
@@ -24,8 +24,24 @@ import { supportsSqlFileExecution, supportsTransaction } from "@/lib/database/da
import { formatError, isManualTransactionSessionExpired } from "@/lib/backend/errorUtils";
import { fetchSqlFileTargetOptions, namespaceOptionsAreSchemas } from "@/composables/useDatabaseOptions";
import { requiresSqlFileTargetDatabaseSelection, supportsConnectionLevelDatabaseBootstrap } from "@/lib/connection/connectionLevelDatabaseBootstrap";
-import { beginManualTransaction, commitManualTransaction, rollbackManualTransaction, cancelSqlFileExecution, executeSqlFiles, inspectSqlFileTables, listenSqlFileProgress, previewSqlFile, type SqlFilePreview, type SqlFileProgress, type SqlFileStatus, type SqlFileTable } from "@/lib/backend/api";
+import {
+ beginManualTransaction,
+ commitManualTransaction,
+ rollbackManualTransaction,
+ cancelSqlFileExecution,
+ executeSqlFiles,
+ refreshSqlFilePreview,
+ inspectSqlFileTables,
+ listenSqlFileProgress,
+ previewSqlFile,
+ type SqlFilePreview,
+ type SqlFileProgress,
+ type SqlFileStatus,
+ type SqlFileTable,
+} from "@/lib/backend/api";
import { activeTabExternalSqlFileTarget, rememberExternalSqlFileTarget, resolveExternalSqlFileTarget, unassociatedExternalSqlFileTarget, type ExternalSqlFileTarget } from "@/lib/sql/externalSqlFileTarget";
+import type { SqlFileEncoding } from "@/lib/backend/tauri";
+import { TABLE_IMPORT_ENCODING_OPTIONS } from "@/lib/table/tableImport";
import { isSqlFilePath } from "@/lib/sql/sqlFileOpen";
import { buildDisplayFileNames, tooltipText as computeTooltipText } from "./sqlFilePreviewLabel";
import { parseSqlFilePathInput } from "./sqlFilePathInput";
@@ -58,6 +74,10 @@ const isDesktopRuntime = isTauriRuntime();
const fileInput = ref<HTMLInputElement | null>(null);
const previews = ref<SqlFilePreview[]>([]);
+const encoding = ref<SqlFileEncoding>("auto");
+const previewError = ref("");
+let previewGeneration = 0;
+let selectedSources: Array<string | File> = [];
const activePreviewPath = ref("");
// Only the active file's preview body is mounted (reka-ui unmounts inactive
// TabsContent), so switching files keeps the DOM small even with many files.
@@ -251,6 +271,7 @@ watch(canUseManualTransaction, (supported) => {
if (!supported && !txnSessionId.value) manualTransaction.value = false;
});
onBeforeUnmount(() => {
+ ++previewGeneration;
disposed = true;
if (!running.value) void releaseManagedPreviews();
if (running.value && manualTransaction.value) {
@@ -290,7 +311,7 @@ function toggleFilteredTables() {
}
}
-watch([restoreSelectedTables, canSelectTables, () => previews.value[0]?.filePath, connectionId, open], async () => {
+watch([restoreSelectedTables, canSelectTables, () => previews.value[0]?.filePath, encoding, connectionId, open], async () => {
const generation = ++tableScanGeneration;
backupTables.value = [];
selectedTableKeys.value = new Set();
@@ -301,7 +322,7 @@ watch([restoreSelectedTables, canSelectTables, () => previews.value[0]?.filePath
if (!open.value || !restoreSelectedTables.value || !canSelectTables.value) return;
loadingTables.value = true;
try {
- const tables = await inspectSqlFileTables(previews.value[0]!.filePath);
+ const tables = await inspectSqlFileTables(previews.value[0]!.filePath, encoding.value);
if (generation !== tableScanGeneration) return;
backupTables.value = tables;
} catch (error: any) {
@@ -313,7 +334,7 @@ watch([restoreSelectedTables, canSelectTables, () => previews.value[0]?.filePath
const canStart = computed(() => {
const connection = selectedConnection.value;
- if (previews.value.length === 0 || !connection || executionLocked.value || loadingPreview.value || loadingDatabases.value) return false;
+ if (previewError.value || previews.value.length === 0 || !connection || executionLocked.value || loadingPreview.value || loadingDatabases.value) return false;
if (restoreSelectedTables.value && (loadingTables.value || tableScanError.value || selectedTables.value.length === 0)) return false;
let hasDatabaseContext = false;
const canExecuteWithoutSelectedDatabase = previews.value.every((item) => {
@@ -481,8 +502,12 @@ function resetExecution() {
}
function resetState() {
+ ++previewGeneration;
+ selectedSources = [];
+ previewError.value = "";
const initialTarget = resolveInitialTarget();
previews.value = [];
+ encoding.value = "auto";
selectingFile.value = false;
loadingPreview.value = false;
applyTarget(initialTarget);
@@ -527,9 +552,9 @@ async function loadDatabasesForConnection(id: string) {
}
}
-async function previewSelectedSqlFile(fileOrPath: string | File) {
+async function previewSelectedSqlFile(fileOrPath: string | File, selectedEncoding: SqlFileEncoding) {
if (isTauriRuntime()) {
- return previewSqlFile(fileOrPath as string);
+ return previewSqlFile(fileOrPath as string, selectedEncoding);
}
const file = fileOrPath as File;
const uploadLimit = webSqlFileUploadMaxBytes.value;
@@ -537,20 +562,26 @@ async function previewSelectedSqlFile(fileOrPath: string | File) {
throw new Error(`File too large: ${file.size} bytes (max ${uploadLimit} bytes)`);
}
const { previewSqlFile: previewWebSqlFile } = await import("@/lib/backend/http");
- return previewWebSqlFile(file);
+ return previewWebSqlFile(file, selectedEncoding);
}
async function loadPreviews(filesOrPaths: Array<string | File>, resolveSelectedFileTarget = false) {
if (executionLocked.value) return;
- await releaseManagedPreviews();
+ selectedSources = [...filesOrPaths];
+ const generation = ++previewGeneration;
+ const selectedEncoding = encoding.value;
+ previewError.value = "";
loadingPreview.value = true;
+ await releaseManagedPreviews();
+ if (generation !== previewGeneration) return;
previews.value = [];
resetExecution();
try {
const nextPreviews: SqlFilePreview[] = [];
for (const fileOrPath of filesOrPaths) {
- nextPreviews.push(await previewSelectedSqlFile(fileOrPath));
+ nextPreviews.push(await previewSelectedSqlFile(fileOrPath, selectedEncoding));
}
+ if (generation !== previewGeneration) return;
previews.value = nextPreviews;
if (resolveSelectedFileTarget && !props.prefillConnectionId) {
const firstPath = typeof filesOrPaths[0] === "string" && isSqlFilePath(filesOrPaths[0]) ? filesOrPaths[0] : undefined;
@@ -562,12 +593,45 @@ async function loadPreviews(filesOrPaths: Array<string | File>, resolveSelectedF
}
}
} catch (e: any) {
- toast(e?.message || String(e), 5000);
+ if (generation !== previewGeneration) return;
+ previewError.value = e?.message || String(e);
+ toast(previewError.value, 5000);
} finally {
- loadingPreview.value = false;
+ if (generation === previewGeneration) loadingPreview.value = false;
}
}
+watch(
+ encoding,
+ async () => {
+ if (!open.value || executionLocked.value) return;
+ if (!previews.value.length) {
+ if (selectedSources.length) await loadPreviews(selectedSources);
+ return;
+ }
+ const generation = ++previewGeneration;
+ const selectedEncoding = encoding.value;
+ const current = [...previews.value];
+ previewError.value = "";
+ loadingPreview.value = true;
+ resetExecution();
+ try {
+ const refreshed = await Promise.all(
+ current.map(async (item) => ({
+ ...item,
+ ...(await refreshSqlFilePreview(item.packageFilePaths?.[0] ?? item.filePath, selectedEncoding)),
+ })),
+ );
+ if (generation === previewGeneration) previews.value = refreshed;
+ } catch (error: any) {
+ if (generation === previewGeneration) previewError.value = error?.message || String(error);
+ } finally {
+ if (generation === previewGeneration) loadingPreview.value = false;
+ }
+ },
+ { flush: "sync" },
+);
+
async function releaseManagedPreviews() {
const tokens = previews.value.flatMap((preview) => (preview.cleanupToken ? [preview.cleanupToken] : []));
if (!tokens.length) return;
@@ -645,6 +709,7 @@ async function refreshTargetAfterImport(target: SqlFileExecutionTarget) {
async function startExecution() {
if (!canStart.value || previews.value.length === 0) return;
+ const generation = previewGeneration;
const target = sqlFileExecutionTarget();
// Await the unlock guard only when it can actually prompt/block (effectively
// read-only); writable or already-unlocked connections stay synchronous so
@@ -668,6 +733,8 @@ async function startExecution() {
if (!confirmed) return;
}
+ // A confirmation may have yielded while the file or encoding changed.
+ if (!canStart.value || generation !== previewGeneration) return;
resetPerFileState();
refreshedTarget.value = false;
const batchId = uuid();
@@ -772,6 +839,7 @@ async function startExecution() {
database: target.database,
...(target.schema ? { schema: target.schema } : {}),
filePath: executionPaths[0]!,
+ encoding: encoding.value,
continueOnError: continueOnError.value,
...(txnSessionId.value ? { txnSessionId: txnSessionId.value } : {}),
...(restoreSelectedTables.value ? { selectedTables: selectedTables.value.map((table) => ({ ...table })) } : {}),
@@ -878,6 +946,7 @@ watch(
open,
(value) => {
if (!value) {
+ ++previewGeneration;
if (!running.value) void releaseManagedPreviews();
if (manualTransaction.value && running.value) void cancelExecution();
else if (txnSessionId.value) void finishTransaction(false);
@@ -928,6 +997,18 @@ watch(
</Button>
</div>
+ <div class="space-y-1.5">
+ <Label for="sql-file-encoding" class="text-xs">{{ t("tableImport.encoding") }}</Label>
+ <Select v-model="encoding" :disabled="executionLocked || selectingFile || loadingPreview">
+ <SelectTrigger id="sql-file-encoding" class="h-8 text-xs"><SelectValue /></SelectTrigger>
+ <SelectContent>
+ <SelectItem v-for="option in TABLE_IMPORT_ENCODING_OPTIONS" :key="option.value" :value="option.value">{{ t(option.labelKey) }}</SelectItem>
+ </SelectContent>
+ </Select>
+ <p class="text-xs text-muted-foreground">{{ t("sqlFile.encodingHint") }}</p>
+ <p v-if="previewError" class="break-words text-xs text-destructive" role="alert">{{ previewError }}</p>
+ </div>
+
<div v-if="previews.length" class="flex min-w-0 gap-3">
<div v-if="previews.length > 1" class="flex w-48 shrink-0 flex-col rounded-md border bg-muted/20">
<div class="flex shrink-0 items-center gap-2 rounded-t-md border-b bg-muted/40 px-3 py-2 text-xs text-muted-foreground">
@@ -980,7 +1061,7 @@ watch(
<div class="grid grid-cols-1 gap-3 sm:grid-cols-2">
<div class="space-y-1.5">
<Label class="text-xs">{{ t("sqlFile.connection") }}</Label>
- <Select v-model="connectionId" :disabled="executionLocked">
+ <Select v-model="connectionId" :disabled="executionLocked" data-sql-file-connection>
<SelectTrigger class="h-8 text-xs">
<div v-if="connectionId" class="flex items-center gap-1.5 min-w-0">
<DatabaseIcon :db-type="connectionIconType(connectionId)" class="w-3.5 h-3.5 shrink-0" />
diff --git a/apps/desktop/src/i18n/locales/az.ts b/apps/desktop/src/i18n/locales/az.ts
index c9900c7715..17abaa9d59 100644
--- a/apps/desktop/src/i18n/locales/az.ts
+++ b/apps/desktop/src/i18n/locales/az.ts
@@ -6732,6 +6732,7 @@ export default withEnglishFallback({
exportData: "Məlumatları ixrac et",
},
sqlFile: {
+ encodingHint: "Bütün seçilmiş fayllara tətbiq olunur. BOM varsa, onun göstərdiyi kodlaşdırmaya üstünlük verilir.",
restoreScope: "Bərpa əhatəsi",
restoreAll: "Bütün məzmun",
restoreSelectedTables: "Seçilmiş cədvəllər",
diff --git a/apps/desktop/src/i18n/locales/en.ts b/apps/desktop/src/i18n/locales/en.ts
index 8595408893..68fe4ae6dc 100644
--- a/apps/desktop/src/i18n/locales/en.ts
+++ b/apps/desktop/src/i18n/locales/en.ts
@@ -7947,6 +7947,7 @@ export default {
exportData: "Export Data",
},
sqlFile: {
+ encodingHint: "Applies to all selected files. A byte order mark (BOM), if present, takes precedence.",
manualTransactionHint: "Changes wait for Commit or Rollback. Errors roll back. DDL and COMMIT inside the file can still commit changes.",
manualCancelHint: "Cancel waits for the current statement, then rolls back uncommitted changes.",
pendingTransaction: "Awaiting Commit or Rollback",
diff --git a/apps/desktop/src/i18n/locales/es.ts b/apps/desktop/src/i18n/locales/es.ts
index 2b43f4210f..c9b49704f6 100644
--- a/apps/desktop/src/i18n/locales/es.ts
+++ b/apps/desktop/src/i18n/locales/es.ts
@@ -7152,6 +7152,7 @@ export default withEnglishFallback({
exportData: "Exportar datos",
},
sqlFile: {
+ encodingHint: "Se aplica a todos los archivos seleccionados. Si hay una marca BOM, su codificación tiene prioridad.",
manualTransactionHint: "Los cambios esperan confirmación o reversión. Los errores provocan una reversión. DDL y COMMIT dentro del archivo aún pueden confirmar cambios.",
manualCancelHint: "Cancelar espera a que termine la sentencia actual y luego revierte los cambios sin confirmar.",
pendingTransaction: "Pendiente de confirmar o revertir",
diff --git a/apps/desktop/src/i18n/locales/id.ts b/apps/desktop/src/i18n/locales/id.ts
index e74f154551..4e092acdbd 100644
--- a/apps/desktop/src/i18n/locales/id.ts
+++ b/apps/desktop/src/i18n/locales/id.ts
@@ -7415,6 +7415,7 @@ export default withEnglishFallback({
exportData: "Ekspor Data",
},
sqlFile: {
+ encodingHint: "Berlaku untuk semua file yang dipilih. Jika ada BOM, pengodean yang ditunjukkannya diutamakan.",
manualTransactionHint: "Perubahan menunggu Commit atau Rollback. Galat akan di-rollback. DDL dan COMMIT di dalam file tetap dapat melakukan commit perubahan.",
manualCancelHint: "Pembatalan menunggu pernyataan saat ini selesai, lalu me-rollback perubahan yang belum di-commit.",
pendingTransaction: "Menunggu Commit atau Rollback",
diff --git a/apps/desktop/src/i18n/locales/it.ts b/apps/desktop/src/i18n/locales/it.ts
index bbc0160cad..a41a223db1 100644
--- a/apps/desktop/src/i18n/locales/it.ts
+++ b/apps/desktop/src/i18n/locales/it.ts
@@ -6945,6 +6945,7 @@ export default withEnglishFallback({
exportData: "Esporta dati",
},
sqlFile: {
+ encodingHint: "Si applica a tutti i file selezionati. Se presente, la codifica indicata dal BOM ha la precedenza.",
manualTransactionHint: "Le modifiche attendono commit o rollback. Gli errori causano il rollback. DDL e COMMIT nel file possono comunque confermare le modifiche.",
manualCancelHint: "L'annullamento attende la fine dell'istruzione corrente, poi esegue il rollback delle modifiche non confermate.",
pendingTransaction: "In attesa di commit o rollback",
diff --git a/apps/desktop/src/i18n/locales/ja.ts b/apps/desktop/src/i18n/locales/ja.ts
index 535952ab6f..4dce0ee453 100644
--- a/apps/desktop/src/i18n/locales/ja.ts
+++ b/apps/desktop/src/i18n/locales/ja.ts
@@ -6973,6 +6973,7 @@ export default withEnglishFallback({
exportData: "データをエクスポート",
},
sqlFile: {
+ encodingHint: "選択したすべてのファイルに適用します。BOM がある場合は、そのエンコーディングを優先します。",
manualTransactionHint: "変更は手動でコミットまたはロールバックするまで保留されます。エラー時はロールバックします。ファイル内の DDL や COMMIT により変更が確定する場合があります。",
manualCancelHint: "キャンセルすると現在の文の終了を待ち、未コミットの変更をロールバックします。",
pendingTransaction: "コミットまたはロールバック待ち",
diff --git a/apps/desktop/src/i18n/locales/ko.ts b/apps/desktop/src/i18n/locales/ko.ts
index 4e0830b8cc..211907a849 100644
--- a/apps/desktop/src/i18n/locales/ko.ts
+++ b/apps/desktop/src/i18n/locales/ko.ts
@@ -6749,6 +6749,7 @@ export default withEnglishFallback({
exportData: "데이터 내보내기",
},
sqlFile: {
+ encodingHint: "선택한 모든 파일에 적용됩니다. BOM이 있으면 해당 인코딩을 우선합니다.",
manualTransactionHint: "변경 사항은 수동 커밋 또는 롤백을 기다립니다. 오류가 발생하면 롤백합니다. 파일의 DDL과 COMMIT은 변경 사항을 커밋할 수 있습니다.",
manualCancelHint: "취소하면 현재 구문이 끝날 때까지 기다린 후 커밋되지 않은 변경 사항을 롤백합니다.",
pendingTransaction: "커밋 또는 롤백 대기 중",
diff --git a/apps/desktop/src/i18n/locales/pt-BR.ts b/apps/desktop/src/i18n/locales/pt-BR.ts
index 8122512b81..8f775371ba 100644
--- a/apps/desktop/src/i18n/locales/pt-BR.ts
+++ b/apps/desktop/src/i18n/locales/pt-BR.ts
@@ -6944,6 +6944,7 @@ export default withEnglishFallback({
exportData: "Exportar dados",
},
sqlFile: {
+ encodingHint: "Aplica-se a todos os arquivos selecionados. Se houver uma marca BOM, sua codificação terá prioridade.",
manualTransactionHint: "As alterações aguardam confirmação ou reversão. Erros provocam reversão. DDL e COMMIT no arquivo ainda podem confirmar alterações.",
manualCancelHint: "Cancelar aguarda o fim da instrução atual e depois reverte as alterações não confirmadas.",
pendingTransaction: "Aguardando confirmação ou reversão",
diff --git a/apps/desktop/src/i18n/locales/ru.ts b/apps/desktop/src/i18n/locales/ru.ts
index 2389bcff37..bb2fb1a0f8 100644
--- a/apps/desktop/src/i18n/locales/ru.ts
+++ b/apps/desktop/src/i18n/locales/ru.ts
@@ -8185,6 +8185,7 @@ export default withEnglishFallback({
exportData: "Экспорт данных",
},
sqlFile: {
+ encodingHint: "Применяется ко всем выбранным файлам. При наличии BOM используется указанная им кодировка.",
manualTransactionHint: "Изменения ожидают Commit или Rollback. При ошибках выполняется откат. DDL и COMMIT внутри файла всё равно могут зафиксировать изменения.",
manualCancelHint: "Отмена ожидает завершения текущего оператора, затем откатывает незафиксированные изменения.",
pendingTransaction: "Ожидание Commit или Rollback",
diff --git a/apps/desktop/src/i18n/locales/th.ts b/apps/desktop/src/i18n/locales/th.ts
index f6bebe1172..6606678f39 100644
--- a/apps/desktop/src/i18n/locales/th.ts
+++ b/apps/desktop/src/i18n/locales/th.ts
@@ -8595,6 +8595,7 @@ export default withEnglishFallback({
exportData: "ส่งออกข้อมูล",
},
sqlFile: {
+ encodingHint: "ใช้กับไฟล์ทั้งหมดที่เลือกในครั้งนี้ หากไฟล์มีเครื่องหมายลำดับไบต์ (BOM) จะใช้การเข้ารหัสที่ระบุโดย BOM เป็นอันดับแรก",
manualTransactionHint: "การเปลี่ยนแปลงจะรอ Commit หรือ Rollback ข้อผิดพลาดจะ Rollback แต่ DDL และ COMMIT ภายในไฟล์ยังสามารถ Commit การเปลี่ยนแปลงได้",
manualCancelHint: "การยกเลิกจะรอ statement ปัจจุบัน แล้ว Rollback การเปลี่ยนแปลงที่ยังไม่ Commit",
pendingTransaction: "กำลังรอ Commit หรือ Rollback",
diff --git a/apps/desktop/src/i18n/locales/tr.ts b/apps/desktop/src/i18n/locales/tr.ts
index d903da43bd..c8e5ca53d4 100644
--- a/apps/desktop/src/i18n/locales/tr.ts
+++ b/apps/desktop/src/i18n/locales/tr.ts
@@ -6613,6 +6613,7 @@ export default withEnglishFallback({
exportData: "Veri Dışa Aktar",
},
sqlFile: {
+ encodingHint: "Seçilen tüm dosyalara uygulanır. BOM varsa belirttiği kodlama önceliklidir.",
restoreScope: "Geri yükleme kapsamı",
restoreAll: "Tüm içerik",
restoreSelectedTables: "Seçili tablolar",
diff --git a/apps/desktop/src/i18n/locales/zh-CN.ts b/apps/desktop/src/i18n/locales/zh-CN.ts
index d897cae7b3..40f3c2aea8 100644
--- a/apps/desktop/src/i18n/locales/zh-CN.ts
+++ b/apps/desktop/src/i18n/locales/zh-CN.ts
@@ -7929,6 +7929,7 @@ export default withEnglishFallback({
exportData: "导出数据",
},
sqlFile: {
+ encodingHint: "应用于本次选择的所有文件;文件含字节序标记(BOM)时,优先采用 BOM 指定的编码。",
manualTransactionHint: "修改等待手动提交或回滚,出错时回滚。文件中的 DDL 和 COMMIT 仍可能提交修改。",
manualCancelHint: "取消会等待当前语句结束,再回滚未提交的修改。",
pendingTransaction: "等待提交或回滚",
diff --git a/apps/desktop/src/i18n/locales/zh-TW.ts b/apps/desktop/src/i18n/locales/zh-TW.ts
index 7de9ab259b..16b3f461e4 100644
--- a/apps/desktop/src/i18n/locales/zh-TW.ts
+++ b/apps/desktop/src/i18n/locales/zh-TW.ts
@@ -6285,6 +6285,7 @@ export default withEnglishFallback({
exportData: "匯出資料",
},
sqlFile: {
+ encodingHint: "套用至本次選取的所有檔案;檔案含位元組順序標記(BOM)時,優先採用 BOM 指定的編碼。",
manualTransactionHint: "修改等待手動提交或回復,出錯時回復。檔案中的 DDL 和 COMMIT 仍可能提交修改。",
manualCancelHint: "取消會等待目前陳述式結束,再回復未提交的修改。",
pendingTransaction: "等待提交或回復",
diff --git a/apps/desktop/src/lib/backend/api.ts b/apps/desktop/src/lib/backend/api.ts
index 2e08f3cfc8..66c0cb94f2 100644
--- a/apps/desktop/src/lib/backend/api.ts
+++ b/apps/desktop/src/lib/backend/api.ts
@@ -483,6 +483,7 @@ export const listLocalSshKeys = forward("listLocalSshKeys");
// SQL File Execution
export const previewSqlFile = forward("previewSqlFile");
+export const refreshSqlFilePreview = forward("refreshSqlFilePreview");
export const releaseSqlFilePreview = forward("releaseSqlFilePreview");
export const executeSqlFile = forward("executeSqlFile");
export const sqlFileFailureLogInfo = forward("sqlFileFailureLogInfo");
diff --git a/apps/desktop/src/lib/backend/http.ts b/apps/desktop/src/lib/backend/http.ts
index 44466029ff..3296e6e791 100644
--- a/apps/desktop/src/lib/backend/http.ts
+++ b/apps/desktop/src/lib/backend/http.ts
@@ -1,3 +1,4 @@
+import type { SqlFileEncoding } from "./tauri";
import type { StarRocksAlterOptions } from "@/lib/table/starrocksAlterOptions";
import type { CreateTableDialectOptions } from "@/lib/table/starrocksPhysicalOptions";
import type { MongoDumpFormat, MongoDumpSourceInput, MongoDumpCatalog, MongoRestoreSourcePreview, MongoDatabaseDumpRequest, MongoDatabaseRestoreRequest, MongoDatabaseDumpProgress } from "./mongodbDumpTypes";
@@ -2874,14 +2875,14 @@ export async function setAiGlobalCustomInstructions(content: string): Promise<vo
// SQL File Execution
// ---------------------------------------------------------------------------
-export async function previewSqlFile(fileOrPath: string | File): Promise<SqlFilePreview> {
+export async function previewSqlFile(fileOrPath: string | File, encoding?: SqlFileEncoding): Promise<SqlFilePreview> {
if (typeof fileOrPath === "string") {
// In web mode a raw path is not useful; throw a clear error
throw new Error("previewSqlFile in web mode requires a File object, not a file path");
}
const formData = new FormData();
formData.append("file", fileOrPath);
- const res = await fetch(apiUrl("/api/sql-file/preview"), {
+ const res = await fetch(apiUrl(`/api/sql-file/preview${encoding ? `?encoding=${encodeURIComponent(encoding)}` : ""}`), {
method: "POST",
body: formData,
});
@@ -2889,6 +2890,10 @@ export async function previewSqlFile(fileOrPath: string | File): Promise<SqlFile
return res.json();
}
+export async function refreshSqlFilePreview(filePath: string, encoding: SqlFileEncoding): Promise<Pick<SqlFilePreview, "preview" | "canExecuteWithoutSelectedDatabase" | "establishesDatabaseContext">> {
+ return post("/api/sql-file/preview/refresh", { filePath, encoding });
+}
+
export async function releaseSqlFilePreview(cleanupToken: string): Promise<void> {
return post("/api/sql-file/preview/release", { cleanupToken });
}
@@ -2924,8 +2929,8 @@ export async function executeSqlFile(request: SqlFileRequest): Promise<void> {
return post("/api/sql-file/execute", { request });
}
-export async function inspectSqlFileTables(filePath: string): Promise<SqlFileTable[]> {
- return post("/api/sql-file/tables", { filePath });
+export async function inspectSqlFileTables(filePath: string, encoding?: SqlFileEncoding): Promise<SqlFileTable[]> {
+ return post("/api/sql-file/tables", { filePath, encoding });
}
export async function executeSqlFiles(request: SqlFileRequest, filePaths: string[]): Promise<void> {
diff --git a/apps/desktop/src/lib/backend/tauri.ts b/apps/desktop/src/lib/backend/tauri.ts
index 529f0a3c5c..82d6388b7d 100644
--- a/apps/desktop/src/lib/backend/tauri.ts
+++ b/apps/desktop/src/lib/backend/tauri.ts
@@ -5654,7 +5654,10 @@ export async function deleteHistoryEntry(id: string): Promise<void> {
// --- SQL File Execution ---
export type SqlFileStatus = "started" | "running" | "statementDone" | "statementFailed" | "done" | "error" | "cancelled";
+export type SqlFileEncoding = "auto" | "utf8" | "gbk" | "utf16Le" | "utf16Be";
+
export interface SqlFileRequest {
+ encoding?: SqlFileEncoding;
executionId: string;
connectionId: string;
database: string;
@@ -5672,8 +5675,8 @@ export interface SqlFileTable {
name: string;
}
-export async function inspectSqlFileTables(filePath: string): Promise<SqlFileTable[]> {
- return invoke("inspect_sql_file_tables", { filePath });
+export async function inspectSqlFileTables(filePath: string, encoding?: SqlFileEncoding): Promise<SqlFileTable[]> {
+ return invoke("inspect_sql_file_tables", { filePath, encoding });
}
export interface SqlFilePreview {
@@ -5688,6 +5691,11 @@ export interface SqlFilePreview {
cleanupToken?: string;
}
+export async function refreshSqlFilePreview(filePath: string, encoding: SqlFileEncoding): Promise<Pick<SqlFilePreview, "preview" | "canExecuteWithoutSelectedDatabase" | "establishesDatabaseContext">> {
+ const result = await previewSqlFile(filePath, encoding);
+ return { preview: result.preview, canExecuteWithoutSelectedDatabase: result.canExecuteWithoutSelectedDatabase, establishesDatabaseContext: result.establishesDatabaseContext };
+}
+
export async function releaseSqlFilePreview(_cleanupToken: string): Promise<void> {}
export interface SqlFileProgress {
@@ -5707,8 +5715,8 @@ export interface SqlFileProgress {
fileName?: string;
}
-export async function previewSqlFile(filePath: string): Promise<SqlFilePreview> {
- return invoke("preview_sql_file", { filePath });
+export async function previewSqlFile(filePath: string, encoding?: SqlFileEncoding): Promise<SqlFilePreview> {
+ return invoke("preview_sql_file", { filePath, encoding });
}
export type SqlFileFailureLogStatus = "available" | "degraded" | "unavailable";
diff --git a/crates/dbx-core/src/data/sql_file_import.rs b/crates/dbx-core/src/data/sql_file_import.rs
index 9b05085869..57d06914a2 100644
--- a/crates/dbx-core/src/data/sql_file_import.rs
+++ b/crates/dbx-core/src/data/sql_file_import.rs
@@ -27,7 +27,7 @@ use crate::query::{
use crate::sql::{
optimize_sql_file_import_statements, optimize_sql_file_import_statements_with_max_insert_batch_statements,
prepare_sql_file_statement, split_sql_batches, starts_with_sqlserver_module_ddl, statement_summary,
- SqlFileImportStatement, SqlFileImportStatementKind, SqlFilePhase, SqlFileProgress, SqlFileRequest,
+ SqlFileEncoding, SqlFileImportStatement, SqlFileImportStatementKind, SqlFilePhase, SqlFileProgress, SqlFileRequest,
SqlFileStatementAction, SqlFileStatus, SqlParsingOptions, SqlStatementSplitter, SqlStatementWithControl,
};
use crate::types::QueryResult;
@@ -860,7 +860,7 @@ async fn execute_sql_file_paths_inner(
let Some(selected) = &request.selected_tables else {
return Ok(None);
};
- let (tables, views) = scan_sql_file_tables(file_paths[0], &token).await?;
+ let (tables, views) = scan_sql_file_tables(file_paths[0], &token, request.encoding).await?;
validate_selected_tables(selected, &tables)?;
Ok::<_, String>(Some(TableRestoreFilter::with_views(views)))
}
@@ -945,10 +945,11 @@ async fn execute_sql_file_paths_inner(
let normalize_mysql_binary_literals = import_target.as_ref().is_some_and(|target| {
crate::sql::is_mysql_compatible_import_target(&target.db_type, target.driver_profile.as_deref())
});
- let mut decoder = match SqlFileStreamDecoder::open_for_target_with_progress(
+ let mut decoder = match SqlFileStreamDecoder::open_for_import(
file_path,
+ request.encoding.to_encoding(),
normalize_mysql_binary_literals,
- read_progress.bytes_read.clone(),
+ Some(read_progress.bytes_read.clone()),
)
.await
{
@@ -1139,14 +1140,22 @@ async fn execute_sql_file_paths_inner(
}
pub async fn inspect_sql_file_tables(file_path: &Path) -> Result<Vec<SqlFileTable>, String> {
- scan_sql_file_tables(file_path, &CancellationToken::new()).await.map(|(tables, _)| tables)
+ inspect_sql_file_tables_with_encoding(file_path, SqlFileEncoding::Auto).await
+}
+
+pub async fn inspect_sql_file_tables_with_encoding(
+ file_path: &Path,
+ encoding: SqlFileEncoding,
+) -> Result<Vec<SqlFileTable>, String> {
+ scan_sql_file_tables(file_path, &CancellationToken::new(), encoding).await.map(|(tables, _)| tables)
}
async fn scan_sql_file_tables(
file_path: &Path,
token: &CancellationToken,
+ encoding: SqlFileEncoding,
) -> Result<(Vec<SqlFileTable>, std::collections::BTreeSet<SqlFileTable>), String> {
- let mut decoder = SqlFileStreamDecoder::open_for_target(file_path, true).await?;
+ let mut decoder = SqlFileStreamDecoder::open_for_import(file_path, encoding.to_encoding(), true, None).await?;
let mut splitter =
StreamingSqlFileSplitter::new(Some(DatabaseType::Mysql), SqlParsingOptions::mysql_compatible(), false);
let mut scan = TableRestoreFilter::default();
@@ -1213,9 +1222,36 @@ fn filter_restore_statements(
}
pub async fn read_sql_file_preview(file_path: &Path, max_chars: usize) -> Result<String, String> {
- let mut decoder =
- SqlFileStreamDecoder::open_with_detection_limit(file_path, Some(SQL_FILE_PREVIEW_ENCODING_SAMPLE_BYTES))
- .await?;
+ read_sql_file_preview_with_encoding(file_path, max_chars, SqlFileEncoding::Auto).await
+}
+
+pub async fn read_sql_file_preview_with_encoding(
+ file_path: &Path,
+ max_chars: usize,
+ encoding: SqlFileEncoding,
+) -> Result<String, String> {
+ let mut decoder = if encoding == SqlFileEncoding::Auto {
+ SqlFileStreamDecoder::open_with_detection_limit(file_path, Some(SQL_FILE_PREVIEW_ENCODING_SAMPLE_BYTES)).await?
+ } else {
+ SqlFileStreamDecoder::open_for_import(file_path, encoding.to_encoding(), false, None).await?
+ };
+ // Preview may run before a database is selected. Do not apply MySQL syntax
+ // rewriting to valid text (for example PostgreSQL dollar-quoted strings).
+ // Like automatic detection, recover raw mysqldump binary bytes only when
+ // ordinary UTF-8 decoding fails. Reopen so the preview cannot mix original
+ // text with normalized chunks. Honor BOM overrides and never change encoding.
+ let recover_mysql_binary = encoding != SqlFileEncoding::Auto && decoder.encoding() == encoding_rs::UTF_8;
+ match collect_sql_file_preview(&mut decoder, max_chars).await {
+ Err(error) if recover_mysql_binary && error == "Unsupported or invalid SQL file encoding" => {
+ let mut decoder =
+ SqlFileStreamDecoder::open_for_import(file_path, encoding.to_encoding(), true, None).await?;
+ collect_sql_file_preview(&mut decoder, max_chars).await
+ }
+ result => result,
+ }
+}
+
+async fn collect_sql_file_preview(decoder: &mut SqlFileStreamDecoder, max_chars: usize) -> Result<String, String> {
let mut preview = String::new();
while preview.chars().count() < max_chars {
let Some(chunk) = decoder.next_chunk().await? else {
@@ -1330,10 +1366,12 @@ impl SqlFileStreamDecoder {
Self::open_with_options(file_path, detection_limit, false, None).await
}
+ #[cfg(test)]
async fn open_for_target(file_path: &Path, normalize_mysql_binary_literals: bool) -> Result<Self, String> {
Self::open_with_options(file_path, None, normalize_mysql_binary_literals, None).await
}
+ #[cfg(test)]
async fn open_for_target_with_progress(
file_path: &Path,
normalize_mysql_binary_literals: bool,
@@ -2899,6 +2937,7 @@ mod tests {
part_cooldown_ms: 0,
skip_relational_constraints: false,
failure_log_enabled: true,
+ encoding: Default::default(),
};
let mut events = Vec::new();
let error = execute_sql_file_content(
@@ -3254,6 +3293,7 @@ mod tests {
part_cooldown_ms: 0,
skip_relational_constraints: false,
failure_log_enabled: true,
+ encoding: Default::default(),
};
let token = CancellationToken::new();
let mut events = Vec::new();
@@ -3577,6 +3617,58 @@ mod tests {
assert!(splitter.finish().is_empty());
}
+ #[tokio::test]
+ async fn encoding_preview_preserves_postgres_dollar_quotes_like_execution() {
+ let sql = "SELECT $$ _binary ' $$;\nSELECT $tag$ _binary 'abc' $tag$;\n";
+ let mut bom_bytes = vec![0xEF, 0xBB, 0xBF];
+ bom_bytes.extend_from_slice(sql.as_bytes());
+ for (bytes, encoding) in [
+ (sql.as_bytes().to_vec(), SqlFileEncoding::Auto),
+ (sql.as_bytes().to_vec(), SqlFileEncoding::Utf8),
+ (bom_bytes, SqlFileEncoding::Gbk),
+ ] {
+ let path = temporary_sql_file(&bytes).await;
+ let preview = read_sql_file_preview_with_encoding(&path, 20_000, encoding).await.unwrap();
+ // PostgreSQL execution passes false for MySQL normalization.
+ let mut execution =
+ SqlFileStreamDecoder::open_for_import(&path, encoding.to_encoding(), false, None).await.unwrap();
+ assert_eq!(preview, sql);
+ assert_eq!(preview, collect_sql_file_preview(&mut execution, 20_000).await.unwrap());
+ tokio::fs::remove_file(path).await.unwrap();
+ }
+ }
+
+ #[tokio::test]
+ async fn encoding_preview_recovers_mysql_binary_without_mixing_chunks() {
+ // The failure is in a later chunk; recovery must discard the first pass.
+ let prefix = format!("SELECT _binary 'abc';\n-- {}\n", "x".repeat(SQL_FILE_READ_CHUNK_BYTES));
+ let mut bytes = prefix.as_bytes().to_vec();
+ bytes.extend_from_slice(b"INSERT INTO t VALUES (_binary '\xAC\\\xED\\0\x05', '");
+ bytes.extend_from_slice("中文');\n".as_bytes());
+ for path in [temporary_sql_file(&bytes).await, temporary_gzip_sql_file(&bytes).await] {
+ for encoding in [SqlFileEncoding::Auto, SqlFileEncoding::Utf8] {
+ let preview = read_sql_file_preview_with_encoding(&path, 1_000_000, encoding).await.unwrap();
+ let mut execution =
+ SqlFileStreamDecoder::open_for_import(&path, encoding.to_encoding(), true, None).await.unwrap();
+ assert_eq!(preview, collect_sql_file_preview(&mut execution, 1_000_000).await.unwrap());
+ assert!(preview.starts_with("SELECT X'616263';"));
+ assert!(preview.ends_with("INSERT INTO t VALUES (X'ACED0005', '中文');\n"));
+ assert_eq!(preview.matches("SELECT").count(), 1);
+ }
+ tokio::fs::remove_file(path).await.unwrap();
+ }
+ }
+
+ #[tokio::test]
+ async fn encoding_preview_does_not_hide_invalid_utf8_outside_binary_literals() {
+ let path = temporary_sql_file(b"SELECT '\xFF';").await;
+ assert_eq!(
+ read_sql_file_preview_with_encoding(&path, 20_000, SqlFileEncoding::Utf8).await.unwrap_err(),
+ "Unsupported or invalid SQL file encoding",
+ );
+ tokio::fs::remove_file(path).await.unwrap();
+ }
+
#[tokio::test]
async fn streaming_decoder_detects_gbk_after_ascii_prefix() {
let (encoded, _, _) = encoding_rs::GBK.encode("-- Navicat dump\nINSERT INTO t VALUES ('中文');");
@@ -3864,6 +3956,7 @@ mod tests {
part_cooldown_ms: 0,
skip_relational_constraints: false,
failure_log_enabled: true,
+ encoding: Default::default(),
};
let mut progress = Vec::new();
diff --git a/crates/dbx-core/src/query/mod.rs b/crates/dbx-core/src/query/mod.rs
index 0210302f6f..bdf37be826 100644
--- a/crates/dbx-core/src/query/mod.rs
+++ b/crates/dbx-core/src/query/mod.rs
@@ -12301,6 +12301,7 @@ for line in sys.stdin:
part_cooldown_ms: 0,
skip_relational_constraints: false,
failure_log_enabled: true,
+ encoding: Default::default(),
}
}
diff --git a/crates/dbx-core/tests/live_mysql57.rs b/crates/dbx-core/tests/live_mysql57.rs
index a477d9de4d..d77a025553 100644
--- a/crates/dbx-core/tests/live_mysql57.rs
+++ b/crates/dbx-core/tests/live_mysql57.rs
@@ -1377,6 +1377,7 @@ INSERT INTO install_check (id) VALUES (1), (2);
part_cooldown_ms: 0,
skip_relational_constraints: false,
failure_log_enabled: true,
+ encoding: Default::default(),
};
let _ = execute_sql_statement(
@@ -1468,6 +1469,7 @@ INSERT INTO children (parent_id) VALUES (LAST_INSERT_ID());
part_cooldown_ms: 0,
skip_relational_constraints: false,
failure_log_enabled: true,
+ encoding: Default::default(),
};
let _ = execute_sql_statement(
@@ -1543,6 +1545,7 @@ async fn live_sql_file_import_preserves_raw_mysql_binary_literal_bytes() {
part_cooldown_ms: 0,
skip_relational_constraints: false,
failure_log_enabled: true,
+ encoding: Default::default(),
};
tokio::fs::write(&request.file_path, script).await.unwrap();
diff --git a/crates/dbx-core/tests/live_mysql_database_export.rs b/crates/dbx-core/tests/live_mysql_database_export.rs
index ff5587a5e7..07d1d9a917 100644
--- a/crates/dbx-core/tests/live_mysql_database_export.rs
+++ b/crates/dbx-core/tests/live_mysql_database_export.rs
@@ -79,6 +79,7 @@ async fn live_mysql_selected_table_restore_preserves_unselected_tables() {
part_cooldown_ms: 0,
skip_relational_constraints: false,
failure_log_enabled: true,
+ encoding: Default::default(),
};
execute_sql_file_path(&state, &request, &path, CancellationToken::new(), std::time::Instant::now(), |_| {}).await?;
let rows = execute_sql_statement(&state, &connection_id, &database, "SELECT id, LENGTH(body) FROM chosen", None, None).await?;
@@ -97,6 +98,7 @@ async fn live_mysql_selected_table_restore_preserves_unselected_tables() {
part_cooldown_ms: 0,
skip_relational_constraints: false,
failure_log_enabled: true,
+ encoding: Default::default(),
};
let mut events = Vec::new();
assert!(execute_sql_file_path(&state, &request, &path, CancellationToken::new(), std::time::Instant::now(), |event| events.push(event)).await.is_err());
@@ -186,6 +188,7 @@ async fn live_mysql_database_export_restores_dependent_views() {
part_cooldown_ms: 0,
skip_relational_constraints: false,
failure_log_enabled: true,
+ encoding: Default::default(),
};
execute_sql_file_path(
&state,
diff --git a/crates/dbx-core/tests/live_oceanbase_sql_file_transaction.rs b/crates/dbx-core/tests/live_oceanbase_sql_file_transaction.rs
index a6b0240d03..708df73781 100644
--- a/crates/dbx-core/tests/live_oceanbase_sql_file_transaction.rs
+++ b/crates/dbx-core/tests/live_oceanbase_sql_file_transaction.rs
@@ -68,6 +68,7 @@ async fn live_oceanbase_sql_files_commit_rollback_failure_and_cancel() {
database: database.clone(), schema: None, file_path: String::new(), continue_on_error: false,
selected_tables: None, part_cooldown_ms: 0, skip_relational_constraints: false, txn_session_id: None,
failure_log_enabled: true,
+ encoding: Default::default(),
};
let first = directory.path().join("first.sql");
let second = directory.path().join("second.sql");
diff --git a/crates/dbx-core/tests/live_oracle_database_export.rs b/crates/dbx-core/tests/live_oracle_database_export.rs
index eac7f53bac..efe6f5be3b 100644
--- a/crates/dbx-core/tests/live_oracle_database_export.rs
+++ b/crates/dbx-core/tests/live_oracle_database_export.rs
@@ -140,6 +140,7 @@ async fn live_oracle_database_export_replays_trigger_sequences() {
part_cooldown_ms: 0,
skip_relational_constraints: false,
failure_log_enabled: true,
+ encoding: Default::default(),
};
execute_sql_file_path(
&state,
diff --git a/crates/dbx-core/tests/live_postgres_all_schema_export.rs b/crates/dbx-core/tests/live_postgres_all_schema_export.rs
index e6a7253989..091c9cb095 100644
--- a/crates/dbx-core/tests/live_postgres_all_schema_export.rs
+++ b/crates/dbx-core/tests/live_postgres_all_schema_export.rs
@@ -194,6 +194,7 @@ async fn live_postgres_all_schema_export_restores_one_sql_file() {
part_cooldown_ms: 0,
skip_relational_constraints: false,
failure_log_enabled: true,
+ encoding: Default::default(),
},
&export_path,
CancellationToken::new(),
@@ -341,6 +342,7 @@ async fn live_postgres_all_schema_export_restores_one_sql_file() {
part_cooldown_ms: 0,
skip_relational_constraints: false,
failure_log_enabled: true,
+ encoding: Default::default(),
},
&path,
CancellationToken::new(),
diff --git a/crates/dbx-core/tests/live_postgres_export_deferred_foreign_keys.rs b/crates/dbx-core/tests/live_postgres_export_deferred_foreign_keys.rs
index 77b633247b..dfbe981241 100644
--- a/crates/dbx-core/tests/live_postgres_export_deferred_foreign_keys.rs
+++ b/crates/dbx-core/tests/live_postgres_export_deferred_foreign_keys.rs
@@ -189,6 +189,7 @@ async fn live_postgres_export_with_foreign_key_cycle_restores_into_empty_databas
part_cooldown_ms: 0,
skip_relational_constraints: false,
failure_log_enabled: true,
+ encoding: Default::default(),
},
&export_path,
tokio_util::sync::CancellationToken::new(),
diff --git a/crates/dbx-core/tests/live_postgres_export_function_order.rs b/crates/dbx-core/tests/live_postgres_export_function_order.rs
index 8a49fed597..291d180991 100644
--- a/crates/dbx-core/tests/live_postgres_export_function_order.rs
+++ b/crates/dbx-core/tests/live_postgres_export_function_order.rs
@@ -154,6 +154,7 @@ async fn live_postgres_export_restores_tables_that_depend_on_functions() {
database: target_database.clone(),
schema: Some("public".to_string()),
file_path: export_path.display().to_string(),
+ encoding: Default::default(),
continue_on_error: false,
selected_tables: None,
part_cooldown_ms: 0,
diff --git a/crates/dbx-core/tests/live_sqlserver_completion.rs b/crates/dbx-core/tests/live_sqlserver_completion.rs
index b8973b4867..204bd6452a 100644
--- a/crates/dbx-core/tests/live_sqlserver_completion.rs
+++ b/crates/dbx-core/tests/live_sqlserver_completion.rs
@@ -1322,6 +1322,7 @@ async fn live_sqlserver_sql_file_import_executes_go_batches() {
part_cooldown_ms: 0,
skip_relational_constraints: false,
failure_log_enabled: true,
+ encoding: Default::default(),
};
let done_seen = AtomicBool::new(false);
diff --git a/crates/dbx-core/tests/live_sqlserver_split_zip_e2e.rs b/crates/dbx-core/tests/live_sqlserver_split_zip_e2e.rs
index 9d94b2638a..270f138f3d 100644
--- a/crates/dbx-core/tests/live_sqlserver_split_zip_e2e.rs
+++ b/crates/dbx-core/tests/live_sqlserver_split_zip_e2e.rs
@@ -228,6 +228,7 @@ async fn manual_e2e_sqlserver_default_split_zip_export_then_import_skip_relation
// before the import and `WITH NOCHECK CHECK CONSTRAINT ALL` after.
skip_relational_constraints: true,
failure_log_enabled: true,
+ encoding: Default::default(),
};
let file_path_refs: Vec<&std::path::Path> = extracted_paths.iter().map(|path| path.as_path()).collect();
diff --git a/crates/dbx-sql-core/src/sql.rs b/crates/dbx-sql-core/src/sql.rs
index 608f2d6af7..d9fbe97eea 100644
--- a/crates/dbx-sql-core/src/sql.rs
+++ b/crates/dbx-sql-core/src/sql.rs
@@ -87,6 +87,33 @@ pub struct SqlFileRequest {
/// logging enabled, so the serde default is `true`.
#[serde(default = "sql_file_failure_log_enabled_by_default")]
pub failure_log_enabled: bool,
+ /// Text encoding used to decode SQL files. `auto` preserves the historical
+ /// BOM/UTF-8/GBK detection behavior.
+ #[serde(default)]
+ pub encoding: SqlFileEncoding,
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
+#[serde(rename_all = "camelCase")]
+pub enum SqlFileEncoding {
+ #[default]
+ Auto,
+ Utf8,
+ Gbk,
+ Utf16Le,
+ Utf16Be,
+}
+
+impl SqlFileEncoding {
+ pub fn to_encoding(self) -> Option<&'static encoding_rs::Encoding> {
+ match self {
+ Self::Auto => None,
+ Self::Utf8 => Some(encoding_rs::UTF_8),
+ Self::Gbk => Some(encoding_rs::GBK),
+ Self::Utf16Le => Some(encoding_rs::UTF_16LE),
+ Self::Utf16Be => Some(encoding_rs::UTF_16BE),
+ }
+ }
}
fn sql_file_failure_log_enabled_by_default() -> bool {
diff --git a/crates/dbx-web/src/main.rs b/crates/dbx-web/src/main.rs
index 8c4518054b..dff2f34897 100644
--- a/crates/dbx-web/src/main.rs
+++ b/crates/dbx-web/src/main.rs
@@ -1421,6 +1421,7 @@ async fn serve() -> Result<(), String> {
.layer(DefaultBodyLimit::max(routes::sql_file::sql_file_upload_hard_cap_bytes())),
)
.route("/sql-file/execute", post(routes::sql_file::execute_sql_file))
+ .route("/sql-file/preview/refresh", post(routes::sql_file::refresh_sql_file_preview))
.route("/sql-file/preview/release", post(routes::sql_file::release_sql_file_preview))
.route("/sql-file/tables", post(routes::sql_file::inspect_sql_file_tables))
.route("/sql-file/progress/{executionId}", get(routes::sql_file::sql_file_progress))
diff --git a/crates/dbx-web/src/routes/sql_file.rs b/crates/dbx-web/src/routes/sql_file.rs
index a235f1c441..d5c3f68dc5 100644
--- a/crates/dbx-web/src/routes/sql_file.rs
+++ b/crates/dbx-web/src/routes/sql_file.rs
@@ -9,7 +9,7 @@ use axum::http::{header, HeaderMap, StatusCode};
use axum::response::sse::{Event, Sse};
use axum::response::Response;
use axum::Json;
-use dbx_core::sql::{self, SqlFileProgress, SqlFileRequest, SqlFileStatus};
+use dbx_core::sql::{SqlFileEncoding, SqlFileProgress, SqlFileRequest, SqlFileStatus};
use dbx_core::sql_file_import::{
execute_sql_file_paths, execute_sql_file_zip_package_paths, export_sql_file_failure_log as derive_failure_log_file,
prune_sql_file_failure_logs, sql_file_error_progress, sql_file_failure_log_text as derive_failure_log_text,
@@ -192,6 +192,8 @@ pub struct CancelSqlFileRequest {
#[serde(rename_all = "camelCase")]
pub struct InspectSqlFileTablesRequest {
pub file_path: String,
+ #[serde(default)]
+ pub encoding: SqlFileEncoding,
}
pub async fn inspect_sql_file_tables(
@@ -199,11 +201,38 @@ pub async fn inspect_sql_file_tables(
Json(body): Json<InspectSqlFileTablesRequest>,
) -> Result<Json<Vec<dbx_core::sql_file_import::SqlFileTable>>, AppError> {
let path = validated_uploaded_sql_path(&state.data_dir, &body.file_path)?;
- Ok(Json(dbx_core::sql_file_import::inspect_sql_file_tables(&path).await.map_err(AppError::from)?))
+ Ok(Json(
+ dbx_core::sql_file_import::inspect_sql_file_tables_with_encoding(&path, body.encoding)
+ .await
+ .map_err(AppError::from)?,
+ ))
+}
+
+#[derive(Deserialize, Default)]
+pub struct SqlFilePreviewOptions {
+ #[serde(default)]
+ pub encoding: SqlFileEncoding,
+}
+
+pub async fn refresh_sql_file_preview(
+ State(state): State<Arc<WebState>>,
+ Json(body): Json<InspectSqlFileTablesRequest>,
+) -> Result<Json<serde_json::Value>, AppError> {
+ let path = validated_uploaded_sql_path(&state.data_dir, &body.file_path)?;
+ let content = dbx_core::sql_file_import::read_sql_file_preview_with_encoding(&path, 1_000_000, body.encoding)
+ .await
+ .map_err(AppError::from)?;
+ let analysis = dbx_core::sql_file_import::mysql_like_sql_file_bootstrap_analysis(&content);
+ Ok(Json(serde_json::json!({
+ "preview": content.chars().take(20_000).collect::<String>(),
+ "canExecuteWithoutSelectedDatabase": analysis.can_execute_without_selected_database,
+ "establishesDatabaseContext": analysis.establishes_database_context,
+ })))
}
pub async fn preview_sql_file(
State(state): State<Arc<WebState>>,
+ Query(options): Query<SqlFilePreviewOptions>,
mut multipart: Multipart,
) -> Result<Json<serde_json::Value>, AppError> {
let tmp_dir = state.data_dir.join("tmp").join("sql_file");
@@ -221,7 +250,9 @@ pub async fn preview_sql_file(
let file_path = safe_uploaded_sql_path(&tmp_dir, &file_name)?;
std::fs::write(&file_path, &data).map_err(|e| AppError::from(e.to_string()))?;
- return preview_uploaded_sql_file(&file_path, &file_name, data.len() as u64).await.map(Json);
+ return preview_uploaded_sql_file_with_encoding(&file_path, &file_name, data.len() as u64, options.encoding)
+ .await
+ .map(Json);
}
Err(AppError::from("No file uploaded".to_string()))
@@ -231,6 +262,15 @@ async fn preview_uploaded_sql_file(
file_path: &Path,
file_name: &str,
size_bytes: u64,
+) -> Result<serde_json::Value, AppError> {
+ preview_uploaded_sql_file_with_encoding(file_path, file_name, size_bytes, SqlFileEncoding::Auto).await
+}
+
+async fn preview_uploaded_sql_file_with_encoding(
+ file_path: &Path,
+ file_name: &str,
+ size_bytes: u64,
+ encoding: SqlFileEncoding,
) -> Result<serde_json::Value, AppError> {
let tmp_dir = file_path.parent().ok_or_else(|| AppError::from("Invalid SQL file path".to_string()))?;
@@ -246,8 +286,10 @@ async fn preview_uploaded_sql_file(
.into_iter()
.map(|path| path.to_string_lossy().to_string())
.collect::<Vec<_>>();
- let content = sql::decode_sql_file_bytes(&std::fs::read(&paths[0]).map_err(|e| AppError::from(e.to_string()))?)
- .map_err(AppError::from)?;
+ let content =
+ dbx_core::sql_file_import::read_sql_file_preview_with_encoding(Path::new(&paths[0]), 1_000_000, encoding)
+ .await
+ .map_err(AppError::from)?;
let preview: String = content.chars().take(20_000).collect();
let bootstrap_analysis = dbx_core::sql_file_import::mysql_like_sql_file_bootstrap_analysis(&content);
return Ok(serde_json::json!({
@@ -262,8 +304,9 @@ async fn preview_uploaded_sql_file(
}));
}
- let content =
- dbx_core::sql_file_import::read_sql_file_preview(file_path, 1_000_000).await.map_err(AppError::from)?;
+ let content = dbx_core::sql_file_import::read_sql_file_preview_with_encoding(file_path, 1_000_000, encoding)
+ .await
+ .map_err(AppError::from)?;
let preview: String = content.chars().take(20_000).collect();
let bootstrap_analysis = dbx_core::sql_file_import::mysql_like_sql_file_bootstrap_analysis(&content);
diff --git a/src-tauri/src/commands/sql_file.rs b/src-tauri/src/commands/sql_file.rs
index 6c75b456c5..7cde133f62 100644
--- a/src-tauri/src/commands/sql_file.rs
+++ b/src-tauri/src/commands/sql_file.rs
@@ -11,7 +11,7 @@ use tokio_util::sync::CancellationToken;
use crate::commands::connection::{ensure_connection_writable, AppState};
use dbx_core::sql_file_import::{
execute_sql_file_paths, execute_sql_file_zip_package_paths, export_sql_file_failure_log as derive_failure_log_file,
- mysql_like_sql_file_bootstrap_analysis, prune_sql_file_failure_logs, read_sql_file_preview,
+ mysql_like_sql_file_bootstrap_analysis, prune_sql_file_failure_logs, read_sql_file_preview_with_encoding,
sql_file_failure_log_text as derive_failure_log_text, sql_file_progress, start_sql_file_failure_log,
SqlFileFailureLogDeriveError, SqlFileFailureLogFileInfo, SqlFileFailureLogFormat, SqlFileFailureLogHandle,
SqlFileFailureLogInfo, SqlFileFailureLogKey, SqlFileFailureLogMeta, SqlFileFailureLogRegistry,
@@ -19,7 +19,7 @@ use dbx_core::sql_file_import::{
SQL_FILE_FAILURE_LOG_DIR,
};
-pub use dbx_core::sql::{SqlFilePreview, SqlFileRequest, SqlFileStatus};
+pub use dbx_core::sql::{SqlFileEncoding, SqlFilePreview, SqlFileRequest, SqlFileStatus};
static SQL_FILE_EXECUTIONS: OnceLock<RwLock<HashMap<String, CancellationToken>>> = OnceLock::new();
@@ -50,12 +50,17 @@ struct SqlFileSummary {
#[tauri::command]
pub async fn inspect_sql_file_tables(
file_path: String,
+ encoding: Option<SqlFileEncoding>,
) -> Result<Vec<dbx_core::sql_file_import::SqlFileTable>, String> {
- dbx_core::sql_file_import::inspect_sql_file_tables(std::path::Path::new(&file_path)).await
+ dbx_core::sql_file_import::inspect_sql_file_tables_with_encoding(
+ std::path::Path::new(&file_path),
+ encoding.unwrap_or_default(),
+ )
+ .await
}
#[tauri::command]
-pub async fn preview_sql_file(file_path: String) -> Result<SqlFilePreview, String> {
+pub async fn preview_sql_file(file_path: String, encoding: Option<SqlFileEncoding>) -> Result<SqlFilePreview, String> {
let path = PathBuf::from(&file_path);
let metadata = tokio::fs::metadata(&path).await.map_err(|e| e.to_string())?;
if path
@@ -83,7 +88,12 @@ pub async fn preview_sql_file(file_path: String) -> Result<SqlFilePreview, Strin
})
.await
.map_err(|error| format!("Failed to extract SQL ZIP package: {error}"))??;
- let prefix = read_sql_file_preview(PathBuf::from(&extracted_paths[0]).as_path(), 1_000_000).await?;
+ let prefix = read_sql_file_preview_with_encoding(
+ PathBuf::from(&extracted_paths[0]).as_path(),
+ 1_000_000,
+ encoding.unwrap_or_default(),
+ )
+ .await?;
let bootstrap_analysis = mysql_like_sql_file_bootstrap_analysis(&prefix);
return Ok(SqlFilePreview {
file_name: path.file_name().and_then(|name| name.to_str()).unwrap_or("package.zip").to_string(),
@@ -96,7 +106,7 @@ pub async fn preview_sql_file(file_path: String) -> Result<SqlFilePreview, Strin
package_part_count: Some(package.part_names.len()),
});
}
- let prefix = read_sql_file_preview(&path, 1_000_000).await?;
+ let prefix = read_sql_file_preview_with_encoding(&path, 1_000_000, encoding.unwrap_or_default()).await?;
let bootstrap_analysis = mysql_like_sql_file_bootstrap_analysis(&prefix);
let preview = prefix.chars().take(20_000).collect();