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。

7.9 KB
Raw Blame
import type { DatabaseType } from "@/types/database";
import { tokenizeSqlSemantic } from "@/lib/sql/semantic/tokens";

export interface SqlErrorLocation {
  line: number;
  column: number;
}

export interface SqlErrorRange {
  from: number;
  to: number;
}

function normalizeSqlForComparison(sql: string): string {
  return sql.trim().replace(/;\s*$/u, "").replace(/\s+/gu, " ").trim().toLowerCase();
}

function removeDbxPagination(sql: string): string {
  return sql.replace(/\s+(?:limit\s+\d+(?:\s+offset\s+\d+)?|offset\s+\d+\s+rows?(?:\s+fetch\s+(?:next|first)\s+\d+\s+rows?(?:\s+only)?)?|fetch\s+(?:first|next)\s+\d+\s+rows?\s+only)\s*;?\s*$/iu, "").trim();
}

/**
 * Pagination is appended to the SQL sent to the database, so an execution
 * error can refer to a SQL string that differs from the editor by only that
 * generated suffix. Keep error highlighting disabled for unrelated stale
 * errors while allowing this known DBX rewrite.
 */
export function sqlErrorSqlMatchesEditor(editorSql: string, executedSql: string): boolean {
  if (editorSql === executedSql) return true;
  const norm = (sql: string) => normalizeSqlForComparison(sql);
  if (norm(executedSql) === norm(editorSql)) return true;
  return norm(removeDbxPagination(executedSql)) === norm(removeDbxPagination(editorSql));
}

function toZeroBased(value: string | undefined): number | null {
  if (!value) return null;
  const parsed = Number.parseInt(value, 10);
  if (!Number.isFinite(parsed) || parsed < 1) return null;
  return parsed - 1;
}

export function parseSqlErrorLocation(message: string): SqlErrorLocation | null {
  const lineColumn = /\bline\s+(\d+)\s*[,:\s]\s*column\s+(\d+)\b/i.exec(message) ?? /\bline\s+(\d+)\b[\s\S]{0,80}?\bcol(?:umn)?\s+(\d+)\b/i.exec(message);
  if (lineColumn) {
    const line = toZeroBased(lineColumn[1]);
    const column = toZeroBased(lineColumn[2]);
    if (line != null && column != null) return { line, column };
  }

  const lines = message.split(/\r?\n/);
  for (let index = 0; index < lines.length; index++) {
    const lineMatch = /^LINE\s+(\d+):/i.exec(lines[index] ?? "");
    if (!lineMatch) continue;
    const caretLine = lines.slice(index + 1).find((line) => line.includes("^"));
    const line = toZeroBased(lineMatch[1]);
    const caretIndex = caretLine?.indexOf("^") ?? -1;
    if (line != null && caretIndex >= 0) return { line, column: caretIndex };
  }

  // MySQL commonly reports only "at line N" for syntax errors. With no
  // column information available, point at the beginning of that line.
  const lineOnly = /\bline\s+(\d+)\b/i.exec(message);
  if (lineOnly) {
    const line = toZeroBased(lineOnly[1]);
    if (line != null) return { line, column: 0 };
  }

  return null;
}

export function lineColumnToOffset(sql: string, location: SqlErrorLocation): number | null {
  const lines = sql.split(/\r?\n/);
  if (location.line < 0 || location.line >= lines.length) return null;

  let offset = 0;
  for (let index = 0; index < location.line; index++) {
    offset += lines[index].length + 1;
  }

  return Math.min(offset + location.column, offset + lines[location.line].length);
}

function oracleInvalidIdentifierRange(sql: string, message: string, position: number): SqlErrorRange | null {
  const identifierMatch = /\bORA-00904:\s*"((?:""|[^"])*)"\s*:\s*invalid identifier\b/i.exec(message);
  if (!identifierMatch?.[1]) return null;

  const reportedIdentifier = identifierMatch[1].replace(/""/g, '"');
  const identifierPattern = /"(?:""|[^"])*"|[\p{L}_][\p{L}\p{N}_$#]*/gu;
  let bestRange: SqlErrorRange | null = null;
  let bestDistance = Number.POSITIVE_INFINITY;

  for (const match of sql.matchAll(identifierPattern)) {
    const token = match[0];
    const tokenStart = match.index;
    const quoted = token.startsWith('"');
    const identifier = quoted ? token.slice(1, -1).replace(/""/g, '"') : token;
    if (quoted ? identifier !== reportedIdentifier : identifier.toUpperCase() !== reportedIdentifier.toUpperCase()) continue;

    const from = tokenStart + (quoted ? 1 : 0);
    const to = tokenStart + token.length - (quoted ? 1 : 0);
    const distance = position < from ? from - position : position > to ? position - to : 0;
    if (distance >= bestDistance) continue;
    bestDistance = distance;
    bestRange = { from, to };
  }

  return bestRange;
}

