feat(mongodb): support use command to switch database

This commit is contained in:
t8y2
2026-06-22 16:42:38 +08:00
parent d9256fc1ec
commit 09d54d09bc
2 changed files with 50 additions and 0 deletions
+22
View File
@@ -22,6 +22,10 @@ export interface MongoGetIndexesCommand {
collection: string;
}
export interface MongoUseCommand {
database: string;
}
export type MongoWriteCommand = { kind: "insert"; collection: string; docsJson: string } | { kind: "update"; collection: string; filter: string; update: string; many: boolean } | { kind: "delete"; collection: string; filter: string; many: boolean };
export interface MongoAggregateSafetyOptions {
@@ -130,6 +134,15 @@ export function parseMongoGetIndexesCommand(input: string): MongoGetIndexesComma
};
}
export function parseMongoUseCommand(input: string): MongoUseCommand | null {
const source = input.trim().replace(/;$/, "").trim();
const match = /^use\s+([a-zA-Z0-9_-]+)$/i.exec(source);
if (!match) return null;
return {
database: match[1],
};
}
export function parseMongoWriteCommand(input: string): MongoWriteCommand | null {
const source = input.trim().replace(/;$/, "").trim();
const insertOne = parseCollectionMethodTarget(source, "insertOne");
@@ -251,6 +264,15 @@ export function mongoWriteToQueryResult(affectedRows: number, executionTimeMs: n
};
}
export function mongoUseToQueryResult(database: string, executionTimeMs: number): QueryResult {
return {
columns: ["message"],
rows: [[`switched to db ${database}`]],
affected_rows: 0,
execution_time_ms: Math.max(0, Math.round(executionTimeMs)),
};
}
export function mongoIndexesToQueryResult(
indexes: {
name: string;
+28
View File
@@ -15,11 +15,13 @@ import {
mongoDocumentsToQueryResult,
mongoIndexesToQueryResult,
mongoWriteToQueryResult,
mongoUseToQueryResult,
parseMongoAggregateCommand,
parseMongoCountDocumentsCommand,
parseMongoFindCommand,
parseMongoGetIndexesCommand,
parseMongoWriteCommand,
parseMongoUseCommand,
type MongoAggregateSafetyOptions,
} from "@/lib/mongoShellCommand";
import { redisCommandResultToQueryResult } from "@/lib/redisQueryResult";
@@ -1538,6 +1540,32 @@ export const useQueryStore = defineStore("query", () => {
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.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;
}
console.info("[DBX][executeTabSql:execute-multi:start]", { traceId, elapsed: elapsed() });
const clientSessionId = tab.mode === "query" ? tab.id : undefined;
const executionOptions = {