feat(query-editor): enhance current statement framing

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-07-06 23:09:21 +08:00
1 parent 75fcd253cb
commit 18e5df6f5e
18 files changed
+480 -10

No files matched your search

@@ -0,0 +1,27 @@
import { describe, expect, it } from "vitest";
import { currentStatementFrameRangeTo, isWideSqlChar, visualSqlColumns } from "@/lib/sql/currentStatementFrame";
import type { SqlTextRange } from "@/lib/sql/sqlStatementRanges";
describe("currentStatementFrameRangeTo", () => {
it("includes a directly adjacent trailing semicolon in frame width calculations", () => {
const range: SqlTextRange = { from: 0, to: "SELECT 1".length, sql: "SELECT 1" };
expect(currentStatementFrameRangeTo(";", range)).toBe(range.to + 1);
});
it("does not extend the frame when the next character is not a semicolon", () => {
const range: SqlTextRange = { from: 0, to: "SELECT 1".length, sql: "SELECT 1" };
expect(currentStatementFrameRangeTo("\n", range)).toBe(range.to);
});
});
describe("visualSqlColumns", () => {
it("counts ASCII as one column, tabs as four, and CJK/fullwidth characters as two", () => {
expect(visualSqlColumns("A\t中B")).toBe(1 + 4 + 2 + 2);
});
it("recognizes common wide SQL text characters", () => {
expect(isWideSqlChar("中")).toBe(true);
expect(isWideSqlChar("A")).toBe(true);
expect(isWideSqlChar("A")).toBe(false);
});
});
@@ -1,6 +1,6 @@
import { Text } from "@codemirror/state";
import { describe, expect, it, vi } from "vitest";
import { executableStatementRangeCacheForDoc, executableStatementRangeStartingAt, type ExecutableStatementRangeParser } from "@/lib/sql/executableStatementRangeCache";
import { executableStatementRangeAtCursor, executableStatementRangeCacheForDoc, executableStatementRangeStartingAt, type ExecutableStatementRangeParser } from "@/lib/sql/executableStatementRangeCache";
describe("executableStatementRangeCacheForDoc", () => {
it("reuses parsed executable statement ranges for the same document and database type", () => {
@@ -27,6 +27,41 @@ describe("executableStatementRangeCacheForDoc", () => {
expect(executableStatementRangeStartingAt(cache, secondStatementLine.from)?.sql).toBe("SELECT *\nFROM menus AS mn\nLIMIT 100");
});
it("resolves the current statement from a cursor inside a continuation line", () => {
const doc = Text.of(["SELECT *", "FROM apis AS ap", "LIMIT 100;", "", "SELECT *", "FROM menus AS mn", "LIMIT 100;"]);
const cache = executableStatementRangeCacheForDoc(null, doc, "mysql");
const cursor = doc.toString().indexOf("menus");
expect(executableStatementRangeAtCursor(cache, cursor)?.sql).toBe("SELECT *\nFROM menus AS mn\nLIMIT 100");
});
it("keeps indentation and same-line semicolon gaps attached to the current statement", () => {
const doc = Text.of(["SELECT 1;", " SELECT 2;"]);
const cache = executableStatementRangeCacheForDoc(null, doc, "mysql");
const indentationCursor = doc.line(2).from + 2;
const semicolonGapCursor = doc.toString().indexOf(";") + 1;
expect(executableStatementRangeAtCursor(cache, indentationCursor)?.sql).toBe("SELECT 2");
expect(executableStatementRangeAtCursor(cache, semicolonGapCursor)?.sql).toBe("SELECT 1");
});
it("returns null for blank and pure comment cursor lines", () => {
const doc = Text.of(["SELECT 1;", "-- comment", "/* block comment */", "", "SELECT 2;"]);
const cache = executableStatementRangeCacheForDoc(null, doc, "mysql");
expect(executableStatementRangeAtCursor(cache, doc.line(2).from + 3)).toBeNull();
expect(executableStatementRangeAtCursor(cache, doc.line(3).from + 3)).toBeNull();
expect(executableStatementRangeAtCursor(cache, doc.line(4).from)).toBeNull();
});
it("resolves SQL after a leading block comment on the same line", () => {
const doc = Text.of(["/* comment */ SELECT 1;"]);
const cache = executableStatementRangeCacheForDoc(null, doc, "mysql");
expect(executableStatementRangeAtCursor(cache, doc.toString().indexOf("SELECT"))?.sql).toBe("SELECT 1");
expect(executableStatementRangeAtCursor(cache, doc.toString().indexOf("comment"))).toBeNull();
});
it("rebuilds the cache when the document instance changes", () => {
const firstDoc = Text.of(["SELECT 1;"]);
const secondDoc = Text.of(["SELECT 1;"]);
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { buildExecutionCandidates, executableStatementRanges, fullSqlRange, hasMultipleExecutionTargets, splitSqlStatementRanges, statementRangeAtCursor, supportsExecutionTargetPicker } from "@/lib/sql/sqlStatementRanges";
import { buildExecutionCandidates, currentExecutableStatementRange, executableStatementRanges, fullSqlRange, hasMultipleExecutionTargets, splitSqlStatementRanges, statementRangeAtCursor, supportsExecutionTargetPicker } from "@/lib/sql/sqlStatementRanges";
function indexOf(sql: string, needle: string, occurrence = 1): number {
let from = 0;
@@ -74,6 +74,17 @@ BEGIN
END;
SELECT 2;`;
const mysqlRoutineWithLoopsFixture = `CREATE PROCEDURE p_loop()
BEGIN
WHILE 1 = 0 DO
SELECT 'while; still body';
END WHILE;
REPEAT
SELECT 'repeat; still body';
UNTIL 1 = 1 END REPEAT;
END;
SELECT 2;`;
describe("splitSqlStatementRanges", () => {
it("splits multiple top-level statements", () => {
const sql = "SELECT 1;\nSELECT 2;\nSELECT 3;";
@@ -149,6 +160,16 @@ describe("splitSqlStatementRanges", () => {
expect(rangeSqlTexts(splitSqlStatementRanges(sql, "mysql"))).toEqual(["BEGIN", "INSERT INTO t VALUES (1)", "COMMIT"]);
});
it("treats SQL Server GO lines as batch delimiters", () => {
const sql = "SELECT 1\nGO\nSELECT 2;\n GO 2\nSELECT 3";
expect(rangeSqlTexts(splitSqlStatementRanges(sql, "sqlserver"))).toEqual(["SELECT 1", "SELECT 2", "SELECT 3"]);
});
it("does not treat GO inside strings or comments as a SQL Server batch delimiter", () => {
const sql = "SELECT 'GO'\n-- GO\nSELECT 2\nGO\nSELECT 3";
expect(rangeSqlTexts(splitSqlStatementRanges(sql, "sqlserver"))).toEqual(["SELECT 'GO'\n-- GO\nSELECT 2", "SELECT 3"]);
});
it("keeps Oracle PL/SQL blocks together and treats slash lines as delimiters", () => {
const ranges = splitSqlStatementRanges(oraclePlSqlFixture, "oracle");
expect(rangeSqlTexts(ranges)).toEqual([oraclePlSqlFixture.slice(0, oraclePlSqlFixture.indexOf("\n/")), "SELECT 1"]);
@@ -386,6 +407,18 @@ WHERE request_json LIKE '%"paperFlag":null%';`;
expect(range?.sql.trim()).toBe(mysqlRoutineFixture.slice(0, mysqlRoutineFixture.indexOf("\nSELECT 2;")).replace(/;$/, "").trim());
});
it("returns null on SQL Server GO batch delimiter lines", () => {
const sql = "SELECT 1\nGO\nSELECT 2";
expect(statementRangeAtCursor(sql, indexOf(sql, "GO"), "sqlserver")).toBeNull();
});
it("returns the current SQL Server batch around GO delimiters", () => {
const sql = "SELECT 1\nGO\nSELECT 2\nGO\nSELECT 3";
expect(statementRangeAtCursor(sql, indexOf(sql, "1"), "sqlserver")?.sql.trim()).toBe("SELECT 1");
expect(statementRangeAtCursor(sql, indexOf(sql, "2"), "sqlserver")?.sql.trim()).toBe("SELECT 2");
expect(statementRangeAtCursor(sql, indexOf(sql, "3"), "sqlserver")?.sql.trim()).toBe("SELECT 3");
});
it("returns the full Oracle PL/SQL block for cursors inside nested statements", () => {
const range = statementRangeAtCursor(oraclePlSqlFixture, indexOf(oraclePlSqlFixture, "ORDERS_10K", 2), "oracle");
expect(range?.sql.trim()).toBe(oraclePlSqlFixture.slice(0, oraclePlSqlFixture.indexOf("\n/")));
@@ -427,6 +460,42 @@ describe("executableStatementRanges", () => {
it("does not split executable MySQL routine ranges at inner statements", () => {
expect(rangeSqlTexts(executableStatementRanges(mysqlRoutineFixture, "mysql"))).toEqual([mysqlRoutineFixture.slice(0, mysqlRoutineFixture.indexOf("\nSELECT 2;")).replace(/;$/, "").trim(), "SELECT 2"]);
});
it("does not split executable MySQL routine ranges at WHILE and REPEAT endings", () => {
expect(rangeSqlTexts(executableStatementRanges(mysqlRoutineWithLoopsFixture, "mysql"))).toEqual([mysqlRoutineWithLoopsFixture.slice(0, mysqlRoutineWithLoopsFixture.indexOf("\nSELECT 2;")).replace(/;$/, "").trim(), "SELECT 2"]);
});
it("returns executable SQL Server batches without GO delimiter lines", () => {
expect(rangeSqlTexts(executableStatementRanges("SELECT 1\nGO\nSELECT 2;", "sqlserver"))).toEqual(["SELECT 1", "SELECT 2"]);
});
});
describe("currentExecutableStatementRange", () => {
it("uses the current SQL statement range for multi-line DDL", () => {
const sql = "ALTER TABLE `yb_course_order`\n ADD COLUMN `audit_status` tinyint(4) DEFAULT NULL\n COMMENT '审核状态:0-待审核,1-已通过,2-已拒绝',\n ADD COLUMN `close_reason` varchar(30) DEFAULT NULL\n COMMENT '关闭原因:timeout-超时关闭,cancel-取消关闭,refund-退款关闭';\nSELECT 1;";
expect(currentExecutableStatementRange(sql, indexOf(sql, "close_reason"), "mysql")?.sql.trim()).toBe(sql.slice(0, sql.indexOf(";\nSELECT")));
});
it("returns null on blank and pure comment lines", () => {
const sql = "SELECT 1;\n-- comment\n\nSELECT 2;";
expect(currentExecutableStatementRange(sql, indexOf(sql, "comment"), "mysql")).toBeNull();
expect(currentExecutableStatementRange(sql, sql.indexOf("\n\n") + 1, "mysql")).toBeNull();
});
it("uses the current Redis command line", () => {
const sql = "GET user:1\n DEL user:2\n# comment";
expect(currentExecutableStatementRange(sql, indexOf(sql, "DEL"), "redis")?.sql).toBe("DEL user:2");
expect(currentExecutableStatementRange(sql, indexOf(sql, "comment"), "redis")).toBeNull();
});
it("does not expose current statement framing for MongoDB", () => {
const sql = "db.users.find({})";
expect(currentExecutableStatementRange(sql, indexOf(sql, "users"), "mongodb")).toBeNull();
});
});
describe("fullSqlRange", () => {
@@ -526,6 +595,12 @@ describe("buildExecutionCandidates", () => {
const candidates = buildExecutionCandidates(sql, indexOf(sql, "COUNT", 2), "mysql");
expect(candidateSummaries(candidates)).toEqual(["cursor:select COUNT(1) FROM your_table;", "all:select COUNT(1) FROM your_table;\ndelimiter ;;\nselect COUNT(1) FROM your_table;\n\n;;\ndelimiter ;"]);
});
it("uses the current SQL Server batch for cursor candidates", () => {
const sql = "SELECT 1\nGO\nSELECT 2;";
const candidates = buildExecutionCandidates(sql, indexOf(sql, "2"), "sqlserver");
expect(candidateSummaries(candidates)).toEqual(["cursor:SELECT 2", "all:SELECT 1\nGO\nSELECT 2;"]);
});
});
describe("hasMultipleExecutionTargets", () => {
@@ -555,6 +630,10 @@ describe("hasMultipleExecutionTargets", () => {
expect(hasMultipleExecutionTargets(mysqlRoutineFixture, "mysql")).toBe(true);
});
it("counts SQL Server GO batches as multiple execution targets", () => {
expect(hasMultipleExecutionTargets("SELECT 1\nGO\nSELECT 2", "sqlserver")).toBe(true);
});
it("does not show multiple targets for MySQL DESC UPDATE joins", () => {
const sql = "desc update test_orders a\njoin test_users b\non a.id=b.id \nset a.name = '张三'\nwhere b.id > 10;";
expect(hasMultipleExecutionTargets(sql, "mysql")).toBe(false);