@@ -19,9 +19,29 @@ import playgroundSearchTemplate from '../templates/playgroundSearchTemplate';
1919import playgroundTemplate from '../templates/playgroundTemplate' ;
2020import { StatusView } from '../views' ;
2121import TelemetryService from '../telemetry/telemetryService' ;
22+ import { ConnectionOptions } from '../types/connectionOptionsType' ;
2223
2324const log = createLogger ( 'playground controller' ) ;
2425
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+
2545/**
2646 * This controller manages playground.
2747 */
@@ -36,7 +56,6 @@ export default class PlaygroundController {
3656 _partialExecutionCodeLensProvider : PartialExecutionCodeLensProvider ;
3757 _outputChannel : OutputChannel ;
3858 _connectionString ?: string ;
39- _connectionOptions ?: EJSON . SerializableTypes ;
4059 _selectedText ?: string ;
4160 _codeToEvaluate = '' ;
4261 _isPartialRun : boolean ;
@@ -135,11 +154,11 @@ export default class PlaygroundController {
135154 selectedText . length > 0 &&
136155 selectedText . length >= lastSelectedLine . length
137156 ) {
138- this . _partialExecutionCodeLensProvider ? .refresh (
157+ this . _partialExecutionCodeLensProvider . refresh (
139158 new vscode . Range ( firstLine , 0 , firstLine , 0 )
140159 ) ;
141160 } else {
142- this . _partialExecutionCodeLensProvider ? .refresh ( ) ;
161+ this . _partialExecutionCodeLensProvider . refresh ( ) ;
143162 }
144163 }
145164
@@ -148,26 +167,40 @@ export default class PlaygroundController {
148167 }
149168
150169 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-
158170 await this . _languageServerController . disconnectFromServiceProvider ( ) ;
159171
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 ( ) ;
163178
164- await this . _languageServerController . connectToServiceProvider ( {
165- connectionString : this . _connectionString ,
166- connectionOptions : this . _connectionOptions
167- } ) ;
179+ return ;
168180 }
169181
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 ( ) ;
171204 }
172205
173206 async _createPlaygroundFileWithContent (
@@ -241,12 +274,21 @@ export default class PlaygroundController {
241274 }
242275
243276 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+
244285 this . _statusView . showMessage ( 'Getting results...' ) ;
245286
246287 // 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+ } ) ;
250292
251293 this . _statusView . hideMessage ( ) ;
252294 this . _telemetryService . trackPlaygroundCodeExecuted (
@@ -267,7 +309,7 @@ export default class PlaygroundController {
267309 }
268310
269311 _evaluateWithCancelModal ( ) : Promise < ShellExecuteAllResult > {
270- if ( ! this . _connectionString ) {
312+ if ( ! this . _connectionController . isCurrentlyConnected ( ) ) {
271313 return Promise . reject (
272314 new Error ( 'Please connect to a database before running a playground.' )
273315 ) ;
@@ -372,7 +414,7 @@ export default class PlaygroundController {
372414 . getConfiguration ( 'mdb' )
373415 . get ( 'confirmRunAll' ) ;
374416
375- if ( ! this . _connectionString ) {
417+ if ( ! this . _connectionController . isCurrentlyConnected ( ) ) {
376418 vscode . window . showErrorMessage (
377419 'Please connect to a database before running a playground.'
378420 ) ;
0 commit comments