1
+
1
2
import { getInstance } from "../base" ;
2
3
import { Query } from "./query" ;
3
- import { ServerComponent } from "./serverComponent" ;
4
+ import { ServerComponent , UpdateStatus } from "./serverComponent" ;
4
5
import { JobStatus , SQLJob } from "./sqlJob" ;
5
- import { QueryOptions , QueryResult , Rows } from "./types" ;
6
+ import { QueryOptions } from "./types" ;
7
+ import { askAboutNewJob , onConnectOrServerInstall } from "../config" ;
6
8
7
9
export interface JobInfo {
8
10
name : string ;
@@ -14,17 +16,17 @@ export class SQLJobManager {
14
16
private jobs : JobInfo [ ] = [ ] ;
15
17
selectedJob : number = - 1 ;
16
18
17
- constructor ( ) { }
19
+ constructor ( ) { }
18
20
19
21
async newJob ( predefinedJob ?: SQLJob , name ?: string ) {
20
22
if ( ServerComponent . isInstalled ( ) ) {
21
23
const instance = getInstance ( ) ;
22
24
const config = instance . getConfig ( ) ;
23
25
24
26
const newJob = predefinedJob || ( new SQLJob ( {
25
- libraries : [ config . currentLibrary , ...config . libraryList ] ,
26
- naming : `system` ,
27
- "full open" : false ,
27
+ libraries : [ config . currentLibrary , ...config . libraryList ] ,
28
+ naming : `system` ,
29
+ "full open" : false ,
28
30
"transaction isolation" : "none" ,
29
31
"query optimize goal" : "1" ,
30
32
"block size" : "512"
@@ -40,7 +42,7 @@ export class SQLJobManager {
40
42
job : newJob
41
43
} ) ;
42
44
43
- this . selectedJob = this . jobs . length - 1 ;
45
+ this . selectedJob = this . jobs . length - 1 ;
44
46
} catch ( e : any ) {
45
47
throw e ;
46
48
}
@@ -60,10 +62,10 @@ export class SQLJobManager {
60
62
async closeJob ( index ?: number ) {
61
63
if ( this . jobs [ index ] ) {
62
64
const selected : JobInfo = this . jobs [ index ] ;
63
-
65
+
64
66
selected . job . close ( ) ;
65
67
this . jobs . splice ( index , 1 ) ;
66
- this . selectedJob = this . selectedJob - 1 ;
68
+ this . selectedJob = this . selectedJob - 1 ;
67
69
}
68
70
}
69
71
@@ -72,11 +74,11 @@ export class SQLJobManager {
72
74
return this . closeJob ( id ) ;
73
75
}
74
76
75
- getSelection ( ) : JobInfo | undefined {
77
+ getSelection ( ) : JobInfo | undefined {
76
78
return this . jobs [ this . selectedJob ] ;
77
79
}
78
80
79
- getJob ( name : string ) : JobInfo | undefined {
81
+ getJob ( name : string ) : JobInfo | undefined {
80
82
return this . jobs . find ( info => info . name === name ) ;
81
83
}
82
84
@@ -89,37 +91,37 @@ export class SQLJobManager {
89
91
}
90
92
91
93
async runSQL < T > ( query : string , parameters : any [ ] = [ ] ) : Promise < T [ ] > {
92
- const selected = this . jobs [ this . selectedJob ]
93
- if ( ServerComponent . isInstalled ( ) && selected ) {
94
- // 2147483647 is NOT arbitrary. On the server side, this is processed as a Java
95
- // int. This is the largest number available without overflow (Integer.MAX_VALUE)
96
- const rowsToFetch = 2147483647 ;
97
-
98
- const statement = selected . job . query < T > ( query , { parameters} ) ;
99
- const results = await statement . run ( rowsToFetch ) ;
100
- statement . close ( ) ;
101
- return results . data ;
102
- } else {
103
- const instance = getInstance ( ) ;
104
- const config = instance . getConfig ( ) ;
105
- const content = instance . getContent ( ) ;
106
-
107
- const queryContext = [
108
- `SET CURRENT SCHEMA = '${ config . currentLibrary . toUpperCase ( ) } '` ,
109
- query
110
- ] . join ( `;\n` ) ;
111
-
112
- return content . runSQL ( queryContext ) as Promise < T [ ] > ;
113
- }
94
+ // 2147483647 is NOT arbitrary. On the server side, this is processed as a Java
95
+ // int. This is the largest number available without overflow (Integer.MAX_VALUE)
96
+ const rowsToFetch = 2147483647 ;
97
+
98
+ const statement = await this . getPagingStatement < T > ( query , { parameters } ) ;
99
+ const results = await statement . run ( rowsToFetch ) ;
100
+ statement . close ( ) ;
101
+ return results . data ;
114
102
}
115
- getPagingStatement < T > ( query : string , opts ?: QueryOptions ) : Query < T > {
103
+
104
+ async getPagingStatement < T > ( query : string , opts ?: QueryOptions ) : Promise < Query < T > > {
116
105
const selected = this . jobs [ this . selectedJob ]
117
106
if ( ServerComponent . isInstalled ( ) && selected ) {
118
107
return selected . job . query < T > ( query , opts ) ;
119
- } else if ( ! ServerComponent . isInstalled ( ) ) {
108
+
109
+ } else if ( ! ServerComponent . isInstalled ( ) ) {
110
+ let updateResult = await ServerComponent . checkForUpdate ( ) ;
111
+ if ( UpdateStatus . JUST_UPDATED === updateResult ) {
112
+ await onConnectOrServerInstall ( ) ;
113
+ return this . getPagingStatement ( query , opts ) ;
114
+ }
120
115
throw new Error ( `Database server component is required. Please see documentation for details.` ) ;
121
- } else {
122
- throw new Error ( `Active SQL job is required. Please spin one up first.` ) ;
116
+
117
+ } else {
118
+ const hasNewJob = await askAboutNewJob ( ) ;
119
+
120
+ if ( hasNewJob ) {
121
+ return this . getPagingStatement ( query , opts ) ;
122
+ } else {
123
+ throw new Error ( `Active SQL job is required. Please spin one up in the 'SQL Job Manager' view and try again.` ) ;
124
+ }
123
125
}
124
126
}
125
127
}
0 commit comments