From a2f71a22d603ff7f03c3390540bdf136eadc1ec0 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:44:28 +0800 Subject: [PATCH] feat(database): SQL in MyBatis mapper XML The statements of + select id, nam from users where id = #{id} + + + +`; + +let view: EditorView | null = null; + +beforeEach(() => { + vi.useFakeTimers(); + published.clear(); + useProject.setState({ + project: { name: "p", root: "/p", trusted: true } as never, + settings: { + database: { resolutionScopes: [{ path: "src/main/resources/mapper", dataSource: "pg", schema: "" }] }, + } as never, + }); + const column = (name: string) => ({ + name, + dataType: "text", + nullable: true, + primaryKey: name === "id", + hasDefault: false, + }); + useDatabase.setState({ + loaded: true, + sources: [ + { id: "pg", name: "Local", driver: "postgres" }, + { id: "other", name: "Other", driver: "postgres" }, + ], + consoles: {}, + consoleSchemas: {}, + completion: { + pg: { + defaultSchema: "public", + tables: [{ schema: "public", name: "users", kind: "table", columns: [column("id"), column("name")] }], + foreignKeys: [], + }, + }, + }); +}); + +afterEach(() => { + view?.destroy(); + view = null; + vi.useRealTimers(); +}); + +function open(doc: string) { + view = new EditorView({ + parent: document.body, + state: EditorState.create({ doc, extensions: mapperExtension({ path: PATH, language: "xml" } as never) }), + }); + return view; +} + +function completionAt(state: EditorState, pos: number): Promise { + const sources = state.languageDataAt("openideaCompletion", pos); + return Promise.resolve(sources[0]!(new CompletionContext(state, pos, true))); +} + +describe("SQL in MyBatis mappers", () => { + it("reports unresolved names of the statements against the scope's data source", () => { + open(DOC); + vi.advanceTimersByTime(1000); + const messages = (published.get(PATH) ?? []).map((d) => d.message); + expect(messages.some((m) => /nam\b/.test(m))).toBe(true); + expect(messages.some((m) => /orders/.test(m))).toBe(true); + expect(messages.some((m) => /users|\bid\b/.test(m))).toBe(false); + }); + + it("documents tables in the statements and nothing in the XML around them", () => { + const v = open(DOC); + const hover = v.state.languageDataAt(CONTRIBUTED_HOVER, 0)[0]!; + expect(hover(v.state, DOC.indexOf("users") + 2)?.markdown).toMatch(/users/); + expect(hover(v.state, DOC.indexOf("namespace") + 2)).toBeNull(); + }); + + it("completes columns inside a statement only", async () => { + const doc = DOC.replace("select id, nam from users", "select n from users"); + const v = open(doc); + const result = await completionAt(v.state, doc.indexOf("select n") + "select n".length); + expect(result?.options.map((o) => o.label)).toContain("name"); + expect(await completionAt(v.state, doc.indexOf("namespace") + 4)).toBeNull(); + }); + + it("leaves other XML files alone", () => { + const v = open(""); + vi.advanceTimersByTime(1000); + expect(published.get(PATH) ?? []).toEqual([]); + expect(v.dom.querySelectorAll("[class*=ͼ]").length).toBe(0); + }); +}); diff --git a/src/features/database/mybatis.ts b/src/features/database/mybatis.ts new file mode 100644 index 0000000000..2f4be8991e --- /dev/null +++ b/src/features/database/mybatis.ts @@ -0,0 +1,123 @@ +/** + * SQL in MyBatis mapper XML: the statements of ` + select id, name from users + + and name = #{name} + and id in + #{i} + + and age > 18 + + + + select last_insert_id() + insert into users (name) values (#{name}) + + + + update users name = #{a}, where id = #{id} +`; + +describe("MyBatis mapper SQL", () => { + it("recognises mapper files", () => { + expect(isMapper(MAPPER)).toBe(true); + expect(isMapper("")).toBe(false); + }); + + it("keeps the statements' SQL at its offsets and blanks the rest", () => { + const { text } = mapperSql(MAPPER); + expect(text).toHaveLength(MAPPER.length); + expect(text.split("\n")).toHaveLength(MAPPER.split("\n").length); + const sql = text.replace(/\s+/g, " ").trim(); + expect(sql).toBe( + "select id, name from users where name = ? and id in ( ? ) and age > 18 ; " + + "insert into users (name) values (? ) ; update users set name = ? where age < 3 ; " + + "update users set name = ? where id = ? ;", + ); + const at = MAPPER.indexOf("users"); + expect(text.slice(at, at + 5)).toBe("users"); + }); + + it("knows which offsets are SQL", () => { + const sql = mapperSql(MAPPER); + expect(inMapperSql(sql, MAPPER.indexOf("from users") + 2)).toBe(true); + expect(inMapperSql(sql, MAPPER.indexOf("resultMap="))).toBe(false); + expect(inMapperSql(sql, MAPPER.indexOf('test="name'))).toBe(false); + expect(inMapperSql(sql, MAPPER.indexOf("last_insert_id"))).toBe(false); + expect(inMapperSql(sql, MAPPER.indexOf("from old"))).toBe(false); + }); +}); diff --git a/src/features/database/sql/mapper.ts b/src/features/database/sql/mapper.ts new file mode 100644 index 0000000000..47e14ac728 --- /dev/null +++ b/src/features/database/sql/mapper.ts @@ -0,0 +1,154 @@ +/** + * The SQL of a MyBatis mapper XML file, as one text the SQL tools read: everything but the + * SQL of `