Skip to content

Commit 7be8c01

Browse files
authored
Merge pull request #223 from codefori/fix/self_enablement
Cleanup of code that checks OS version and features
2 parents d4e78c6 + 19c94f7 commit 7be8c01

File tree

8 files changed

+123
-90
lines changed

8 files changed

+123
-90
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@
5353
"title": "General",
5454
"order": 1,
5555
"properties": {
56-
"vscode-db2i.autoRefreshSelfCodesView": {
56+
"vscode-db2i.jobSelfViewAutoRefresh": {
5757
"type": "boolean",
58+
"title": "Auto-refresh SELF Codes view",
5859
"description": "Enable auto-refresh for SELF Codes view when connecting to a system",
5960
"default": false
6061
},

src/IBMiDetail.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { getInstance } from "./base";
2+
import { ServerComponent } from "./connection/serverComponent";
3+
4+
export type Db2FeatureIds = `SELF`;
5+
6+
const featureRequirements: { [id in Db2FeatureIds]: { [osVersion: number]: number } } = {
7+
'SELF': {
8+
7.4: 26,
9+
7.5: 5
10+
}
11+
};
12+
13+
export class IBMiDetail {
14+
private version: number = 0;
15+
private db2Level: number = 0;
16+
private features: { [id in Db2FeatureIds]: boolean } = {
17+
'SELF': false
18+
};
19+
20+
setFeatureSupport(featureId: Db2FeatureIds, supported: boolean) {
21+
this.features[featureId] = supported;
22+
}
23+
24+
getVersion() {
25+
return this.version;
26+
}
27+
28+
getDb2Level() {
29+
return this.db2Level;
30+
}
31+
32+
async fetchSystemInfo() {
33+
const instance = getInstance();
34+
const content = instance.getContent();
35+
36+
let levelCheckFailed = false;
37+
38+
const versionResults = await content.runSQL(`select OS_VERSION concat '.' concat OS_RELEASE as VERSION from sysibmadm.env_sys_info`);
39+
this.version = Number(versionResults[0].VERSION);
40+
41+
try {
42+
const db2LevelResults = await content.runSQL([
43+
`select max(ptf_group_level) as HIGHEST_DB2_PTF_GROUP_LEVEL`,
44+
`from qsys2.group_ptf_info`,
45+
`where PTF_GROUP_DESCRIPTION like 'DB2 FOR IBM I%' and`,
46+
`ptf_group_status = 'INSTALLED';`
47+
].join(` `));
48+
49+
this.db2Level = Number(db2LevelResults[0].HIGHEST_DB2_PTF_GROUP_LEVEL);
50+
} catch (e) {
51+
ServerComponent.writeOutput(`Failed to get Db2 level: ${e.message}`);
52+
levelCheckFailed = true;
53+
}
54+
55+
const features = Object.keys(featureRequirements) as Db2FeatureIds[];
56+
for (const featureId of features) {
57+
const requiredLevelForFeature = featureRequirements[featureId][String(this.version)];
58+
const supported = requiredLevelForFeature && this.db2Level >= requiredLevelForFeature;
59+
this.setFeatureSupport(featureId, supported);
60+
}
61+
62+
if (levelCheckFailed) {
63+
const selfSupported = await this.validateSelfInstallation();
64+
this.setFeatureSupport('SELF', selfSupported);
65+
}
66+
}
67+
68+
getFeatures() {
69+
return this.features;
70+
}
71+
72+
private async validateSelfInstallation() {
73+
const instance = getInstance();
74+
const content = instance.getContent();
75+
76+
try {
77+
await content.runSQL(`values SYSIBMADM.SELFCODES`);
78+
79+
// This means we have the SELF feature
80+
return true;
81+
} catch (e) {
82+
// If we can't run this, then we don't have the SELF feature
83+
return false;
84+
}
85+
}
86+
}

src/config.ts

Lines changed: 13 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,24 @@ import Configuration from "./configuration";
88
import { ConfigManager } from "./views/jobManager/ConfigManager";
99
import { Examples, ServiceInfoLabel } from "./views/examples";
1010
import { updateStatusBar } from "./views/jobManager/statusBar";
11-
12-
interface IBMiLevels {
13-
version: number;
14-
db2Level: number;
15-
}
11+
import { IBMiDetail } from "./IBMiDetail";
1612

1713
export let Config: ConnectionStorage;
18-
export let OSData: IBMiLevels|undefined;
14+
export let osDetail: IBMiDetail;
1915
export let JobManager: SQLJobManager = new SQLJobManager();
2016

21-
export type Db2Features = `SELF`;
22-
23-
const featureRequirements: {[id in Db2Features]: {[osVersion: number]: number}} = {
24-
'SELF': {
25-
7.4: 26,
26-
7.5: 5
27-
}
28-
}
2917

3018
export async function onConnectOrServerInstall(): Promise<boolean> {
3119
const instance = getInstance();
3220

3321
Config.setConnectionName(instance.getConnection().currentConnectionName);
34-
determineFeatures();
3522

3623
await Config.fixPastQueries();
3724

25+
osDetail = new IBMiDetail();
26+
27+
await osDetail.fetchSystemInfo();
28+
3829
await ServerComponent.initialise().then(installed => {
3930
if (installed) {
4031
JobManagerView.setVisible(true);
@@ -44,6 +35,7 @@ export async function onConnectOrServerInstall(): Promise<boolean> {
4435
await ServerComponent.checkForUpdate();
4536

4637
updateStatusBar();
38+
toggleViews();
4739

4840
if (ServerComponent.isInstalled()) {
4941
JobManagerView.setVisible(true);
@@ -75,7 +67,7 @@ export async function onConnectOrServerInstall(): Promise<boolean> {
7567
return false;
7668
}
7769

78-
export function setupConfig(context: ExtensionContext) {
70+
export function initConfig(context: ExtensionContext) {
7971
Config = new ConnectionStorage(context);
8072

8173
getInstance().onEvent(`disconnected`, async () => {
@@ -91,55 +83,12 @@ export function setupConfig(context: ExtensionContext) {
9183
});
9284
}
9385

94-
export async function fetchSystemInfo() {
95-
const instance = getInstance();
96-
const content = instance.getContent();
97-
98-
const [versionResults, db2LevelResults] = await Promise.all([
99-
content.runSQL(`select OS_VERSION concat '.' concat OS_RELEASE as VERSION from sysibmadm.env_sys_info`),
100-
content.runSQL([
101-
`select max(ptf_group_level) as HIGHEST_DB2_PTF_GROUP_LEVEL`,
102-
`from qsys2.group_ptf_info`,
103-
`where PTF_GROUP_DESCRIPTION like 'DB2 FOR IBM I%' and`,
104-
`ptf_group_status = 'INSTALLED';`
105-
].join(` `))
106-
]);
107-
108-
const version = Number(versionResults[0].VERSION);
109-
const db2Level = Number(db2LevelResults[0].HIGHEST_DB2_PTF_GROUP_LEVEL);
110-
111-
if (version && db2Level) {
112-
OSData = {
113-
version,
114-
db2Level
115-
}
116-
}
117-
}
118-
119-
export function determineFeatures() {
120-
const result: {[id in Db2Features]: boolean} = {
121-
'SELF': false
122-
};
123-
124-
if (OSData) {
125-
const {version, db2Level} = OSData;
126-
127-
const features = Object.keys(featureRequirements) as Db2Features[];
128-
for (const featureId of features) {
129-
const requiredLevelForFeature = featureRequirements[featureId][String(version)];
130-
const supported = requiredLevelForFeature && db2Level >= requiredLevelForFeature;
131-
commands.executeCommand(`setContext`, `vscode-db2i:${featureId}Supported`, supported);
132-
result[featureId] = supported;
133-
}
134-
}
135-
136-
return result;
137-
}
86+
export function toggleViews() {
87+
const features = osDetail.getFeatures();
13888

139-
export function turnOffAllFeatures() {
140-
const features = Object.keys(featureRequirements);
141-
for (const featureId of features) {
142-
commands.executeCommand(`setContext`, `vscode-db2i:${featureId}Supported`, false);
89+
const featureIds = Object.keys(features) as (keyof typeof features)[];
90+
for (const featureId of featureIds) {
91+
commands.executeCommand(`setContext`, `vscode-db2i:${featureId}Supported`, features[featureId]);
14392
}
14493
}
14594

src/connection/manager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Query } from "./query";
44
import { ServerComponent, UpdateStatus } from "./serverComponent";
55
import { JobStatus, SQLJob } from "./sqlJob";
66
import { QueryOptions } from "./types";
7-
import { askAboutNewJob, determineFeatures, onConnectOrServerInstall } from "../config";
7+
import { askAboutNewJob, onConnectOrServerInstall, osDetail } from "../config";
88
import { SelfValue } from "../views/jobManager/selfCodes/nodes";
99
import Configuration from "../configuration";
1010

@@ -24,7 +24,7 @@ export class SQLJobManager {
2424

2525
async newJob(predefinedJob?: SQLJob, name?: string) {
2626
if (ServerComponent.isInstalled()) {
27-
const features = determineFeatures();
27+
const features = osDetail.getFeatures();
2828

2929
const instance = getInstance();
3030
const config = instance.getConfig();

src/extension.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as JSONServices from "./language/json";
77
import * as resultsProvider from "./views/results";
88

99
import { getInstance, loadBase } from "./base";
10-
import { JobManager, fetchSystemInfo, onConnectOrServerInstall, setupConfig, turnOffAllFeatures } from "./config";
10+
import { JobManager, onConnectOrServerInstall, initConfig } from "./config";
1111
import { queryHistory } from "./views/queryHistoryView";
1212
import { ExampleBrowser } from "./views/examples/exampleBrowser";
1313
import { languageInit } from "./language";
@@ -70,7 +70,7 @@ export function activate(context: vscode.ExtensionContext): Db2i {
7070
JSONServices.initialise(context);
7171
resultsProvider.initialise(context);
7272

73-
setupConfig(context);
73+
initConfig(context);
7474

7575
console.log(`Developer environment: ${process.env.DEV}`);
7676
if (process.env.DEV) {
@@ -81,19 +81,14 @@ export function activate(context: vscode.ExtensionContext): Db2i {
8181
const instance = getInstance();
8282

8383
instance.onEvent(`connected`, () => {
84-
// We need to fetch the system info
85-
fetchSystemInfo().then(() => {
86-
// Refresh the examples when we have it, so we only display certain examples
87-
onConnectOrServerInstall();
84+
selfCodesView.setRefreshEnabled(false);
85+
// Refresh the examples when we have it, so we only display certain examples
86+
onConnectOrServerInstall().then(() => {
8887
exampleBrowser.refresh();
89-
selfCodesView.setRefreshEnabled(Configuration.get(`autoRefreshSelfCodesView`) || false)
90-
})
88+
selfCodesView.setRefreshEnabled(Configuration.get(`jobSelfViewAutoRefresh`) || false)
89+
});
9190
});
9291

93-
instance.onEvent(`disconnected`, () => {
94-
turnOffAllFeatures();
95-
})
96-
9792
return { sqlJobManager: JobManager, sqlJob: (options?: JDBCOptions) => new SQLJob(options) };
9893
}
9994

src/views/examples/exampleBrowser.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Event, EventEmitter, ExtensionContext, MarkdownString, ThemeIcon, TreeDataProvider, TreeItem, TreeItemCollapsibleState, Uri, commands, window, workspace } from "vscode";
22
import { Examples, SQLExample, ServiceInfoLabel } from ".";
3-
import { getInstance } from "../../base";
4-
import { OSData, fetchSystemInfo } from "../../config";
53
import { getServiceInfo } from "../../database/serviceInfo";
4+
import { osDetail } from "../../config";
65

76
export const openExampleCommand = `vscode-db2i.examples.open`;
87

@@ -113,13 +112,14 @@ class SQLExampleItem extends TreeItem {
113112
}
114113

115114
function exampleWorksForOnOS(example: SQLExample): boolean {
116-
if (OSData) {
117-
const myOsVersion = OSData.version;
115+
116+
if (osDetail) {
117+
const myOsVersion = osDetail.getVersion();
118118

119119
// If this example has specific system requirements defined
120120
if (example.requirements &&
121121
example.requirements[myOsVersion] &&
122-
OSData.db2Level < example.requirements[myOsVersion]) {
122+
osDetail.getDb2Level() < example.requirements[myOsVersion]) {
123123
return false;
124124
}
125125
}

src/views/jobManager/jobManagerView.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class JobManagerView implements TreeDataProvider<any> {
2929
...ConfigManager.initialiseSaveCommands(),
3030

3131
vscode.commands.registerCommand(`vscode-db2i.jobManager.defaultSelfSettings`, () => {
32-
vscode.commands.executeCommand('workbench.action.openSettings', 'vscode-db2i.jobSelfDefault');
32+
vscode.commands.executeCommand('workbench.action.openSettings', 'vscode-db2i.jobSelf');
3333
}),
3434

3535
vscode.commands.registerCommand(`vscode-db2i.jobManager.newJob`, async (options?: JDBCOptions, name?: string) => {

src/views/jobManager/selfCodes/selfCodesResultsView.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,13 @@ export class selfCodesResultsView implements TreeDataProvider<any> {
137137
token: vscode.CancellationToken
138138
): ProviderResult<TreeItem> {
139139
if (!item.tooltip) {
140-
// Only set the tooltip if it hasn't been set yet
141-
item.tooltip = new vscode.MarkdownString(
142-
`**SQL Statement:** ${element.STMTTEXT}\n\n---\n\n**Detail:** ${element.MESSAGE_SECOND_LEVEL_TEXT}`
143-
);
144-
item.tooltip.isTrusted = true; // Make sure to allow Markdown content
140+
if (element.STMTTEXT && element.MESSAGE_SECOND_LEVEL_TEXT) {
141+
// Only set the tooltip if it hasn't been set yet
142+
item.tooltip = new vscode.MarkdownString(
143+
`**SQL Statement:** ${element.STMTTEXT}\n\n---\n\n**Detail:** ${element.MESSAGE_SECOND_LEVEL_TEXT}`
144+
);
145+
item.tooltip.isTrusted = true; // Make sure to allow Markdown content
146+
}
145147
}
146148
return item;
147149
}

0 commit comments

Comments
 (0)