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.8 KB
Raw Blame
import type { TreeNode } from "@/types/database";
import type { SqlCompletionTable } from "@/lib/sql/sqlCompletion";
import { isTdengineStableTableType } from "@/lib/table/tableEditing";

const TABLE_NODE_TYPES = new Set(["table", "view", "materialized_view"]);

function completionTableDetail(comment: string | null | undefined): string | undefined {
  const normalized = comment?.trim();
  return normalized ? `→ ${normalized}` : undefined;
}

export function completionSchemasFromTree(nodes: readonly TreeNode[], connectionId: string, database: string): string[] {
  const seen = new Set<string>();
  const schemas: string[] = [];
  visitTree(nodes, (node) => {
    if (node.connectionId !== connectionId || node.database !== database || node.type !== "schema" || !node.schema) return;
    const key = node.schema.toLowerCase();
    if (seen.has(key)) return;
    seen.add(key);
    schemas.push(node.schema);
  });
  return schemas;
}

export function completionTablesFromTree(nodes: readonly TreeNode[], connectionId: string, database: string, schema?: string, catalog?: string): SqlCompletionTable[] {
  const preferredSchema = schema?.toLowerCase();
  const preferredCatalog = catalog?.toLowerCase();
  const tables: SqlCompletionTable[] = [];
  visitTree(nodes, (node) => {
    if (node.connectionId !== connectionId || node.database !== database || !TABLE_NODE_TYPES.has(node.type)) return;
    // External catalogs can contain databases and tables with the same names as
    // the internal catalog. Keep the two metadata scopes isolated.
    if ((node.catalog?.toLowerCase() ?? undefined) !== preferredCatalog) return;
    if (preferredSchema && node.schema?.toLowerCase() !== preferredSchema) return;
    const detail = completionTableDetail(node.comment);
    tables.push({
      name: node.tableName || node.label,
      catalog: node.catalog,
      schema: node.schema,
      type: node.type === "materialized_view" ? "materialized_view" : node.type === "view" ? "view" : "table",
      ...(detail ? { detail } : {}),
      ...(isTdengineStableTableType(node.tableType) ? { tableType: node.tableType } : {}),
    });
  });
  return dedupeCompletionTables(tables);
}

function visitTree(nodes: readonly TreeNode[], visit: (node: TreeNode) => void) {
  for (const node of nodes) {
    visit(node);
    if (node.children?.length) visitTree(node.children, visit);
    if (node.hiddenChildren?.length) visitTree(node.hiddenChildren, visit);
  }
}

function dedupeCompletionTables(tables: SqlCompletionTable[]): SqlCompletionTable[] {
  const seen = new Set<string>();
  const result: SqlCompletionTable[] = [];
  for (const table of tables) {
    const key = `${table.catalog ?? ""}.${table.schema ?? ""}.${table.name}`.toLowerCase();
    if (seen.has(key)) continue;
    seen.add(key);
    result.push(table);
  }
  return result;
}