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。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
improve: 优化 SQL 语义诊断范围
zipg79298f49d933
diff --git a/apps/desktop/src/components/editor/QueryEditor.vue b/apps/desktop/src/components/editor/QueryEditor.vueindex b3da3757b3..85081ae440 100644--- a/apps/desktop/src/components/editor/QueryEditor.vue+++ b/apps/desktop/src/components/editor/QueryEditor.vue@@ -10,7 +10,7 @@ import SqlExecutionTargetPicker from "./SqlExecutionTargetPicker.vue";import CustomContextMenu, { type ContextMenuItem } from "@/components/ui/CustomContextMenu.vue";import { copyToClipboard } from "@/lib/clipboard";import { resolveExecutableSql, type SqlExecutionSnapshot, type SqlExecutionOverride, type SqlExecutionCandidate } from "@/lib/sqlExecutionTarget";-import { buildExecutionCandidates, executableStatementRanges, hasMultipleExecutionTargets, supportsExecutionTargetPicker } from "@/lib/sqlStatementRanges";+import { buildExecutionCandidates, executableStatementRanges, hasMultipleExecutionTargets, supportsExecutionTargetPicker, type SqlTextRange } from "@/lib/sqlStatementRanges";import { formatSqlText, type SqlFormatDialect } from "@/lib/sqlFormatter";import { formatMongoShellText } from "@/lib/mongoFormatter";import { useConnectionStore } from "@/stores/connectionStore";@@ -43,7 +43,7 @@ import { isSchemaAware, isSingleDatabase } from "@/lib/databaseFeatureSupport";import { usesLocalOnlyEditorCompletionMetadata, usesOnDemandOnlyEditorColumnMetadata } from "@/lib/completionMetadataPolicy";import { qualifiedTableNameAtSqlPosition } from "@/lib/queryCursorTableTarget";import * as api from "@/lib/api";-import { areSqlSemanticDiagnosticsEqual, buildSqlParserErrorDiagnostic, buildSqlSemanticDiagnostics, isSqlSemanticDiagnosticInputContext, shouldRunSqlSemanticDiagnostics, tableReferenceKey, type SqlSemanticDiagnostic } from "@/lib/sqlSemanticDiagnostics";+import { areSqlSemanticDiagnosticsEqual, buildSqlParserErrorDiagnostic, buildSqlSemanticDiagnostics, isSqlSemanticDiagnosticInputContext, shouldRunSqlSemanticDiagnostics, sqlSemanticDiagnosticRangesForViewport, tableReferenceKey, type SqlSemanticDiagnostic } from "@/lib/sqlSemanticDiagnostics";import { buildRedisSyntaxDiagnostics, shouldRunRedisDiagnostics } from "@/lib/redisSyntaxDiagnostics";import { buildRedisCompletionItemsFromContext, getRedisCompletionContext, getRedisCompletionResultValidFor, shouldAutoOpenRedisCompletion, takesKeyArgument, type RedisCompletionItem } from "@/lib/redisCompletion";import type { SqlCompletionColumn, SqlCompletionForeignKey, SqlCompletionItem, SqlCompletionObject, SqlCompletionTable } from "@/lib/sqlCompletion";@@ -208,6 +208,7 @@ let codeMirrorIndentUnit: typeof import("@codemirror/language").indentUnit | nullet semanticDiagnostics: SqlSemanticDiagnostic[] = [];let semanticDiagnosticTimer: ReturnType<typeof setTimeout> | null = null;let semanticDiagnosticRunId = 0;+let pendingSemanticDiagnosticPreserveOutsideRanges = false;let editorIsActive = true;let tableReferenceDropListenerRegistered = false;let imeCompositionActive = false;@@ -922,6 +923,54 @@ function setSemanticDiagnostics(next: SqlSemanticDiagnostic[]) {reconfigureDiagnostics();}+function rangesOverlap(left: { from: number; to: number }, right: { from: number; to: number }): boolean {+ return left.from < right.to && right.from < left.to;+}++function sqlLineColumnAtOffset(sql: string, offset: number): { line: number; column: number } {+ const safeOffset = Math.max(0, Math.min(offset, sql.length));+ let line = 1;+ let lineStart = 0;+ for (let index = 0; index < safeOffset; index += 1) {+ if (sql[index] === "\n") {+ line += 1;+ lineStart = index + 1;+ }+ }+ return { line, column: safeOffset - lineStart + 1 };+}++function offsetSqlTextSpan(span: SqlTextSpan, rangeStart: { line: number; column: number }): SqlTextSpan {+ const offsetLine = (line: number) => rangeStart.line + line - 1;+ const offsetColumn = (line: number, column: number) => (line === 1 ? rangeStart.column + column - 1 : column);+ return {+ start_line: offsetLine(span.start_line),+ start_column: offsetColumn(span.start_line, span.start_column),+ end_line: offsetLine(span.end_line),+ end_column: offsetColumn(span.end_line, span.end_column),+ };+}++function offsetSqlSemanticDiagnostics(diagnostics: readonly SqlSemanticDiagnostic[], range: SqlTextRange, fullSql: string): SqlSemanticDiagnostic[] {+ const rangeStart = sqlLineColumnAtOffset(fullSql, range.from);+ return diagnostics.map((diagnostic) => ({+ ...diagnostic,+ span: offsetSqlTextSpan(diagnostic.span, rangeStart),+ }));+}++function replaceSemanticDiagnosticsInRanges(next: SqlSemanticDiagnostic[], ranges: readonly SqlTextRange[], fullSql: string) {+ const retained = semanticDiagnostics.filter((diagnostic) => {+ const diagnosticRange = sqlTextSpanToRange(fullSql, diagnostic.span);+ return !diagnosticRange || !ranges.some((range) => rangesOverlap(diagnosticRange, range));+ });+ setSemanticDiagnostics([...retained, ...next].sort(compareSqlSemanticDiagnostics));+}++function compareSqlSemanticDiagnostics(left: SqlSemanticDiagnostic, right: SqlSemanticDiagnostic): number {+ return left.span.start_line - right.span.start_line || left.span.start_column - right.span.start_column || left.span.end_line - right.span.end_line || left.span.end_column - right.span.end_column || left.message.localeCompare(right.message);+}+async function enrichSemanticDiagnosticTables(tables: SqlTableReference[]): Promise<{ tables: SqlTableReference[]; missingTables: Set<string> }> {if (!props.connectionId || props.database == null) return { tables, missingTables: new Set() };@@ -968,7 +1017,7 @@ async function ensureColumnsForSemanticDiagnostics(tables: SqlTableReference[]):return missingTables;}-async function refreshSemanticDiagnostics() {+async function refreshSemanticDiagnostics(options: { preserveOutsideRanges?: boolean } = {}) {const currentView = view.value;const runId = ++semanticDiagnosticRunId;if (!currentView || !props.connectionId || props.database == null) {@@ -988,54 +1037,75 @@ async function refreshSemanticDiagnostics() {if (props.databaseType === "redis") {// Redis has no SQL semantics; run command-name / arity / quote / danger checks instead.if (!shouldRunRedisDiagnostics(sql, currentView.state.selection.main.head)) {- scheduleSemanticDiagnostics(900);+ scheduleSemanticDiagnostics(900, { preserveOutsideRanges: options.preserveOutsideRanges });return;}setSemanticDiagnostics(buildRedisSyntaxDiagnostics(sql));return;}if (!shouldRunSqlSemanticDiagnostics(sql, currentView.state.selection.main.head, { databaseType: props.databaseType })) {- scheduleSemanticDiagnostics(1200);+ scheduleSemanticDiagnostics(1200, { preserveOutsideRanges: options.preserveOutsideRanges });return;}if (codeMirrorCompletionStatus?.(currentView.state) && isSqlSemanticDiagnosticInputContext(sql, currentView.state.selection.main.head, { databaseType: props.databaseType })) {- scheduleSemanticDiagnostics(900);+ scheduleSemanticDiagnostics(900, { preserveOutsideRanges: options.preserveOutsideRanges });return;}- try {- const analysis = await api.analyzeSqlReferences(sql, props.formatDialect ?? props.dialect ?? "generic");- if (runId !== semanticDiagnosticRunId) return;-- const { tables, missingTables } = await enrichSemanticDiagnosticTables(analysis.tables);- const columnMetadataMissingTables = await ensureColumnsForSemanticDiagnostics(tables);- for (const tableKey of columnMetadataMissingTables) missingTables.add(tableKey);- if (runId !== semanticDiagnosticRunId) return;-- const enrichedAnalysis: SqlReferenceAnalysis = { ...analysis, tables };- setSemanticDiagnostics(- buildSqlSemanticDiagnostics(enrichedAnalysis, {- tables: cachedTables,- columnsByTable: cachedColumnsByTable,- missingTables,- loadedColumnTables: loadedColumnsByTable,- sql,- }),- );- } catch (error) {- if (runId === semanticDiagnosticRunId) {- const diagnostic = buildSqlParserErrorDiagnostic(error, sql);- setSemanticDiagnostics(diagnostic ? [diagnostic] : []);+ const visibleRanges = currentView.visibleRanges.length > 0 ? currentView.visibleRanges : [currentView.viewport];+ const diagnosticRanges = sqlSemanticDiagnosticRangesForViewport(sql, visibleRanges, props.databaseType);+ if (diagnosticRanges.length === 0) {+ if (!options.preserveOutsideRanges) setSemanticDiagnostics([]);+ return;+ }++ const nextDiagnostics: SqlSemanticDiagnostic[] = [];+ for (const range of diagnosticRanges) {+ try {+ const analysis = await api.analyzeSqlReferences(range.sql, props.formatDialect ?? props.dialect ?? "generic");+ if (runId !== semanticDiagnosticRunId) return;++ const { tables, missingTables } = await enrichSemanticDiagnosticTables(analysis.tables);+ const columnMetadataMissingTables = await ensureColumnsForSemanticDiagnostics(tables);+ for (const tableKey of columnMetadataMissingTables) missingTables.add(tableKey);+ if (runId !== semanticDiagnosticRunId) return;++ const enrichedAnalysis: SqlReferenceAnalysis = { ...analysis, tables };+ nextDiagnostics.push(+ ...offsetSqlSemanticDiagnostics(+ buildSqlSemanticDiagnostics(enrichedAnalysis, {+ tables: cachedTables,+ columnsByTable: cachedColumnsByTable,+ missingTables,+ loadedColumnTables: loadedColumnsByTable,+ sql: range.sql,+ }),+ range,+ sql,+ ),+ );+ } catch (error) {+ if (runId !== semanticDiagnosticRunId) return;+ const diagnostic = buildSqlParserErrorDiagnostic(error, range.sql);+ if (diagnostic) nextDiagnostics.push(...offsetSqlSemanticDiagnostics([diagnostic], range, sql));}}+ if (options.preserveOutsideRanges) {+ replaceSemanticDiagnosticsInRanges(nextDiagnostics, diagnosticRanges, sql);+ } else {+ setSemanticDiagnostics(nextDiagnostics.sort(compareSqlSemanticDiagnostics));+ }}-function scheduleSemanticDiagnostics(delay = 500) {+function scheduleSemanticDiagnostics(delay = 500, options: { preserveOutsideRanges?: boolean } = {}) {if (!editorIsActive) return;+ pendingSemanticDiagnosticPreserveOutsideRanges = !!options.preserveOutsideRanges;if (semanticDiagnosticTimer) clearTimeout(semanticDiagnosticTimer);semanticDiagnosticTimer = setTimeout(() => {+ const preserveOutsideRanges = pendingSemanticDiagnosticPreserveOutsideRanges;+ pendingSemanticDiagnosticPreserveOutsideRanges = false;semanticDiagnosticTimer = null;- void refreshSemanticDiagnostics();+ void refreshSemanticDiagnostics({ preserveOutsideRanges });}, delay);}@@ -2545,6 +2615,7 @@ function emitEditorViewport(viewport: { scrollTop: number; scrollLeft: number })function scheduleEditorViewportEmit() {if (!view.value || !editorIsActive) return;latestViewport = readEditorViewport(view.value);+ scheduleSemanticDiagnostics(700, { preserveOutsideRanges: true });if (viewportEmitFrame !== null) return;viewportEmitFrame = requestAnimationFrame(() => {viewportEmitFrame = null;diff --git a/apps/desktop/src/lib/sqlSemanticDiagnostics.ts b/apps/desktop/src/lib/sqlSemanticDiagnostics.tsindex d887a4fc6d..d8cea1727c 100644--- a/apps/desktop/src/lib/sqlSemanticDiagnostics.ts+++ b/apps/desktop/src/lib/sqlSemanticDiagnostics.ts@@ -1,5 +1,6 @@import type { SqlCompletionColumn, SqlCompletionTable } from "@/lib/sqlCompletion";import { getSqlCompletionContext } from "@/lib/sqlCompletion";+import { executableStatementRanges, type SqlTextRange } from "@/lib/sqlStatementRanges";import type { DatabaseType, SqlColumnReference, SqlReferenceAnalysis, SqlTableReference, SqlTextSpan } from "@/types/database";export interface SqlSemanticDiagnostic {@@ -16,6 +17,27 @@ export interface SqlSemanticDiagnosticSchema {sql?: string;}+export interface SqlSemanticDiagnosticVisibleRange {+ from: number;+ to: number;+}++export function sqlSemanticDiagnosticRangesForViewport(sql: string, visibleRanges: readonly SqlSemanticDiagnosticVisibleRange[], databaseType?: DatabaseType): SqlTextRange[] {+ const statements = executableStatementRanges(sql, databaseType);+ if (statements.length === 0 || visibleRanges.length === 0) return [];++ const selected: SqlTextRange[] = [];+ const seen = new Set<string>();+ for (const statement of statements) {+ if (!visibleRanges.some((visibleRange) => rangesIntersect(statement, visibleRange))) continue;+ const key = `${statement.from}:${statement.to}`;+ if (seen.has(key)) continue;+ seen.add(key);+ selected.push({ from: statement.from, to: statement.to, sql: sql.slice(statement.from, statement.to) });+ }+ return selected;+}+export function buildSqlSemanticDiagnostics(analysis: SqlReferenceAnalysis, schema: SqlSemanticDiagnosticSchema): SqlSemanticDiagnostic[] {const diagnostics: SqlSemanticDiagnostic[] = [];const tables = analysis.tables.filter((table) => table.name.trim());@@ -170,6 +192,10 @@ function keysWithTableName(columnsByTable: Map<string, SqlCompletionColumn[]>, treturn [...columnsByTable.keys()].filter((key) => normalizeName(key).endsWith(suffix));}+function rangesIntersect(left: SqlSemanticDiagnosticVisibleRange, right: SqlSemanticDiagnosticVisibleRange): boolean {+ return left.from < right.to && right.from < left.to;+}+export function tableReferenceKey(table: Pick<SqlTableReference, "name" | "schema">): string {return normalizeName(table.schema ? `${table.schema}.${table.name}` : table.name);}@@ -180,7 +206,7 @@ function displayTableName(table: SqlTableReference): string {function isCursorAfterTableTrigger(sql: string, cursor: number): boolean {const beforeCursor = sql.slice(0, cursor).trimEnd();- return /\b(from|join|update|into|table)(?:\s+[\w$`"'\[\].]*)?$/i.test(beforeCursor);+ return /\b(from|join|update|into|table)(?:\s+[\w$`"'[\].]*)?$/i.test(beforeCursor);}function normalizeName(value: string): string {diff --git a/packages/app-tests/sqlSemanticDiagnostics.test.ts b/packages/app-tests/sqlSemanticDiagnostics.test.tsindex 9e809394bf..b948a493cb 100644--- a/packages/app-tests/sqlSemanticDiagnostics.test.ts+++ b/packages/app-tests/sqlSemanticDiagnostics.test.ts@@ -1,6 +1,6 @@import { strict as assert } from "node:assert";import { test } from "vitest";-import { buildSqlParserErrorDiagnostic, buildSqlSemanticDiagnostics, areSqlSemanticDiagnosticsEqual, isSqlSemanticDiagnosticInputContext, shouldRunSqlSemanticDiagnostics } from "../../apps/desktop/src/lib/sqlSemanticDiagnostics.ts";+import { buildSqlParserErrorDiagnostic, buildSqlSemanticDiagnostics, areSqlSemanticDiagnosticsEqual, isSqlSemanticDiagnosticInputContext, shouldRunSqlSemanticDiagnostics, sqlSemanticDiagnosticRangesForViewport } from "../../apps/desktop/src/lib/sqlSemanticDiagnostics.ts";import type { SqlReferenceAnalysis } from "../../apps/desktop/src/types/database.ts";const span = (startColumn: number, endColumn: number) => ({@@ -232,3 +232,40 @@ test("skips diagnostics for Elasticsearch connections", () => {test("still runs diagnostics for SQL connections", () => {assert.equal(shouldRunSqlSemanticDiagnostics("SELECT * FROM users WHERE id = 1", 42, { databaseType: "mysql" }), true);});++test("selects complete SQL statements intersecting the visible viewport for diagnostics", () => {+ const sql = "SELECT * FROM first;\nSELECT id, missing_field FROM second WHERE id > 1;\nSELECT * FROM third;";+ const visibleFrom = sql.indexOf("missing_field");+ const visibleTo = visibleFrom + "missing_field".length;++ const ranges = sqlSemanticDiagnosticRangesForViewport(sql, [{ from: visibleFrom, to: visibleTo }], "mysql");++ assert.equal(ranges.length, 1);+ assert.equal(ranges[0]?.sql, "SELECT id, missing_field FROM second WHERE id > 1");+ assert.equal(ranges[0]?.from, sql.indexOf("SELECT id"));+ assert.equal(ranges[0]?.to, sql.indexOf(";\nSELECT * FROM third"));+});++test("keeps a long statement complete when only its middle is visible", () => {+ const sql = "SELECT id,\n name,\n missing_field\nFROM users\nWHERE id > 1;";+ const visibleFrom = sql.indexOf("missing_field");+ const visibleTo = sql.indexOf("FROM users");++ const ranges = sqlSemanticDiagnosticRangesForViewport(sql, [{ from: visibleFrom, to: visibleTo }], "mysql");++ assert.deepEqual(+ ranges.map((range) => range.sql),+ ["SELECT id,\n name,\n missing_field\nFROM users\nWHERE id > 1"],+ );+});++test("uses executable soft statement ranges for viewport diagnostics", () => {+ const sql = "SELECT * FROM first\nSELECT missing_field FROM second";+ const visibleFrom = sql.indexOf("missing_field");+ const ranges = sqlSemanticDiagnosticRangesForViewport(sql, [{ from: visibleFrom, to: visibleFrom + 1 }], "mysql");++ assert.deepEqual(+ ranges.map((range) => range.sql),+ ["SELECT missing_field FROM second"],+ );+});