11'use strict' ;
22import * as vscode from 'vscode' ;
33import * as request from 'request-promise-native' ;
4- //import * as sqlops from 'sqlops';
4+ import * as sqlops from 'sqlops' ;
5+ import { error } from 'util' ;
56
67
78export function activate ( context : vscode . ExtensionContext ) {
@@ -15,17 +16,7 @@ export function activate(context: vscode.ExtensionContext) {
1516 } ;
1617 console . log ( 'Bringing in the first responder kit from the mothership.' ) ;
1718 const scriptText = await request . get ( options ) ;
18- var setting : vscode . Uri = vscode . Uri . parse ( "untitled:" + fileName ) ;
19- vscode . workspace . openTextDocument ( setting ) . then ( ( a : vscode . TextDocument ) => {
20- vscode . window . showTextDocument ( a , 1 , false ) . then ( e => {
21- e . edit ( edit => {
22- edit . insert ( new vscode . Position ( 0 , 0 ) , scriptText ) ;
23- } ) ;
24- } ) ;
25- } , ( error : any ) => {
26- console . error ( error ) ;
27- } ) ;
28-
19+ await placescript ( fileName , scriptText ) ;
2920 } ;
3021 var disposable_spblitz = vscode . commands . registerCommand ( 'extension.sp_blitz' , getblitz ) ;
3122 context . subscriptions . push ( disposable_spblitz ) ;
@@ -38,17 +29,7 @@ export function activate(context: vscode.ExtensionContext) {
3829 } ;
3930 console . log ( 'Bringing in the first responder kit from the mothership.' ) ;
4031 const scriptText = await request . get ( options ) ;
41- var setting : vscode . Uri = vscode . Uri . parse ( "untitled:" + fileName ) ;
42- vscode . workspace . openTextDocument ( setting ) . then ( ( a : vscode . TextDocument ) => {
43- vscode . window . showTextDocument ( a , 1 , false ) . then ( e => {
44- e . edit ( edit => {
45- edit . insert ( new vscode . Position ( 0 , 0 ) , scriptText ) ;
46- } ) ;
47- } ) ;
48- } , ( error : any ) => {
49- console . error ( error ) ;
50- } ) ;
51-
32+ await placescript ( fileName , scriptText ) ;
5233 } ;
5334 var disposable_spblitzcache = vscode . commands . registerCommand ( 'extension.sp_blitzcache' , getblitzcache ) ;
5435 context . subscriptions . push ( disposable_spblitzcache ) ;
@@ -61,17 +42,7 @@ export function activate(context: vscode.ExtensionContext) {
6142 } ;
6243 console . log ( 'Bringing in the first responder kit from the mothership.' ) ;
6344 const scriptText = await request . get ( options ) ;
64- var setting : vscode . Uri = vscode . Uri . parse ( "untitled:" + fileName ) ;
65- vscode . workspace . openTextDocument ( setting ) . then ( ( a : vscode . TextDocument ) => {
66- vscode . window . showTextDocument ( a , 1 , false ) . then ( e => {
67- e . edit ( edit => {
68- edit . insert ( new vscode . Position ( 0 , 0 ) , scriptText ) ;
69- } ) ;
70- } ) ;
71- } , ( error : any ) => {
72- console . error ( error ) ;
73- } ) ;
74-
45+ await placescript ( fileName , scriptText ) ;
7546 } ;
7647 var disposable_spblitzfirst = vscode . commands . registerCommand ( 'extension.sp_blitzfirst' , getblitzfirst ) ;
7748 context . subscriptions . push ( disposable_spblitzfirst ) ;
@@ -84,23 +55,13 @@ export function activate(context: vscode.ExtensionContext) {
8455 } ;
8556 console . log ( 'Bringing in the first responder kit from the mothership.' ) ;
8657 const scriptText = await request . get ( options ) ;
87- var setting : vscode . Uri = vscode . Uri . parse ( "untitled:" + fileName ) ;
88- vscode . workspace . openTextDocument ( setting ) . then ( ( a : vscode . TextDocument ) => {
89- vscode . window . showTextDocument ( a , 1 , false ) . then ( e => {
90- e . edit ( edit => {
91- edit . insert ( new vscode . Position ( 0 , 0 ) , scriptText ) ;
92- } ) ;
93- } ) ;
94- } , ( error : any ) => {
95- console . error ( error ) ;
96- } ) ;
97-
58+ await placescript ( fileName , scriptText ) ;
9859 } ;
9960 var disposable_spblitzwho = vscode . commands . registerCommand ( 'extension.sp_blitzwho' , getblitzwho ) ;
10061 context . subscriptions . push ( disposable_spblitzwho ) ;
10162
10263 //creating the quickrun script
103- var disposable_runspblitz = vscode . commands . registerCommand ( 'extension.run_sp_blitz' , ( ) => {
64+ var runspblitz = async ( ) => {
10465 console . log ( 'Preparing sample run script.' ) ;
10566 let fileName = "exec_sp_blitz.sql" ;
10667 const scriptText = `EXEC [dbo].[sp_Blitz]
@@ -113,16 +74,9 @@ export function activate(context: vscode.ExtensionContext) {
11374 -- uncomment the following line to write results to an output table
11475 --, @OutputDatabaseName = 'DBAtools', @OutputSchemaName = 'dbo', @OutputTableName = 'BlitzResults'` ;
11576 var setting : vscode . Uri = vscode . Uri . parse ( "untitled:" + fileName ) ;
116- vscode . workspace . openTextDocument ( setting ) . then ( ( a : vscode . TextDocument ) => {
117- vscode . window . showTextDocument ( a , 1 , false ) . then ( e => {
118- e . edit ( edit => {
119- edit . insert ( new vscode . Position ( 0 , 0 ) , scriptText ) ;
120- } ) ;
121- } ) ;
122- } , ( error : any ) => {
123- console . error ( error ) ;
124- } ) ;
125- } ) ;
77+ await placescript ( fileName , scriptText ) ;
78+ } ;
79+ var disposable_runspblitz = vscode . commands . registerCommand ( 'extension.run_sp_blitz' , runspblitz ) ;
12680 context . subscriptions . push ( disposable_runspblitz ) ;
12781
12882 //importing the full spblitz script
@@ -133,23 +87,13 @@ export function activate(context: vscode.ExtensionContext) {
13387 } ;
13488 console . log ( 'Bringing in the first responder kit from the mothership.' ) ;
13589 const scriptText = await request . get ( options ) ;
136- var setting : vscode . Uri = vscode . Uri . parse ( "untitled:" + fileName ) ;
137- vscode . workspace . openTextDocument ( setting ) . then ( ( a : vscode . TextDocument ) => {
138- vscode . window . showTextDocument ( a , 1 , false ) . then ( e => {
139- e . edit ( edit => {
140- edit . insert ( new vscode . Position ( 0 , 0 ) , scriptText ) ;
141- } ) ;
142- } ) ;
143- } , ( error : any ) => {
144- console . error ( error ) ;
145- } ) ;
146-
90+ await placescript ( fileName , scriptText ) ;
14791 } ;
14892 var disposable_spblitzindex = vscode . commands . registerCommand ( 'extension.sp_blitzindex' , getblitzindex ) ;
14993 context . subscriptions . push ( disposable_spblitzindex ) ;
15094
15195 //creating the quickrun script
152- var disposable_runspblitzindex = vscode . commands . registerCommand ( 'extension.run_sp_blitzindex' , ( ) => {
96+ var runspblitzindex = async ( ) => {
15397 console . log ( 'Preparing sample run script.' ) ;
15498 let fileName = "exec_sp_blitzindex.sql" ;
15599 const scriptText = `EXEC [dbo].[sp_BlitzIndex]
@@ -161,64 +105,59 @@ export function activate(context: vscode.ExtensionContext) {
161105 @CheckServerInfo = 1
162106 -- uncomment the following line to write results to an output table
163107 --, @OutputDatabaseName = 'DBAtools', @OutputSchemaName = 'dbo', @OutputTableName = 'BlitzResults'` ;
164- var setting : vscode . Uri = vscode . Uri . parse ( "untitled:" + fileName ) ;
165- vscode . workspace . openTextDocument ( setting ) . then ( ( a : vscode . TextDocument ) => {
166- vscode . window . showTextDocument ( a , 1 , false ) . then ( e => {
167- e . edit ( edit => {
168- edit . insert ( new vscode . Position ( 0 , 0 ) , scriptText ) ;
169- } ) ;
170- } ) ;
171- } , ( error : any ) => {
172- console . error ( error ) ;
173- } ) ;
174- } ) ;
108+ await placescript ( fileName , scriptText ) ;
109+ } ;
110+ var disposable_runspblitzindex = vscode . commands . registerCommand ( 'extension.run_sp_blitzindex' , runspblitzindex ) ;
175111 context . subscriptions . push ( disposable_runspblitzindex ) ;
176112
177113 //creating the quickrun script for blitzcache
178- var disposable_runspblitzcache = vscode . commands . registerCommand ( 'extension.run_sp_blitzcache' , ( ) => {
114+ var runspblitzcache = async ( ) => {
179115 let fileName = "exec_sp_blitzcache.sql" ;
180116 console . log ( 'Preparing sample run script.' ) ;
181117 const scriptText = `EXEC [dbo].[sp_BlitzCache]
182118 @SortOrder = 'reads',
183119 -- CPU, executions, xpm, recent compilations, memory grant, writes, all
184120 @Top = 10
185121 ` ;
186- var setting : vscode . Uri = vscode . Uri . parse ( "untitled:" + fileName ) ;
187- vscode . workspace . openTextDocument ( setting ) . then ( ( a : vscode . TextDocument ) => {
188- vscode . window . showTextDocument ( a , 1 , false ) . then ( e => {
189- e . edit ( edit => {
190- edit . insert ( new vscode . Position ( 0 , 0 ) , scriptText ) ;
191- } ) ;
192- } ) ;
193- } , ( error : any ) => {
194- console . error ( error ) ;
195- } ) ;
196- } ) ;
122+ await placescript ( fileName , scriptText ) ;
123+ } ;
124+ var disposable_runspblitzcache = vscode . commands . registerCommand ( 'extension.run_sp_blitzcache' , runspblitzcache )
197125 context . subscriptions . push ( disposable_runspblitzcache ) ;
198126
199127 //creating the quickrun script
200- var disposable_runspblitzfirst = vscode . commands . registerCommand ( 'extension.run_sp_blitzfirst' , ( ) => {
128+ var runspblitzfirst = async ( ) => {
201129 let fileName = "exec_sp_blitzfirst.sql" ;
202130 console . log ( 'Preparing sample run script.' ) ;
203131 const scriptText = `EXEC [dbo].[sp_BlitzFirst]
204132 @Seconds = 5,
205133 @ShowSleepingSPIDs = 0,
206134 @ExpertMode = 0 --1 will also run sp_BlitzWho
207135 ` ;
208- var setting : vscode . Uri = vscode . Uri . parse ( "untitled:" + fileName ) ;
209- vscode . workspace . openTextDocument ( setting ) . then ( ( a : vscode . TextDocument ) => {
210- vscode . window . showTextDocument ( a , 1 , false ) . then ( e => {
211- e . edit ( edit => {
212- edit . insert ( new vscode . Position ( 0 , 0 ) , scriptText ) ;
213- } ) ;
214- } ) ;
215- } , ( error : any ) => {
216- console . error ( error ) ;
217- } ) ;
218- } ) ;
136+ await placescript ( fileName , scriptText ) ;
137+ } ;
138+ var disposable_runspblitzfirst = vscode . commands . registerCommand ( 'extension.run_sp_blitzfirst' , runspblitzfirst ) ;
219139 context . subscriptions . push ( disposable_runspblitzfirst ) ;
220140
221141
142+
143+ // places scriptText into fileName editor with current connection
144+ async function placescript ( fileName , scriptText ) {
145+ var setting : vscode . Uri = vscode . Uri . parse ( "untitled:" + fileName ) ;
146+ try {
147+ let connection = await sqlops . connection . getCurrentConnection ( ) ;
148+ let doc = await vscode . workspace . openTextDocument ( setting ) ;
149+ let editor = await vscode . window . showTextDocument ( doc , 1 , false ) ;
150+ editor . edit ( edit => {
151+ edit . insert ( new vscode . Position ( 0 , 0 ) , scriptText ) ;
152+ } ) ;
153+ if ( connection ) {
154+ await sqlops . queryeditor . connect ( doc . uri . toString ( ) , connection . connectionId ) ;
155+ }
156+ } catch ( err ) {
157+ vscode . window . showErrorMessage ( err ) ;
158+ }
159+ }
160+
222161}
223162
224163// this method is called when your extension is deactivated
0 commit comments