dzsquared / sqlops-firstresponderkit

Install and Run the First Responder Kit Scripts from Azure Data Studio
https://bit.ly/2lChw1Z
MIT License
73 stars 9 forks source link

Allow auto-connect to active connection as part of the commands #1

Closed kevcunnane closed 6 years ago

kevcunnane commented 6 years ago

It'd be great if you could auto-connect to the active connection when you open new query windows for the various sp_blitz import and run commands. It's possible to do this but the APIs are still in a proposed state right now. I've opened https://github.com/Microsoft/sqlopsstudio/issues/2076 to request this to change. Once that's ported in the next release, this'd make it much quicker to go from "I want to run this code" to "my code runs!". You could even use the queryeditor.runQuery command to programmatically run it after connect, in the case of the run sp_blitz command.

Basic process one this is done:

import * as sqlops from 'sqlops';

// sp_blitz example
var getblitz = async () => {
        let fileName = "sp_Blitz.sql";
        var options = {
            uri: baseUrl + fileName,
        };
        console.log('Bringing in the first responder kit from the mothership.');
        const scriptText = await request.get(options);
        var setting: vscode.Uri = vscode.Uri.parse("untitled:" + fileName);
        try {
            let doc = await vscode.workspace.openTextDocument(setting);
            let editor = await vscode.window.showTextDocument(doc, 1, false);
            editor.edit(edit => {
                edit.insert(new vscode.Position(0, 0), scriptText);
            });
            let connection = await sqlops.connection.getCurrentConnection();
            if (connection) {
                await sqlops.queryeditor.connect(doc.uri.toString(), connection.connectionId);
            }
        } catch (err) {
            vscode.window.showErrorMessage(err);
        }
    };
dzsquared commented 6 years ago

implemented with reusable function, releasing in 0.2.0

    async function placescript(fileName, scriptText) {
        var setting: vscode.Uri = vscode.Uri.parse("untitled:" + fileName);
        try {
            let connection = await sqlops.connection.getCurrentConnection();
            let doc = await vscode.workspace.openTextDocument(setting);
            let editor = await vscode.window.showTextDocument(doc, 1, false);
            editor.edit(edit => {
                edit.insert(new vscode.Position(0, 0), scriptText);
            });
            if (connection) {
                await sqlops.queryeditor.connect(doc.uri.toString(), connection.connectionId);
            }
        } catch (err) {
            vscode.window.showErrorMessage(err);
        }
    }