Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
70 changes: 70 additions & 0 deletions src/api/components/mbrAttr/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import path from "path";
import IBMi from "../../IBMi";
import { ComponentIdentification, IBMiComponent, SecureComponentState } from "../component";
import { buildMbrAttrSqlSource, MBR_ATTR_RPGLE_SOURCE } from "./source";

export class MbrAttr implements IBMiComponent {
static readonly ID = "mbr_attr";
static readonly VERSION = 1;
static readonly SIGNATURE = `MBR_ATTR:${MbrAttr.VERSION}`;
static readonly PGM_NAME = "MBR_ATTR";
static readonly SPECIFIC_NAME = "MBR_ATTR";

getIdentification(): ComponentIdentification {
return {
name: MbrAttr.ID,
version: MbrAttr.VERSION,
signature: MbrAttr.SIGNATURE
};
}

async getRemoteState(connection: IBMi): Promise<SecureComponentState> {
const library = connection.getConfig().tempLibrary.toUpperCase();
const [row] = await connection.runSQL(/* sql */`
select cast(long_comment as varchar(200)) as LONG_COMMENT
from qsys2.sysroutines
where routine_type = 'FUNCTION'
and routine_schema = '${library}'
and specific_name = '${MbrAttr.SPECIFIC_NAME}'
fetch first row only
`);

if (!row) {
return { status: "NotInstalled", remoteSignature: MbrAttr.SIGNATURE };

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return a blank remoteSignature if it's not installed.

Suggested change
return { status: "NotInstalled", remoteSignature: MbrAttr.SIGNATURE };
return { status: "NotInstalled", remoteSignature: '' };

}

const longComment = String(row.LONG_COMMENT || "").trim();
const remoteVersion = Number(longComment.match(/^(\d+)/)?.[1] || -1);
const status = remoteVersion >= MbrAttr.VERSION ? "Installed" : "NeedsUpdate";

return { status, remoteSignature: MbrAttr.SIGNATURE };

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use connection.getContent().getSQLRoutineSignature to get the function's signature. It will at least ensure that it's not been changed to call another program.

}

async update(connection: IBMi, installDirectory: string): Promise<SecureComponentState> {
const library = connection.getConfig().tempLibrary.toUpperCase();
const componentDir = path.posix.join(installDirectory, "mbr_attr");
const rpglePath = path.posix.join(componentDir, `${MbrAttr.PGM_NAME}.RPGLE`);
const sqlPath = path.posix.join(componentDir, `${MbrAttr.PGM_NAME}.SQL`);

await connection.sendCommand({ command: `mkdir -p ${componentDir}` });

await connection.getContent().writeStreamfileRaw(rpglePath, MBR_ATTR_RPGLE_SOURCE);
await connection.getContent().writeStreamfileRaw(sqlPath, buildMbrAttrSqlSource(library, MbrAttr.VERSION));

const compileResult = await connection.runCommand({
command: `QSYS/CRTBNDRPG PGM(${library}/${MbrAttr.PGM_NAME}) SRCSTMF('${rpglePath}') OPTION(*EVENTF) DBGVIEW(*NONE) TGTCCSID(*JOB)`
});
if (compileResult.code !== 0) {
throw new Error(`Failed to compile ${MbrAttr.PGM_NAME}: ${compileResult.stderr || compileResult.stdout}`);
}

const sqlResult = await connection.runCommand({
command: `QSYS/RUNSQLSTM SRCSTMF('${sqlPath}') COMMIT(*NONE) NAMING(*SQL)`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the SQL code should not be qualified, this will create the function in the right library.

Suggested change
command: `QSYS/RUNSQLSTM SRCSTMF('${sqlPath}') COMMIT(*NONE) NAMING(*SQL)`
command: `QSYS/RUNSQLSTM SRCSTMF('${sqlPath}') COMMIT(*NONE) NAMING(*SQL) DFTRDBCOL(${library})`

});
if (sqlResult.code !== 0) {
throw new Error(`Failed to install SQL routine ${library}.${MbrAttr.SPECIFIC_NAME}: ${sqlResult.stderr || sqlResult.stdout}`);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can wrap that part and take advantage of the connection.withTempDirectory method here. It creates a temporary directory that gets deleted after it's done, regardless of its result. Since we don't need to keep the source files, that may be better.

Suggested change
const componentDir = path.posix.join(installDirectory, "mbr_attr");
const rpglePath = path.posix.join(componentDir, `${MbrAttr.PGM_NAME}.RPGLE`);
const sqlPath = path.posix.join(componentDir, `${MbrAttr.PGM_NAME}.SQL`);
await connection.sendCommand({ command: `mkdir -p ${componentDir}` });
await connection.getContent().writeStreamfileRaw(rpglePath, MBR_ATTR_RPGLE_SOURCE);
await connection.getContent().writeStreamfileRaw(sqlPath, buildMbrAttrSqlSource(library, MbrAttr.VERSION));
const compileResult = await connection.runCommand({
command: `QSYS/CRTBNDRPG PGM(${library}/${MbrAttr.PGM_NAME}) SRCSTMF('${rpglePath}') OPTION(*EVENTF) DBGVIEW(*NONE) TGTCCSID(*JOB)`
});
if (compileResult.code !== 0) {
throw new Error(`Failed to compile ${MbrAttr.PGM_NAME}: ${compileResult.stderr || compileResult.stdout}`);
}
const sqlResult = await connection.runCommand({
command: `QSYS/RUNSQLSTM SRCSTMF('${sqlPath}') COMMIT(*NONE) NAMING(*SQL)`
});
if (sqlResult.code !== 0) {
throw new Error(`Failed to install SQL routine ${library}.${MbrAttr.SPECIFIC_NAME}: ${sqlResult.stderr || sqlResult.stdout}`);
}
await connection.withTempDirectory(async componentDir => {
const rpglePath = path.posix.join(componentDir, `${MbrAttr.PGM_NAME}.RPGLE`);
const sqlPath = path.posix.join(componentDir, `${MbrAttr.PGM_NAME}.SQL`);
await connection.sendCommand({ command: `mkdir -p ${componentDir}` });
await connection.getContent().writeStreamfileRaw(rpglePath, MBR_ATTR_RPGLE_SOURCE);
await connection.getContent().writeStreamfileRaw(sqlPath, buildMbrAttrSqlSource(library, MbrAttr.VERSION));
const compileResult = await connection.runCommand({
command: `QSYS/CRTBNDRPG PGM(${library}/${MbrAttr.PGM_NAME}) SRCSTMF('${rpglePath}') OPTION(*EVENTF) DBGVIEW(*NONE) TGTCCSID(*JOB)`
});
if (compileResult.code !== 0) {
throw new Error(`Failed to compile ${MbrAttr.PGM_NAME}: ${compileResult.stderr || compileResult.stdout}`);
}
const sqlResult = await connection.runCommand({
command: `QSYS/RUNSQLSTM SRCSTMF('${sqlPath}') COMMIT(*NONE) NAMING(*SQL)`
});
if (sqlResult.code !== 0) {
throw new Error(`Failed to install SQL routine ${library}.${MbrAttr.SPECIFIC_NAME}: ${sqlResult.stderr || sqlResult.stdout}`);
}
});


return this.getRemoteState(connection);
}
}
Loading
Loading