Exemplo n.º 1
0
        /// <summary>
        /// Handles request to start the scripting operation
        /// </summary>
        public async Task HandleScriptExecuteRequest(ScriptingParams parameters, RequestContext <ScriptingResult> requestContext)
        {
            SmoScriptingOperation operation = null;

            try
            {
                // if a connection string wasn't provided as a parameter then
                // use the owner uri property to lookup its associated ConnectionInfo
                // and then build a connection string out of that
                ConnectionInfo connInfo    = null;
                string         accessToken = null;
                if (parameters.ConnectionString == null)
                {
                    ScriptingService.ConnectionServiceInstance.TryFindConnection(parameters.OwnerUri, out connInfo);
                    if (connInfo != null)
                    {
                        parameters.ConnectionString = ConnectionService.BuildConnectionString(connInfo.ConnectionDetails);
                        accessToken = connInfo.ConnectionDetails.AzureAccountToken;
                    }
                    else
                    {
                        throw new Exception("Could not find ConnectionInfo");
                    }
                }

                if (parameters.FilePath == null)
                {
                    // Create a temporary and random path to handle this operation
                    parameters.FilePath = Path.GetTempFileName();
                }

                if (!ShouldCreateScriptAsOperation(parameters))
                {
                    operation = new ScriptingScriptOperation(parameters, accessToken);
                }
                else
                {
                    operation = new ScriptAsScriptingOperation(parameters, accessToken);
                }

                operation.PlanNotification     += (sender, e) => requestContext.SendEvent(ScriptingPlanNotificationEvent.Type, e).Wait();
                operation.ProgressNotification += (sender, e) => requestContext.SendEvent(ScriptingProgressNotificationEvent.Type, e).Wait();
                operation.CompleteNotification += (sender, e) => this.SendScriptingCompleteEvent(requestContext, ScriptingCompleteEvent.Type, e, operation, parameters.ScriptDestination);

                RunTask(requestContext, operation);
            }
            catch (Exception e)
            {
                await requestContext.SendError(e);
            }
        }
        /// <summary>
        /// Handles request to start the scripting operation
        /// </summary>
        public async Task HandleScriptingScriptAsRequest(ScriptingParams parameters, RequestContext <ScriptingResult> requestContext)
        {
            try
            {
                // if a connection string wasn't provided as a parameter then
                // use the owner uri property to lookup its associated ConnectionInfo
                // and then build a connection string out of that
                ConnectionInfo connInfo = null;
                if (parameters.ConnectionString == null || parameters.ScriptOptions.ScriptCreateDrop == "ScriptSelect")
                {
                    ScriptingService.ConnectionServiceInstance.TryFindConnection(parameters.OwnerUri, out connInfo);
                    if (connInfo != null)
                    {
                        connInfo.ConnectionDetails.PersistSecurityInfo = true;
                        parameters.ConnectionString = ConnectionService.BuildConnectionString(connInfo.ConnectionDetails);
                    }
                    else
                    {
                        throw new Exception("Could not find ConnectionInfo");
                    }
                }

                // if the scripting operation is for SELECT then handle that message differently
                // for SELECT we'll build the SQL directly whereas other scripting operations depend on SMO
                if (parameters.ScriptOptions.ScriptCreateDrop == "ScriptSelect")
                {
                    RunSelectTask(connInfo, parameters, requestContext);
                }
                else
                {
                    ScriptAsScriptingOperation operation = new ScriptAsScriptingOperation(parameters);
                    operation.ProgressNotification += (sender, e) => requestContext.SendEvent(ScriptingProgressNotificationEvent.Type, e);
                    operation.CompleteNotification += (sender, e) => this.SendScriptingCompleteEvent(requestContext, ScriptingCompleteEvent.Type, e, operation, parameters.ScriptDestination);

                    RunTask(requestContext, operation);
                }
            }
            catch (Exception e)
            {
                await requestContext.SendError(e);
            }
        }