@@ -19,9 +19,29 @@ import playgroundSearchTemplate from '../templates/playgroundSearchTemplate';
19
19
import playgroundTemplate from '../templates/playgroundTemplate' ;
20
20
import { StatusView } from '../views' ;
21
21
import TelemetryService from '../telemetry/telemetryService' ;
22
+ import { ConnectionOptions } from '../types/connectionOptionsType' ;
22
23
23
24
const log = createLogger ( 'playground controller' ) ;
24
25
26
+ const getSSLFilePathsFromConnectionModel = (
27
+ connectionModelDriverOptions : ConnectionOptions
28
+ ) : {
29
+ sslCA ?: string | string [ ] ;
30
+ sslCert ?: string | string [ ] ;
31
+ sslKey ?: string | string [ ] ;
32
+ } => {
33
+ const sslFilePaths = { } ;
34
+ [ 'sslCA' , 'sslCert' , 'sslKey' ] . forEach ( ( key ) => {
35
+ if ( connectionModelDriverOptions [ key ] ) {
36
+ sslFilePaths [ key ] = connectionModelDriverOptions [ key ] as (
37
+ string | string [ ]
38
+ ) ;
39
+ }
40
+ } ) ;
41
+
42
+ return sslFilePaths ;
43
+ } ;
44
+
25
45
/**
26
46
* This controller manages playground.
27
47
*/
@@ -36,7 +56,6 @@ export default class PlaygroundController {
36
56
_partialExecutionCodeLensProvider : PartialExecutionCodeLensProvider ;
37
57
_outputChannel : OutputChannel ;
38
58
_connectionString ?: string ;
39
- _connectionOptions ?: EJSON . SerializableTypes ;
40
59
_selectedText ?: string ;
41
60
_codeToEvaluate = '' ;
42
61
_isPartialRun : boolean ;
@@ -135,11 +154,11 @@ export default class PlaygroundController {
135
154
selectedText . length > 0 &&
136
155
selectedText . length >= lastSelectedLine . length
137
156
) {
138
- this . _partialExecutionCodeLensProvider ? .refresh (
157
+ this . _partialExecutionCodeLensProvider . refresh (
139
158
new vscode . Range ( firstLine , 0 , firstLine , 0 )
140
159
) ;
141
160
} else {
142
- this . _partialExecutionCodeLensProvider ? .refresh ( ) ;
161
+ this . _partialExecutionCodeLensProvider . refresh ( ) ;
143
162
}
144
163
}
145
164
@@ -148,26 +167,40 @@ export default class PlaygroundController {
148
167
}
149
168
150
169
async _connectToServiceProvider ( ) : Promise < void > {
151
- const model = this . _connectionController
152
- . getActiveConnectionModel ( )
153
- ?. getAttributes ( { derived : true } ) ;
154
-
155
- this . _connectionString = undefined ;
156
- this . _connectionOptions = undefined ;
157
-
158
170
await this . _languageServerController . disconnectFromServiceProvider ( ) ;
159
171
160
- if ( model && model . driverUrlWithSsh ) {
161
- this . _connectionString = model . driverUrlWithSsh ;
162
- this . _connectionOptions = model . driverOptions ? model . driverOptions : { } ;
172
+ const dataService = this . _connectionController . getActiveDataService ( ) ;
173
+ const connectionId = this . _connectionController . getActiveConnectionId ( ) ;
174
+ const connectionModel = this . _connectionController
175
+ . getActiveConnectionModel ( ) ;
176
+ if ( ! dataService || ! connectionId || ! connectionModel ) {
177
+ this . _activeConnectionCodeLensProvider . refresh ( ) ;
163
178
164
- await this . _languageServerController . connectToServiceProvider ( {
165
- connectionString : this . _connectionString ,
166
- connectionOptions : this . _connectionOptions
167
- } ) ;
179
+ return ;
168
180
}
169
181
170
- this . _activeConnectionCodeLensProvider ?. refresh ( ) ;
182
+ const connectionDetails = dataService . getConnectionOptions ( ) ;
183
+ const connectionString = connectionDetails . url ;
184
+ // We pass file paths to the language server since it doesn't
185
+ // handle being passsed buffers well.
186
+ // With driver version 4.0 we should be able to remove any use
187
+ // of buffers and just pass file paths.
188
+ const sslOptionsFilePaths = getSSLFilePathsFromConnectionModel (
189
+ connectionModel . getAttributes ( { derived : true } ) . driverOptions
190
+ ) ;
191
+
192
+ const connectionOptions : ConnectionOptions = {
193
+ ...connectionDetails . options ,
194
+ ...sslOptionsFilePaths
195
+ } ;
196
+
197
+ await this . _languageServerController . connectToServiceProvider ( {
198
+ connectionId,
199
+ connectionString,
200
+ connectionOptions
201
+ } ) ;
202
+
203
+ this . _activeConnectionCodeLensProvider . refresh ( ) ;
171
204
}
172
205
173
206
async _createPlaygroundFileWithContent (
@@ -241,12 +274,21 @@ export default class PlaygroundController {
241
274
}
242
275
243
276
async _evaluate ( codeToEvaluate : string ) : Promise < ShellExecuteAllResult > {
277
+ const connectionId = this . _connectionController . getActiveConnectionId ( ) ;
278
+
279
+ if ( ! connectionId ) {
280
+ return Promise . reject (
281
+ new Error ( 'Please connect to a database before running a playground.' )
282
+ ) ;
283
+ }
284
+
244
285
this . _statusView . showMessage ( 'Getting results...' ) ;
245
286
246
287
// Send a request to the language server to execute scripts from a playground.
247
- const result : ShellExecuteAllResult = await this . _languageServerController . executeAll (
248
- codeToEvaluate
249
- ) ;
288
+ const result : ShellExecuteAllResult = await this . _languageServerController . executeAll ( {
289
+ codeToEvaluate,
290
+ connectionId
291
+ } ) ;
250
292
251
293
this . _statusView . hideMessage ( ) ;
252
294
this . _telemetryService . trackPlaygroundCodeExecuted (
@@ -267,7 +309,7 @@ export default class PlaygroundController {
267
309
}
268
310
269
311
_evaluateWithCancelModal ( ) : Promise < ShellExecuteAllResult > {
270
- if ( ! this . _connectionString ) {
312
+ if ( ! this . _connectionController . isCurrentlyConnected ( ) ) {
271
313
return Promise . reject (
272
314
new Error ( 'Please connect to a database before running a playground.' )
273
315
) ;
@@ -372,7 +414,7 @@ export default class PlaygroundController {
372
414
. getConfiguration ( 'mdb' )
373
415
. get ( 'confirmRunAll' ) ;
374
416
375
- if ( ! this . _connectionString ) {
417
+ if ( ! this . _connectionController . isCurrentlyConnected ( ) ) {
376
418
vscode . window . showErrorMessage (
377
419
'Please connect to a database before running a playground.'
378
420
) ;
0 commit comments