Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 63 additions & 3 deletions src/views/results/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import { generateSqlForAdvisedIndexes } from "./explain/advice";
import { updateStatusBar } from "../jobManager/statusBar";
import { DbCache } from "../../language/providers/logic/cache";
import { ExplainType } from "../../connection/types";
import { ColumnMetaData } from "@ibm/mapepire-js";

export type StatementQualifier = "statement" | "update" | "explain" | "onlyexplain" | "json" | "csv" | "cl" | "sql";
export type StatementQualifier = "statement" | "update" | "explain" | "onlyexplain" | "json" | "csv" | "cl" | "sql" | "rpg";

export interface StatementInfo {
content: string,
Expand Down Expand Up @@ -375,7 +376,19 @@ async function runHandler(options?: StatementInfo) {
} else {
vscode.window.showInformationMessage(`No job currently selected.`);
}


} else if (statementDetail.qualifier === `rpg`) {
if (statementDetail.statement.type !== StatementType.Select) {
vscode.window.showErrorMessage('RPG qualifier only supported for select statements');
} else {
chosenView.setLoadingText(`Executing SQL statement...`, false);
let content: string = await statementToRpgDs(statementDetail);
const textDoc = await vscode.workspace.openTextDocument({ language: 'rpgle', content });
await vscode.window.showTextDocument(textDoc);
updateStatusBar({executing: false});
chosenView.setLoadingText(`RPG data structure generated.`, false);
}

} else {
// Otherwise... it's a bit complicated.
chosenView.setLoadingText(`Executing SQL statement...`, false);
Expand Down Expand Up @@ -480,6 +493,53 @@ async function runHandler(options?: StatementInfo) {
}
}

async function statementToRpgDs(statement: ParsedStatementInfo) : Promise<string> {
setCancelButtonVisibility(true);
updateStatusBar({executing: true});
const result = await JobManager.runSQLVerbose(statement.content, undefined, 1);
setCancelButtonVisibility(false);

let content = `**free\n\n`
+ `// statement: ${statement.content}\n\n`
+ `// Row data structure\ndcl-ds row_t qualified template;\n`;

for (let i = 0; i < result.metadata.column_count; i++) {
content += ` ${isNaN(+result.metadata.columns[i].label.charAt(0)) ? '' : 'col'}${result.metadata.columns[i].label.toLowerCase()} `;
content += columnToRpgDefinition(result.metadata.columns[i]);
}
content += `end-ds;\n`;
return content;
}

function columnToRpgDefinition(column: ColumnMetaData) : string {
switch (column.type) {
case `NUMERIC`:
return `zoned(${column.precision}${column.scale > 0 ? ' : ' + column.scale : ''});\n`;
case `DECIMAL`:
return `packed(${column.precision}${column.scale > 0 ? ' : ' + column.scale : ''});\n`;
case `CHAR`:
return `char(${column.precision});\n`;
case `VARCHAR`:
return `varchar(${column.precision});\n`;
case `DATE`:
return `date;\n`;
case `TIME`:
return `time;\n`;
case `TIMESTAMP`:
return `timestamp;\n`;
case `SMALLINT`:
return `int(5);\n`;
case `INTEGER`:
return `int(10);\n`;
case `BIGINT`:
return `int(20);\n`;
case `BOOLEAN`:
return `ind;\n`;
default:
return `// type:${column.type} precision:${column.precision} scale:${column.scale}\n`;
}
}

export function parseStatement(editor?: vscode.TextEditor, existingInfo?: StatementInfo): ParsedStatementInfo {
let statementInfo: ParsedStatementInfo = {
content: ``,
Expand Down Expand Up @@ -524,7 +584,7 @@ export function parseStatement(editor?: vscode.TextEditor, existingInfo?: Statem
}

if (statementInfo.content) {
[`cl`, `json`, `csv`, `sql`, `explain`, `update`].forEach(mode => {
[`cl`, `json`, `csv`, `sql`, `explain`, `update`, `rpg`].forEach(mode => {
if (statementInfo.content.trim().toLowerCase().startsWith(mode + `:`)) {
statementInfo.content = statementInfo.content.substring(mode.length + 1).trim();

Expand Down
Loading