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。

14.0 KB
Raw Blame
import type { ConnectionConfig, DatabaseType, SidebarLayout } from "@/types/database";
import { uuid } from "@/lib/common/utils";
import { JDBCX_JDBC_DRIVER_CLASS } from "@/lib/database/jdbcxBuiltinDriver";
import { buildSidebarLayoutFromFolderPaths } from "@/lib/sidebar/sidebarLayout";
import { DEFAULT_QUERY_TIMEOUT_SECS } from "@/lib/connection/timeoutLimits";

type PartialConnection = Omit<ConnectionConfig, "id">;

type DbeaverImportPayload = {
  format: "dbeaver-import";
  dataSources: string;
  credentialsBase64?: string;
};

type DbeaverConnectionEntry = {
  id: string;
  name?: string;
  folder?: string;
  provider?: string;
  driver?: string;
  configuration?: Record<string, any>;
  [key: string]: any;
};

export type DbeaverImportResult = {
  connections: ConnectionConfig[];
  layout?: SidebarLayout;
};

type ConnectionProfile = {
  dbType: DatabaseType;
  profile: string;
  label: string;
  port: number;
  user: string;
};

const dbeaverKey = new Uint8Array([186, 187, 74, 159, 119, 74, 184, 83, 201, 108, 45, 101, 61, 254, 84, 74]);

const profileMap: Record<string, ConnectionProfile> = {
  mysql: { dbType: "mysql", profile: "mysql", label: "MySQL", port: 3306, user: "root" },
  mariadb: { dbType: "mysql", profile: "mariadb", label: "MariaDB", port: 3306, user: "root" },
  postgresql: { dbType: "postgres", profile: "postgres", label: "PostgreSQL", port: 5432, user: "postgres" },
  postgres: { dbType: "postgres", profile: "postgres", label: "PostgreSQL", port: 5432, user: "postgres" },
  cloudberry: { dbType: "postgres", profile: "cloudberry", label: "Apache Cloudberry", port: 5432, user: "postgres" },
  opentenbase: { dbType: "postgres", profile: "opentenbase", label: "OpenTenBase", port: 11000, user: "opentenbase" },
  sqlite: { dbType: "sqlite", profile: "sqlite", label: "SQLite", port: 0, user: "" },
  sqlserver: { dbType: "sqlserver", profile: "sqlserver", label: "SQL Server", port: 1433, user: "sa" },
  mssql: { dbType: "sqlserver", profile: "sqlserver", label: "SQL Server", port: 1433, user: "sa" },
  oracle: { dbType: "oracle", profile: "oracle", label: "Oracle", port: 1521, user: "system" },
  db2: { dbType: "db2", profile: "db2", label: "IBM DB2", port: 50000, user: "db2inst1" },
  clickhouse: { dbType: "clickhouse", profile: "clickhouse", label: "ClickHouse", port: 8123, user: "default" },
  duckdb: { dbType: "duckdb", profile: "duckdb", label: "DuckDB", port: 0, user: "" },
  mongodb: { dbType: "mongodb", profile: "mongodb", label: "MongoDB", port: 27017, user: "" },
  mongo: { dbType: "mongodb", profile: "mongodb", label: "MongoDB", port: 27017, user: "" },
  redshift: { dbType: "redshift", profile: "redshift", label: "Redshift", port: 5439, user: "awsuser" },
  elasticsearch: { dbType: "elasticsearch", profile: "elasticsearch", label: "Elasticsearch", port: 9200, user: "" },
  easysearch: { dbType: "easysearch", profile: "easysearch", label: "Easysearch", port: 9200, user: "" },
  doris: { dbType: "doris", profile: "doris", label: "Doris", port: 9030, user: "root" },
  starrocks: { dbType: "starrocks", profile: "starrocks", label: "StarRocks", port: 9030, user: "root" },
  dameng: { dbType: "dameng", profile: "dm", label: "达梦 Dameng", port: 5236, user: "SYSDBA" },
  dm: { dbType: "dameng", profile: "dm", label: "达梦 Dameng", port: 5236, user: "SYSDBA" },
  gaussdb: { dbType: "gaussdb", profile: "gaussdb", label: "GaussDB", port: 5432, user: "gaussdb" },
  kwdb: { dbType: "kwdb", profile: "kwdb", label: "KWDB", port: 26257, user: "root" },
  opengauss: { dbType: "gaussdb", profile: "opengauss", label: "openGauss", port: 5432, user: "gaussdb" },
  questdb: { dbType: "questdb", profile: "questdb", label: "QuestDB", port: 8812, user: "questdb" },
  influxdb: { dbType: "influxdb", profile: "influxdb", label: "InfluxDB", port: 8086, user: "" },
  jdbcx: { dbType: "jdbc", profile: "jdbcx", label: "JDBCX", port: 0, user: "" },
};

