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.4 KB
Raw Blame
import type { SqlExportColumnSelection } from "@/lib/export/sqlInsertMode";

export function sqlExportColumnChoices(columns: readonly (string | undefined)[]): SqlExportColumnSelection[] {
  const occurrences = new Map<string, number>();
  return columns.flatMap((name, sourceIndex) => {
    if (!name) return [];
    const nameOccurrence = occurrences.get(name) ?? 0;
    occurrences.set(name, nameOccurrence + 1);
    return [{ sourceIndex, name, nameOccurrence }];
  });
}

export function resolveSqlExportColumnIndexes(columns: readonly (string | undefined)[], selectedColumns?: readonly SqlExportColumnSelection[]): number[] {
  if (selectedColumns === undefined) return columns.map((_, index) => index);
  if (!selectedColumns.length) throw new Error("Select at least one column for SQL export.");
  const indexesByName = new Map<string, number[]>();
  columns.forEach((name, index) => {
    if (!name) return;
    const indexes = indexesByName.get(name) ?? [];
    indexes.push(index);
    indexesByName.set(name, indexes);
  });
  const used = new Set<number>();
  return selectedColumns.map((column) => {
    const index = indexesByName.get(column.name)?.[column.nameOccurrence];
    if (index === undefined) throw new Error(`Selected SQL export column was not found: ${column.name}`);
    if (used.has(index)) throw new Error(`SQL export column was selected more than once: ${column.name}`);
    used.add(index);
    return index;
  });
}