/**
 * A position a driver reported in its error text, without needing the SQL it
 * refers to. Engines that do not attach a typed position to their error
 * envelope (Oracle) still name one in the message.
 */
export type SqlErrorMessagePosition = { kind: "location"; location: SqlErrorLocation } | { kind: "offset"; offset: number };

const ORACLE_AGENT_POSITION_PATTERN = /\berror\s+occur(?:red)?\s+at\s+position\s*:\s*(\d+)\b/i;

/**
 * Parse the position carried by a driver error message.
 *
 * Textual locations (`line N, column M`, `LINE N:` with a `^` caret, and the
 * line-only form MySQL emits) win over the Oracle Agent's absolute offset, the
 * same precedence {@link sqlErrorDecorationRange} has always used.
 */
export function sqlErrorMessagePosition(message: string): SqlErrorMessagePosition | null {
  const location = parseSqlErrorLocation(message);
  if (location) return { kind: "location", location };

  // Oracle Agent reports a zero-based absolute offset into the statement text.
  const positionMatch = ORACLE_AGENT_POSITION_PATTERN.exec(message);
  if (!positionMatch?.[1]) return null;
  const offset = Number.parseInt(positionMatch[1], 10);
  if (!Number.isSafeInteger(offset) || offset < 0) return null;
  return { kind: "offset", offset };
}

function oracleMissingObjectRange(sql: string, offset: number): SqlErrorRange | null {
  const tokens = tokenizeSqlSemantic(sql, "oracle");
  const identifier = (index: number) => {
    const token = tokens[index];
    return token?.kind === "word" || (token?.kind === "quoted_identifier" && token.quote === '"' && token.closed === true);
  };
  let index = tokens.findIndex((token) => token.span.start <= offset && offset < token.span.end);
  if (index < 0) index = tokens.findIndex((token, candidate) => token.span.end === offset && identifier(candidate));
  if (tokens[index]?.text === "." && identifier(index - 1) && identifier(index + 1)) index -= 1;
  if (!identifier(index)) return null;
  // An Oracle object reference is a single name or `schema.table`; stop there
  // instead of walking a column access (`hr.emp.empno`) or a longer chain.
  let first = index;
  let last = index;
  if (tokens[first - 1]?.text === "." && identifier(first - 2)) {
    first -= 2;
  } else if (tokens[last + 1]?.text === "." && identifier(last + 2)) {
    last += 2;
  }
  return { from: tokens[first].span.start, to: tokens[last].span.end };
}

export function sqlErrorDecorationRange(sql: string, message: string, databaseType?: DatabaseType): SqlErrorRange | null {
  const position = sqlErrorMessagePosition(message);
  if (!position) return null;

  if (position.kind === "location") {
    const offset = lineColumnToOffset(sql, position.location);
    if (offset == null || offset >= sql.length) return null;
    return { from: offset, to: offset + 1 };
  }

  let { offset } = position;
  if (databaseType === "oracle") {
    let bytes = 0;
    let utf16 = 0;
    for (const character of sql) {
      if (bytes >= offset) break;
      const point = character.codePointAt(0)!;
      bytes += point <= 0x7f ? 1 : point <= 0x7ff ? 2 : point <= 0xffff ? 3 : 4;
      utf16 += character.length;
    }
    if (bytes !== offset) return null;
    offset = utf16;
  }
  // An Oracle Agent offset is a byte offset only for Oracle itself; other
  // engines that happen to emit ORA-00942 keep their own position units.
  if (databaseType === "oracle" && /\bORA-00942\b/i.test(message)) {
    const object = oracleMissingObjectRange(sql, offset);
    if (object) return object;
  }
  if (offset >= sql.length) return null;
  // For qualified invalid identifiers the offset can point later in the
  // selector, so prefer the named token.
  return oracleInvalidIdentifierRange(sql, message, offset) ?? { from: offset, to: offset + 1 };
}