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。

3.6 KB
Raw Blame
import type { ConnectionConfig, DatabaseType } from "@/types/database";
import { h2FilePathFromJdbcUrl } from "@/lib/database/h2Connection";
import { isKnownDatabaseType, isLocalFileDatabaseType } from "@/lib/database/databaseDriverManifest";

/**
 * Whether the database type is one that (potentially) backs onto a local file.
 * This is a type-level check only — it does not verify that a valid path is
 * configured. Use this to decide whether to show file-path UI; use
 * `isLocalFileDb` / `connectionFilePath` to decide whether a reveal action is
 * available.
 */
export function isLocalFileTypeDb(dbType: DatabaseType | string): boolean {
  return isKnownDatabaseType(dbType) && isLocalFileDatabaseType(dbType);
}

/**
 * Extract the on-disk file path of a connection if (and only if) the
 * connection backs onto a single local file.
 *
 * - SQLite / DuckDB / Access: `host` holds the file path.
 * - H2 file mode: path is parsed out of the JDBC URL via `h2FilePathFromJdbcUrl`.
 *   H2 stores its data in `<base>.mv.db` (default) or `<base>.h2.db`; we hand
 *   back the base path the user actually configured so the Tauri side can try
 *   reasonable variants when the literal path isn't a file.
 *
 * Returns `null` for in-memory (`:memory:`), empty paths, H2 server/mem mode,
 * and any DB type that isn't local-file.
 */
/**
 * Whether `host` carries an on-disk file path instead of a network hostname.
 *
 * SQLite, DuckDB and Access store their database file in `host`; whitespace there can
 * be part of the path, so submit normalization must leave the field byte-exact.
 */
export function connectionHostIsFilePath(dbType: DatabaseType): boolean {
  return dbType === "sqlite" || dbType === "duckdb" || dbType === "access";
}

export function connectionFilePath(config: Pick<ConnectionConfig, "db_type" | "host" | "connection_string">): string | null {
  const dbType = config.db_type;
  if (connectionHostIsFilePath(dbType)) {
    const host = (config.host ?? "").trim();
    if (!host || host === ":memory:") return null;
    return host;
  }
  if (dbType === "h2") {
    const fromUrl = h2FilePathFromJdbcUrl(config.connection_string);
    const trimmed = (fromUrl ?? "").trim();
    if (!trimmed) return null;
    return trimmed;
  }
  return null;
}

/**
 * Whether the connection has a meaningful local file path that can be revealed
 * in the OS file manager. True iff `connectionFilePath` returns non-null.
 */
export function isLocalFileDb(config: Pick<ConnectionConfig, "db_type" | "host" | "connection_string">): boolean {
  return connectionFilePath(config) !== null;
}

export function isMemorySqlitePath(path: string | undefined | null): boolean {
  return (path ?? "").trim().toLowerCase() === ":memory:";
}

export function sqliteBackupSourcePath(config: Pick<ConnectionConfig, "db_type" | "host">): string | null {
  if (config.db_type !== "sqlite") return null;
  const host = (config.host ?? "").trim();
  return host || null;
}

export function defaultSqliteBackupFileName(config: Pick<ConnectionConfig, "host" | "name">): string {
  const source = (config.host || config.name || "database").trim();
  const rawFileName = isMemorySqlitePath(source) ? "memory.db" : source.split(/[\\/]/).filter(Boolean).pop() || "database.db";
  const fileName = sanitizeFileName(rawFileName) || "database.db";
  const dotIndex = fileName.lastIndexOf(".");
  if (dotIndex <= 0) return `${fileName}.backup.db`;
  return `${fileName.slice(0, dotIndex)}.backup${fileName.slice(dotIndex)}`;
}

function sanitizeFileName(fileName: string): string {
  return fileName
    .replace(/[<>:"/\\|?*\p{Cc}]/gu, "_")
    .trim()
    .replace(/[. ]+$/g, "");
}