refactor(desktop): organize lib modules by domain
This commit is contained in:
1 parent
90f0594e93
commit
7e7c4944e9
700 files changed
+1292
-1264
No files matched your search
@@ -0,0 +1,91 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildSqlCompletionItemsFromContext, getSqlCompletionContext, type SqlCompletionColumn, type SqlCompletionProviderInput } from "@/lib/sql/sqlCompletion";
|
||||
import { sqlCompletionContextFromSemantic, sqlSemanticLocalColumnsByTable } from "@/lib/sql/semantic/completion";
|
||||
import { buildSqlSemanticModel } from "@/lib/sql/semantic/model";
|
||||
import { sqlFixtureCursor } from "@/lib/sql/semantic/fixtures";
|
||||
import type { DatabaseType } from "@/types/database";
|
||||
|
||||
function mergeColumns(...maps: Array<Map<string, SqlCompletionColumn[]> | undefined>): Map<string, SqlCompletionColumn[]> {
|
||||
const merged = new Map<string, SqlCompletionColumn[]>();
|
||||
for (const map of maps) {
|
||||
for (const [key, columns] of map ?? []) merged.set(key, columns);
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
|
||||
function semanticCompletion(markedSql: string, input: Partial<SqlCompletionProviderInput> = {}, options: { databaseType?: DatabaseType; dialect?: "mysql" | "postgres" | "sqlserver" } = {}) {
|
||||
const { sql, cursor } = sqlFixtureCursor(markedSql);
|
||||
const model = buildSqlSemanticModel(sql, cursor, options);
|
||||
const context = sqlCompletionContextFromSemantic(model, getSqlCompletionContext(sql, cursor));
|
||||
const columnsByTable = mergeColumns(sqlSemanticLocalColumnsByTable(model), input.columnsByTable);
|
||||
const items = buildSqlCompletionItemsFromContext(context, {
|
||||
tables: input.tables ?? [],
|
||||
objects: input.objects ?? [],
|
||||
columnsByTable,
|
||||
foreignKeysByTable: input.foreignKeysByTable,
|
||||
schemas: input.schemas,
|
||||
translations: input.translations,
|
||||
snippets: input.snippets,
|
||||
dialect: options.dialect,
|
||||
databaseType: options.databaseType,
|
||||
keywordCase: input.keywordCase,
|
||||
autoAliasTables: input.autoAliasTables,
|
||||
});
|
||||
return { sql, cursor, model, context, items };
|
||||
}
|
||||
|
||||
describe("semantic SQL completion candidates", () => {
|
||||
it("keeps alias-qualified column completion scoped to one row source", () => {
|
||||
const columnsByTable = new Map<string, SqlCompletionColumn[]>([
|
||||
["users", ["id", "name", "email"].map((name) => ({ name, table: "users" }))],
|
||||
["orders", ["id", "total"].map((name) => ({ name, table: "orders" }))],
|
||||
]);
|
||||
|
||||
const { items } = semanticCompletion("SELECT * FROM users u JOIN orders o ON o.user_id = u.id WHERE u.|", { columnsByTable });
|
||||
|
||||
expect(items.filter((item) => item.type === "column").map((item) => item.label)).toEqual(["id", "name", "email"]);
|
||||
});
|
||||
|
||||
it("uses CTE projected columns without remote metadata", () => {
|
||||
const { items, context } = semanticCompletion("WITH recent_orders(id, total) AS (SELECT id, total FROM orders) SELECT * FROM recent_orders ro WHERE ro.|");
|
||||
|
||||
expect(context.exclusiveColumnSuggestions).toBe(true);
|
||||
expect(items.filter((item) => item.type === "column").map((item) => item.label)).toEqual(["id", "total"]);
|
||||
});
|
||||
|
||||
it("uses subquery projected columns without remote metadata", () => {
|
||||
const { items } = semanticCompletion("SELECT * FROM (SELECT id, name AS user_name FROM users) sq WHERE sq.|");
|
||||
|
||||
expect(items.filter((item) => item.type === "column").map((item) => item.label)).toEqual(["id", "user_name"]);
|
||||
});
|
||||
|
||||
it("expands alias star from only the qualified row source", () => {
|
||||
const columnsByTable = new Map<string, SqlCompletionColumn[]>([
|
||||
["users", ["id", "name"].map((name) => ({ name, table: "users" }))],
|
||||
["orders", ["id", "total"].map((name) => ({ name, table: "orders" }))],
|
||||
]);
|
||||
|
||||
const { context, items } = semanticCompletion("SELECT u.*| FROM users u JOIN orders o ON o.user_id = u.id", { columnsByTable });
|
||||
const star = items.find((item) => item.label === "* \u2192 columns");
|
||||
|
||||
expect(context.qualifier).toBe("u");
|
||||
expect(star?.apply).toBe("id, u.name");
|
||||
});
|
||||
|
||||
it("generates collision-free table aliases from semantic row sources", () => {
|
||||
const { items } = semanticCompletion("SELECT * FROM order_items oi JOIN ord|", {
|
||||
tables: [{ name: "order_items", type: "table" }],
|
||||
autoAliasTables: true,
|
||||
});
|
||||
|
||||
expect(items.find((item) => item.label === "order_items")?.apply).toBe("order_items AS oi2");
|
||||
});
|
||||
|
||||
it("preserves dialect-aware identifier quoting in apply text", () => {
|
||||
const columnsByTable = new Map<string, SqlCompletionColumn[]>([["Order Details", [{ name: "User Name", table: "Order Details" }]]]);
|
||||
|
||||
const { items } = semanticCompletion('SELECT od."User| FROM "Order Details" od', { columnsByTable }, { databaseType: "postgres", dialect: "postgres" });
|
||||
|
||||
expect(items.find((item) => item.label === "User Name")?.apply).toBe('"User Name"');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
# SQL Semantic Completion DBeaver References
|
||||
|
||||
This change uses the local DBeaver checkout at `/Users/skyler/VsCodeProjects/dbeaver` as the behavioral reference for semantic SQL completion.
|
||||
|
||||
Key files reviewed:
|
||||
|
||||
- `plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/syntax/SQLCompletionProcessor.java`
|
||||
- Selects semantic, legacy, or combined completion paths and handles asynchronous proposal jobs.
|
||||
- `plugins/org.jkiss.dbeaver.model.sql/src/org/jkiss/dbeaver/model/sql/semantics/completion/SQLQueryCompletionContext.java`
|
||||
- Builds completion proposals from syntax inspection, lexical scope, row-source context, and cursor offset.
|
||||
- `plugins/org.jkiss.dbeaver.model.sql/src/org/jkiss/dbeaver/model/sql/semantics/context/SQLQueryRowsSourceContext.java`
|
||||
- Tracks table sources, aliases, dynamic CTE sources, unresolved sources, and known-source collections.
|
||||
- `plugins/org.jkiss.dbeaver.model.sql/src/org/jkiss/dbeaver/model/sql/semantics/completion/SQLQueryCompletionAnalyzer.java`
|
||||
- Converts semantic completion items into editor proposals with replacement ranges, descriptions, images, and scoring.
|
||||
|
||||
DBX intentionally implements a smaller frontend semantic model first. The immediate goal is not DBeaver parser parity; it is to move cursor intent, row-source resolution, CTE/subquery handling, and fallback confidence into one reusable layer before routing completion, diagnostics, and navigation through it.
|
||||
|
||||
## Completion Assistant Field Audit
|
||||
|
||||
The semantic completion scopes added in this change fit the existing DBX completion assistant and item-builder fields:
|
||||
|
||||
- Table/schema/catalog lookup maps to `suggestTables`, `qualifier`, `qualifierParts`, `schemas`, and existing table metadata lookup methods.
|
||||
- Routine/package lookup maps to `suggestRoutines`, `exclusiveRoutineSuggestions`, `qualifier`, and existing completion object lookup methods.
|
||||
- Alias, CTE, subquery, INSERT target, UPDATE target, join, and star column lookup maps to `referencedTables`, `columns`, `insertTable`, `updateTarget`, `deleteTarget`, `onStar`, and `columnsByTable`.
|
||||
- Projection aliases map to `prioritizeSelectAliases` and `selectAliases`.
|
||||
- Fallback confidence maps to the existing legacy `getSqlCompletionContext()` path without requiring broad all-column scans.
|
||||
|
||||
No backend/API field gap was found for the current semantic scopes. The implementation therefore keeps the Java/Rust assistant protocol unchanged and limits new work to frontend semantic context, conversion, and tests.
|
||||
@@ -0,0 +1,156 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { sqlSemanticCompletionScope, sqlSemanticLocalColumnsByTable, sqlSemanticProjectionAliasColumns } from "@/lib/sql/semantic/completion";
|
||||
import { SQL_SEMANTIC_BASELINE_FIXTURES, sqlFixtureCursor } from "@/lib/sql/semantic/fixtures";
|
||||
import { buildSqlSemanticModel } from "@/lib/sql/semantic/model";
|
||||
|
||||
describe("sqlSemanticModel baseline fixtures", () => {
|
||||
for (const fixture of SQL_SEMANTIC_BASELINE_FIXTURES) {
|
||||
it(fixture.name, () => {
|
||||
const { sql, cursor } = sqlFixtureCursor(fixture.sql);
|
||||
const model = buildSqlSemanticModel(sql, cursor, { databaseType: fixture.databaseType });
|
||||
const scope = sqlSemanticCompletionScope(model);
|
||||
|
||||
expect(model.statement.kind).toBe(fixture.expected.statementKind);
|
||||
expect(model.cursorIntent.kind).toBe(fixture.expected.cursorKind);
|
||||
expect(scope.kind).toBe(fixture.expected.completionScope);
|
||||
expect(model.cursorIntent.prefix).toBe(fixture.expected.prefix);
|
||||
expect(model.cursorIntent.qualifierParts).toEqual(fixture.expected.qualifierParts ?? []);
|
||||
expect(model.cursorIntent.confidence).toBe(fixture.expected.confidence);
|
||||
|
||||
for (const expectedSource of fixture.expected.rowSources ?? []) {
|
||||
const expectedObject = Object.fromEntries(Object.entries(expectedSource).filter(([, value]) => value !== undefined));
|
||||
expect(model.rowSources).toEqual(expect.arrayContaining([expect.objectContaining(expectedObject)]));
|
||||
}
|
||||
|
||||
if (fixture.expected.completionLabels) {
|
||||
const labels = [...sqlSemanticLocalColumnsByTable(model).values()].flat().map((column) => column.name);
|
||||
expect(labels).toEqual(expect.arrayContaining(fixture.expected.completionLabels));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
it("does not mix row sources from inactive statements", () => {
|
||||
const { sql, cursor } = sqlFixtureCursor("select * from users u; select * from orders o where o.|");
|
||||
const model = buildSqlSemanticModel(sql, cursor);
|
||||
|
||||
expect(model.rowSources.some((source) => source.name === "orders")).toBe(true);
|
||||
expect(model.rowSources.some((source) => source.name === "users")).toBe(false);
|
||||
});
|
||||
|
||||
it("does not expose CTE body tables as outer query row sources", () => {
|
||||
const { sql, cursor } = sqlFixtureCursor("WITH recent_orders AS (SELECT id FROM orders) SELECT * FROM recent_orders ro WHERE ro.|");
|
||||
const model = buildSqlSemanticModel(sql, cursor);
|
||||
|
||||
expect(model.rowSources.some((source) => source.name === "recent_orders")).toBe(true);
|
||||
expect(model.rowSources.some((source) => source.name === "orders")).toBe(false);
|
||||
});
|
||||
|
||||
it("does not expose subquery body tables as outer query row sources", () => {
|
||||
const { sql, cursor } = sqlFixtureCursor("SELECT * FROM (SELECT id FROM users) sq WHERE sq.|");
|
||||
const model = buildSqlSemanticModel(sql, cursor);
|
||||
|
||||
expect(model.rowSources).toEqual(expect.arrayContaining([expect.objectContaining({ name: "sq", kind: "subquery" })]));
|
||||
expect(model.rowSources.some((source) => source.name === "users")).toBe(false);
|
||||
});
|
||||
|
||||
it("suppresses completion inside string literals without metadata scope", () => {
|
||||
const { sql, cursor } = sqlFixtureCursor("SELECT 'u.|' FROM users");
|
||||
const model = buildSqlSemanticModel(sql, cursor);
|
||||
const scope = sqlSemanticCompletionScope(model);
|
||||
|
||||
expect(model.cursorIntent.kind).toBe("suppressed");
|
||||
expect(scope.useRemoteMetadata).toBe(false);
|
||||
});
|
||||
|
||||
it("classifies table references after comma-separated table lists", () => {
|
||||
const { sql, cursor } = sqlFixtureCursor("SELECT * FROM users u, ord|");
|
||||
const model = buildSqlSemanticModel(sql, cursor);
|
||||
const scope = sqlSemanticCompletionScope(model);
|
||||
|
||||
expect(model.cursorIntent.kind).toBe("table");
|
||||
expect(model.cursorIntent.prefix).toBe("ord");
|
||||
expect(scope.kind).toBe("table");
|
||||
});
|
||||
|
||||
it("classifies alias-qualified star with replacement range", () => {
|
||||
const { sql, cursor } = sqlFixtureCursor("SELECT u.*| FROM users u");
|
||||
const model = buildSqlSemanticModel(sql, cursor);
|
||||
|
||||
expect(model.cursorIntent.kind).toBe("star");
|
||||
expect(model.cursorIntent.prefix).toBe("*");
|
||||
expect(model.cursorIntent.qualifierParts).toEqual(["u"]);
|
||||
expect(sql.slice(model.cursorIntent.replacementRange.start, model.cursorIntent.replacementRange.end)).toBe("*");
|
||||
});
|
||||
|
||||
it("returns low-confidence keyword fallback for unknown SQL", () => {
|
||||
const { sql, cursor } = sqlFixtureCursor("explain analyze |");
|
||||
const model = buildSqlSemanticModel(sql, cursor);
|
||||
const scope = sqlSemanticCompletionScope(model);
|
||||
|
||||
expect(model.cursorIntent.kind).toBe("keyword");
|
||||
expect(model.cursorIntent.confidence).toBe("low");
|
||||
expect(scope.useRemoteMetadata).toBe(false);
|
||||
});
|
||||
|
||||
it("exposes PostgreSQL projection aliases in ORDER BY but not WHERE", () => {
|
||||
const orderBy = sqlFixtureCursor("select total_amount as total from orders order by to|");
|
||||
const where = sqlFixtureCursor("select total_amount as total from orders where to|");
|
||||
|
||||
expect(sqlSemanticProjectionAliasColumns(buildSqlSemanticModel(orderBy.sql, orderBy.cursor, { databaseType: "postgres" })).map((column) => column.name)).toContain("total");
|
||||
expect(sqlSemanticProjectionAliasColumns(buildSqlSemanticModel(where.sql, where.cursor, { databaseType: "postgres" })).map((column) => column.name)).not.toContain("total");
|
||||
});
|
||||
|
||||
it("exposes MySQL projection aliases in GROUP BY and HAVING", () => {
|
||||
const groupBy = sqlFixtureCursor("select total_amount as total from orders group by to|");
|
||||
const having = sqlFixtureCursor("select total_amount as total from orders having to|");
|
||||
|
||||
expect(sqlSemanticProjectionAliasColumns(buildSqlSemanticModel(groupBy.sql, groupBy.cursor, { databaseType: "mysql" })).map((column) => column.name)).toContain("total");
|
||||
expect(sqlSemanticProjectionAliasColumns(buildSqlSemanticModel(having.sql, having.cursor, { databaseType: "mysql" })).map((column) => column.name)).toContain("total");
|
||||
});
|
||||
|
||||
it("keeps dialect-specific identifier normalization and qualifier scopes", () => {
|
||||
const sqlServer = sqlFixtureCursor("SELECT * FROM [dbo].[Users] u WHERE u.|");
|
||||
const postgres = sqlFixtureCursor('SELECT total AS "Order Total" FROM "Sales"."Orders" o ORDER BY "Order|');
|
||||
const mysql = sqlFixtureCursor("SELECT * FROM `analytics`.`events` e WHERE e.|");
|
||||
const sqlite = sqlFixtureCursor("SELECT * FROM main.users u WHERE u.|");
|
||||
|
||||
expect(buildSqlSemanticModel(sqlServer.sql, sqlServer.cursor, { databaseType: "sqlserver" }).rowSources[0]).toEqual(expect.objectContaining({ name: "Users", qualifierParts: ["dbo"], alias: "u" }));
|
||||
expect(sqlSemanticProjectionAliasColumns(buildSqlSemanticModel(postgres.sql, postgres.cursor, { databaseType: "postgres" })).map((column) => column.name)).toContain("Order Total");
|
||||
expect(buildSqlSemanticModel(mysql.sql, mysql.cursor, { databaseType: "mysql" }).rowSources[0]).toEqual(expect.objectContaining({ name: "events", qualifierParts: ["analytics"], alias: "e" }));
|
||||
expect(buildSqlSemanticModel(sqlite.sql, sqlite.cursor, { databaseType: "sqlite" }).rowSources[0]).toEqual(expect.objectContaining({ name: "users", qualifierParts: ["main"], alias: "u" }));
|
||||
});
|
||||
|
||||
it("covers SQL Server case-insensitive bracket and multi-part qualifier contexts", () => {
|
||||
const { sql, cursor } = sqlFixtureCursor("SELECT * FROM [ServerOne].[AppDb].[dbo].[Users] U WHERE u.na|");
|
||||
const model = buildSqlSemanticModel(sql, cursor, { databaseType: "sqlserver" });
|
||||
|
||||
expect(model.rowSources[0]).toEqual(expect.objectContaining({ name: "Users", qualifierParts: ["ServerOne", "AppDb", "dbo"], alias: "U" }));
|
||||
expect(model.cursorIntent.kind).toBe("alias_column");
|
||||
expect(model.cursorIntent.qualifierParts).toEqual(["u"]);
|
||||
});
|
||||
|
||||
it("covers PostgreSQL lower-case folding with CTEs and ORDER BY projection aliases", () => {
|
||||
const { sql, cursor } = sqlFixtureCursor("WITH RecentOrders AS (SELECT id, total FROM orders) SELECT total AS total_alias FROM RecentOrders ro ORDER BY total_|");
|
||||
const model = buildSqlSemanticModel(sql, cursor, { databaseType: "postgres" });
|
||||
|
||||
expect(model.rowSources).toEqual(expect.arrayContaining([expect.objectContaining({ name: "recentorders", alias: "ro", columns: ["id", "total"] })]));
|
||||
expect(sqlSemanticProjectionAliasColumns(model).map((column) => column.name)).toContain("total_alias");
|
||||
});
|
||||
|
||||
it("covers MySQL database-qualified backticks and projection alias visibility", () => {
|
||||
const groupBy = sqlFixtureCursor("SELECT amount AS total FROM `analytics`.`events` e GROUP BY to|");
|
||||
const where = sqlFixtureCursor("SELECT amount AS total FROM `analytics`.`events` e WHERE to|");
|
||||
|
||||
expect(buildSqlSemanticModel(groupBy.sql, groupBy.cursor, { databaseType: "mysql" }).rowSources[0]).toEqual(expect.objectContaining({ name: "events", qualifierParts: ["analytics"], alias: "e" }));
|
||||
expect(sqlSemanticProjectionAliasColumns(buildSqlSemanticModel(groupBy.sql, groupBy.cursor, { databaseType: "mysql" })).map((column) => column.name)).toContain("total");
|
||||
expect(sqlSemanticProjectionAliasColumns(buildSqlSemanticModel(where.sql, where.cursor, { databaseType: "mysql" })).map((column) => column.name)).not.toContain("total");
|
||||
});
|
||||
|
||||
it("covers SQLite and DuckDB schema-light local row-source behavior", () => {
|
||||
const sqlite = sqlFixtureCursor("SELECT * FROM main.users u WHERE u.|");
|
||||
const duckdb = sqlFixtureCursor("SELECT * FROM read_csv('users.csv') csv WHERE csv.|");
|
||||
|
||||
expect(sqlSemanticCompletionScope(buildSqlSemanticModel(sqlite.sql, sqlite.cursor, { databaseType: "sqlite" })).useRemoteMetadata).toBe(true);
|
||||
expect(buildSqlSemanticModel(duckdb.sql, duckdb.cursor, { databaseType: "duckdb" }).rowSources[0]).toEqual(expect.objectContaining({ kind: "table_function", name: "csv", alias: "csv" }));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,54 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildSqlSemanticDiagnostics } from "@/lib/sql/semantic/diagnostics";
|
||||
import { buildSqlSemanticModel } from "@/lib/sql/semantic/model";
|
||||
import { sqlFixtureCursor } from "@/lib/sql/semantic/fixtures";
|
||||
import { resolveSqlSemanticNavigationTarget, mergeSqlSemanticReferenceAnalysis, sqlSemanticCompletionReferenceTables, sqlSemanticTableReferences } from "@/lib/sql/semantic/references";
|
||||
import { splitQualifiedIdentifier } from "@/lib/sql/sqlNavigation";
|
||||
import type { SqlReferenceAnalysis } from "@/types/database";
|
||||
|
||||
const span = (startColumn: number, endColumn: number) => ({
|
||||
start_line: 1,
|
||||
start_column: startColumn,
|
||||
end_line: 1,
|
||||
end_column: endColumn,
|
||||
});
|
||||
|
||||
describe("sqlSemanticReferences shared consumers", () => {
|
||||
it("feeds CTE projected columns to diagnostics without physical table metadata", () => {
|
||||
const { sql, cursor } = sqlFixtureCursor("WITH recent_orders(id, total) AS (SELECT id, total FROM orders) SELECT ro.missing FROM recent_orders ro WHERE ro.|");
|
||||
const model = buildSqlSemanticModel(sql, cursor);
|
||||
const analysis: SqlReferenceAnalysis = {
|
||||
tables: [],
|
||||
columns: [{ name: "missing", qualifier: "ro", span: span(sql.indexOf("missing") + 1, sql.indexOf("missing") + "missing".length) }],
|
||||
};
|
||||
|
||||
const diagnostics = buildSqlSemanticDiagnostics(mergeSqlSemanticReferenceAnalysis(analysis, model), {
|
||||
tables: [],
|
||||
columnsByTable: new Map(),
|
||||
sql,
|
||||
});
|
||||
|
||||
expect(diagnostics.map((diagnostic) => diagnostic.message)).toEqual(["Unknown column ro.missing"]);
|
||||
});
|
||||
|
||||
it("resolves navigation targets from subquery aliases and projected columns", () => {
|
||||
const { sql, cursor } = sqlFixtureCursor("SELECT sq.user_| FROM (SELECT id, name AS user_name FROM users) sq");
|
||||
const model = buildSqlSemanticModel(sql, cursor);
|
||||
const target = resolveSqlSemanticNavigationTarget(model, splitQualifiedIdentifier("sq.user_name"));
|
||||
|
||||
expect(target).toEqual(expect.objectContaining({ name: "sq", alias: "sq", columns: ["id", "user_name"] }));
|
||||
expect(target?.source.kind).toBe("subquery");
|
||||
});
|
||||
|
||||
it("keeps completion diagnostics and navigation row-source resolution consistent", () => {
|
||||
const { sql, cursor } = sqlFixtureCursor("WITH recent_orders(id, total) AS (SELECT id, total FROM orders) SELECT * FROM recent_orders ro WHERE ro.|");
|
||||
const model = buildSqlSemanticModel(sql, cursor);
|
||||
const completionRefs = sqlSemanticCompletionReferenceTables(model);
|
||||
const diagnosticRefs = sqlSemanticTableReferences(model);
|
||||
const navigationTarget = resolveSqlSemanticNavigationTarget(model, ["ro", "total"]);
|
||||
|
||||
expect(completionRefs).toEqual(expect.arrayContaining([expect.objectContaining({ name: "recent_orders", alias: "ro", columns: ["id", "total"] })]));
|
||||
expect(diagnosticRefs).toEqual(expect.arrayContaining([expect.objectContaining({ name: "recent_orders", alias: "ro", columns: ["id", "total"] })]));
|
||||
expect(navigationTarget).toEqual(expect.objectContaining({ name: "recent_orders", alias: "ro", columns: ["id", "total"] }));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { findActiveSqlStatementSpan, isSuppressedSqlSemanticContext, tokenizeSqlSemantic, unquoteSqlSemanticIdentifier } from "@/lib/sql/semantic/tokens";
|
||||
|
||||
describe("sqlSemanticTokens", () => {
|
||||
it("tokenizes comments, strings, quoted identifiers, brackets, and backticks", () => {
|
||||
const sql = "select [User Name], `order`, \"Mixed\" from users -- comment\nwhere name = 'it''s ok'";
|
||||
const tokens = tokenizeSqlSemantic(sql);
|
||||
|
||||
expect(tokens.some((token) => token.kind === "quoted_identifier" && unquoteSqlSemanticIdentifier(token) === "User Name")).toBe(true);
|
||||
expect(tokens.some((token) => token.kind === "quoted_identifier" && unquoteSqlSemanticIdentifier(token) === "order")).toBe(true);
|
||||
expect(tokens.some((token) => token.kind === "quoted_identifier" && unquoteSqlSemanticIdentifier(token) === "Mixed")).toBe(true);
|
||||
expect(tokens.some((token) => token.kind === "comment" && token.text.includes("comment"))).toBe(true);
|
||||
expect(tokens.some((token) => token.kind === "string" && token.text === "'it''s ok'")).toBe(true);
|
||||
});
|
||||
|
||||
it("marks comments and string literals as suppressed contexts", () => {
|
||||
const sql = "select * from users -- user.";
|
||||
const tokens = tokenizeSqlSemantic(sql);
|
||||
|
||||
expect(isSuppressedSqlSemanticContext(tokens, sql.length)).toBe(true);
|
||||
expect(isSuppressedSqlSemanticContext(tokens, "select * from users".length)).toBe(false);
|
||||
});
|
||||
|
||||
it("finds active statement spans across semicolon-separated scripts", () => {
|
||||
const sql = "select * from users;\nselect * from orders where id = 1;";
|
||||
const cursor = sql.indexOf("orders");
|
||||
const span = findActiveSqlStatementSpan(sql, tokenizeSqlSemantic(sql), cursor);
|
||||
|
||||
expect(sql.slice(span.start, span.end)).toBe("select * from orders where id = 1");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user