1
1
import { posix } from "path" ;
2
2
import IBMi from "../api/IBMi" ;
3
- import { instance } from "../instantiate" ;
4
3
import { ComponentState , IBMiComponent } from "./component" ;
5
4
6
5
export class GetNewLibl implements IBMiComponent {
7
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
+
8
15
getIdentification ( ) {
9
- return { name : GetNewLibl . ID , version : 1 } ;
16
+ return { name : GetNewLibl . ID , version : this . installedVersion } ;
10
17
}
11
18
12
19
async getRemoteState ( connection : IBMi ) : Promise < ComponentState > {
13
- 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` ;
14
33
}
15
34
16
35
update ( connection : IBMi ) : Promise < ComponentState > {
17
36
const config = connection . config !
18
- const content = instance . getContent ( ) ;
19
37
return connection . withTempDirectory ( async ( tempDir ) : Promise < ComponentState > => {
20
38
const tempSourcePath = posix . join ( tempDir , `getnewlibl.sql` ) ;
21
39
22
- await content ! . writeStreamfileRaw ( tempSourcePath , getSource ( config . tempLibrary ) ) ;
40
+ await connection . getContent ( ) . writeStreamfileRaw ( tempSourcePath , this . getSource ( config . tempLibrary ) ) ;
23
41
const result = await connection . runCommand ( {
24
42
command : `RUNSQLSTM SRCSTMF('${ tempSourcePath } ') COMMIT(*NONE) NAMING(*SQL)` ,
25
43
cwd : `/` ,
@@ -36,7 +54,7 @@ export class GetNewLibl implements IBMiComponent {
36
54
37
55
async getLibraryListFromCommand ( connection : IBMi , ileCommand : string ) {
38
56
const tempLib = connection . config ! . tempLibrary ;
39
- 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' ) , `''` ) } ')` ) ;
40
58
41
59
const result = {
42
60
currentLibrary : `QGPL` ,
@@ -57,20 +75,22 @@ export class GetNewLibl implements IBMiComponent {
57
75
58
76
return result ;
59
77
}
60
- }
61
78
62
- function getSource ( library : string ) {
63
- return Buffer . from ( [
64
- `CREATE OR REPLACE PROCEDURE ${ library } .GETNEWLIBL(IN COMMAND VARCHAR(2000))` ,
65
- `DYNAMIC RESULT SETS 1 ` ,
66
- `BEGIN` ,
67
- ` DECLARE clibl CURSOR FOR ` ,
68
- ` SELECT ORDINAL_POSITION, TYPE as PORTION, SYSTEM_SCHEMA_NAME` ,
69
- ` FROM QSYS2.LIBRARY_LIST_INFO;` ,
70
- ` CALL QSYS2.QCMDEXC(COMMAND);` ,
71
- ` OPEN clibl;` ,
72
- `END;` ,
73
- `` ,
74
- `call QSYS2.QCMDEXC( 'grtobjaut ${ library } /GETNEWLIBL *PGM *PUBLIC *ALL' );`
75
- ] . 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
+ }
76
96
}
0 commit comments