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。

3.4 KB
Raw Blame
import * as api from "@/lib/backend/api";
import { mongoCommandRangeAtCursor, statementRangeAtCursor } from "@/lib/sql/sqlStatementRanges";
import type { MongoQueryLanguage } from "@/lib/mongo/mongoSql";
import type { DatabaseType } from "@/types/database";

export type ExecuteMode = "all" | "current";

export interface SqlExecutionSnapshot {
  fullSql: string;
  selectedSql: string;
  cursorPos: number;
  selectionFrom: number;
  selectionTo: number;
  editorViewportRequestId?: number;
}

export type SqlExecutionOverride = string | SqlExecutionSnapshot;

export type SqlExecutionTargetKind = "cursor" | "all";

/**
 * A candidate execution target surfaced by the execution target picker.
 * `from`/`to` are offsets into the full document so the editor can highlight
 * the corresponding range as a preview while the picker is open.
 */
export interface SqlExecutionCandidate {
  kind: SqlExecutionTargetKind;
  supportedKinds: SqlExecutionTargetKind[];
  label: string;
  sql: string;
  from: number;
  to: number;
}

export interface SqlExecutionChoiceRequest {
  fullSql: string;
  selectedSql: string;
  cursorPos: number;
  candidates: SqlExecutionCandidate[];
}

export function isSqlExecutionSnapshot(value: SqlExecutionOverride | undefined): value is SqlExecutionSnapshot {
  return typeof value === "object" && value !== null && typeof value.fullSql === "string" && typeof value.selectedSql === "string" && typeof value.cursorPos === "number" && typeof value.selectionFrom === "number" && typeof value.selectionTo === "number";
}

export function executionCandidateForMode(candidates: SqlExecutionCandidate[], mode: ExecuteMode, options: { executeAllOnBlankLine?: boolean } = {}): SqlExecutionCandidate | null {
  const targetKind: SqlExecutionTargetKind = mode === "current" ? "cursor" : "all";
  const candidate = candidates.find((item) => item.supportedKinds.includes(targetKind));
  if (candidate || mode !== "current" || !options.executeAllOnBlankLine) return candidate ?? null;
  return candidates.find((item) => item.supportedKinds.includes("all")) ?? null;
}

export function resolveExecutableSql(fullSql: string, selectedSql: string, options?: { mode?: ExecuteMode; cursorPos?: number }): string {
  const trimmedSelection = selectedSql.trim();
  if (trimmedSelection) return trimmedSelection;

  if (options?.mode === "current" && options.cursorPos !== undefined) {
    return fullSql;
  }

  return fullSql;
}

export async function resolveExecutableSqlWithBackend(fullSql: string, selectedSql: string, options?: { mode?: ExecuteMode; cursorPos?: number; databaseType?: DatabaseType; mongoQueryLanguage?: MongoQueryLanguage }): Promise<string> {
  const trimmedSelection = selectedSql.trim();
  if (trimmedSelection) return trimmedSelection;

  if (options?.databaseType === "mongodb" && options.mongoQueryLanguage === "sql") {
    if (options.mode === "current" && options.cursorPos !== undefined) {
      return statementRangeAtCursor(fullSql, options.cursorPos)?.sql ?? fullSql;
    }
    return fullSql;
  }

  if (options?.databaseType === "mongodb") {
    if (options.mode === "current" && options.cursorPos !== undefined) {
      return mongoCommandRangeAtCursor(fullSql, options.cursorPos)?.sql ?? fullSql;
    }
    return fullSql;
  }

  if (options?.mode === "current" && options.cursorPos !== undefined) {
    return await api.findStatementAtCursor(fullSql, options.cursorPos, options.databaseType);
  }

  return fullSql;
}