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.9 KB
Raw Blame
import { parseConnectionUrl } from "@/lib/connection/connectionUrl";

function splitXuguDsnParts(value: string): string[] {
  const parts: string[] = [];
  let current = "";
  let inQuotes = false;

  for (let index = 0; index < value.length; index += 1) {
    const character = value[index];
    if (character === "'") {
      current += character;
      if (inQuotes && value[index + 1] === "'") {
        current += value[index + 1];
        index += 1;
      } else {
        inQuotes = !inQuotes;
      }
    } else if (character === ";" && !inQuotes) {
      parts.push(current);
      current = "";
    } else {
      current += character;
    }
  }

  parts.push(current);
  return parts;
}

function xuguDatabaseFromDsn(value: string): string | undefined {
  let database: string | undefined;
  for (const part of splitXuguDsnParts(value)) {
    const separator = part.indexOf("=");
    if (separator < 0 || part.slice(0, separator).trim().toLowerCase() !== "db") continue;

    let candidate = part.slice(separator + 1).trim();
    if (candidate.startsWith("'") && candidate.endsWith("'")) {
      candidate = candidate.slice(1, -1).replaceAll("''", "'");
    }
    database = candidate.trim() || undefined;
  }
  return database;
}

export function xuguDatabaseFromConnectionString(connectionString: string | undefined): string | undefined {
  const value = connectionString?.trim();
  if (!value) return undefined;

  if (/^(?:ip|ips)\s*=/i.test(value)) {
    return xuguDatabaseFromDsn(value);
  }

  try {
    const parsed = parseConnectionUrl(value, "xugu");
    return parsed.dbType === "xugu" ? parsed.database?.trim() || undefined : undefined;
  } catch {
    return undefined;
  }
}

export function hasXuguConnectionDatabase(database: string | undefined, connectionString: string | undefined): boolean {
  return !!database?.trim() || !!xuguDatabaseFromConnectionString(connectionString);
}