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。

2.6 KB
Raw Blame
import type { DatabaseType } from "@/types/database";
import * as api from "@/lib/backend/api";

const MYSQL_COMPATIBLE_PROFILES = new Set(["mysql", "mariadb", "tidb", "oceanbase", "doris", "starrocks", "custom_mysql"]);
const MYSQL_COMPATIBLE_TYPES = new Set<DatabaseType>(["mysql", "doris", "starrocks", "goldendb"]);

export interface CreateDatabaseSqlOptions {
  databaseType?: DatabaseType;
  driverProfile?: string | null;
  target?: "database" | "schema" | "catalog" | "namespace";
  parent?: string | null;
  name: string;
  charset?: string;
  collation?: string;
}

export function supportsCreateDatabaseCharset(databaseType?: DatabaseType, driverProfile?: string | null): boolean {
  return MYSQL_COMPATIBLE_TYPES.has(databaseType as DatabaseType) || (!!driverProfile && MYSQL_COMPATIBLE_PROFILES.has(driverProfile.toLowerCase()));
}

// GBase 8s creates a database whose codeset follows the creating session's DB_LOCALE; there is
// no charset clause in the SQL. The create dialog still offers a locale picker, and the chosen
// value is applied by the gbase8s agent opening the CREATE DATABASE session with that DB_LOCALE.
// Plain Informix is excluded until InformixAgent learns to honor the locale directive.
export function supportsCreateDatabaseLocale(databaseType?: DatabaseType): boolean {
  return databaseType === "gbase8s";
}

export function buildCreateDatabaseSql(options: CreateDatabaseSqlOptions): Promise<string> {
  return api.buildCreateDatabaseSql(options);
}

export function buildDuckDbAttachDatabaseSql(path: string, name: string): Promise<string> {
  return api.buildDuckDbAttachDatabaseSql(path, name);
}

export function buildSqliteAttachDatabaseSql(path: string, name: string): Promise<string> {
  return api.buildSqliteAttachDatabaseSql(path, name);
}

export function attachedDatabaseNameFromPath(path: string, fallbackName = "database"): string {
  const fileName = path.split(/[\\/]/).pop() ?? "";
  const withoutExtension = fileName.replace(/\.[^.\\/]+$/, "");
  const normalized = withoutExtension
    .trim()
    .replace(/[^\p{L}\p{N}_]+/gu, "_")
    .replace(/^_+|_+$/g, "");
  return normalized || fallbackName;
}

export function uniqueAttachedDatabaseName(baseName: string, existingNames: string[], reservedNames: string[] = []): string {
  const existing = new Set([...existingNames, ...reservedNames].map((name) => name.toLowerCase()));
  if (!existing.has(baseName.toLowerCase())) return baseName;
  for (let index = 2; index < Number.MAX_SAFE_INTEGER; index++) {
    const candidate = `${baseName}_${index}`;
    if (!existing.has(candidate.toLowerCase())) return candidate;
  }
  return `${baseName}_${Date.now()}`;
}