Skip to content

Commit f8acbaa

Browse files
committed
Substatement history
Signed-off-by: worksofliam <[email protected]>
1 parent 7954e8d commit f8acbaa

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

src/Storage.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import vscode from 'vscode';
22

33
const QUERIES_KEY = `queries`;
4-
const SERVERCOMPONENT_KEY = `serverVersion`
4+
const SERVERCOMPONENT_KEY = `serverVersion`;
55

66
export interface QueryHistoryItem {
77
query: string;
88
unix: number;
9+
substatements?: string[];
910
starred?: boolean;
1011
}
1112

src/views/queryHistoryView/index.ts

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Config } from "../../config";
44
import { QueryHistoryItem } from "../../Storage";
55

66
const openSqlDocumentCommand = `vscode-db2i.openSqlDocument`;
7+
const openHistoryItemCommand = `vscode-db2i.queryHistory.openItem`;
78

89
export class queryHistory implements TreeDataProvider<any> {
910
private _onDidChangeTreeData: EventEmitter<TreeItem | undefined | null | void> = new EventEmitter<TreeItem | undefined | null | void>();
@@ -19,24 +20,50 @@ export class queryHistory implements TreeDataProvider<any> {
1920
window.showTextDocument(doc);
2021
});
2122
}),
23+
24+
commands.registerCommand(openHistoryItemCommand, (item?: QueryHistoryItem) => {
25+
if (!item) {
26+
return;
27+
}
28+
29+
let content = item.query + `;`;
30+
31+
if (item.substatements && item.substatements.length > 0) {
32+
content += `\n\n-- Substatements: ${item.substatements.length}\n`;
33+
content += item.substatements.map(sub => sub + `;`).join(`\n`);
34+
}
35+
36+
workspace.openTextDocument({
37+
language: `sql`,
38+
content
39+
}).then(doc => {
40+
window.showTextDocument(doc);
41+
});
42+
}),
43+
2244
commands.registerCommand(`vscode-db2i.queryHistory.find`, async () => {
2345
commands.executeCommand('queryHistory.focus');
2446
commands.executeCommand('list.find');
2547
}),
2648

27-
commands.registerCommand(`vscode-db2i.queryHistory.prepend`, async (newQuery?: string) => {
49+
commands.registerCommand(`vscode-db2i.queryHistory.prepend`, async (newQuery?: string, substatement?: string) => {
2850
if (newQuery && Config.ready) {
2951
let currentList = Config.getPastQueries();
3052
const existingQueryi = currentList.findIndex(queryItem => queryItem.query.trim() === newQuery.trim());
31-
const existingQuery = currentList[existingQueryi];
32-
33-
const newQueryItem: QueryHistoryItem = {
53+
const existingQuery = currentList[existingQueryi] || {
3454
query: newQuery,
3555
unix: Math.floor(Date.now() / 1000),
3656
};
3757

38-
if (existingQuery) {
39-
newQueryItem.starred = existingQuery.starred; // Preserve starred status
58+
if (substatement) {
59+
if (!existingQuery.substatements) {
60+
existingQuery.substatements = [];
61+
}
62+
63+
// If the substatement already exists, don't add it again
64+
if (!existingQuery.substatements.includes(substatement)) {
65+
existingQuery.substatements.push(substatement);
66+
}
4067
}
4168

4269
// If it exists, remove it
@@ -46,7 +73,7 @@ export class queryHistory implements TreeDataProvider<any> {
4673

4774
// If it's at the top, don't add it, it's already at the top
4875
if (existingQueryi !== 0) {
49-
currentList.splice(0, 0, newQueryItem);
76+
currentList.splice(0, 0, existingQuery);
5077
}
5178

5279
await Config.setPastQueries(currentList);
@@ -193,11 +220,17 @@ class PastQueryNode extends TreeItem {
193220

194221
this.contextValue = `query`;
195222

196-
this.tooltip = new MarkdownString(['```sql', item.query, '```'].join(`\n`));
223+
let markdownLines = ['```sql', item.query];
224+
225+
if (item.substatements && item.substatements.length > 0) {
226+
markdownLines.push(``, `-- substatements: ${item.substatements.length}`);
227+
}
228+
229+
this.tooltip = new MarkdownString(markdownLines.join(`\n`));
197230

198231
this.command = {
199-
command: openSqlDocumentCommand,
200-
arguments: [item.query],
232+
command: openHistoryItemCommand,
233+
arguments: [item],
201234
title: `Open into new document`
202235
};
203236

src/views/results/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,10 @@ async function runHandler(options?: StatementInfo) {
353353
vscode.window.showErrorMessage(`Incorrect number of parameters for statement. Expected ${runStatement.parameters}, got ${parameters.length}.`);
354354
return;
355355
}
356+
357+
vscode.commands.executeCommand(`vscode-db2i.queryHistory.prepend`, runStatement.statement, `bind: ${statementDetail.content}`);
356358

359+
// Overwrite to run the prior statement
357360
statementDetail.content = runStatement.statement;
358361
}
359362
}

0 commit comments

Comments
 (0)