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。

1.6 KB
Raw Blame
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
import { ensureSqlExtension, stripSqlExtension } from "@/lib/savedSql/savedSqlFileName";

export function savedSqlExportFileName(name: string): string {
  return (
    ensureSqlExtension(name)
      .replace(/[<>:"/\\|?*\p{Cc}]/gu, "_")
      .trim() || "untitled.sql"
  );
}

export function uniqueSavedSqlExportFileName(name: string, takenNames: Set<string>): string {
  const normalized = savedSqlExportFileName(name);
  const occupied = new Set([...takenNames].map((taken) => taken.toLocaleLowerCase()));
  const base = stripSqlExtension(normalized);
  let candidate = normalized;
  let counter = 2;
  while (occupied.has(candidate.toLocaleLowerCase())) {
    candidate = `${base} (${counter++}).sql`;
  }
  takenNames.add(candidate);
  return candidate;
}

export async function exportSavedSqlFileContent(sql: string, fileName: string): Promise<"saved" | "cancelled"> {
  const defaultFileName = savedSqlExportFileName(fileName);
  if (isTauriRuntime()) {
    const { save } = await import("@tauri-apps/plugin-dialog");
    const { writeTextFile } = await import("@tauri-apps/plugin-fs");
    const path = await save({
      defaultPath: defaultFileName,
      filters: [{ name: "SQL", extensions: ["sql"] }],
    });
    if (!path) return "cancelled";
    await writeTextFile(path, sql);
    return "saved";
  }

  const blob = new Blob([sql], { type: "text/plain;charset=utf-8" });
  const url = URL.createObjectURL(blob);
  const anchor = document.createElement("a");
  anchor.href = url;
  anchor.download = defaultFileName;
  anchor.click();
  URL.revokeObjectURL(url);
  return "saved";
}