function normalizeKey(value: unknown) {
  return String(value || "")
    .toLowerCase()
    .replace(/[^a-z0-9]/g, "");
}

function getString(value: unknown) {
  return typeof value === "string" ? value.trim() : value == null ? "" : String(value).trim();
}

function getNumber(value: unknown) {
  const parsed = Number(value);
  return Number.isFinite(parsed) && parsed > 0 ? parsed : 0;
}

// DBeaver's own driver id (e.g. "db2", "mysql8") is a short internal registry
// key, not a Java class name; passing it straight to the JDBC agent as
// `jdbc_driver_class` makes it try `Class.forName("db2")` and fail with
// ClassNotFoundException. Only fall back to it when it is actually
// package-qualified, which is how DBeaver identifies genuinely custom drivers.
function looksLikeJdbcDriverClassName(value: string) {
  return /^[a-zA-Z_$][\w$]*(\.[a-zA-Z_$][\w$]*)+$/.test(value);
}

function firstNonEmptyString(...values: unknown[]) {
  for (const value of values) {
    if (typeof value !== "string") continue;
    const normalized = value.trim();
    if (normalized) return normalized;
  }
  return "";
}

function inferProfile(entry: DbeaverConnectionEntry): ConnectionProfile {
  if (/^jdbcx:/i.test(getString(entry.configuration?.url))) return profileMap.jdbcx;
  if (normalizeKey(entry.provider) === "opentenbase") return profileMap.opentenbase;
  const driverProfile = profileMap[normalizeKey(entry.driver)];
  if (driverProfile) return driverProfile;
  const candidates = [entry.provider, entry.driver, entry.configuration?.url, entry.name].map(normalizeKey).join(" ");
  for (const [needle, profile] of Object.entries(profileMap)) {
    if (candidates.includes(needle)) return profile;
  }
  return { dbType: "jdbc", profile: "jdbc", label: getString(entry.driver) || "JDBC", port: 0, user: "" };
}

function base64ToBytes(value: string) {
  const binary = atob(value);
  const bytes = new Uint8Array(binary.length);
  for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
  return bytes;
}

async function decryptCredentialsFile(base64?: string): Promise<Record<string, Record<string, Record<string, string>>>> {
  if (!base64) return {};
  const bytes = base64ToBytes(base64);
  if (bytes.length <= 16) return {};

  try {
    const iv = bytes.slice(0, 16);
    const encrypted = bytes.slice(16);
    const cryptoKey = await crypto.subtle.importKey("raw", dbeaverKey, { name: "AES-CBC" }, false, ["decrypt"]);
    const decrypted = await crypto.subtle.decrypt({ name: "AES-CBC", iv }, cryptoKey, encrypted);
    return JSON.parse(new TextDecoder().decode(decrypted));
  } catch {
    return {};
  }
}

