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.3 KB
Raw Blame
import { describe, expect, it } from "vitest";
import { buildSqlCompletionItems, quoteSqlIdentifier } from "@/lib/sql/sqlCompletion";

describe("quoteSqlIdentifier oracle folding polarity", () => {
  it.each([
    ["ID", "ID"],
    ["V$SESSION", "V$SESSION"],
    ["ACCOUNT$SYS", "ACCOUNT$SYS"],
    ["Id", '"Id"'],
    ["id", '"id"'],
    ["SELECT", '"SELECT"'],
    ["_SYS", '"_SYS"'],
    ['we"ird', '"we""ird"'],
  ] as const)("quotes %s as %s for the oracle apply dialect", (identifier, expected) => {
    expect(quoteSqlIdentifier(identifier, "oracle")).toBe(expected);
  });

  it.each([
    ["upper", "Id", '"Id"'],
    ["mysql", "Id", "Id"],
    ["postgres", "Id", '"Id"'],
    ["postgres", "order", '"order"'],
    ["postgres", "article", "article"],
  ] as const)("keeps neighbour dialects unchanged: %s %s -> %s", (dialect, identifier, expected) => {
    expect(quoteSqlIdentifier(identifier, dialect)).toBe(expected);
  });
});

describe("oracle completion apply identifiers", () => {
  // Reproduction for #9526: the reported table stores mixed-case names quoted,
  // so bare inserts fold to the wrong (uppercase) name and fail with ORA-00904.
  const oracleInput = {
    databaseType: "oracle" as const,
    tables: [{ name: "BomTemplate", schema: "ZTZS_ERP2", type: "table" as const }],
    columnsByTable: new Map([
      [
        "BomTemplate",
        [
          { name: "Id", table: "BomTemplate" },
          { name: "NO", table: "BomTemplate" },
        ],
      ],
    ]),
    schemas: ["ZTZS_ERP2"],
  };

  it("quotes a mixed-case column suggestion after WHERE", () => {
    const sql = "SELECT * FROM BomTemplate WHERE Id";
    const items = buildSqlCompletionItems(sql, sql.length, oracleInput);
    const column = items.find((item) => item.label === "Id");
    expect(column?.apply).toBe('"Id"');
  });

  it("keeps an all-uppercase column suggestion bare", () => {
    const sql = "SELECT * FROM BomTemplate WHERE N";
    const items = buildSqlCompletionItems(sql, sql.length, oracleInput);
    const column = items.find((item) => item.label === "NO");
    expect(column?.apply).toBe("NO");
  });

  it("quotes a mixed-case routine suggestion", () => {
    const sql = "CALL GetBo";
    const items = buildSqlCompletionItems(sql, sql.length, {
      databaseType: "oracle" as const,
      tables: [],
      columnsByTable: new Map(),
      objects: [{ name: "GetBom", type: "procedure" }],
    });
    const routine = items.find((item) => item.label === "GetBom");
    expect(routine?.apply).toBe('"GetBom"()');
  });
});

describe("oracle completion with identifier quoting disabled (#9735)", () => {
  const sql = "SELECT t.c FROM abc t";
  const cursor = "SELECT t.c".length;
  const input = {
    databaseType: "oracle" as const,
    tables: [{ name: "abc", type: "table" as const }],
    columnsByTable: new Map([["abc", [{ name: "col1", table: "abc" }]]]),
  };
  const applyOf = (extra: { quoteIdentifiers?: boolean }) => buildSqlCompletionItems(sql, cursor, { ...input, ...extra }).find((item) => item.label === "col1")?.apply;

  it("keeps lowercase columns bare when quoting is disabled", () => {
    expect(applyOf({ quoteIdentifiers: false })).toBe("col1");
  });

  it("still quotes lowercase columns by default and when quoting is enabled", () => {
    expect(applyOf({})).toBe('"col1"');
    expect(applyOf({ quoteIdentifiers: true })).toBe('"col1"');
  });
});