Skip to content

Commit eb9ea6e

Browse files
authored
Merge pull request #326 from chrjorgensen/fix/Generate_SQL
Fix linebreaks in generated SQL
2 parents 7151d67 + f07c5ad commit eb9ea6e

File tree

4 files changed

+74
-58
lines changed

4 files changed

+74
-58
lines changed

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@
970970
},
971971
{
972972
"command": "vscode-db2i.getObjectLocks",
973-
"when": "viewItem == table || viewItem == view || viewItem == alias || viewItem == constraint || viewItem == function || viewItem == variable || viewItem == index || viewItem == procedure || viewItem == sequence || viewItem == package || viewItem == trigger || viewItem == type",
973+
"when": "viewItem == table || viewItem == view || viewItem == alias || viewItem == function || viewItem == variable || viewItem == index || viewItem == procedure || viewItem == sequence || viewItem == package || viewItem == trigger || viewItem == type",
974974
"group": "db2workWith@5"
975975
},
976976
{
@@ -1322,7 +1322,7 @@
13221322
},
13231323
"devDependencies": {
13241324
"@continuedev/core": "^1.0.13",
1325-
"@halcyontech/vscode-ibmi-types": "^2.14.0",
1325+
"@halcyontech/vscode-ibmi-types": "^2.14.5",
13261326
"@types/glob": "^7.1.3",
13271327
"@types/node": "18.x",
13281328
"@types/vscode": "^1.95.0",

src/database/schemas.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2+
import path from "path";
23
import { getInstance } from "../base";
3-
44
import { JobManager } from "../config";
55

66
export type SQLType = "schemas" | "tables" | "views" | "aliases" | "constraints" | "functions" | "variables" | "indexes" | "procedures" | "sequences" | "packages" | "triggers" | "types" | "logicals";
@@ -220,13 +220,27 @@ export default class Schemas {
220220
* @param object Not user input
221221
*/
222222
static async generateSQL(schema: string, object: string, internalType: string): Promise<string> {
223-
const lines = await JobManager.runSQL<{ SRCDTA: string }>([
224-
`CALL QSYS2.GENERATE_SQL(?, ?, ?, CREATE_OR_REPLACE_OPTION => '1', PRIVILEGES_OPTION => '0')`
225-
].join(` `), { parameters: [object, schema, internalType] });
226-
227-
const generatedStatement = lines.map(line => line.SRCDTA).join(`\n`);
228-
229-
return generatedStatement;
223+
const instance = getInstance();
224+
const connection = instance.getConnection();
225+
226+
const result = await connection.withTempDirectory<string>(async (tempDir) => {
227+
const tempFilePath = path.posix.join(tempDir, `generatedSql.sql`);
228+
await JobManager.runSQL<{ SRCDTA: string }>([
229+
`CALL QSYS2.GENERATE_SQL( DATABASE_OBJECT_NAME => ?, DATABASE_OBJECT_LIBRARY_NAME => ?, DATABASE_OBJECT_TYPE => ?
230+
, CREATE_OR_REPLACE_OPTION => '1', PRIVILEGES_OPTION => '0'
231+
, DATABASE_SOURCE_FILE_NAME => '*STMF'
232+
, STATEMENT_FORMATTING_OPTION => '0'
233+
, SOURCE_STREAM_FILE => '${tempFilePath}'
234+
, SOURCE_STREAM_FILE_END_OF_LINE => 'LF'
235+
, SOURCE_STREAM_FILE_CCSID => 1208 )`
236+
].join(` `), { parameters: [object, schema, internalType] });
237+
238+
// TODO: eventually .content -> .getContent(), it's not available yet
239+
const contents = (await connection.content.downloadStreamfileRaw(tempFilePath)).toString();
240+
return contents;
241+
})
242+
243+
return result;
230244
}
231245

232246
static async deleteObject(schema: string, name: string, type: string): Promise<void> {

src/views/schemaBrowser/index.ts

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ const itemIcons = {
4444
export default class schemaBrowser {
4545
emitter: vscode.EventEmitter<any | undefined | null | void>;
4646
onDidChangeTreeData: vscode.Event<any | undefined | null | void>;
47-
cache: {[key: string]: object[]};
47+
cache: { [key: string]: object[] };
4848

49-
filters: {[schema: string]: string} = {};
49+
filters: { [schema: string]: string } = {};
5050

5151
/**
5252
* @param {vscode.ExtensionContext} context
@@ -138,13 +138,15 @@ export default class schemaBrowser {
138138

139139
vscode.commands.registerCommand(`vscode-db2i.generateSQL`, async (object: SQLObject) => {
140140
if (object) {
141-
try {
142-
const content = await Schemas.generateSQL(object.schema, object.uniqueName(), object.type.toUpperCase());
143-
const textDoc = await vscode.workspace.openTextDocument({language: `sql`, content});
144-
await vscode.window.showTextDocument(textDoc);
145-
} catch (e) {
146-
vscode.window.showErrorMessage(e.message);
147-
}
141+
vscode.window.withProgress({ location: vscode.ProgressLocation.Window, title: `Generating SQL` }, async () => {
142+
try {
143+
const content = await Schemas.generateSQL(object.schema, object.uniqueName(), object.type.toUpperCase());
144+
const textDoc = await vscode.workspace.openTextDocument({ language: `sql`, content });
145+
await vscode.window.showTextDocument(textDoc);
146+
} catch (e) {
147+
vscode.window.showErrorMessage(e.message);
148+
}
149+
});
148150
}
149151
}),
150152

@@ -162,10 +164,10 @@ export default class schemaBrowser {
162164
}
163165
}),
164166

165-
vscode.commands.registerCommand(`vscode-db2i.getMTIs`, async (object: SQLObject|SchemaItem) => {
167+
vscode.commands.registerCommand(`vscode-db2i.getMTIs`, async (object: SQLObject | SchemaItem) => {
166168
if (object) {
167169
const content = getMTIStatement(object.schema, (`name` in object ? object.name : undefined));
168-
170+
169171
if (content) {
170172
vscode.commands.executeCommand(`vscode-db2i.runEditorStatement`, {
171173
content,
@@ -186,7 +188,7 @@ export default class schemaBrowser {
186188
});
187189
}
188190
}),
189-
191+
190192
vscode.commands.registerCommand(`vscode-db2i.getAuthorities`, async (object: SQLObject) => {
191193
if (object) {
192194
const content = getAuthoritiesStatement(object.schema, object.name, object.type.toUpperCase(), object.tableType);
@@ -209,16 +211,16 @@ export default class schemaBrowser {
209211
}
210212
}),
211213

212-
vscode.commands.registerCommand(`vscode-db2i.advisedIndexes`, async (object: SQLObject|SchemaItem) => { //table
214+
vscode.commands.registerCommand(`vscode-db2i.advisedIndexes`, async (object: SQLObject | SchemaItem) => { //table
213215
if (object) {
214-
let content: string|undefined;
216+
let content: string | undefined;
215217
if (`name` in object) {
216218
content = getAdvisedIndexesStatement(object.schema, object.name);
217219
}
218220
else {
219221
content = getAdvisedIndexesStatement(object.schema);
220222
}
221-
223+
222224
if (content) {
223225
vscode.commands.executeCommand(`vscode-db2i.runEditorStatement`, {
224226
content,
@@ -229,16 +231,16 @@ export default class schemaBrowser {
229231
}
230232
}),
231233

232-
vscode.commands.registerCommand(`vscode-db2i.clearAdvisedIndexes`, async (object: SQLObject|SchemaItem) => {
234+
vscode.commands.registerCommand(`vscode-db2i.clearAdvisedIndexes`, async (object: SQLObject | SchemaItem) => {
233235
if (object) {
234236
const isObject = `name` in object;
235237
let result;
236238

237-
result = await vscode.window.showWarningMessage(`Are you sure you want to clear all of the advised index rows from the Index Advisor for ${object.schema}${isObject ? `${object.name}` : ''}?`, {
239+
result = await vscode.window.showWarningMessage(`Are you sure you want to clear all of the advised index rows from the Index Advisor for ${object.schema}${isObject ? `${object.name}` : ''}?`, {
238240
modal: true,
239241
}, 'No', 'Yes');
240-
241-
if(result === 'Yes') {
242+
243+
if (result === 'Yes') {
242244
try {
243245
await Schemas.clearAdvisedIndexes(object.schema, isObject ? object.name : undefined);
244246
} catch (e) {
@@ -254,7 +256,7 @@ export default class schemaBrowser {
254256
modal: true,
255257
}, 'No', 'Yes');
256258

257-
if(result === 'Yes') {
259+
if (result === 'Yes') {
258260
try {
259261
await vscode.window.withProgress({
260262
location: vscode.ProgressLocation.Notification,
@@ -289,7 +291,7 @@ export default class schemaBrowser {
289291
}, async () => {
290292
await Schemas.renameObject(object.schema, object.name, name, object.type);
291293
});
292-
294+
293295
vscode.window.showInformationMessage(`Renamed ${object.name} to ${name}`);
294296
this.clearCacheAndRefresh();
295297
} catch (e) {
@@ -301,14 +303,14 @@ export default class schemaBrowser {
301303
}
302304
}
303305
}),
304-
306+
305307
vscode.commands.registerCommand(`vscode-db2i.clearData`, async (object: SQLObject) => {
306308
if (object) {
307309
const result = await vscode.window.showWarningMessage(`Are you sure you want to clear ${object.name}?`, {
308310
modal: true,
309311
}, 'No', 'Yes');
310312

311-
if(result === 'Yes') {
313+
if (result === 'Yes') {
312314
try {
313315
await vscode.window.withProgress({
314316
location: vscode.ProgressLocation.Notification,
@@ -328,8 +330,8 @@ export default class schemaBrowser {
328330
vscode.commands.registerCommand(`vscode-db2i.copyData`, async (object: SQLObject) => {
329331
if (object) {
330332
const page = await getCopyUi().loadPage<any>((`Copy File - ${object.schema}.${object.name}`));
331-
332-
if(page && page.data) {
333+
334+
if (page && page.data) {
333335
const data = page.data;
334336
page.panel.dispose();
335337

@@ -341,7 +343,7 @@ export default class schemaBrowser {
341343
}, async () => {
342344
await Table.copyFile(object.system.schema, object.system.name, data);
343345
});
344-
346+
345347
vscode.window.showInformationMessage(`Table copied`);
346348
this.clearCacheAndRefresh();
347349
} catch (e) {
@@ -369,7 +371,7 @@ export default class schemaBrowser {
369371

370372
const config = getInstance().getConfig();
371373
const currentLibrary = config.currentLibrary.toUpperCase();
372-
374+
373375
if (schema && schema !== currentLibrary) {
374376
config.currentLibrary = schema;
375377
await getInstance().setConfig(config);
@@ -477,7 +479,7 @@ export default class schemaBrowser {
477479
return element;
478480
}
479481

480-
async getChildren(element?: Schema|SchemaItem|SQLObject) {
482+
async getChildren(element?: Schema | SchemaItem | SQLObject) {
481483
let items = [];
482484

483485
if (element) {
@@ -489,7 +491,7 @@ export default class schemaBrowser {
489491
let filterValue = this.filters[element.schema];
490492
if (filterValue) {
491493
const validSchemaName = Statement.noQuotes(element.schema);
492-
const filteredObjects = await Schemas.getObjects(validSchemaName, AllSQLTypes, {filter: filterValue});
494+
const filteredObjects = await Schemas.getObjects(validSchemaName, AllSQLTypes, { filter: filterValue });
493495
items = filteredObjects.map(obj => new SQLObject(obj));
494496

495497
} else {
@@ -499,16 +501,16 @@ export default class schemaBrowser {
499501

500502

501503
} else
502-
if (element instanceof SchemaItem) {
503-
items = await this.fetchData(element.schema, contextValue as SQLType, false);
504-
} else
505-
if (element instanceof SQLObject) {
506-
const type = element.type;
507-
508-
if (Types[type]) {
509-
items = await Types[type].getChildren(element.schema, element.uniqueName());
510-
}
511-
}
504+
if (element instanceof SchemaItem) {
505+
items = await this.fetchData(element.schema, contextValue as SQLType, false);
506+
} else
507+
if (element instanceof SQLObject) {
508+
const type = element.type;
509+
510+
if (Types[type]) {
511+
items = await Types[type].getChildren(element.schema, element.uniqueName());
512+
}
513+
}
512514

513515
} else {
514516
const connection = getInstance().getConnection();

0 commit comments

Comments
 (0)