@@ -4,21 +4,40 @@ import { ComponentState, IBMiComponent } from "./component";
4
4
5
5
export class GetNewLibl implements IBMiComponent {
6
6
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
+
7
15
getIdentification ( ) {
8
- return { name : GetNewLibl . ID , version : 1 } ;
16
+ return { name : GetNewLibl . ID , version : this . installedVersion } ;
9
17
}
10
18
11
19
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` ;
13
33
}
14
34
15
35
update ( connection : IBMi ) : Promise < ComponentState > {
16
- const config = connection . config !
17
- const content = connection . getContent ( ) ;
36
+ const config = connection . getConfig ( ) ;
18
37
return connection . withTempDirectory ( async ( tempDir ) : Promise < ComponentState > => {
19
38
const tempSourcePath = posix . join ( tempDir , `getnewlibl.sql` ) ;
20
39
21
- await content ! . writeStreamfileRaw ( tempSourcePath , getSource ( config . tempLibrary ) ) ;
40
+ await connection . getContent ( ) . writeStreamfileRaw ( tempSourcePath , this . getSource ( config . tempLibrary ) ) ;
22
41
const result = await connection . runCommand ( {
23
42
command : `RUNSQLSTM SRCSTMF('${ tempSourcePath } ') COMMIT(*NONE) NAMING(*SQL)` ,
24
43
cwd : `/` ,
@@ -35,7 +54,7 @@ export class GetNewLibl implements IBMiComponent {
35
54
36
55
async getLibraryListFromCommand ( connection : IBMi , ileCommand : string ) {
37
56
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' ) , `''` ) } ')` ) ;
39
58
40
59
const result = {
41
60
currentLibrary : `QGPL` ,
@@ -56,20 +75,22 @@ export class GetNewLibl implements IBMiComponent {
56
75
57
76
return result ;
58
77
}
59
- }
60
78
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
+ }
75
96
}
0 commit comments