Skip to content

Commit 65220c7

Browse files
ThePrezworksofliam
andauthored
re-check for server updates if not installed (#74)
Co-authored-by: worksofliam <[email protected]>
1 parent 0101f72 commit 65220c7

File tree

3 files changed

+99
-87
lines changed

3 files changed

+99
-87
lines changed

src/config.ts

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ExtensionContext, commands, window } from "vscode";
22
import { ConnectionStorage } from "./Storage";
33
import { getInstance } from "./base";
44
import { SQLJobManager } from "./connection/manager";
5-
import { ServerComponent } from "./connection/serverComponent";
5+
import { ServerComponent, UpdateStatus } from "./connection/serverComponent";
66
import { JobManagerView } from "./views/jobManager/jobManagerView";
77
import Configuration from "./configuration";
88
import { ConfigManager } from "./views/jobManager/ConfigManager";
@@ -16,58 +16,53 @@ export let Config: ConnectionStorage;
1616
export let OSData: IBMiLevels|undefined;
1717
export let JobManager: SQLJobManager = new SQLJobManager();
1818

19-
export function setupConfig(context: ExtensionContext) {
20-
Config = new ConnectionStorage(context);
19+
export async function onConnectOrServerInstall(): Promise<boolean> {
20+
const instance = getInstance();
2121

22-
getInstance().onEvent(`connected`, async () => {
23-
const instance = getInstance();
22+
Config.setConnectionName(instance.getConnection().currentConnectionName);
2423

25-
Config.setConnectionName(instance.getConnection().currentConnectionName);
24+
await ServerComponent.initialise().then(installed => {
25+
if (installed) {
26+
JobManagerView.setVisible(true);
27+
}
28+
});
2629

27-
await ServerComponent.initialise().then(installed => {
28-
if (installed) {
29-
JobManagerView.setVisible(true);
30-
}
31-
});
32-
33-
ServerComponent.checkForUpdate().then(result => {
34-
if (ServerComponent.isInstalled()) {
35-
JobManagerView.setVisible(true);
36-
37-
let newJob = Configuration.get<string>(`alwaysStartSQLJob`) || `ask`;
38-
if (typeof newJob !== `string`) newJob = `ask`; //For legacy settings where it used to be a boolean
39-
40-
switch (newJob) {
41-
case `ask`:
42-
window.showInformationMessage(`Would you like to start an SQL Job?`, `Yes`, `Always`, `No`).then(async chosen => {
43-
if (chosen === `Yes` || chosen === `Always`) {
44-
if (chosen === `Always`) {
45-
await Configuration.set(`alwaysStartSQLJob`, `new`);
46-
}
47-
48-
commands.executeCommand(`vscode-db2i.jobManager.newJob`);
49-
}
50-
});
51-
break;
52-
53-
case `new`:
54-
commands.executeCommand(`vscode-db2i.jobManager.newJob`);
55-
break;
56-
57-
case `never`:
58-
break;
59-
60-
default:
61-
if (ConfigManager.getConfig(newJob)) {
62-
commands.executeCommand(`vscode-db2i.jobManager.startJobFromConfig`, newJob);
63-
} else {
64-
commands.executeCommand(`vscode-db2i.jobManager.newJob`);
65-
}
66-
break;
67-
}
30+
await ServerComponent.checkForUpdate();
31+
32+
if (ServerComponent.isInstalled()) {
33+
JobManagerView.setVisible(true);
34+
35+
let newJob = Configuration.get<string>(`alwaysStartSQLJob`) || `ask`;
36+
if (typeof newJob !== `string`) newJob = `ask`; //For legacy settings where it used to be a boolean
37+
38+
switch (newJob) {
39+
case `ask`:
40+
return await askAboutNewJob();
41+
42+
case `new`:
43+
await commands.executeCommand(`vscode-db2i.jobManager.newJob`);
44+
return true;
45+
46+
case `never`:
47+
break;
48+
49+
default:
50+
if (ConfigManager.getConfig(newJob)) {
51+
await commands.executeCommand(`vscode-db2i.jobManager.startJobFromConfig`, newJob);
52+
} else {
53+
await commands.executeCommand(`vscode-db2i.jobManager.newJob`);
54+
return true;
6855
}
69-
});
70-
});
56+
break;
57+
}
58+
}
59+
return false;
60+
}
61+
62+
export function setupConfig(context: ExtensionContext) {
63+
Config = new ConnectionStorage(context);
64+
65+
getInstance().onEvent(`connected`, onConnectOrServerInstall);
7166

7267
getInstance().onEvent(`disconnected`, async () => {
7368
JobManagerView.setVisible(false);
@@ -98,4 +93,18 @@ export async function fetchSystemInfo() {
9893
db2Level
9994
}
10095
}
96+
}
97+
98+
export async function askAboutNewJob(): Promise<boolean> {
99+
const chosen = await window.showInformationMessage(`Would you like to start an SQL Job?`, `Yes`, `Always`, `No`);
100+
if (chosen === `Yes` || chosen === `Always`) {
101+
if (chosen === `Always`) {
102+
await Configuration.set(`alwaysStartSQLJob`, `new`);
103+
}
104+
105+
await commands.executeCommand(`vscode-db2i.jobManager.newJob`);
106+
return true;
107+
}
108+
109+
return false;
101110
}

src/connection/manager.ts

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
12
import { getInstance } from "../base";
23
import { Query } from "./query";
3-
import { ServerComponent } from "./serverComponent";
4+
import { ServerComponent, UpdateStatus } from "./serverComponent";
45
import { JobStatus, SQLJob } from "./sqlJob";
5-
import { QueryOptions, QueryResult, Rows } from "./types";
6+
import { QueryOptions } from "./types";
7+
import { askAboutNewJob, onConnectOrServerInstall } from "../config";
68

79
export interface JobInfo {
810
name: string;
@@ -14,17 +16,17 @@ export class SQLJobManager {
1416
private jobs: JobInfo[] = [];
1517
selectedJob: number = -1;
1618

17-
constructor() {}
19+
constructor() { }
1820

1921
async newJob(predefinedJob?: SQLJob, name?: string) {
2022
if (ServerComponent.isInstalled()) {
2123
const instance = getInstance();
2224
const config = instance.getConfig();
2325

2426
const newJob = predefinedJob || (new SQLJob({
25-
libraries: [config.currentLibrary, ...config.libraryList],
26-
naming: `system`,
27-
"full open": false,
27+
libraries: [config.currentLibrary, ...config.libraryList],
28+
naming: `system`,
29+
"full open": false,
2830
"transaction isolation": "none",
2931
"query optimize goal": "1",
3032
"block size": "512"
@@ -40,7 +42,7 @@ export class SQLJobManager {
4042
job: newJob
4143
});
4244

43-
this.selectedJob = this.jobs.length-1;
45+
this.selectedJob = this.jobs.length - 1;
4446
} catch (e: any) {
4547
throw e;
4648
}
@@ -60,10 +62,10 @@ export class SQLJobManager {
6062
async closeJob(index?: number) {
6163
if (this.jobs[index]) {
6264
const selected: JobInfo = this.jobs[index];
63-
65+
6466
selected.job.close();
6567
this.jobs.splice(index, 1);
66-
this.selectedJob = this.selectedJob-1;
68+
this.selectedJob = this.selectedJob - 1;
6769
}
6870
}
6971

@@ -72,11 +74,11 @@ export class SQLJobManager {
7274
return this.closeJob(id);
7375
}
7476

75-
getSelection(): JobInfo|undefined {
77+
getSelection(): JobInfo | undefined {
7678
return this.jobs[this.selectedJob];
7779
}
7880

79-
getJob(name: string): JobInfo|undefined {
81+
getJob(name: string): JobInfo | undefined {
8082
return this.jobs.find(info => info.name === name);
8183
}
8284

@@ -89,37 +91,37 @@ export class SQLJobManager {
8991
}
9092

9193
async runSQL<T>(query: string, parameters: any[] = []): Promise<T[]> {
92-
const selected = this.jobs[this.selectedJob]
93-
if (ServerComponent.isInstalled() && selected) {
94-
// 2147483647 is NOT arbitrary. On the server side, this is processed as a Java
95-
// int. This is the largest number available without overflow (Integer.MAX_VALUE)
96-
const rowsToFetch = 2147483647;
97-
98-
const statement = selected.job.query<T>(query, {parameters});
99-
const results = await statement.run(rowsToFetch);
100-
statement.close();
101-
return results.data;
102-
} else {
103-
const instance = getInstance();
104-
const config = instance.getConfig();
105-
const content = instance.getContent();
106-
107-
const queryContext = [
108-
`SET CURRENT SCHEMA = '${config.currentLibrary.toUpperCase()}'`,
109-
query
110-
].join(`;\n`);
111-
112-
return content.runSQL(queryContext) as Promise<T[]>;
113-
}
94+
// 2147483647 is NOT arbitrary. On the server side, this is processed as a Java
95+
// int. This is the largest number available without overflow (Integer.MAX_VALUE)
96+
const rowsToFetch = 2147483647;
97+
98+
const statement = await this.getPagingStatement<T>(query, { parameters });
99+
const results = await statement.run(rowsToFetch);
100+
statement.close();
101+
return results.data;
114102
}
115-
getPagingStatement<T>(query: string, opts?: QueryOptions): Query<T> {
103+
104+
async getPagingStatement<T>(query: string, opts?: QueryOptions): Promise<Query<T>> {
116105
const selected = this.jobs[this.selectedJob]
117106
if (ServerComponent.isInstalled() && selected) {
118107
return selected.job.query<T>(query, opts);
119-
} else if(!ServerComponent.isInstalled()) {
108+
109+
} else if (!ServerComponent.isInstalled()) {
110+
let updateResult = await ServerComponent.checkForUpdate();
111+
if (UpdateStatus.JUST_UPDATED === updateResult) {
112+
await onConnectOrServerInstall();
113+
return this.getPagingStatement(query, opts);
114+
}
120115
throw new Error(`Database server component is required. Please see documentation for details.`);
121-
}else {
122-
throw new Error(`Active SQL job is required. Please spin one up first.`);
116+
117+
} else {
118+
const hasNewJob = await askAboutNewJob();
119+
120+
if (hasNewJob) {
121+
return this.getPagingStatement(query, opts);
122+
} else {
123+
throw new Error(`Active SQL job is required. Please spin one up in the 'SQL Job Manager' view and try again.`);
124+
}
123125
}
124126
}
125127
}

src/connection/serverComponent.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class ServerComponent {
115115
const lastInstalledName = Config.getServerComponentName();
116116

117117
if (lastInstalledName !== basename || this.installed === false) {
118-
const updateQuestion = await window.showInformationMessage(`An update to the database server component is available: ${basename}`, `Update`);
118+
const updateQuestion = await window.showInformationMessage(`An update to the database server component is required: ${basename}`, `Update`);
119119

120120
if (updateQuestion === `Update`) {
121121
// This means we're currently running a different version,
@@ -162,6 +162,7 @@ export class ServerComponent {
162162

163163
} else {
164164
// Uh oh. A release was made by there's no jar file??
165+
ServerComponent.writeOutput('Unable to get file name from server component release');
165166
updateResult = UpdateStatus.NONE_AVAILABLE;
166167
}
167168
} catch (e) {

0 commit comments

Comments
 (0)