@@ -4,21 +4,40 @@ import { ComponentState, IBMiComponent } from "./component";
44
55export class GetNewLibl implements IBMiComponent {
66 static ID = "GetNewLibl" ;
7+ private readonly procedureName = 'GETNEWLIBL' ;
8+ private readonly currentVersion = 1 ;
9+ private installedVersion = 0 ;
10+
11+ reset ( ) {
12+ this . installedVersion = 0 ;
13+ }
14+
715 getIdentification ( ) {
8- return { name : GetNewLibl . ID , version : 1 } ;
16+ return { name : GetNewLibl . ID , version : this . installedVersion } ;
917 }
1018
1119 async getRemoteState ( connection : IBMi ) : Promise < ComponentState > {
12- return connection . remoteFeatures [ `GETNEWLIBL.PGM` ] ? `Installed` : `NotInstalled` ;
20+ const [ result ] = await connection . runSQL ( `select cast(LONG_COMMENT as VarChar(200)) LONG_COMMENT from qsys2.sysprocs where routine_schema = '${ connection . getConfig ( ) . tempLibrary . toUpperCase ( ) } ' and routine_name = '${ this . procedureName } '` ) ;
21+ if ( result ?. LONG_COMMENT ) {
22+ const comment = result . LONG_COMMENT as string ;
23+ const dash = comment . indexOf ( '-' ) ;
24+ if ( dash > - 1 ) {
25+ this . installedVersion = Number ( comment . substring ( 0 , dash ) . trim ( ) ) ;
26+ }
27+ }
28+ if ( this . installedVersion < this . currentVersion ) {
29+ return `NeedsUpdate` ;
30+ }
31+
32+ return `Installed` ;
1333 }
1434
1535 update ( connection : IBMi ) : Promise < ComponentState > {
16- const config = connection . config !
17- const content = connection . getContent ( ) ;
36+ const config = connection . getConfig ( ) ;
1837 return connection . withTempDirectory ( async ( tempDir ) : Promise < ComponentState > => {
1938 const tempSourcePath = posix . join ( tempDir , `getnewlibl.sql` ) ;
2039
21- await content ! . writeStreamfileRaw ( tempSourcePath , getSource ( config . tempLibrary ) ) ;
40+ await connection . getContent ( ) . writeStreamfileRaw ( tempSourcePath , this . getSource ( config . tempLibrary ) ) ;
2241 const result = await connection . runCommand ( {
2342 command : `RUNSQLSTM SRCSTMF('${ tempSourcePath } ') COMMIT(*NONE) NAMING(*SQL)` ,
2443 cwd : `/` ,
@@ -35,7 +54,7 @@ export class GetNewLibl implements IBMiComponent {
3554
3655 async getLibraryListFromCommand ( connection : IBMi , ileCommand : string ) {
3756 const tempLib = connection . config ! . tempLibrary ;
38- const resultSet = await connection . runSQL ( `CALL ${ tempLib } .GETNEWLIBL ('${ ileCommand . replace ( new RegExp ( `'` , 'g' ) , `''` ) } ')` ) ;
57+ const resultSet = await connection . runSQL ( `CALL ${ tempLib } .${ this . procedureName } ('${ ileCommand . replace ( new RegExp ( `'` , 'g' ) , `''` ) } ')` ) ;
3958
4059 const result = {
4160 currentLibrary : `QGPL` ,
@@ -56,20 +75,22 @@ export class GetNewLibl implements IBMiComponent {
5675
5776 return result ;
5877 }
59- }
6078
61- function getSource ( library : string ) {
62- return Buffer . from ( [
63- `CREATE OR REPLACE PROCEDURE ${ library } .GETNEWLIBL(IN COMMAND VARCHAR(2000))` ,
64- `DYNAMIC RESULT SETS 1 ` ,
65- `BEGIN` ,
66- ` DECLARE clibl CURSOR FOR ` ,
67- ` SELECT ORDINAL_POSITION, TYPE as PORTION, SYSTEM_SCHEMA_NAME` ,
68- ` FROM QSYS2.LIBRARY_LIST_INFO;` ,
69- ` CALL QSYS2.QCMDEXC(COMMAND);` ,
70- ` OPEN clibl;` ,
71- `END;` ,
72- `` ,
73- `call QSYS2.QCMDEXC( 'grtobjaut ${ library } /GETNEWLIBL *PGM *PUBLIC *ALL' );`
74- ] . join ( `\n` ) , "utf8" ) ;
79+ private getSource ( library : string ) {
80+ return Buffer . from ( [
81+ `CREATE OR REPLACE PROCEDURE ${ library } .${ this . procedureName } (IN COMMAND VARCHAR(2000))` ,
82+ `DYNAMIC RESULT SETS 1 ` ,
83+ `BEGIN` ,
84+ ` DECLARE clibl CURSOR FOR ` ,
85+ ` SELECT ORDINAL_POSITION, TYPE as PORTION, SYSTEM_SCHEMA_NAME` ,
86+ ` FROM QSYS2.LIBRARY_LIST_INFO;` ,
87+ ` CALL QSYS2.QCMDEXC(COMMAND);` ,
88+ ` OPEN clibl;` ,
89+ `END;` ,
90+ `` ,
91+ `comment on procedure ${ library } .${ this . procedureName } is '${ this . currentVersion } - Validate member information';` ,
92+ `` ,
93+ `call QSYS2.QCMDEXC( 'grtobjaut ${ library } /${ this . procedureName } *PGM *PUBLIC *ALL' );`
94+ ] . join ( `\n` ) , "utf8" ) ;
95+ }
7596}
0 commit comments