Skip to content

Commit 4b47af4

Browse files
authored
Merge pull request #278 from codefori/feature/peek
Go to Definition support
2 parents bee3380 + 854d70b commit 4b47af4

File tree

16 files changed

+372
-217
lines changed

16 files changed

+372
-217
lines changed

package.json

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,22 @@
186186
}
187187
},
188188
{
189-
"id": "vscode-db2i.self",
190-
"title": "SQL Error Logging Facility (SELF)",
189+
"id": "vscode-db2i.jobManager",
190+
"title": "SQL Job Manager",
191191
"properties": {
192-
"vscode-db2i.jobSelfDefault": {
192+
"vscode-db2i.jobManager.jobNamingDefault": {
193193
"type": "string",
194194
"order": 0,
195+
"description": "Default naming mode for new jobs",
196+
"default": "system",
197+
"enum": [
198+
"system",
199+
"sql"
200+
]
201+
},
202+
"vscode-db2i.jobManager.jobSelfDefault": {
203+
"type": "string",
204+
"order": 1,
195205
"description": "Default SELF setting for new jobs",
196206
"default": "*NONE",
197207
"enum": [
@@ -200,7 +210,13 @@
200210
"*ERROR",
201211
"*WARNING"
202212
]
203-
},
213+
}
214+
}
215+
},
216+
{
217+
"id": "vscode-db2i.self",
218+
"title": "SQL Error Logging Facility (SELF)",
219+
"properties": {
204220
"vscode-db2i.jobSelfViewAutoRefresh": {
205221
"type": "boolean",
206222
"title": "Auto-refresh SELF Codes view",
@@ -423,11 +439,6 @@
423439
"category": "Db2 for i",
424440
"icon": "$(output)"
425441
},
426-
{
427-
"command": "vscode-db2i.setCurrentSchema",
428-
"title": "Set current schema",
429-
"category": "Db2 for i"
430-
},
431442
{
432443
"command": "vscode-db2i.setSchemaFilter",
433444
"title": "Set filter",
@@ -588,8 +599,8 @@
588599
"icon": "$(edit)"
589600
},
590601
{
591-
"command": "vscode-db2i.jobManager.defaultSelfSettings",
592-
"title": "SQL Error Logging Facility (SELF) - Default Setting",
602+
"command": "vscode-db2i.jobManager.defaultSettings",
603+
"title": "Default Job Settings",
593604
"category": "Db2 for i",
594605
"icon": "$(gear)"
595606
},
@@ -948,7 +959,7 @@
948959
"when": "view == jobManager"
949960
},
950961
{
951-
"command": "vscode-db2i.jobManager.defaultSelfSettings",
962+
"command": "vscode-db2i.jobManager.defaultSettings",
952963
"group": "navigation",
953964
"when": "view == jobManager"
954965
},
@@ -1014,11 +1025,6 @@
10141025
}
10151026
],
10161027
"view/item/context": [
1017-
{
1018-
"command": "vscode-db2i.setCurrentSchema",
1019-
"when": "viewItem == schema",
1020-
"group": "db2@1"
1021-
},
10221028
{
10231029
"command": "vscode-db2i.setSchemaFilter",
10241030
"when": "viewItem == schema",
@@ -1375,7 +1381,7 @@
13751381
},
13761382
{
13771383
"view": "vscode-db2i.self.nodes",
1378-
"contents": "🛠️ SELF Codes will appear here. You can set the SELF log level on specific jobs, or you can set the default for new jobs in the User Settings.\n\n[Set Default for New Jobs](command:vscode-db2i.jobManager.defaultSelfSettings)\n\n[Learn about SELF](command:vscode-db2i.self.help)"
1384+
"contents": "🛠️ SELF Codes will appear here. You can set the SELF log level on specific jobs, or you can set the default for new jobs in the User Settings.\n\n[Set Default for New Jobs](command:vscode-db2i.jobManager.defaultSettings)\n\n[Learn about SELF](command:vscode-db2i.self.help)"
13791385
}
13801386
],
13811387
"notebooks": [

src/connection/manager.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export interface JobInfo {
1313
job: OldSQLJob;
1414
}
1515

16+
export type NamingFormats = "sql"|"system";
17+
1618
const NO_SELECTED_JOB = -1;
1719

1820
export class SQLJobManager {
@@ -27,11 +29,12 @@ export class SQLJobManager {
2729
if (ServerComponent.isInstalled()) {
2830

2931
const instance = getInstance();
30-
const config = instance.getConfig();
32+
const connection = instance.getConnection()!;
33+
const config = connection.getConfig();
3134

3235
const newJob = predefinedJob || (new OldSQLJob({
3336
libraries: [config.currentLibrary, ...config.libraryList.filter((item) => item != config.currentLibrary)],
34-
naming: `system`,
37+
naming: SQLJobManager.getNamingDefault(),
3538
"full open": false,
3639
"transaction isolation": "none",
3740
"query optimize goal": "1",
@@ -117,6 +120,16 @@ export class SQLJobManager {
117120
return this.jobs[jobExists];
118121
}
119122

123+
private resetCurrentSchema(query: string, job: OldSQLJob) {
124+
if (query.toUpperCase().startsWith(`SET`)) {
125+
const newSchema = query.split(` `)[2];
126+
if (newSchema) {
127+
job.resetCurrentSchemaCache();
128+
}
129+
}
130+
return query;
131+
}
132+
120133
async runSQL<T>(query: string, opts?: QueryOptions, rowsToFetch = 2147483647): Promise<T[]> {
121134
// 2147483647 is NOT arbitrary. On the server side, this is processed as a Java
122135
// int. This is the largest number available without overflow (Integer.MAX_VALUE)
@@ -128,10 +141,7 @@ export class SQLJobManager {
128141
const results = await statement.execute(rowsToFetch);
129142
statement.close();
130143

131-
// const e = performance.now()
132-
// console.log(`Statement executed in ${e - s} ms. ${results.data.length} rows returned.`);
133-
// console.log(`\t${query.padEnd(40).substring(0, 40)}`)
134-
144+
this.resetCurrentSchema(query, this.jobs[this.selectedJob].job);
135145
return results.data;
136146
}
137147

@@ -143,12 +153,15 @@ export class SQLJobManager {
143153
const results = await statement.execute(rowsToFetch);
144154
statement.close();
145155

156+
this.resetCurrentSchema(query, this.jobs[this.selectedJob].job);
146157
return results;
147158
}
148159

149160
async getPagingStatement<T>(query: string, opts?: QueryOptions): Promise<Query<T>> {
150-
const selected = this.jobs[this.selectedJob]
161+
const selected = this.jobs[this.selectedJob];
151162
if (ServerComponent.isInstalled() && selected) {
163+
this.resetCurrentSchema(query, selected?.job);
164+
152165
return selected.job.query<T>(query, opts);
153166

154167
} else if (!ServerComponent.isInstalled()) {
@@ -171,6 +184,10 @@ export class SQLJobManager {
171184
}
172185

173186
static getSelfDefault(): SelfValue {
174-
return Configuration.get<SelfValue>(`jobSelfDefault`) || `*NONE`;
187+
return Configuration.get<SelfValue>(`jobManager.jobSelfDefault`) || `*NONE`;
188+
}
189+
190+
static getNamingDefault(): NamingFormats {
191+
return (Configuration.get<string>(`jobManager.jobNamingDefault`) || `system`) as NamingFormats;
175192
}
176193
}

src/connection/sqlJob.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { SQLJob } from "@ibm/mapepire-js";
55
import { ConnectionResult, QueryResult, ServerRequest, ServerResponse } from "@ibm/mapepire-js/dist/src/types";
66
import { JobLogEntry, JobStatus } from "./types";
77
import Statement from "../database/statement";
8+
import { NamingFormats } from "./manager";
89

910
const DB2I_VERSION = (process.env[`DB2I_VERSION`] || `<version unknown>`) + ((process.env.DEV) ? ``:`-dev`);
1011

@@ -13,11 +14,35 @@ export class OldSQLJob extends SQLJob {
1314
private selfState: SelfValue = "*NONE";
1415

1516
id: string | undefined;
17+
private currentSchemaStore: string | undefined;
1618

1719
getSelfCode(): SelfValue {
1820
return this.selfState;
1921
}
2022

23+
resetCurrentSchemaCache() {
24+
this.currentSchemaStore = undefined;
25+
}
26+
27+
async getCurrentSchema(): Promise<string> {
28+
if (this.getNaming() === `sql`) {
29+
if (this.currentSchemaStore)
30+
return this.currentSchemaStore;
31+
32+
const result = await this.execute<{'00001': string}>(`values (current schema)`);
33+
if (result.success && result.data.length > 0) {
34+
this.currentSchemaStore = result.data[0]['00001'];
35+
return this.currentSchemaStore;
36+
}
37+
}
38+
39+
return this.options.libraries[0] || `QGPL`;
40+
}
41+
42+
getNaming(): NamingFormats {
43+
return this.options.naming;
44+
}
45+
2146
public static async useExec() {
2247
let useExec = false;
2348

@@ -169,19 +194,6 @@ export class OldSQLJob extends SQLJob {
169194
throw e;
170195
}
171196
}
172-
173-
async setCurrentSchema(schema: string): Promise<QueryResult<any>> {
174-
if (schema) {
175-
const upperSchema = Statement.delimName(schema, true);
176-
const result = await this.execute(`set current schema = ?`, {parameters: [upperSchema]});
177-
if (result.success) {
178-
this.options.libraries[0] = upperSchema;
179-
}
180-
181-
return result;
182-
183-
}
184-
}
185197

186198
getJobLog(): Promise<QueryResult<JobLogEntry>> {
187199
return this.query<JobLogEntry>(`select * from table(qsys2.joblog_info('*')) a`).execute();

src/connection/syntaxChecker/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class SQLStatementChecker implements IBMiComponent {
4747

4848
private getLibrary(connection: IBMi) {
4949
if (!this.library) {
50-
this.library = connection?.config?.tempLibrary.toUpperCase() || `ILEDITOR`;
50+
this.library = connection?.getConfig()?.tempLibrary.toUpperCase() || `ILEDITOR`;
5151
}
5252

5353
return this.library;
@@ -79,7 +79,7 @@ export class SQLStatementChecker implements IBMiComponent {
7979
return -1;
8080
}
8181

82-
async getRemoteState(connection: IBMi) {
82+
async getRemoteState(connection: IBMi): Promise<ComponentState> {
8383
const lib = this.getLibrary(connection);
8484

8585
const wrapperVersion = await SQLStatementChecker.getVersionOf(connection, lib, WRAPPER_NAME);
@@ -98,7 +98,7 @@ export class SQLStatementChecker implements IBMiComponent {
9898
update(connection: IBMi): ComponentState | Promise<ComponentState> {
9999
return connection.withTempDirectory(async tempDir => {
100100
const tempSourcePath = posix.join(tempDir, `sqlchecker.sql`);
101-
await connection.content.writeStreamfileRaw(tempSourcePath, Buffer.from(this.getSource(connection), "utf-8"));
101+
await connection.getConfig().writeStreamfileRaw(tempSourcePath, Buffer.from(this.getSource(connection), "utf-8"));
102102
const result = await connection.runCommand({
103103
command: `RUNSQLSTM SRCSTMF('${tempSourcePath}') COMMIT(*NONE) NAMING(*SYS)`,
104104
noLibList: true

0 commit comments

Comments
 (0)