Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPG statement qualifier with code/snippet generation #276

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions src/connection/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { OldSQLJob } from "./sqlJob";
import { askAboutNewJob, onConnectOrServerInstall, osDetail } from "../config";
import { SelfValue } from "../views/jobManager/selfCodes/nodes";
import Configuration from "../configuration";
import { QueryOptions } from "@ibm/mapepire-js/dist/src/types";
import { QueryOptions, QueryResult } from "@ibm/mapepire-js/dist/src/types";
import { Query } from "@ibm/mapepire-js/dist/src/query";

export interface JobInfo {
Expand Down Expand Up @@ -123,15 +123,15 @@ export class SQLJobManager {
* of arrays. When set to false, data is returned as an array of objects (compatible with legacy API).
* @returns
*/
async runSQL<T>(query: string, opts?: QueryOptions): Promise<T[]> {
async runSQL<T>(query: string, opts?: QueryOptions): Promise<QueryResult<T>> {
// 2147483647 is NOT arbitrary. On the server side, this is processed as a Java
// int. This is the largest number available without overflow (Integer.MAX_VALUE)
const rowsToFetch = 2147483647;

const statement = await this.getPagingStatement<T>(query, opts);
const results = await statement.execute(rowsToFetch);
statement.close();
return results.data;
return results;
}

async getPagingStatement<T>(query: string, opts?: QueryOptions): Promise<Query<T>> {
Expand Down
15 changes: 8 additions & 7 deletions src/database/callable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

import vscode from "vscode"
import { JobManager } from "../config";
import { QueryOptions } from "../connection/types";
import { QueryOptions } from "@ibm/mapepire-js/dist/src/types";
import { QueryResult } from "@ibm/mapepire-js/dist/src/types";
const {instance} = vscode.extensions.getExtension(`halcyontechltd.code-for-ibmi`).exports;

export type CallableType = "PROCEDURE"|"FUNCTION";
Expand Down Expand Up @@ -29,8 +30,8 @@ export default class Callable {
type: forType
}

if (result.length > 0) {
routine.specificNames = result.map(row => row.SPECIFIC_NAME);
if (result.data.length > 0) {
routine.specificNames = result.data.map(row => row.SPECIFIC_NAME);
return routine;
}

Expand All @@ -50,13 +51,13 @@ export default class Callable {
);

// find unique specific names
const uniqueSpecificNames = Array.from(new Set(results.map(row => row.SPECIFIC_NAME)));
const uniqueSpecificNames = Array.from(new Set(results.data.map(row => row.SPECIFIC_NAME)));

// group results by specific name
const groupedResults: CallableSignature[] = uniqueSpecificNames.map(name => {
return {
specificName: name,
parms: results.filter(row => row.SPECIFIC_NAME === name)
parms: results.data.filter(row => row.SPECIFIC_NAME === name)
}
});

Expand All @@ -68,7 +69,7 @@ export default class Callable {
* @param specificName Not user input
* @returns
*/
static getParms(schema: string, specificName: string, resolveName: boolean = false): Promise<SQLParm[]> {
static getParms(schema: string, specificName: string, resolveName: boolean = false): Promise<QueryResult<SQLParm>> {
const rowType = `P`; // Parameter
return Callable.getFromSysParms(schema, specificName, rowType, resolveName);
}
Expand All @@ -78,7 +79,7 @@ export default class Callable {
return Callable.getFromSysParms(schema, specificName, rowType, resolveName);
}

static getFromSysParms(schema: string, name: string, rowType: "P"|"R", resolveName: boolean = false): Promise<SQLParm[]> {
static getFromSysParms(schema: string, name: string, rowType: "P"|"R", resolveName: boolean = false): Promise<QueryResult<SQLParm>> {
let parameters = [schema, rowType];

let specificNameClause = undefined;
Expand Down
8 changes: 4 additions & 4 deletions src/database/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,15 @@ export default class Schemas {

const query = `with results as (${selects.join(" UNION ALL ")}) select * from results Order by QSYS2.DELIMIT_NAME(NAME) asc`;

const objects: any[] = await JobManager.runSQL(
const objects: any[] = (await JobManager.runSQL(
[
query,
`${details.limit ? `limit ${details.limit}` : ``} ${details.offset ? `offset ${details.offset}` : ``}`
].join(` `),
{
parameters
}
);
)).data;

return objects.map(object => ({
type: object.OBJ_TYPE,
Expand All @@ -219,9 +219,9 @@ export default class Schemas {
* @param object Not user input
*/
static async generateSQL(schema: string, object: string, internalType: string): Promise<string> {
const lines = await JobManager.runSQL<{ SRCDTA: string }>([
const lines = (await JobManager.runSQL<{ SRCDTA: string }>([
`CALL QSYS2.GENERATE_SQL(?, ?, ?, CREATE_OR_REPLACE_OPTION => '1', PRIVILEGES_OPTION => '0')`
].join(` `), { parameters: [object, schema, internalType] });
].join(` `), { parameters: [object, schema, internalType] })).data;

const generatedStatement = lines.map(line => line.SRCDTA).join(`\n`);

Expand Down
2 changes: 1 addition & 1 deletion src/database/serviceInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Statement from "./statement";
export async function getServiceInfo(): Promise<SQLExample[]> {
// The reason we check for a selection is because we don't want it to prompt the user to start one here
if (JobManager.getSelection()) {
const resultSet = await JobManager.runSQL<{ SERVICE_NAME: string, EXAMPLE: string }>(`select SERVICE_NAME, EXAMPLE from qsys2.services_info`);
const resultSet = (await JobManager.runSQL<{ SERVICE_NAME: string, EXAMPLE: string }>(`select SERVICE_NAME, EXAMPLE from qsys2.services_info`)).data;

return resultSet.map(r => ({
name: Statement.prettyName(r.SERVICE_NAME),
Expand Down
3 changes: 2 additions & 1 deletion src/database/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import vscode from "vscode"
import { JobManager } from "../config";
import { getInstance } from "../base";
import Statement from "./statement";
import { QueryResult } from "@ibm/mapepire-js/dist/src/types";

export default class Table {
/**
* @param {string} schema Not user input
* @param {string} name Not user input
* @returns {Promise<TableColumn[]>}
*/
static async getItems(schema: string, name: string): Promise<TableColumn[]> {
static async getItems(schema: string, name: string): Promise<QueryResult<TableColumn>> {
const sql = [
`SELECT `,
` column.COLUMN_NAME,`,
Expand Down
3 changes: 2 additions & 1 deletion src/database/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import vscode from "vscode"
import { JobManager } from "../config";
import Statement from "./statement";
import { QueryResult } from "@ibm/mapepire-js/dist/src/types";

export default class View {
static getColumns(schema: string, name: string): Promise<TableColumn[]> {
static getColumns(schema: string, name: string): Promise<QueryResult<TableColumn>> {
return JobManager.runSQL([
`SELECT * FROM QSYS2.SYSCOLUMNS`,
`WHERE TABLE_SCHEMA = '${schema}' AND TABLE_NAME = '${name}'`,
Expand Down
4 changes: 2 additions & 2 deletions src/language/providers/completionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ async function getObjectColumns(
let completionItems: CompletionItem[] = [];

if (isUDTF) {
const resultSet = await Callable.getResultColumns(schema, name, true);
const resultSet = (await Callable.getResultColumns(schema, name, true)).data;

if (!resultSet?.length ? true : false) {
completionItemCache.set(cacheKey, []);
Expand All @@ -120,7 +120,7 @@ async function getObjectColumns(
);

} else {
const columns = await Table.getItems(schema, name);
const columns = (await Table.getItems(schema, name)).data;

if (!columns?.length ? true : false) {
completionItemCache.set(cacheKey, []);
Expand Down
18 changes: 9 additions & 9 deletions src/testing/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,31 +113,31 @@ export const DatabaseSuite: TestSuite = {
const objects = await Database.getObjects(systemLibrary, [`tables`]);
assert.notStrictEqual(objects.length, 0);

const cols = await Table.getItems(systemLibrary, objects[0].name);
const cols = (await Table.getItems(systemLibrary, objects[0].name)).data;
assert.notStrictEqual(cols.length, 0);
}},

{name: `Get columns, sqlSchema name`, test: async () => {
const objects = await Database.getObjects(Statement.noQuotes(sqlSchema), [`tables`]);
assert.notStrictEqual(objects.length, 0);

const cols = await Table.getItems(Statement.noQuotes(sqlSchema), objects[0].name);
const cols = (await Table.getItems(Statement.noQuotes(sqlSchema), objects[0].name)).data;
assert.notStrictEqual(cols.length, 0);
}},

{name: `Get view columns, system name`, test: async () => {
const objects = await Database.getObjects(systemLibrary, [`views`]);
assert.notStrictEqual(objects.length, 0);

const cols = await View.getColumns(systemLibrary, objects[0].name);
const cols = (await View.getColumns(systemLibrary, objects[0].name)).data;
assert.notStrictEqual(cols.length, 0);
}},

{name: `Get view columns, sqlSchema name`, test: async () => {
const objects = await Database.getObjects(Statement.noQuotes(sqlSchema), [`views`]);
assert.notStrictEqual(objects.length, 0);

const cols = await View.getColumns(Statement.noQuotes(sqlSchema), objects[0].name);
const cols = (await View.getColumns(Statement.noQuotes(sqlSchema), objects[0].name)).data;
assert.notStrictEqual(cols.length, 0);
}},

Expand All @@ -161,7 +161,7 @@ export const DatabaseSuite: TestSuite = {
const qsys = Statement.delimName(`qsys`, true);
const createSqlSample = Statement.delimName(`CREATE_SQL_SAMPLE`, true);

const parms = await Callable.getParms(qsys, createSqlSample);
const parms = (await Callable.getParms(qsys, createSqlSample)).data;
assert.notStrictEqual(parms.length, 0);
}},

Expand Down Expand Up @@ -207,11 +207,11 @@ export const DatabaseSuite: TestSuite = {
}},

{name: `Retrieve overloaded function parameters`, test: async () => {
const parms1 = await Callable.getParms(`OVERLOAD`, `MULTI_FUNC_SQUARED`);
const parms1 = (await Callable.getParms(`OVERLOAD`, `MULTI_FUNC_SQUARED`)).data;
// Verify one parameter for this function
assert.strictEqual(parms1.length, 1);

const parms2 = await Callable.getParms(`OVERLOAD`, `MULTI_FUNC_PRODUCT`);
const parms2 = (await Callable.getParms(`OVERLOAD`, `MULTI_FUNC_PRODUCT`)).data;
// Verify two parameter for this function
assert.strictEqual(parms2.length, 2);
}},
Expand Down Expand Up @@ -239,11 +239,11 @@ export const DatabaseSuite: TestSuite = {
}},

{name: `Retrieve overloaded procedure parameters`, test: async () => {
const parms1 = await Callable.getParms(`OVERLOAD`, `MULTI_PROC_ALL_MONITORS`);
const parms1 = (await Callable.getParms(`OVERLOAD`, `MULTI_PROC_ALL_MONITORS`)).data;
// Verify no parameters for this procedure
assert.strictEqual(parms1.length, 0);

const parms2 = await Callable.getParms(`OVERLOAD`, `MULTI_PROC_MY_MONITORS`);
const parms2 = (await Callable.getParms(`OVERLOAD`, `MULTI_PROC_MY_MONITORS`)).data;
// Verify one parameter for this procedure
assert.strictEqual(parms2.length, 1);
}},
Expand Down
74 changes: 63 additions & 11 deletions src/views/results/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { generateSqlForAdvisedIndexes } from "./explain/advice";
import { updateStatusBar } from "../jobManager/statusBar";
import { ExplainType } from "@ibm/mapepire-js/dist/src/types";

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

export interface StatementInfo {
content: string,
Expand Down Expand Up @@ -255,31 +255,32 @@ async function runHandler(options?: StatementInfo) {

setCancelButtonVisibility(true);
updateStatusBar({executing: true});
const data = await JobManager.runSQL(statementDetail.content);
const result = await JobManager.runSQL(statementDetail.content);
setCancelButtonVisibility(false);

if (data.length > 0) {
if (result.data.length > 0) {
switch (statementDetail.qualifier) {

case `csv`:
case `json`:
case `sql`:
case `rpg`:
let content = ``;
switch (statementDetail.qualifier) {
case `csv`: content = csv.stringify(data, {
case `csv`: content = csv.stringify(result.data, {
header: true,
quoted_string: true,
}); break;
case `json`: content = JSON.stringify(data, null, 2); break;
case `json`: content = JSON.stringify(result.data, null, 2); break;

case `sql`:
const keys = Object.keys(data[0]);
const keys = Object.keys(result.data[0]);

// split array into groups of 1k
const insertLimit = 1000;
const dataChunks = [];
for (let i = 0; i < data.length; i += insertLimit) {
dataChunks.push(data.slice(i, i + insertLimit));
for (let i = 0; i < result.data.length; i += insertLimit) {
dataChunks.push(result.data.slice(i, i + insertLimit));
}

content = `-- Generated ${dataChunks.length} insert statement${dataChunks.length === 1 ? `` : `s`}\n\n`;
Expand All @@ -300,11 +301,62 @@ async function runHandler(options?: StatementInfo) {
content += insertStatement.join(`\n`) + `;\n`;
}
break;

case `rpg`:

content = `**free\n\n-- Row data structure\n`
+ `dcl-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()} `;
switch (result.metadata.columns[i].type) {
case `NUMERIC`:
content += `zoned(${result.metadata.columns[i].precision}${result.metadata.columns[i].scale > 0 ? ' : ' + result.metadata.columns[i].scale : ''});\n`;
break;
case `DECIMAL`:
content += `packed(${result.metadata.columns[i].precision}${result.metadata.columns[i].scale > 0 ? ' : ' + result.metadata.columns[i].scale : ''});\n`;
break;
case `CHAR`:
content += `char(${result.metadata.columns[i].precision});\n`;
break;
case `VARCHAR`:
content += `varchar(${result.metadata.columns[i].precision});\n`;
break;
case `DATE`:
content += `date;\n`;
break;
case `TIME`:
content += `time;\n`;
break;
case `TIMESTAMP`:
content += `timestamp;\n`;
break;
case `SMALLINT`:
content += `int(5);\n`;
break;
case `INTEGER`:
content += `int(10);\n`;
break;
case `BIGINT`:
content += `int(20);\n`;
break;
case `BOOLEAN`:
content += `ind;\n`;
break;
default:
content += `// type:${result.metadata.columns[i].type} precision:${result.metadata.columns[i].precision} scale:${result.metadata.columns[i].scale}\n`;
break;
}
}
content += `end-ds;\n`;

break;

}

const textDoc = await vscode.workspace.openTextDocument({ language: statementDetail.qualifier, content });
const textDoc = await vscode.workspace.openTextDocument({ language: statementDetail.qualifier === 'rpg' ? 'rpgle' : statementDetail.qualifier, content });
await vscode.window.showTextDocument(textDoc);
chosenView.setLoadingText(`Query executed with ${data.length} rows returned.`, false);
chosenView.setLoadingText(`Query executed with ${result.data.length} rows returned.`, false);
break;
}

Expand Down Expand Up @@ -379,7 +431,7 @@ export function parseStatement(editor?: vscode.TextEditor, existingInfo?: Statem
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/views/types/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Function from "../../database/callable";
import ParmTreeItem from "./ParmTreeItem";

export async function getChildren (schema: string, specificName: string): Promise<ParmTreeItem[]> {
const parms = await Function.getParms(schema, specificName);
const parms = (await Function.getParms(schema, specificName)).data;

return parms.map(parm => new ParmTreeItem(schema, specificName, parm));
}
2 changes: 1 addition & 1 deletion src/views/types/procedure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Procedure from "../../database/callable";
import ParmTreeItem from "./ParmTreeItem";

export async function getChildren(schema: string, specificName: string): Promise<ParmTreeItem[]> {
const parms = await Procedure.getParms(schema, specificName);
const parms = (await Procedure.getParms(schema, specificName)).data;

return parms.map(parm => new ParmTreeItem(schema, specificName, parm));
}
2 changes: 1 addition & 1 deletion src/views/types/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Table from "../../database/table";
import ColumnTreeItem from "./ColumnTreeItem";

export async function getChildren(schema: string, name: string): Promise<ColumnTreeItem[]> {
const columns = await Table.getItems(schema, name);
const columns = (await Table.getItems(schema, name)).data;

return columns.map(column => new ColumnTreeItem(schema, name, column));
}
Loading