function readCredentials(entry: DbeaverConnectionEntry, credentials: Record<string, Record<string, Record<string, string>>>) {
  const secure = credentials[entry.id]?.["#connection"] || {};
  const inline = entry.configuration?.credentials || {};
  return {
    username: getString(secure.user || secure.username || inline.user || inline.username || entry.configuration?.user),
    password: getString(secure.password || inline.password || entry.configuration?.password),
  };
}

function parseJdbcUrl(url: string, profile: ConnectionProfile) {
  const result: {
    host?: string;
    port?: number;
    database?: string;
    params?: string;
    username?: string;
    password?: string;
    oracleConnectionType?: "service_name" | "sid";
  } = {};
  const source = url.trim();
  if (!source) return result;

  const withoutJdbc = source.replace(/^jdbc:/i, "");
  const sqlServerMatch = withoutJdbc.match(/^sqlserver:\/\/([^;:/]+)(?::(\d+))?(?:;(.*))?/i);
  if (sqlServerMatch) {
    result.host = sqlServerMatch[1];
    result.port = getNumber(sqlServerMatch[2]);
    for (const part of (sqlServerMatch[3] || "").split(";")) {
      const [key, ...rest] = part.split("=");
      const value = rest.join("=");
      if (/^(databasename|database)$/i.test(key)) result.database = value;
      else if (/^user$/i.test(key)) result.username = value;
      else if (/^password$/i.test(key)) result.password = value;
    }
    return result;
  }

  const oracleServiceMatch = withoutJdbc.match(/^oracle:thin:@\/\/([^:/]+)(?::(\d+))?\/([^?]+)/i);
  if (oracleServiceMatch) {
    result.host = oracleServiceMatch[1];
    result.port = getNumber(oracleServiceMatch[2]);
    result.database = oracleServiceMatch[3];
    result.oracleConnectionType = "service_name";
    return result;
  }

  const oracleSidMatch = withoutJdbc.match(/^oracle:thin:@([^:/]+)(?::(\d+))?:([^?]+)/i);
  if (oracleSidMatch) {
    result.host = oracleSidMatch[1];
    result.port = getNumber(oracleSidMatch[2]);
    result.database = oracleSidMatch[3];
    result.oracleConnectionType = "sid";
    return result;
  }

  const sqliteMatch = withoutJdbc.match(/^sqlite:(.+)$/i);
  if (sqliteMatch) {
    result.host = sqliteMatch[1];
    return result;
  }

  try {
    const parsed = new URL(withoutJdbc);
    if (!parsed.hostname) return result;
    result.host = parsed.hostname;
    result.port = parsed.port ? Number(parsed.port) : profile.port;
    result.database = parsed.pathname.replace(/^\/+/, "").split("/")[0] || undefined;
    result.params = parsed.search.replace(/^\?/, "");
    result.username = decodeURIComponent(parsed.username || "");
    result.password = decodeURIComponent(parsed.password || "");
  } catch {
    return result;
  }

  return result;
}

function extractConnections(parsed: any): DbeaverConnectionEntry[] {
  const source = parsed?.connections || parsed?.dataSources || parsed?.datasources || parsed;
  if (!source || typeof source !== "object") return [];

  if (Array.isArray(source)) {
    return source.filter((entry) => entry && typeof entry === "object").map((entry) => ({ ...entry, id: getString(entry.id || entry.uuid || entry.name) }));
  }

  return Object.entries(source)
    .filter(([, entry]) => entry && typeof entry === "object")
    .map(([id, entry]) => ({ ...(entry as Record<string, any>), id: getString((entry as any).id || id) }));
}

function extractFolderPaths(parsed: any): string[] {
  const folders = parsed?.folders;
  if (!folders || typeof folders !== "object") return [];

  const entries = Array.isArray(folders) ? folders.map((folder) => [getString(folder?.name), folder] as const) : Object.entries(folders);

  return entries.flatMap(([name, folder]) => {
    if (!name || !folder || typeof folder !== "object") return [];
    const parent = getString((folder as Record<string, any>).parent);
    return [parent ? `${parent}/${name}` : name];
  });
}

