@@ -3,7 +3,7 @@ import { ComponentIdentification, ComponentState, IBMiComponent } from "@halcyon
3
3
import { posix } from "path" ;
4
4
import { getValidatorSource , VALIDATOR_NAME , WRAPPER_NAME } from "./checker" ;
5
5
import { JobManager } from "../../config" ;
6
- import { getInstance } from "../../base" ;
6
+ import { getBase , getInstance } from "../../base" ;
7
7
8
8
interface SqlCheckError {
9
9
CURSTMTLENGTH : number ;
@@ -42,14 +42,23 @@ export class SQLStatementChecker implements IBMiComponent {
42
42
private readonly functionName = VALIDATOR_NAME ;
43
43
private readonly currentVersion = 5 ;
44
44
45
- private library = "" ;
45
+ private library : string | undefined ;
46
+
47
+ private getLibrary ( connection : IBMi ) {
48
+ if ( ! this . library ) {
49
+ this . library = connection ?. config ?. tempLibrary . toUpperCase ( ) || `ILEDITOR` ;
50
+ }
51
+
52
+ return this . library ;
53
+ }
46
54
47
55
static get ( ) : SQLStatementChecker | undefined {
48
56
return getInstance ( ) ?. getConnection ( ) ?. getComponent < SQLStatementChecker > ( SQLStatementChecker . ID ) ;
49
57
}
50
58
51
59
reset ( ) {
52
- this . library = "" ;
60
+ // This is called when connecting to a new system.
61
+ this . library = undefined ;
53
62
}
54
63
55
64
getIdentification ( ) : ComponentIdentification {
@@ -70,14 +79,14 @@ export class SQLStatementChecker implements IBMiComponent {
70
79
}
71
80
72
81
async getRemoteState ( connection : IBMi ) {
73
- this . library = connection . config ?. tempLibrary . toUpperCase ( ) || "ILEDITOR" ;
82
+ const lib = this . getLibrary ( connection ) ;
74
83
75
- const wrapperVersion = await SQLStatementChecker . getVersionOf ( connection , this . library , WRAPPER_NAME ) ;
84
+ const wrapperVersion = await SQLStatementChecker . getVersionOf ( connection , lib , WRAPPER_NAME ) ;
76
85
if ( wrapperVersion < this . currentVersion ) {
77
86
return `NeedsUpdate` ;
78
87
}
79
88
80
- const installedVersion = await SQLStatementChecker . getVersionOf ( connection , this . library , this . functionName ) ;
89
+ const installedVersion = await SQLStatementChecker . getVersionOf ( connection , lib , this . functionName ) ;
81
90
if ( installedVersion < this . currentVersion ) {
82
91
return `NeedsUpdate` ;
83
92
}
@@ -88,7 +97,7 @@ export class SQLStatementChecker implements IBMiComponent {
88
97
update ( connection : IBMi ) : ComponentState | Promise < ComponentState > {
89
98
return connection . withTempDirectory ( async tempDir => {
90
99
const tempSourcePath = posix . join ( tempDir , `sqlchecker.sql` ) ;
91
- await connection . content . writeStreamfileRaw ( tempSourcePath , Buffer . from ( this . getSource ( ) , "utf-8" ) ) ;
100
+ await connection . content . writeStreamfileRaw ( tempSourcePath , Buffer . from ( this . getSource ( connection ) , "utf-8" ) ) ;
92
101
const result = await connection . runCommand ( {
93
102
command : `RUNSQLSTM SRCSTMF('${ tempSourcePath } ') COMMIT(*NONE) NAMING(*SYS)` ,
94
103
noLibList : true
@@ -102,14 +111,19 @@ export class SQLStatementChecker implements IBMiComponent {
102
111
} ) ;
103
112
}
104
113
105
- private getSource ( ) {
106
- return getValidatorSource ( this . library , this . currentVersion ) ;
114
+ private getSource ( connection : IBMi ) {
115
+ return getValidatorSource ( this . getLibrary ( connection ) , this . currentVersion ) ;
107
116
}
108
117
109
118
async call ( statement : string ) : Promise < SqlSyntaxError | undefined > {
119
+ const connection = getInstance ( ) ?. getConnection ( ) ;
120
+ if ( ! connection ) return undefined ;
121
+
110
122
const currentJob = JobManager . getSelection ( ) ;
111
- if ( currentJob ) {
112
- const result = await currentJob . job . execute < SqlCheckError > ( `select * from table(${ this . library } .${ this . functionName } (?)) x` , { parameters : [ statement ] } ) ;
123
+ const library = this . getLibrary ( connection ) ;
124
+
125
+ if ( currentJob && library ) {
126
+ const result = await currentJob . job . execute < SqlCheckError > ( `select * from table(${ library } .${ this . functionName } (?)) x` , { parameters : [ statement ] } ) ;
113
127
114
128
if ( ! result . success || result . data . length === 0 ) return ;
115
129
@@ -121,10 +135,14 @@ export class SQLStatementChecker implements IBMiComponent {
121
135
}
122
136
123
137
async checkMultipleStatements ( statements : string [ ] ) : Promise < SqlSyntaxError [ ] | undefined > {
124
- const checks = statements . map ( stmt => `select * from table(${ this . library } .${ this . functionName } (?)) x` ) . join ( ` union all ` ) ;
138
+ const connection = getInstance ( ) ?. getConnection ( ) ;
139
+ if ( ! connection ) return undefined ;
140
+
125
141
const currentJob = JobManager . getSelection ( ) ;
142
+ const library = this . getLibrary ( connection ) ;
126
143
127
- if ( currentJob ) {
144
+ if ( currentJob && library ) {
145
+ const checks = statements . map ( stmt => `select * from table(${ library } .${ this . functionName } (?)) x` ) . join ( ` union all ` ) ;
128
146
const stmt = currentJob . job . query < SqlCheckError > ( checks , { parameters : statements } ) ;
129
147
const result = await stmt . execute ( statements . length ) ;
130
148
stmt . close ( ) ;
0 commit comments