fix(mongodb): support multi-command execution

* fix mongodb multi-command execution

* fix(connection): 复制连接时保持在同一组内

- 修改 `addConnection` 方法以支持指定目标组 ID
- 更新 `duplicateConnection` 函数以传递目标组 ID
- 添加测试用例以验证复制连接的功能

* fix: add MongoDB data transfer entry

* feat(editor): SQL 编辑器选区大小写转换

Co-authored-by: staff <staff@qimaos-MacBook-Pro.local>

* fix(editor): 修复SQL快捷执行导致执行全量SQL的问题 (#2384)

- 将执行SQL语句的逻辑从直接请求改为通过事件发射
- 增强了代码的可扩展性和可维护性

* fix(sidebar): cache tooltip overflow measurement

* fix(dialog): allow dragging window from modal overlay

* fix(oracle): reuse shared pool for data tabs

* fix(sidebar): reduce comment text repaint cost

* fix(saved-sql): preserve folder on metadata save

* fix(agent): execute transaction control SQL as statements

* feat(vector): add Qdrant/Milvus collection dimension display (#2396)

* fix(oracle): 修复时间字段编辑和表结构修改报错

Co-authored-by: staff <staff@qimaos-MacBook-Pro.local>

* fix(agents): limit common version bumps

---------

Co-authored-by: 二丫讲梵 <eryajf@163.com>
Co-authored-by: zipg <geeky@foxmail.com>
Co-authored-by: staff <staff@qimaos-MacBook-Pro.local>
Co-authored-by: t8y2 <1156263951@qq.com>
Co-authored-by: lexmin0412 <zhangle_dev@outlook.com>
This commit is contained in:
authored and GitHub committed 2026-07-02 12:19:38 +08:00
1 parent b869bcd379
commit 8747df53ef
9 files changed
+861 -272

No files matched your search

@@ -598,6 +598,8 @@ function executeSqlStatementFromGutter(currentView: EditorViewType, line: { from
if (!statementRange) return false;
event.preventDefault();
event.stopPropagation();
// Gutter play is always scoped to the statement/command for that line, even
// when the main editor execute action would run the full document.
emit("execute", statementRange.sql);
currentView.focus();
return true;
@@ -342,6 +342,14 @@ describe("executableStatementRanges", () => {
expect(ranges.map((range) => range.from)).toEqual([0, sql.indexOf("DEL")]);
});
it("returns MongoDB command ranges for newline-separated shell commands", () => {
const sql = 'use archive\ndb.users.find({ status: "open" })\n .limit(5)';
const ranges = executableStatementRanges(sql, "mongodb");
expect(rangeSqlTexts(ranges)).toEqual(["use archive", 'db.users.find({ status: "open" })\n .limit(5)']);
expect(ranges.map((range) => range.from)).toEqual([0, sql.indexOf("db.users.find")]);
});
it("does not split executable Oracle PL/SQL ranges at inner statement starts", () => {
expect(rangeSqlTexts(executableStatementRanges(oraclePlSqlFixture, "oracle"))).toEqual([oraclePlSqlFixture.slice(0, oraclePlSqlFixture.indexOf("\n/")), "SELECT 1"]);
});
+337 -1
View File
@@ -31,7 +31,15 @@ export interface MongoVersionCommand {
kind: "version";
}
export type MongoWriteCommand =
type MongoWriteKind = "insert" | "update" | "delete" | "createIndex" | "dropIndex" | "dropIndexes";
export type MongoCommand =
| ({ kind: "find" } & MongoFindCommand)
| MongoVersionCommand
| ({ kind: "countDocuments" } & MongoCountDocumentsCommand)
| ({ kind: "aggregate" } & MongoAggregateCommand)
| ({ kind: "getIndexes" } & MongoGetIndexesCommand)
| ({ kind: "use" } & MongoUseCommand)
| { kind: "insert"; collection: string; docsJson: string }
| { kind: "update"; collection: string; filter: string; update: string; many: boolean }
| { kind: "delete"; collection: string; filter: string; many: boolean }
@@ -39,6 +47,18 @@ export type MongoWriteCommand =
| { kind: "dropIndex"; collection: string; index: string }
| { kind: "dropIndexes"; collection: string; indexes?: string };
export type MongoWriteCommand = Extract<MongoCommand, { kind: MongoWriteKind }>;
export interface ParsedMongoCommand {
text: string;
command: MongoCommand;
}
export interface ParsedMongoCommandRange extends ParsedMongoCommand {
from: number;
to: number;
}
export interface MongoAggregateSafetyOptions {
allowWrites?: boolean;
allowDangerous?: boolean;
@@ -241,6 +261,65 @@ export function parseMongoWriteCommand(input: string): MongoWriteCommand | null
return null;
}
export function parseMongoCommand(input: string): ParsedMongoCommand | null {
const text = trimMongoOuterComments(input);
if (!text) return null;
// Keep the more specific readers ahead of generic write parsing so the
// returned kind matches the result renderer we want to use downstream.
const parsers: Array<(source: string) => MongoCommand | null> = [
(source) => {
const find = parseMongoFindCommand(source);
return find ? { kind: "find", ...find } : null;
},
(source) => {
const version = parseMongoVersionCommand(source);
return version ?? null;
},
(source) => {
const count = parseMongoCountDocumentsCommand(source);
return count ? { kind: "countDocuments", ...count } : null;
},
(source) => {
const aggregate = parseMongoAggregateCommand(source);
return aggregate ? { kind: "aggregate", ...aggregate } : null;
},
(source) => {
const getIndexes = parseMongoGetIndexesCommand(source);
return getIndexes ? { kind: "getIndexes", ...getIndexes } : null;
},
(source) => {
const write = parseMongoWriteCommand(source);
return write ?? null;
},
(source) => {
const use = parseMongoUseCommand(source);
return use ? { kind: "use", ...use } : null;
},
];
for (const parse of parsers) {
const command = parse(text);
if (command) return { text, command };
}
return null;
}
export function splitMongoCommands(input: string): ParsedMongoCommand[] {
return splitMongoCommandRanges(input).map(({ from: _from, to: _to, ...command }) => command);
}
export function splitMongoCommandRanges(input: string): ParsedMongoCommandRange[] {
const commands: ParsedMongoCommandRange[] = [];
for (const segment of splitMongoCommandTextRanges(input)) {
const parsed = parseMongoCommand(segment.text);
if (!parsed) return [];
commands.push({ from: segment.from, to: segment.to, ...parsed });
}
return commands;
}
export function evaluateMongoWriteSafety(command: MongoWriteCommand, options: MongoAggregateSafetyOptions): { allowed: boolean; reason?: string } {
if (!options.allowWrites) {
return {
@@ -447,6 +526,263 @@ function parseMethodArgs(source: string, methodCallIndex: number): string[] | nu
return splitTopLevel(source.slice(openIndex + 1, closeIndex));
}
interface MongoTextRange {
from: number;
to: number;
text: string;
}
function splitMongoCommandTextRanges(input: string): MongoTextRange[] {
const commands: MongoTextRange[] = [];
for (const segment of splitMongoSemicolonSeparatedSegments(input)) {
const parsed = parseMongoCommand(segment.text);
if (parsed) {
commands.push({ ...segment, text: parsed.text });
continue;
}
// Mongo shell users often omit semicolons and rely on one top-level
// command per line, so fall back to a conservative newline split.
const softSplit = splitMongoSegmentAtSoftStarts(segment);
if (softSplit.length > 1) {
commands.push(...softSplit);
continue;
}
commands.push(segment);
}
return commands;
}
function splitMongoSemicolonSeparatedSegments(input: string): MongoTextRange[] {
const segments: MongoTextRange[] = [];
let start = 0;
let depth = 0;
let quote: string | null = null;
let escaped = false;
let lineComment = false;
let blockComment = false;
// Respect semicolons only when they appear at the top level; JSON literals,
// strings and comments are allowed to contain semicolons verbatim.
for (let i = 0; i < input.length; i += 1) {
const char = input[i] ?? "";
const next = input[i + 1] ?? "";
if (lineComment) {
if (char === "\n") lineComment = false;
continue;
}
if (blockComment) {
if (char === "*" && next === "/") {
blockComment = false;
i += 1;
}
continue;
}
if (quote) {
if (escaped) escaped = false;
else if (char === "\\") escaped = true;
else if (char === quote) quote = null;
continue;
}
if (char === "/" && next === "/") {
lineComment = true;
i += 1;
continue;
}
if (char === "/" && next === "*") {
blockComment = true;
i += 1;
continue;
}
if (char === '"' || char === "'" || char === "`") {
quote = char;
continue;
}
if (char === "{" || char === "[" || char === "(") depth += 1;
else if ((char === "}" || char === "]" || char === ")") && depth > 0) depth -= 1;
else if (char === ";" && depth === 0) {
pushMongoSegment(segments, input, start, i);
start = i + 1;
}
}
pushMongoSegment(segments, input, start, input.length);
return segments;
}
function splitMongoSegmentAtSoftStarts(segment: MongoTextRange): MongoTextRange[] {
const boundaries = mongoTopLevelCommandLineStarts(segment.text);
if (boundaries.length <= 1) return [segment];
const segments: MongoTextRange[] = [];
let start = boundaries[0] ?? 0;
for (let index = 1; index < boundaries.length; index += 1) {
const boundary = boundaries[index] ?? 0;
const candidate = trimMongoOuterCommentRange(segment.text, start, boundary);
// Only accept newline-based splitting when every slice is a valid command;
// otherwise keep the original text intact and let normal parsing reject it.
if (!candidate || !parseMongoCommand(candidate.text)) return [segment];
segments.push({
from: segment.from + candidate.from,
to: segment.from + candidate.to,
text: candidate.text,
});
start = boundary;
}
const last = trimMongoOuterCommentRange(segment.text, start, segment.text.length);
if (!last || !parseMongoCommand(last.text)) return [segment];
segments.push({
from: segment.from + last.from,
to: segment.from + last.to,
text: last.text,
});
return segments;
}
function mongoTopLevelCommandLineStarts(segment: string): number[] {
const starts: number[] = [];
let depth = 0;
let quote: string | null = null;
let escaped = false;
let lineComment = false;
let blockComment = false;
let lineStart = 0;
let firstNonWhitespaceOnLine = -1;
for (let i = 0; i < segment.length; i += 1) {
const char = segment[i] ?? "";
const next = segment[i + 1] ?? "";
if (char === "\n") {
if (lineComment) lineComment = false;
lineStart = i + 1;
firstNonWhitespaceOnLine = -1;
continue;
}
if (lineComment) continue;
if (blockComment) {
if (char === "*" && next === "/") {
blockComment = false;
i += 1;
}
continue;
}
if (quote) {
if (escaped) escaped = false;
else if (char === "\\") escaped = true;
else if (char === quote) quote = null;
continue;
}
if (char === "/" && next === "/") {
lineComment = true;
i += 1;
continue;
}
if (char === "/" && next === "*") {
blockComment = true;
i += 1;
continue;
}
if (char === '"' || char === "'" || char === "`") {
if (firstNonWhitespaceOnLine === -1 && !/\s/.test(char)) firstNonWhitespaceOnLine = i;
quote = char;
continue;
}
if (char === "{" || char === "[" || char === "(") depth += 1;
else if ((char === "}" || char === "]" || char === ")") && depth > 0) depth -= 1;
if (firstNonWhitespaceOnLine === -1 && !/\s/.test(char)) {
firstNonWhitespaceOnLine = i;
if (depth === 0 && char !== "." && isMongoCommandLineStart(segment, i)) starts.push(i);
}
}
return starts.length > 0 ? starts : [lineStart];
}
function isMongoCommandLineStart(segment: string, index: number): boolean {
const rest = segment.slice(index);
return /^use\b/i.test(rest) || /^db(?:\s*\.|\b)/i.test(rest);
}
function pushMongoSegment(segments: MongoTextRange[], source: string, from: number, to: number) {
const trimmed = trimMongoOuterCommentRange(source, from, to);
if (trimmed) segments.push(trimmed);
}
function trimMongoOuterComments(source: string): string {
let value = source.trim();
while (value) {
const next = value.replace(/^(?:\/\/[^\n]*(?:\n|$)|\/\*[\s\S]*?\*\/)\s*/u, "");
if (next === value) break;
value = next.trimStart();
}
while (value) {
const next = value.replace(/\s*(?:\/\/[^\n]*|\/\*[\s\S]*?\*\/)\s*$/u, "");
if (next === value) break;
value = next.trimEnd();
}
return value.trim();
}
function trimMongoOuterCommentRange(source: string, from: number, to: number): MongoTextRange | null {
let start = from;
let end = to;
while (start < end) {
const value = source.slice(start, end);
const trimmed = value.trimStart();
if (trimmed !== value) {
start += value.length - trimmed.length;
continue;
}
const next = value.replace(/^(?:\/\/[^\n]*(?:\n|$)|\/\*[\s\S]*?\*\/)\s*/u, "");
if (next !== value) {
start += value.length - next.length;
continue;
}
break;
}
while (start < end) {
const value = source.slice(start, end);
const trimmed = value.trimEnd();
if (trimmed !== value) {
end -= value.length - trimmed.length;
continue;
}
const next = value.replace(/\s*(?:\/\/[^\n]*|\/\*[\s\S]*?\*\/)\s*$/u, "");
if (next !== value) {
end -= value.length - next.length;
continue;
}
break;
}
if (start >= end) return null;
return {
from: start,
to: end,
text: source.slice(start, end),
};
}
function convertSingleQuotedStrings(source: string): string {
let result = "";
let copiedUntil = 0;
@@ -52,6 +52,11 @@ export async function resolveExecutableSqlWithBackend(fullSql: string, selectedS
const trimmedSelection = selectedSql.trim();
if (trimmedSelection) return trimmedSelection;
// MongoDB uses dedicated per-command gutter actions for "current command";
// the main editor execute action keeps its long-standing "run all text"
// behavior when nothing is selected.
if (options?.databaseType === "mongodb") return fullSql;
if (options?.mode === "current" && options.cursorPos !== undefined) {
return await api.findStatementAtCursor(fullSql, options.cursorPos, options.databaseType);
}
@@ -1,4 +1,5 @@
import type { SqlExecutionCandidate } from "./sqlExecutionTarget";
import { splitMongoCommandRanges } from "./mongoShellCommand";
import type { DatabaseType } from "@/types/database";
/**
@@ -1103,6 +1104,7 @@ function normalizeSql(sql: string): string {
*/
export function executableStatementRanges(sql: string, databaseType?: DatabaseType): SqlTextRange[] {
if (databaseType === "redis") return redisExecutableCommandRanges(sql);
if (databaseType === "mongodb") return splitMongoCommandRanges(sql).map(({ from, to, text }) => ({ from, to, sql: text }));
return splitSqlStatementRanges(sql, databaseType).flatMap((statement) => splitStatementRangeAtSoftStarts(sql, statement, databaseType).map((range) => rangeFor(range, sql)));
}
+201 -271
View File
@@ -19,13 +19,7 @@ import {
mongoUseToQueryResult,
mongoVersionToQueryResult,
mongoWriteToQueryResult,
parseMongoAggregateCommand,
parseMongoCountDocumentsCommand,
parseMongoFindCommand,
parseMongoGetIndexesCommand,
parseMongoWriteCommand,
parseMongoUseCommand,
parseMongoVersionCommand,
splitMongoCommands,
type MongoAggregateSafetyOptions,
} from "@/lib/mongoShellCommand";
import { redisCommandResultToQueryResult } from "@/lib/redisQueryResult";
@@ -1605,14 +1599,24 @@ export const useQueryStore = defineStore("query", () => {
let useAgentResultSession = false;
try {
const connStore = useConnectionStore();
let conn = connStore.getConfig(tab.connectionId);
const parsedMongoCommands = conn?.db_type === "mongodb" ? splitMongoCommands(sql) : undefined;
let mongoCommands = parsedMongoCommands ?? [];
const mongoNeedsConnection = mongoCommands.some(({ command }) => command.kind !== "use");
if (options?.skipEnsureConnected) {
console.info("[DBX][executeTabSql:ensure-connected:skip]", { traceId, elapsed: elapsed(), reason: "caller" });
} else if (conn?.db_type === "mongodb" && mongoCommands.length > 0 && !mongoNeedsConnection) {
console.info("[DBX][executeTabSql:ensure-connected:skip]", { traceId, elapsed: elapsed(), reason: "mongo-use-only" });
} else {
console.info("[DBX][executeTabSql:ensure-connected:start]", { traceId, elapsed: elapsed() });
await connStore.ensureConnected(tab.connectionId);
console.info("[DBX][executeTabSql:ensure-connected:done]", { traceId, elapsed: elapsed() });
}
const conn = connStore.getConfig(tab.connectionId);
conn = connStore.getConfig(tab.connectionId);
if (parsedMongoCommands === undefined && conn?.db_type === "mongodb") {
mongoCommands = splitMongoCommands(sql);
}
const effectiveDbType = effectiveDatabaseTypeForConnection(conn);
const useAgentCursor = usesAgentCursorForQuery(conn?.db_type);
const queryTimeoutSecs = queryTimeoutSecsForConnection(conn);
@@ -1690,6 +1694,195 @@ export const useQueryStore = defineStore("query", () => {
return;
}
if (mongoCommands.length > 0) {
console.info("[DBX][executeTabSql:mongo:start]", { traceId, database: tab.database, commandCount: mongoCommands.length, sql });
const allResults: QueryResult[] = [];
// Track the effective db as we walk the batch so later commands observe
// earlier `use ...` statements in the same editor selection.
let currentDatabase = tab.database;
let mongoEditTarget: QueryTab["mongoEditTarget"] | undefined;
for (const parsedCommand of mongoCommands) {
const mongoCommand = parsedCommand.command;
const commandStartedAt = performance.now();
try {
switch (mongoCommand.kind) {
case "find": {
console.info("[DBX][executeTabSql:mongo-find:start]", { traceId, collection: mongoCommand.collection, database: currentDatabase });
const result = await api.mongoFindDocuments(tab.connectionId, currentDatabase, mongoCommand.collection, mongoCommand.skip, mongoCommand.limit, mongoCommand.filter, mongoCommand.projection, mongoCommand.sort, executionId);
const queryResult = markQueryResultRowsRaw(mongoDocumentsToQueryResult(result.documents, performance.now() - commandStartedAt, result.total));
allResults.push(queryResult);
mongoEditTarget = mongoCommands.length === 1 && queryResult.columns.includes("_id") ? { collection: mongoCommand.collection, idColumn: "_id" } : undefined;
console.info("[DBX][executeTabSql:mongo-find:done]", {
traceId,
collection: mongoCommand.collection,
database: currentDatabase,
rowCount: result.documents.length,
total: result.total,
elapsed: elapsed(),
});
break;
}
case "version": {
console.info("[DBX][executeTabSql:mongo-version:start]", { traceId, database: currentDatabase });
const version = await api.mongoServerVersion(tab.connectionId, currentDatabase, executionId);
allResults.push(markQueryResultRowsRaw(mongoVersionToQueryResult(version, performance.now() - commandStartedAt)));
mongoEditTarget = undefined;
console.info("[DBX][executeTabSql:mongo-version:done]", {
traceId,
database: currentDatabase,
version,
elapsed: elapsed(),
});
break;
}
case "countDocuments": {
console.info("[DBX][executeTabSql:mongo-count:start]", { traceId, collection: mongoCommand.collection, database: currentDatabase });
const result = await api.mongoFindDocuments(tab.connectionId, currentDatabase, mongoCommand.collection, 0, 1, mongoCommand.filter, undefined, undefined, executionId);
allResults.push(markQueryResultRowsRaw(mongoCountToQueryResult(result.total, performance.now() - commandStartedAt)));
mongoEditTarget = undefined;
console.info("[DBX][executeTabSql:mongo-count:done]", {
traceId,
collection: mongoCommand.collection,
database: currentDatabase,
total: result.total,
elapsed: elapsed(),
});
break;
}
case "aggregate": {
if (options?.mongoSafety) {
const safety = evaluateMongoAggregateSafety(mongoCommand, options.mongoSafety);
if (!safety.allowed) throw new Error(safety.reason);
}
console.info("[DBX][executeTabSql:mongo-aggregate:start]", { traceId, collection: mongoCommand.collection, database: currentDatabase });
const aggregateMaxRows = normalizeResultPageSize(pageLimit ?? options?.pagination?.limit ?? settingsStore.editorSettings.pageSize);
const result = await api.mongoAggregateDocuments(tab.connectionId, currentDatabase, mongoCommand.collection, mongoCommand.pipeline, aggregateMaxRows, executionId);
allResults.push(markQueryResultRowsRaw(mongoDocumentsToQueryResult(result.documents, performance.now() - commandStartedAt, result.total)));
mongoEditTarget = undefined;
console.info("[DBX][executeTabSql:mongo-aggregate:done]", {
traceId,
collection: mongoCommand.collection,
database: currentDatabase,
rowCount: result.documents.length,
total: result.total,
elapsed: elapsed(),
});
break;
}
case "getIndexes": {
console.info("[DBX][executeTabSql:mongo-indexes:start]", { traceId, collection: mongoCommand.collection, database: currentDatabase });
const indexes = await api.listIndexes(tab.connectionId, currentDatabase, "", mongoCommand.collection);
allResults.push(markQueryResultRowsRaw(mongoIndexesToQueryResult(indexes, performance.now() - commandStartedAt)));
mongoEditTarget = undefined;
console.info("[DBX][executeTabSql:mongo-indexes:done]", {
traceId,
collection: mongoCommand.collection,
database: currentDatabase,
indexCount: indexes.length,
elapsed: elapsed(),
});
break;
}
case "insert":
case "update":
case "delete":
case "createIndex":
case "dropIndex":
case "dropIndexes": {
if (options?.mongoSafety) {
const safety = evaluateMongoWriteSafety(mongoCommand, options.mongoSafety);
if (!safety.allowed) throw new Error(safety.reason);
}
console.info("[DBX][executeTabSql:mongo-write:start]", {
traceId,
database: currentDatabase,
kind: mongoCommand.kind,
collection: mongoCommand.collection,
});
mongoEditTarget = undefined;
if (mongoCommand.kind === "insert") {
const result = await api.mongoInsertDocuments(tab.connectionId, currentDatabase, mongoCommand.collection, mongoCommand.docsJson);
allResults.push(markQueryResultRowsRaw(mongoWriteToQueryResult(result.affected_rows, performance.now() - commandStartedAt)));
} else if (mongoCommand.kind === "update") {
const result = await api.mongoUpdateDocuments(tab.connectionId, currentDatabase, mongoCommand.collection, mongoCommand.filter, mongoCommand.update, mongoCommand.many);
allResults.push(markQueryResultRowsRaw(mongoWriteToQueryResult(result.affected_rows, performance.now() - commandStartedAt)));
} else if (mongoCommand.kind === "createIndex") {
const result = await api.mongoCreateIndex(tab.connectionId, currentDatabase, mongoCommand.collection, mongoCommand.keys, mongoCommand.options);
allResults.push(markQueryResultRowsRaw(mongoCreateIndexToQueryResult(result.name, performance.now() - commandStartedAt)));
} else if (mongoCommand.kind === "dropIndex" || mongoCommand.kind === "dropIndexes") {
const result = await api.mongoDropIndexes(tab.connectionId, currentDatabase, mongoCommand.collection, mongoCommand.kind === "dropIndex" ? mongoCommand.index : mongoCommand.indexes, mongoCommand.kind === "dropIndex");
allResults.push(markQueryResultRowsRaw(mongoDroppedIndexesToQueryResult(result.dropped_names, performance.now() - commandStartedAt)));
} else {
const result = await api.mongoDeleteDocuments(tab.connectionId, currentDatabase, mongoCommand.collection, mongoCommand.filter, mongoCommand.many);
allResults.push(markQueryResultRowsRaw(mongoWriteToQueryResult(result.affected_rows, performance.now() - commandStartedAt)));
}
console.info("[DBX][executeTabSql:mongo-write:done]", {
traceId,
database: currentDatabase,
kind: mongoCommand.kind,
collection: mongoCommand.collection,
elapsed: elapsed(),
});
break;
}
case "use": {
currentDatabase = mongoCommand.database;
allResults.push(markQueryResultRowsRaw(mongoUseToQueryResult(currentDatabase, performance.now() - commandStartedAt)));
mongoEditTarget = undefined;
console.info("[DBX][executeTabSql:mongo-use:done]", {
traceId,
database: currentDatabase,
elapsed: elapsed(),
});
break;
}
}
} catch (error: any) {
// Surface per-command failures inline and continue collecting results
// for the rest of the batch, matching the grouped-result UX.
allResults.push(toErrorResult(error));
mongoEditTarget = undefined;
}
}
console.info("[DBX][executeTabSql:mongo:done]", {
traceId,
database: currentDatabase,
commandCount: mongoCommands.length,
elapsed: elapsed(),
});
const current = tabs.value.find((t) => t.id === id);
if (current?.executionId === executionId) {
if (allResults.length > 1) {
// Open grouped output on the first non-error result when possible so
// mixed success/error batches land on the most useful table first.
const activeResultIndex = allResults.findIndex((result) => !result.columns.includes("Error"));
const resultIndex = activeResultIndex >= 0 ? activeResultIndex : 0;
current.results = allResults;
current.activeResultIndex = resultIndex;
current.result = allResults[resultIndex];
} else {
current.results = undefined;
current.activeResultIndex = undefined;
current.result = allResults[0];
}
touchResult(current);
current.queryAnalysis = undefined;
current.querySourceColumns = undefined;
current.queryEditabilityReason = undefined;
current.mongoEditTarget = mongoCommands.length === 1 ? mongoEditTarget : undefined;
current.tableMeta = undefined;
current.resultBaseSql = options?.resultBaseSql ?? sql;
current.resultSortedSql = options?.resultSortedSql;
syncDisplayedResultRun(current, options?.resultBaseSql ?? sql);
if (current.database !== currentDatabase) current.database = currentDatabase;
}
return;
}
if (tab.mode === "query") {
const pagination = options?.pagination ?? { limit: settingsStore.editorSettings.pageSize, offset: 0 };
const plan = await api.prepareQueryPaginationExecutionPlan({
@@ -1709,269 +1902,6 @@ export const useQueryStore = defineStore("query", () => {
pageLimit = options?.pagination?.limit ?? tableOpenPageLimit();
pageOffset = options?.pagination?.offset ?? 0;
}
const mongoFind = conn?.db_type === "mongodb" ? parseMongoFindCommand(sql) : null;
if (mongoFind) {
await connStore.ensureConnected(tab.connectionId);
console.info("[DBX][executeTabSql:mongo-find:start]", { traceId, collection: mongoFind.collection });
const result = await api.mongoFindDocuments(tab.connectionId, tab.database, mongoFind.collection, mongoFind.skip, mongoFind.limit, mongoFind.filter, mongoFind.projection, mongoFind.sort, executionId);
console.info("[DBX][executeTabSql:mongo-find:done]", {
traceId,
rowCount: result.documents.length,
total: result.total,
elapsed: elapsed(),
});
const current = tabs.value.find((t) => t.id === id);
if (current?.executionId === executionId) {
current.results = undefined;
current.activeResultIndex = undefined;
current.result = markQueryResultRowsRaw(mongoDocumentsToQueryResult(result.documents, performance.now() - startedAt, result.total));
touchResult(current);
current.queryAnalysis = undefined;
current.querySourceColumns = undefined;
current.queryEditabilityReason = undefined;
current.mongoEditTarget = current.result.columns.includes("_id") ? { collection: mongoFind.collection, idColumn: "_id" } : undefined;
current.tableMeta = undefined;
current.resultBaseSql = options?.resultBaseSql ?? sql;
current.resultSortedSql = options?.resultSortedSql;
syncDisplayedResultRun(current, options?.resultBaseSql ?? sql);
}
return;
}
const mongoVersion = conn?.db_type === "mongodb" ? parseMongoVersionCommand(sql) : null;
if (mongoVersion) {
await connStore.ensureConnected(tab.connectionId);
console.info("[DBX][executeTabSql:mongo-version:start]", { traceId });
const version = await api.mongoServerVersion(tab.connectionId, tab.database, executionId);
console.info("[DBX][executeTabSql:mongo-version:done]", {
traceId,
version,
elapsed: elapsed(),
});
const current = tabs.value.find((t) => t.id === id);
if (current?.executionId === executionId) {
current.results = undefined;
current.activeResultIndex = undefined;
current.result = markQueryResultRowsRaw(mongoVersionToQueryResult(version, performance.now() - startedAt));
touchResult(current);
current.queryAnalysis = undefined;
current.querySourceColumns = undefined;
current.queryEditabilityReason = undefined;
current.mongoEditTarget = undefined;
current.tableMeta = undefined;
current.resultBaseSql = options?.resultBaseSql ?? sql;
current.resultSortedSql = options?.resultSortedSql;
syncDisplayedResultRun(current, options?.resultBaseSql ?? sql);
}
return;
}
const mongoCount = conn?.db_type === "mongodb" ? parseMongoCountDocumentsCommand(sql) : null;
if (mongoCount) {
await connStore.ensureConnected(tab.connectionId);
console.info("[DBX][executeTabSql:mongo-count:start]", { traceId, collection: mongoCount.collection });
const result = await api.mongoFindDocuments(tab.connectionId, tab.database, mongoCount.collection, 0, 1, mongoCount.filter, undefined, undefined, executionId);
console.info("[DBX][executeTabSql:mongo-count:done]", {
traceId,
total: result.total,
elapsed: elapsed(),
});
const current = tabs.value.find((t) => t.id === id);
if (current?.executionId === executionId) {
current.results = undefined;
current.activeResultIndex = undefined;
current.result = markQueryResultRowsRaw(mongoCountToQueryResult(result.total, performance.now() - startedAt));
touchResult(current);
current.queryAnalysis = undefined;
current.querySourceColumns = undefined;
current.queryEditabilityReason = undefined;
current.mongoEditTarget = undefined;
current.tableMeta = undefined;
current.resultBaseSql = options?.resultBaseSql ?? sql;
current.resultSortedSql = options?.resultSortedSql;
syncDisplayedResultRun(current, options?.resultBaseSql ?? sql);
}
return;
}
const mongoAggregate = conn?.db_type === "mongodb" ? parseMongoAggregateCommand(sql) : null;
if (mongoAggregate) {
if (options?.mongoSafety) {
const safety = evaluateMongoAggregateSafety(mongoAggregate, options.mongoSafety);
if (!safety.allowed) throw new Error(safety.reason);
}
await connStore.ensureConnected(tab.connectionId);
console.info("[DBX][executeTabSql:mongo-aggregate:start]", { traceId, collection: mongoAggregate.collection });
const aggregateMaxRows = normalizeResultPageSize(pageLimit ?? options?.pagination?.limit ?? settingsStore.editorSettings.pageSize);
const result = await api.mongoAggregateDocuments(tab.connectionId, tab.database, mongoAggregate.collection, mongoAggregate.pipeline, aggregateMaxRows, executionId);
console.info("[DBX][executeTabSql:mongo-aggregate:done]", {
traceId,
rowCount: result.documents.length,
total: result.total,
elapsed: elapsed(),
});
const current = tabs.value.find((t) => t.id === id);
if (current?.executionId === executionId) {
current.results = undefined;
current.activeResultIndex = undefined;
current.result = markQueryResultRowsRaw(mongoDocumentsToQueryResult(result.documents, performance.now() - startedAt, result.total));
touchResult(current);
current.queryAnalysis = undefined;
current.querySourceColumns = undefined;
current.queryEditabilityReason = undefined;
current.mongoEditTarget = undefined;
current.tableMeta = undefined;
current.resultBaseSql = options?.resultBaseSql ?? sql;
current.resultSortedSql = options?.resultSortedSql;
syncDisplayedResultRun(current, options?.resultBaseSql ?? sql);
}
return;
}
const mongoGetIndexes = conn?.db_type === "mongodb" ? parseMongoGetIndexesCommand(sql) : null;
if (mongoGetIndexes) {
await connStore.ensureConnected(tab.connectionId);
console.info("[DBX][executeTabSql:mongo-indexes:start]", { traceId, collection: mongoGetIndexes.collection });
const indexes = await api.listIndexes(tab.connectionId, tab.database, "", mongoGetIndexes.collection);
console.info("[DBX][executeTabSql:mongo-indexes:done]", {
traceId,
indexCount: indexes.length,
elapsed: elapsed(),
});
const current = tabs.value.find((t) => t.id === id);
if (current?.executionId === executionId) {
current.results = undefined;
current.activeResultIndex = undefined;
current.result = markQueryResultRowsRaw(mongoIndexesToQueryResult(indexes, performance.now() - startedAt));
touchResult(current);
current.queryAnalysis = undefined;
current.querySourceColumns = undefined;
current.queryEditabilityReason = undefined;
current.mongoEditTarget = undefined;
current.tableMeta = undefined;
current.resultBaseSql = options?.resultBaseSql ?? sql;
current.resultSortedSql = options?.resultSortedSql;
syncDisplayedResultRun(current, options?.resultBaseSql ?? sql);
}
return;
}
const mongoWrite = conn?.db_type === "mongodb" ? parseMongoWriteCommand(sql) : null;
if (mongoWrite) {
if (options?.mongoSafety) {
const safety = evaluateMongoWriteSafety(mongoWrite, options.mongoSafety);
if (!safety.allowed) throw new Error(safety.reason);
}
await connStore.ensureConnected(tab.connectionId);
console.info("[DBX][executeTabSql:mongo-write:start]", {
traceId,
kind: mongoWrite.kind,
collection: mongoWrite.collection,
});
let affectedRows = 0;
if (mongoWrite.kind === "insert") {
const result = await api.mongoInsertDocuments(tab.connectionId, tab.database, mongoWrite.collection, mongoWrite.docsJson);
affectedRows = result.affected_rows;
} else if (mongoWrite.kind === "update") {
const result = await api.mongoUpdateDocuments(tab.connectionId, tab.database, mongoWrite.collection, mongoWrite.filter, mongoWrite.update, mongoWrite.many);
affectedRows = result.affected_rows;
} else if (mongoWrite.kind === "createIndex") {
const result = await api.mongoCreateIndex(tab.connectionId, tab.database, mongoWrite.collection, mongoWrite.keys, mongoWrite.options);
console.info("[DBX][executeTabSql:mongo-write:done]", {
traceId,
indexName: result.name,
elapsed: elapsed(),
});
const current = tabs.value.find((t) => t.id === id);
if (current?.executionId === executionId) {
current.results = undefined;
current.activeResultIndex = undefined;
current.result = markQueryResultRowsRaw(mongoCreateIndexToQueryResult(result.name, performance.now() - startedAt));
touchResult(current);
current.queryAnalysis = undefined;
current.querySourceColumns = undefined;
current.queryEditabilityReason = undefined;
current.mongoEditTarget = undefined;
current.tableMeta = undefined;
current.resultBaseSql = options?.resultBaseSql ?? sql;
current.resultSortedSql = options?.resultSortedSql;
syncDisplayedResultRun(current, options?.resultBaseSql ?? sql);
}
return;
} else if (mongoWrite.kind === "dropIndex" || mongoWrite.kind === "dropIndexes") {
const result = await api.mongoDropIndexes(tab.connectionId, tab.database, mongoWrite.collection, mongoWrite.kind === "dropIndex" ? mongoWrite.index : mongoWrite.indexes, mongoWrite.kind === "dropIndex");
console.info("[DBX][executeTabSql:mongo-write:done]", {
traceId,
droppedNames: result.dropped_names,
elapsed: elapsed(),
});
const current = tabs.value.find((t) => t.id === id);
if (current?.executionId === executionId) {
current.results = undefined;
current.activeResultIndex = undefined;
current.result = markQueryResultRowsRaw(mongoDroppedIndexesToQueryResult(result.dropped_names, performance.now() - startedAt));
touchResult(current);
current.queryAnalysis = undefined;
current.querySourceColumns = undefined;
current.queryEditabilityReason = undefined;
current.mongoEditTarget = undefined;
current.tableMeta = undefined;
current.resultBaseSql = options?.resultBaseSql ?? sql;
current.resultSortedSql = options?.resultSortedSql;
syncDisplayedResultRun(current, options?.resultBaseSql ?? sql);
}
return;
} else {
const result = await api.mongoDeleteDocuments(tab.connectionId, tab.database, mongoWrite.collection, mongoWrite.filter, mongoWrite.many);
affectedRows = result.affected_rows;
}
console.info("[DBX][executeTabSql:mongo-write:done]", {
traceId,
affectedRows,
elapsed: elapsed(),
});
const current = tabs.value.find((t) => t.id === id);
if (current?.executionId === executionId) {
current.results = undefined;
current.activeResultIndex = undefined;
current.result = markQueryResultRowsRaw(mongoWriteToQueryResult(affectedRows, performance.now() - startedAt));
touchResult(current);
current.queryAnalysis = undefined;
current.querySourceColumns = undefined;
current.queryEditabilityReason = undefined;
current.mongoEditTarget = undefined;
current.tableMeta = undefined;
current.resultBaseSql = options?.resultBaseSql ?? sql;
current.resultSortedSql = options?.resultSortedSql;
syncDisplayedResultRun(current, options?.resultBaseSql ?? sql);
}
return;
}
const mongoUse = conn?.db_type === "mongodb" ? parseMongoUseCommand(sql) : null;
if (mongoUse) {
console.info("[DBX][executeTabSql:mongo-use:start]", { traceId, database: mongoUse.database });
const current = tabs.value.find((t) => t.id === id);
if (current?.executionId === executionId) {
current.database = mongoUse.database;
current.results = undefined;
current.activeResultIndex = undefined;
current.result = markQueryResultRowsRaw(mongoUseToQueryResult(mongoUse.database, performance.now() - startedAt));
touchResult(current);
current.queryAnalysis = undefined;
current.querySourceColumns = undefined;
current.queryEditabilityReason = undefined;
current.mongoEditTarget = undefined;
current.tableMeta = undefined;
current.resultBaseSql = options?.resultBaseSql ?? sql;
current.resultSortedSql = options?.resultSortedSql;
syncDisplayedResultRun(current, options?.resultBaseSql ?? sql);
}
console.info("[DBX][executeTabSql:mongo-use:done]", {
traceId,
database: mongoUse.database,
elapsed: elapsed(),
});
return;
}
const executionSchema = connectionUsesSchemaExecutionContext(conn) ? tab.schema || tab.database : tab.mode === "data" || connectionUsesDatabaseObjectTreeMode(conn) ? undefined : tab.schema;
const frontendTimeoutSecs = Math.max(queryTimeoutSecs * 2, 60);