'use strict' ; import * as azdata from 'azdata' ; import * as vscode from 'vscode' ; import { DataScripter } from './DataScripter' ; export function activate ( context ) { context . subscriptions . push ( vscode . commands . registerCommand ( 'extension.scriptTableData' , async ( oeContext ) => { if ( ! oeContext ) { vscode . window . showErrorMessage ( "This extension cannot be run from the command menu." ) ; return ; } let tableName = `[${ oeContext . connectionProfile . databaseName }].[${ oeContext . nodeInfo . metadata . schema }].[${ oeContext . nodeInfo . metadata . name }]` ; let options = { prompt : `Press [Enter] to accept the default of all data or edit the SQL to select subsets of data. You can use any valid sql syntax. Note that scripting all data in the table can have serious performance issues for extremely large tables. ` , value : `select * from ${ tableName };` } ; let sql = await vscode . window . showInputBox ( options ) ; if ( ! sql || sql . trim ( ) === "" ) { vscode . window . showInformationMessage ( "Query was cancelled" ) ; return ; } let args = { context : oeContext , tableName : tableName , sqlString : sql } ; let backgroundOperationInfo = { connection : undefined , displayName : `Scripting Data for : ${ tableName } ` , description : "A data scripting operation" , isCancelable : true , operation : ( operation ) => { return scriptData ( operation , args ) ; } } ; azdata . tasks . startBackgroundOperation ( backgroundOperationInfo ) ; } ) ) ; } async function scriptData ( backgroundOperation , args ) { let connectionResult = await azdata . connection . connect ( args . context . connectionProfile , false , false ) ; if ( ! connectionResult . connected ) { backgroundOperation . updateStatus ( azdata . TaskStatus . Failed , "Could not connect to database server" ) ; vscode . window . showErrorMessage ( connectionResult . errorMessage ) ; return ; } let connectionUri = await azdata . connection . getUriForConnection ( connectionResult . connectionId ) ; let providerId = args . context . connectionProfile . providerName ; let databaseName = args . context . connectionProfile . databaseName ; let connectionProvider = azdata . dataprotocol . getProvider < azdata . ConnectionProvider > ( providerId , azdata . DataProviderType . ConnectionProvider ) ; let queryProvider = azdata . dataprotocol . getProvider < azdata . QueryProvider > ( providerId , azdata . DataProviderType . QueryProvider ) ; backgroundOperation . updateStatus ( azdata . TaskStatus . InProgress , "Getting records..." ) ; var changeDatabaseResults = await connectionProvider . changeDatabase ( connectionUri , databaseName ) ; if ( ! changeDatabaseResults ) { backgroundOperation . updateStatus ( azdata . TaskStatus . Failed , `Could not switch to [${ databaseName }] database` ) ; vscode . window . showErrorMessage ( connectionResult . errorMessage ) ; return ; } queryProvider . runQueryAndReturn ( connectionUri , args . sqlString ) . then ( function ( results ) { if ( ! results || results . rowCount === 0 ) { backgroundOperation . updateStatus ( azdata . TaskStatus . Succeeded , "No data retrieved" ) ; vscode . window . showErrorMessage ( "Nothing to script! The query produced no results!" ) ; return ; } let dataScripter = new DataScripter ( results , args . context . nodeInfo . metadata . name ) ; backgroundOperation . updateStatus ( azdata . TaskStatus . InProgress , "Parsing records..." ) ; vscode . workspace . openTextDocument ( { language : 'sql' } ) . then ( textDocument => { vscode . window . showTextDocument ( textDocument , 1 , false ) . then ( textEditor => { textEditor . edit ( editBuilder => { editBuilder . insert ( new vscode . Position ( 0 , 0 ) , dataScripter . Script ( ) ) ; backgroundOperation . updateStatus ( azdata . TaskStatus . Succeeded ) ; } ) ; } ) ; } ) ; } , function ( error ) { let message = ( error instanceof Error ) ? error . message : "There was an unknown error retrieving data" ; backgroundOperation . updateStatus ( azdata . TaskStatus . Failed , message ) ; vscode . window . showErrorMessage ( message ) ; } ) ; } export function deactivate ( ) { } interface ScriptingArgs { context : azdata . ObjectExplorerContext ; tableName : string ; sqlString : string ; }