fix(query): preserve selected statement source range
This commit is contained in:
1 parent
6bd49fcd19
commit
8cebc4a366
10 files changed
+127
-32
No files matched your search
@@ -111,6 +111,17 @@ describe("query result labels", () => {
|
||||
});
|
||||
|
||||
describe("query result source ranges", () => {
|
||||
it("prefers the preserved editor range for a selected duplicate statement", () => {
|
||||
const sql = "SELECT * FROM users;\nSELECT * FROM users;";
|
||||
const from = sql.lastIndexOf("SELECT");
|
||||
|
||||
expect(resultSourceRange(sql, { sourceStatement: "SELECT * FROM users", sourceFrom: from, sourceTo: sql.length - 1 }, 0, "mysql")).toEqual({
|
||||
from,
|
||||
to: sql.length - 1,
|
||||
sql: "SELECT * FROM users",
|
||||
});
|
||||
});
|
||||
|
||||
it("uses the result index to distinguish repeated statements", () => {
|
||||
const sql = "SELECT * FROM users;\nSELECT * FROM users;";
|
||||
const range = resultSourceRange(sql, { sourceStatement: "SELECT * FROM users" }, 1, "mysql");
|
||||
|
||||
@@ -8,6 +8,8 @@ export interface SqlExecutionSnapshot {
|
||||
fullSql: string;
|
||||
selectedSql: string;
|
||||
cursorPos: number;
|
||||
selectionFrom: number;
|
||||
selectionTo: number;
|
||||
}
|
||||
|
||||
export type SqlExecutionOverride = string | SqlExecutionSnapshot;
|
||||
@@ -35,7 +37,7 @@ export interface SqlExecutionChoiceRequest {
|
||||
}
|
||||
|
||||
export function isSqlExecutionSnapshot(value: SqlExecutionOverride | undefined): value is SqlExecutionSnapshot {
|
||||
return typeof value === "object" && value !== null && typeof value.fullSql === "string" && typeof value.selectedSql === "string" && typeof value.cursorPos === "number";
|
||||
return typeof value === "object" && value !== null && typeof value.fullSql === "string" && typeof value.selectedSql === "string" && typeof value.cursorPos === "number" && typeof value.selectionFrom === "number" && typeof value.selectionTo === "number";
|
||||
}
|
||||
|
||||
export function resolveExecutableSql(fullSql: string, selectedSql: string, options?: { mode?: ExecuteMode; cursorPos?: number }): string {
|
||||
|
||||
@@ -166,9 +166,12 @@ export function resultSqlForGrid(tab: Pick<QueryTab, "result" | "resultBaseSql"
|
||||
* A stale or ambiguous source is ignored instead of highlighting a different
|
||||
* statement that happens to have the same text.
|
||||
*/
|
||||
export function resultSourceRange(editorSql: string, result: Pick<QueryResult, "sourceStatement"> | undefined, resultIndex: number | undefined, databaseType?: DatabaseType): SqlTextRange | undefined {
|
||||
export function resultSourceRange(editorSql: string, result: Pick<QueryResult, "sourceStatement" | "sourceFrom" | "sourceTo"> | undefined, resultIndex: number | undefined, databaseType?: DatabaseType): SqlTextRange | undefined {
|
||||
const sourceStatement = result?.sourceStatement;
|
||||
if (!sourceStatement) return undefined;
|
||||
if (typeof result.sourceFrom === "number" && typeof result.sourceTo === "number" && editorSql.slice(result.sourceFrom, result.sourceTo) === sourceStatement) {
|
||||
return { from: result.sourceFrom, to: result.sourceTo, sql: sourceStatement };
|
||||
}
|
||||
|
||||
const statements = databaseType === "redis" ? executableStatementRanges(editorSql, databaseType) : databaseType === "mongodb" ? splitMongoCommandRanges(editorSql).map(({ from, to, text }) => ({ from, to, sql: text })) : splitSqlStatementRanges(editorSql, databaseType);
|
||||
const indexed = typeof resultIndex === "number" ? statements[resultIndex] : undefined;
|
||||
|
||||
@@ -51,6 +51,8 @@ interface ColumnarQueryResult {
|
||||
has_more?: boolean;
|
||||
sourceLabel?: string;
|
||||
sourceStatement?: string;
|
||||
sourceFrom?: number;
|
||||
sourceTo?: number;
|
||||
}
|
||||
|
||||
type QueryResultRunSnapshot = NonNullable<QueryTab["resultRuns"]>[number];
|
||||
@@ -146,6 +148,8 @@ function stripSessionIds(result: QueryResult | undefined): QueryResult | undefin
|
||||
has_more: result.has_more,
|
||||
sourceLabel: result.sourceLabel,
|
||||
sourceStatement: result.sourceStatement,
|
||||
sourceFrom: result.sourceFrom,
|
||||
sourceTo: result.sourceTo,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -180,6 +184,8 @@ function toColumnarResult(result: QueryResult | undefined): ColumnarQueryResult
|
||||
has_more: result.has_more,
|
||||
sourceLabel: result.sourceLabel,
|
||||
sourceStatement: result.sourceStatement,
|
||||
sourceFrom: result.sourceFrom,
|
||||
sourceTo: result.sourceTo,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -199,6 +205,8 @@ function fromColumnarResult(result: ColumnarQueryResult | undefined): QueryResul
|
||||
has_more: result.has_more,
|
||||
sourceLabel: result.sourceLabel,
|
||||
sourceStatement: result.sourceStatement,
|
||||
sourceFrom: result.sourceFrom,
|
||||
sourceTo: result.sourceTo,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user