@@ -3,7 +3,7 @@ import { ComponentIdentification, ComponentState, IBMiComponent } from "@halcyon
33import { posix } from "path" ;
44import { getValidatorSource , VALIDATOR_NAME , WRAPPER_NAME } from "./checker" ;
55import { JobManager } from "../../config" ;
6- import { getInstance } from "../../base" ;
6+ import { getBase , getInstance } from "../../base" ;
77
88interface SqlCheckError {
99 CURSTMTLENGTH : number ;
@@ -42,14 +42,23 @@ export class SQLStatementChecker implements IBMiComponent {
4242 private readonly functionName = VALIDATOR_NAME ;
4343 private readonly currentVersion = 5 ;
4444
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+ }
4654
4755 static get ( ) : SQLStatementChecker | undefined {
4856 return getInstance ( ) ?. getConnection ( ) ?. getComponent < SQLStatementChecker > ( SQLStatementChecker . ID ) ;
4957 }
5058
5159 reset ( ) {
52- this . library = "" ;
60+ // This is called when connecting to a new system.
61+ this . library = undefined ;
5362 }
5463
5564 getIdentification ( ) : ComponentIdentification {
@@ -70,14 +79,14 @@ export class SQLStatementChecker implements IBMiComponent {
7079 }
7180
7281 async getRemoteState ( connection : IBMi ) {
73- this . library = connection . config ?. tempLibrary . toUpperCase ( ) || "ILEDITOR" ;
82+ const lib = this . getLibrary ( connection ) ;
7483
75- const wrapperVersion = await SQLStatementChecker . getVersionOf ( connection , this . library , WRAPPER_NAME ) ;
84+ const wrapperVersion = await SQLStatementChecker . getVersionOf ( connection , lib , WRAPPER_NAME ) ;
7685 if ( wrapperVersion < this . currentVersion ) {
7786 return `NeedsUpdate` ;
7887 }
7988
80- const installedVersion = await SQLStatementChecker . getVersionOf ( connection , this . library , this . functionName ) ;
89+ const installedVersion = await SQLStatementChecker . getVersionOf ( connection , lib , this . functionName ) ;
8190 if ( installedVersion < this . currentVersion ) {
8291 return `NeedsUpdate` ;
8392 }
@@ -88,7 +97,7 @@ export class SQLStatementChecker implements IBMiComponent {
8897 update ( connection : IBMi ) : ComponentState | Promise < ComponentState > {
8998 return connection . withTempDirectory ( async tempDir => {
9099 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" ) ) ;
92101 const result = await connection . runCommand ( {
93102 command : `RUNSQLSTM SRCSTMF('${ tempSourcePath } ') COMMIT(*NONE) NAMING(*SYS)` ,
94103 noLibList : true
@@ -102,14 +111,19 @@ export class SQLStatementChecker implements IBMiComponent {
102111 } ) ;
103112 }
104113
105- private getSource ( ) {
106- return getValidatorSource ( this . library , this . currentVersion ) ;
114+ private getSource ( connection : IBMi ) {
115+ return getValidatorSource ( this . getLibrary ( connection ) , this . currentVersion ) ;
107116 }
108117
109118 async call ( statement : string ) : Promise < SqlSyntaxError | undefined > {
119+ const connection = getInstance ( ) ?. getConnection ( ) ;
120+ if ( ! connection ) return undefined ;
121+
110122 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 ] } ) ;
113127
114128 if ( ! result . success || result . data . length === 0 ) return ;
115129
@@ -121,10 +135,14 @@ export class SQLStatementChecker implements IBMiComponent {
121135 }
122136
123137 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+
125141 const currentJob = JobManager . getSelection ( ) ;
142+ const library = this . getLibrary ( connection ) ;
126143
127- if ( currentJob ) {
144+ if ( currentJob && library ) {
145+ const checks = statements . map ( stmt => `select * from table(${ library } .${ this . functionName } (?)) x` ) . join ( ` union all ` ) ;
128146 const stmt = currentJob . job . query < SqlCheckError > ( checks , { parameters : statements } ) ;
129147 const result = await stmt . execute ( statements . length ) ;
130148 stmt . close ( ) ;
0 commit comments