/// <summary> /// Handles a request to cancel this query if it is in progress /// </summary> internal async Task HandleCancelRequest(QueryCancelParams cancelParams, RequestContext <QueryCancelResult> requestContext) { try { // Attempt to find the query for the owner uri Query result; if (!ActiveQueries.TryGetValue(cancelParams.OwnerUri, out result)) { await requestContext.SendResult(new QueryCancelResult { Messages = SR.QueryServiceRequestsNoQuery }); return; } // Cancel the query and send a success message result.Cancel(); await requestContext.SendResult(new QueryCancelResult()); } catch (InvalidOperationException e) { // If this exception occurred, we most likely were trying to cancel a completed query await requestContext.SendResult(new QueryCancelResult { Messages = e.Message }); } catch (Exception e) { await requestContext.SendError(e.Message); } }
private async Task SaveResultsHelper(SaveResultsRequestParams saveParams, RequestContext <SaveResultRequestResult> requestContext, IFileStreamFactory fileFactory) { // retrieve query for OwnerUri Query query; if (!ActiveQueries.TryGetValue(saveParams.OwnerUri, out query)) { await requestContext.SendError(SR.QueryServiceQueryInvalidOwnerUri); return; } //Setup the callback for completion of the save task ResultSet.SaveAsAsyncEventHandler successHandler = async parameters => { await requestContext.SendResult(new SaveResultRequestResult()); }; ResultSet.SaveAsFailureAsyncEventHandler errorHandler = async(parameters, reason) => { string message = SR.QueryServiceSaveAsFail(Path.GetFileName(parameters.FilePath), reason); await requestContext.SendError(message); }; try { // Launch the task query.SaveAs(saveParams, fileFactory, successHandler, errorHandler); } catch (Exception e) { await errorHandler(saveParams, e.Message); } }
/// <summary> /// Handles a request to get an execution plan /// </summary> internal async Task HandleExecutionPlanRequest(QueryExecutionPlanParams planParams, RequestContext <QueryExecutionPlanResult> requestContext) { try { // Attempt to load the query Query query; if (!ActiveQueries.TryGetValue(planParams.OwnerUri, out query)) { await requestContext.SendError(SR.QueryServiceRequestsNoQuery); return; } // Retrieve the requested execution plan and return it var result = new QueryExecutionPlanResult { ExecutionPlan = await query.GetExecutionPlan(planParams.BatchIndex, planParams.ResultSetIndex) }; await requestContext.SendResult(result); } catch (Exception e) { // This was unexpected, so send back as error await requestContext.SendError(e.Message); } }
private async Task <Query> CreateAndActivateNewQuery(ExecuteRequestParamsBase executeParams, Func <Task> successAction, Func <string, Task> failureAction) { try { // Attempt to get the connection for the editor ConnectionInfo connectionInfo; if (!ConnectionService.TryFindConnection(executeParams.OwnerUri, out connectionInfo)) { await failureAction(SR.QueryServiceQueryInvalidOwnerUri); return(null); } // Attempt to clean out any old query on the owner URI Query oldQuery; if (ActiveQueries.TryGetValue(executeParams.OwnerUri, out oldQuery) && oldQuery.HasExecuted) { oldQuery.Dispose(); ActiveQueries.TryRemove(executeParams.OwnerUri, out oldQuery); } // Retrieve the current settings for executing the query with QueryExecutionSettings querySettings = Settings.QueryExecutionSettings; // Apply execution parameter settings querySettings.ExecutionPlanOptions = executeParams.ExecutionPlanOptions; // If we can't add the query now, it's assumed the query is in progress Query newQuery = new Query(GetSqlText(executeParams), connectionInfo, querySettings, BufferFileFactory); if (!ActiveQueries.TryAdd(executeParams.OwnerUri, newQuery)) { await failureAction(SR.QueryServiceQueryInProgress); newQuery.Dispose(); return(null); } // Successfully created query await successAction(); return(newQuery); } catch (Exception e) { await failureAction(e.Message); return(null); } }
public async Task HandleResultSubsetRequest(QueryExecuteSubsetParams subsetParams, RequestContext <QueryExecuteSubsetResult> requestContext) { try { // Attempt to load the query Query query; if (!ActiveQueries.TryGetValue(subsetParams.OwnerUri, out query)) { await requestContext.SendResult(new QueryExecuteSubsetResult { Message = SR.QueryServiceRequestsNoQuery }); return; } // Retrieve the requested subset and return it var result = new QueryExecuteSubsetResult { Message = null, ResultSubset = await query.GetSubset(subsetParams.BatchIndex, subsetParams.ResultSetIndex, subsetParams.RowsStartIndex, subsetParams.RowsCount) }; await requestContext.SendResult(result); } catch (InvalidOperationException ioe) { // Return the error as a result await requestContext.SendResult(new QueryExecuteSubsetResult { Message = ioe.Message }); } catch (ArgumentOutOfRangeException aoore) { // Return the error as a result await requestContext.SendResult(new QueryExecuteSubsetResult { Message = aoore.Message }); } catch (Exception e) { // This was unexpected, so send back as error await requestContext.SendError(e.Message); } }
/// <summary> /// Retrieves the requested subset of rows from the requested result set. Intended to be /// called by another service. /// </summary> /// <param name="subsetParams">Parameters for the subset to retrieve</param> /// <returns>The requested subset</returns> /// <exception cref="ArgumentOutOfRangeException">The requested query does not exist</exception> public async Task <ResultSetSubset> InterServiceResultSubset(SubsetParams subsetParams) { Validate.IsNotNullOrEmptyString(nameof(subsetParams.OwnerUri), subsetParams.OwnerUri); // Attempt to load the query Query query; if (!ActiveQueries.TryGetValue(subsetParams.OwnerUri, out query)) { throw new ArgumentOutOfRangeException(SR.QueryServiceRequestsNoQuery); } // Retrieve the requested subset and return it return(await query.GetSubset(subsetParams.BatchIndex, subsetParams.ResultSetIndex, subsetParams.RowsStartIndex, subsetParams.RowsCount)); }
private Query CreateQuery(ExecuteRequestParamsBase executeParams, ConnectionInfo connInfo) { // Attempt to get the connection for the editor ConnectionInfo connectionInfo; if (connInfo != null) { connectionInfo = connInfo; } else if (!ConnectionService.TryFindConnection(executeParams.OwnerUri, out connectionInfo)) { throw new ArgumentOutOfRangeException(nameof(executeParams.OwnerUri), SR.QueryServiceQueryInvalidOwnerUri); } // Attempt to clean out any old query on the owner URI Query oldQuery; // DevNote: // if any oldQuery exists on the executeParams.OwnerUri but it has not yet executed, // then shouldn't we cancel and clean out that query since we are about to create a new query object on the current OwnerUri. // if (ActiveQueries.TryGetValue(executeParams.OwnerUri, out oldQuery) && (oldQuery.HasExecuted || oldQuery.HasCancelled)) { oldQuery.Dispose(); ActiveQueries.TryRemove(executeParams.OwnerUri, out oldQuery); } // Retrieve the current settings for executing the query with QueryExecutionSettings settings = Settings.QueryExecutionSettings; // Apply execution parameter settings settings.ExecutionPlanOptions = executeParams.ExecutionPlanOptions; // If we can't add the query now, it's assumed the query is in progress Query newQuery = new Query(GetSqlText(executeParams), connectionInfo, settings, BufferFileFactory, executeParams.GetFullColumnSchema); if (!ActiveQueries.TryAdd(executeParams.OwnerUri, newQuery)) { newQuery.Dispose(); throw new InvalidOperationException(SR.QueryServiceQueryInProgress); } Logger.Write(TraceEventType.Information, $"Query object for URI:'{executeParams.OwnerUri}' created"); return(newQuery); }
/// <summary> /// Process request to save a resultSet to a file in CSV format /// </summary> internal async Task HandleSaveResultsAsCsvRequest(SaveResultsAsCsvRequestParams saveParams, RequestContext <SaveResultRequestResult> requestContext) { // retrieve query for OwnerUri Query result; if (!ActiveQueries.TryGetValue(saveParams.OwnerUri, out result)) { await requestContext.SendResult(new SaveResultRequestResult { Messages = SR.QueryServiceRequestsNoQuery }); return; } ResultSet selectedResultSet = result.Batches[saveParams.BatchIndex].ResultSets[saveParams.ResultSetIndex]; if (!selectedResultSet.IsBeingDisposed) { // Create SaveResults object and add success and error handlers to respective events SaveResults saveAsCsv = new SaveResults(); SaveResults.AsyncSaveEventHandler successHandler = async message => { selectedResultSet.RemoveSaveTask(saveParams.FilePath); await requestContext.SendResult(new SaveResultRequestResult { Messages = message }); }; saveAsCsv.SaveCompleted += successHandler; SaveResults.AsyncSaveEventHandler errorHandler = async message => { selectedResultSet.RemoveSaveTask(saveParams.FilePath); await requestContext.SendError(new SaveResultRequestError { message = message }); }; saveAsCsv.SaveFailed += errorHandler; saveAsCsv.SaveResultSetAsCsv(saveParams, requestContext, result); // Associate the ResultSet with the save task selectedResultSet.AddSaveTask(saveParams.FilePath, saveAsCsv.SaveTask); } }
private Query CreateQuery(ExecuteRequestParamsBase executeParams, ConnectionInfo connInfo) { // Attempt to get the connection for the editor ConnectionInfo connectionInfo; if (connInfo != null) { connectionInfo = connInfo; } else if (!ConnectionService.TryFindConnection(executeParams.OwnerUri, out connectionInfo)) { throw new ArgumentOutOfRangeException(nameof(executeParams.OwnerUri), SR.QueryServiceQueryInvalidOwnerUri); } // Attempt to clean out any old query on the owner URI Query oldQuery; if (ActiveQueries.TryGetValue(executeParams.OwnerUri, out oldQuery) && oldQuery.HasExecuted) { oldQuery.Dispose(); ActiveQueries.TryRemove(executeParams.OwnerUri, out oldQuery); } // Retrieve the current settings for executing the query with QueryExecutionSettings settings = Settings.QueryExecutionSettings; // Apply execution parameter settings settings.ExecutionPlanOptions = executeParams.ExecutionPlanOptions; // If we can't add the query now, it's assumed the query is in progress Query newQuery = new Query(GetSqlText(executeParams), connectionInfo, settings, BufferFileFactory, executeParams.GetFullColumnSchema); if (!ActiveQueries.TryAdd(executeParams.OwnerUri, newQuery)) { newQuery.Dispose(); throw new InvalidOperationException(SR.QueryServiceQueryInProgress); } return(newQuery); }
private Query CreateQuery( ExecuteRequestParamsBase executeParams, ConnectionInfo connInfo, bool applyExecutionSettings) { // Attempt to get the connection for the editor ConnectionInfo connectionInfo; if (connInfo != null) { connectionInfo = connInfo; } else if (!ConnectionService.TryFindConnection(executeParams.OwnerUri, out connectionInfo)) { throw new ArgumentOutOfRangeException(nameof(executeParams.OwnerUri), SR.QueryServiceQueryInvalidOwnerUri); } // Attempt to clean out any old query on the owner URI Query oldQuery; // DevNote: // if any oldQuery exists on the executeParams.OwnerUri but it has not yet executed, // then shouldn't we cancel and clean out that query since we are about to create a new query object on the current OwnerUri. // if (ActiveQueries.TryGetValue(executeParams.OwnerUri, out oldQuery) && (oldQuery.HasExecuted || oldQuery.HasCancelled || oldQuery.HasErrored)) { oldQuery.Dispose(); ActiveQueries.TryRemove(executeParams.OwnerUri, out oldQuery); } // check if there are active query execution settings for the editor, otherwise, use the global settings QueryExecutionSettings settings; if (this.ActiveQueryExecutionSettings.TryGetValue(executeParams.OwnerUri, out settings)) { // special-case handling for query plan options to maintain compat with query execution API parameters // the logic is that if either the query execute API parameters or the active query setttings // request a plan then enable the query option ExecutionPlanOptions executionPlanOptions = executeParams.ExecutionPlanOptions; if (settings.IncludeActualExecutionPlanXml) { executionPlanOptions.IncludeActualExecutionPlanXml = settings.IncludeActualExecutionPlanXml; } if (settings.IncludeEstimatedExecutionPlanXml) { executionPlanOptions.IncludeEstimatedExecutionPlanXml = settings.IncludeEstimatedExecutionPlanXml; } settings.ExecutionPlanOptions = executionPlanOptions; } else { settings = Settings.QueryExecutionSettings; settings.ExecutionPlanOptions = executeParams.ExecutionPlanOptions; } // If we can't add the query now, it's assumed the query is in progress Query newQuery = new Query( GetSqlText(executeParams), connectionInfo, settings, BufferFileFactory, executeParams.GetFullColumnSchema, applyExecutionSettings); if (!ActiveQueries.TryAdd(executeParams.OwnerUri, newQuery)) { newQuery.Dispose(); throw new InvalidOperationException(SR.QueryServiceQueryInProgress); } Logger.Write(TraceEventType.Information, $"Query object for URI:'{executeParams.OwnerUri}' created"); return(newQuery); }
private async Task <Query> CreateAndActivateNewQuery(QueryExecuteParams executeParams, RequestContext <QueryExecuteResult> requestContext) { try { // Attempt to get the connection for the editor ConnectionInfo connectionInfo; if (!ConnectionService.TryFindConnection(executeParams.OwnerUri, out connectionInfo)) { await requestContext.SendError(SR.QueryServiceQueryInvalidOwnerUri); return(null); } // Attempt to clean out any old query on the owner URI Query oldQuery; if (ActiveQueries.TryGetValue(executeParams.OwnerUri, out oldQuery) && oldQuery.HasExecuted) { oldQuery.Dispose(); ActiveQueries.TryRemove(executeParams.OwnerUri, out oldQuery); } // Retrieve the current settings for executing the query with QueryExecutionSettings settings = WorkspaceService.CurrentSettings.QueryExecutionSettings; // Get query text from the workspace. ScriptFile queryFile = WorkspaceService.Workspace.GetFile(executeParams.OwnerUri); string queryText; if (executeParams.QuerySelection != null) { string[] queryTextArray = queryFile.GetLinesInRange( new BufferRange( new BufferPosition( executeParams.QuerySelection.StartLine + 1, executeParams.QuerySelection.StartColumn + 1 ), new BufferPosition( executeParams.QuerySelection.EndLine + 1, executeParams.QuerySelection.EndColumn + 1 ) ) ); queryText = queryTextArray.Aggregate((a, b) => a + '\r' + '\n' + b); } else { queryText = queryFile.Contents; } // If we can't add the query now, it's assumed the query is in progress Query newQuery = new Query(queryText, connectionInfo, settings, BufferFileFactory); if (!ActiveQueries.TryAdd(executeParams.OwnerUri, newQuery)) { await requestContext.SendError(SR.QueryServiceQueryInProgress); newQuery.Dispose(); return(null); } return(newQuery); } catch (Exception e) { await requestContext.SendError(e.Message); return(null); } // Any other exceptions will fall through here and be collected at the end }