function buildConnection(entry: DbeaverConnectionEntry, credentials: ReturnType<typeof readCredentials>): ConnectionConfig | null {
  const profile = inferProfile(entry);
  const config = entry.configuration || {};
  const url = getString(config.url);
  const parsedUrl = parseJdbcUrl(url, profile);
  const configuredPort = getNumber(config.port || config["host-port"] || parsedUrl.port) || profile.port;
  const configuredDatabase = firstNonEmptyString(config.database, config["database-name"], config.schema, parsedUrl.database);
  const host = getString(config.host || config["host-name"] || parsedUrl.host || (profile.dbType === "sqlite" ? configuredDatabase : "127.0.0.1"));
  const database = profile.dbType === "sqlite" ? "" : configuredDatabase;
  const name = getString(entry.name || database || host || profile.label);
  if (!entry.id || !name) return null;

  const partial: PartialConnection = {
    name,
    db_type: profile.dbType,
    driver_profile: profile.profile,
    driver_label: profile.label,
    url_params: getString(parsedUrl.params),
    host,
    port: configuredPort,
    username: credentials.username || getString(parsedUrl.username) || profile.user,
    password: credentials.password || getString(parsedUrl.password),
    database: database || undefined,
    color: getString(config.color || config["connection-color"]),
    transport_layers: [],
    connect_timeout_secs: 10,
    query_timeout_secs: DEFAULT_QUERY_TIMEOUT_SECS,
    ssl: false,
    oracle_connection_type: profile.dbType === "oracle" ? parsedUrl.oracleConnectionType || "service_name" : undefined,
    connection_string: profile.dbType === "jdbc" || profile.dbType === "mongodb" ? url || undefined : undefined,
    jdbc_driver_class: profile.dbType === "jdbc" ? getString(config["driver-class"] || (profile.profile === "jdbcx" ? JDBCX_JDBC_DRIVER_CLASS : looksLikeJdbcDriverClassName(getString(entry.driver)) ? entry.driver : undefined)) || undefined : undefined,
    jdbc_driver_paths: [],
  };

  return { ...partial, id: uuid() };
}

export function isDbeaverImportPayload(content: string) {
  try {
    const parsed = JSON.parse(content);
    return parsed?.format === "dbeaver-import";
  } catch {
    return false;
  }
}

export async function parseDbeaverImport(content: string): Promise<DbeaverImportResult> {
  const payload = JSON.parse(content) as DbeaverImportPayload;
  if (payload.format !== "dbeaver-import" || !payload.dataSources) {
    throw new Error("Invalid DBeaver import payload");
  }

  const dataSources = JSON.parse(payload.dataSources);
  const encryptedCredentials = await decryptCredentialsFile(payload.credentialsBase64);
  const configs: ConnectionConfig[] = [];
  const connectionFolderPaths = new Map<string, string>();
  const seen = new Set<string>();

  for (const entry of extractConnections(dataSources)) {
    const config = buildConnection(entry, readCredentials(entry, encryptedCredentials));
    if (!config) continue;
    const key = [config.name, config.db_type, config.host, config.port, config.database || ""].join("\u0000");
    if (seen.has(key)) continue;
    seen.add(key);
    configs.push(config);
    const folderPath = getString(entry.folder);
    if (folderPath) connectionFolderPaths.set(config.id, folderPath);
  }

  // DBeaver stores declared folders separately and connections reference full
  // slash-delimited paths. Missing ancestors are created during DBeaver load,
  // so mirror that behavior when producing DBX's nested sidebar layout.
  const layout = buildSidebarLayoutFromFolderPaths(
    configs.map((config) => config.id),
    extractFolderPaths(dataSources),
    connectionFolderPaths,
  );
  return { connections: configs, layout };
}

export async function parseDbeaverConnections(content: string): Promise<ConnectionConfig[]> {
  return (await parseDbeaverImport(content)).connections;
}