From ae331effdee9c0a501aa4872e40790b3b6138ec0 Mon Sep 17 00:00:00 2001 From: alex <48489896+devnaumov@users.noreply.github.com> Date: Thu, 19 Feb 2026 21:58:11 +0100 Subject: [PATCH] dbeaver/pro#3822 make selection accountable (#4144) * dbeaver/pro#3822 make selection accountable * dbeaver/pro#3822 pass cursor to get queries in range * dbeaver/pro#3822 keep range queries clamped in selection --------- Co-authored-by: Evgenia <139753579+EvgeniaBzzz@users.noreply.github.com> --- .../plugin-sql-editor/src/SQLParser.ts | 6 +++- .../src/SqlEditor/useSqlEditor.ts | 33 +++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/webapp/packages/plugin-sql-editor/src/SQLParser.ts b/webapp/packages/plugin-sql-editor/src/SQLParser.ts index 5b50599884..adf2661dfc 100644 --- a/webapp/packages/plugin-sql-editor/src/SQLParser.ts +++ b/webapp/packages/plugin-sql-editor/src/SQLParser.ts @@ -1,6 +1,6 @@ /* * CloudBeaver - Cloud Database Manager - * Copyright (C) 2020-2025 DBeaver Corp and others + * Copyright (C) 2020-2026 DBeaver Corp and others * * Licensed under the Apache License, Version 2.0. * you may not use this file except in compliance with the License. @@ -109,6 +109,10 @@ export class SQLParser { }; } + getQueriesInRange(begin: number, end: number): ISQLScriptSegment[] { + return this._scripts.filter(script => script.begin <= end && script.end >= begin); + } + setScript(script: string): void { this.script = script; } diff --git a/webapp/packages/plugin-sql-editor/src/SqlEditor/useSqlEditor.ts b/webapp/packages/plugin-sql-editor/src/SqlEditor/useSqlEditor.ts index ac2e01a74e..65346523d3 100644 --- a/webapp/packages/plugin-sql-editor/src/SqlEditor/useSqlEditor.ts +++ b/webapp/packages/plugin-sql-editor/src/SqlEditor/useSqlEditor.ts @@ -1,6 +1,6 @@ /* * CloudBeaver - Cloud Database Manager - * Copyright (C) 2020-2025 DBeaver Corp and others + * Copyright (C) 2020-2026 DBeaver Corp and others * * Licensed under the Apache License, Version 2.0. * you may not use this file except in compliance with the License. @@ -241,10 +241,39 @@ export function useSqlEditor(state: ISqlEditorTabState): ISQLEditorData { } this.onExecute.execute(true); + try { this.executingScript = true; + await this.model.getResolvedSegment(); - const queries = this.model.parser.scripts; + + const cursor = this.model.cursor; + const hasSelection = cursor.anchor !== cursor.head; + + let queries = this.model.parser.scripts; + + // Returns queries that are in range of [from, to]. If query is partially in range, it will be clamped to the range. + // Selection sele[ct 1; select 2;] will produce [ct 1, select 2] queries. + if (hasSelection) { + const from = Math.min(cursor.anchor, cursor.head); + const to = Math.max(cursor.anchor, cursor.head); + const rangeQueries = this.model.parser.getQueriesInRange(from, to); + + queries = rangeQueries.map(script => { + const clampedBegin = Math.max(script.begin, from); + const clampedEnd = Math.min(script.end, to); + + if (clampedBegin === script.begin && clampedEnd === script.end) { + return script; + } + + return { + query: script.query.substring(clampedBegin - script.begin, clampedEnd - script.begin), + begin: clampedBegin, + end: clampedEnd, + }; + }); + } await this.sqlQueryService.executeQueries( this.state,