mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-17 09:35:37 -05:00
SqlCmd Connect/On Error/Include commands support (#898)
* Initial Investigation * Working code with include, connect, on error and tests * Adding some loc strings * Some cleanup and more tests * Some dummy change to trigger build * Adding PR comments * Addressing PR comments
This commit is contained in:
@@ -30,12 +30,12 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
public class Query : IDisposable
|
||||
{
|
||||
#region Constants
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// "Error" code produced by SQL Server when the database context (name) for a connection changes.
|
||||
/// </summary>
|
||||
private const int DatabaseContextChangeErrorNumber = 5701;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// ON keyword
|
||||
/// </summary>
|
||||
@@ -45,7 +45,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
/// OFF keyword
|
||||
/// </summary>
|
||||
private const string Off = "OFF";
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// showplan_xml statement
|
||||
/// </summary>
|
||||
@@ -55,7 +55,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
/// statistics xml statement
|
||||
/// </summary>
|
||||
private const string SetStatisticsXml = "SET STATISTICS XML {0}";
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Member Variables
|
||||
@@ -84,7 +84,23 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
/// <summary>
|
||||
/// Name of the new database if the database name was changed in the query
|
||||
/// </summary>
|
||||
private string newDatabaseName;
|
||||
private string newDatabaseName;
|
||||
|
||||
/// <summary>
|
||||
/// On Error Action for the query in SQLCMD Mode- Ignore or Exit
|
||||
/// </summary>
|
||||
private OnErrorAction onErrorAction;
|
||||
|
||||
/// <summary>
|
||||
/// Connection that is used for query to run
|
||||
/// This is always initialized from editor connection but might be different in case of SQLCMD mode
|
||||
/// </summary>
|
||||
private DbConnection queryConnection;
|
||||
|
||||
/// <summary>
|
||||
/// Cancelled but not user but by SQLCMD settings
|
||||
/// </summary>
|
||||
private bool CancelledBySqlCmd;
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -96,9 +112,9 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
/// <param name="settings">Settings for how to execute the query, from the user</param>
|
||||
/// <param name="outputFactory">Factory for creating output files</param>
|
||||
public Query(
|
||||
string queryText,
|
||||
ConnectionInfo connection,
|
||||
QueryExecutionSettings settings,
|
||||
string queryText,
|
||||
ConnectionInfo connection,
|
||||
QueryExecutionSettings settings,
|
||||
IFileStreamFactory outputFactory,
|
||||
bool getFullColumnSchema = false,
|
||||
bool applyExecutionSettings = false)
|
||||
@@ -125,13 +141,14 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
|
||||
var batchSelection = parserResult
|
||||
.Select((batchDefinition, index) =>
|
||||
new Batch(batchDefinition.BatchText,
|
||||
new Batch(batchDefinition.BatchText,
|
||||
new SelectionData(
|
||||
batchDefinition.StartLine-1,
|
||||
batchDefinition.StartColumn-1,
|
||||
batchDefinition.EndLine-1,
|
||||
batchDefinition.EndColumn-1),
|
||||
batchDefinition.EndColumn-1),
|
||||
index, outputFactory,
|
||||
batchDefinition.SqlCmdCommand,
|
||||
batchDefinition.BatchExecutionCount,
|
||||
getFullColumnSchema));
|
||||
|
||||
@@ -154,14 +171,14 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
/// </summary>
|
||||
/// <param name="query">The query that completed</param>
|
||||
public delegate Task QueryAsyncEventHandler(Query query);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Delegate type for callback when a query fails
|
||||
/// </summary>
|
||||
/// <param name="query">Query that raised the event</param>
|
||||
/// <param name="exception">Exception that caused the query to fail</param>
|
||||
public delegate Task QueryAsyncErrorEventHandler(Query query, Exception exception);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Event to be called when a batch is completed.
|
||||
/// </summary>
|
||||
@@ -356,7 +373,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
/// </param>
|
||||
/// <param name="successHandler">Delegate to call when the request completes successfully</param>
|
||||
/// <param name="failureHandler">Delegate to call if the request fails</param>
|
||||
public void SaveAs(SaveResultsRequestParams saveParams, IFileStreamFactory fileFactory,
|
||||
public void SaveAs(SaveResultsRequestParams saveParams, IFileStreamFactory fileFactory,
|
||||
ResultSet.SaveAsAsyncEventHandler successHandler, ResultSet.SaveAsFailureAsyncEventHandler failureHandler)
|
||||
{
|
||||
// Sanity check to make sure that the batch is within bounds
|
||||
@@ -385,7 +402,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
|
||||
// Mark that we've internally executed
|
||||
hasExecuteBeenCalled = true;
|
||||
|
||||
|
||||
// Don't actually execute if there aren't any batches to execute
|
||||
if (Batches.Length == 0)
|
||||
{
|
||||
@@ -399,9 +416,10 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Locate and setup the connection
|
||||
DbConnection queryConnection = await ConnectionService.Instance.GetOrOpenConnection(editorConnection.OwnerUri, ConnectionType.Query);
|
||||
queryConnection = await ConnectionService.Instance.GetOrOpenConnection(editorConnection.OwnerUri, ConnectionType.Query);
|
||||
onErrorAction = OnErrorAction.Ignore;
|
||||
sqlConn = queryConnection as ReliableSqlConnection;
|
||||
if (sqlConn != null)
|
||||
{
|
||||
@@ -410,7 +428,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
sqlConn.GetUnderlyingConnection().InfoMessage += OnInfoMessage;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Execute beforeBatches synchronously, before the user defined batches
|
||||
foreach (Batch b in BeforeBatches)
|
||||
{
|
||||
@@ -427,7 +445,8 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
b.ResultSetCompletion += ResultSetCompleted;
|
||||
b.ResultSetAvailable += ResultSetAvailable;
|
||||
b.ResultSetUpdated += ResultSetUpdated;
|
||||
await b.Execute(queryConnection, cancellationSource.Token);
|
||||
|
||||
await ExecuteBatch(b);
|
||||
}
|
||||
|
||||
// Execute afterBatches synchronously, after the user defined batches
|
||||
@@ -445,7 +464,11 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
catch (Exception e)
|
||||
{
|
||||
HasErrored = true;
|
||||
if (e is OperationCanceledException)
|
||||
if (e is SqlCmdException || CancelledBySqlCmd)
|
||||
{
|
||||
await BatchMessageSent(new ResultMessage(SR.SqlCmdExitOnError, false, null));
|
||||
}
|
||||
else if (e is OperationCanceledException)
|
||||
{
|
||||
await BatchMessageSent(new ResultMessage(SR.QueryServiceQueryCancelled, false, null));
|
||||
}
|
||||
@@ -463,7 +486,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
// Subscribe to database informational messages
|
||||
sqlConn.GetUnderlyingConnection().InfoMessage -= OnInfoMessage;
|
||||
}
|
||||
|
||||
|
||||
// If any message notified us we had changed databases, then we must let the connection service know
|
||||
if (newDatabaseName != null)
|
||||
{
|
||||
@@ -480,6 +503,63 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
}
|
||||
}
|
||||
}
|
||||
private async Task ExecuteBatch(Batch b)
|
||||
{
|
||||
if (b.SqlCmdCommand != null)
|
||||
{
|
||||
b.HandleOnErrorAction += HandleOnErrorAction;
|
||||
await PerformInplaceSqlCmdAction(b);
|
||||
}
|
||||
|
||||
await b.Execute(queryConnection, cancellationSource.Token, this.onErrorAction);
|
||||
|
||||
if (CancelledBySqlCmd)
|
||||
{
|
||||
throw new SqlCmdException(SR.SqlCmdExitOnError);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task PerformInplaceSqlCmdAction(Batch b)
|
||||
{
|
||||
try
|
||||
{
|
||||
switch (b.SqlCmdCommand.LexerTokenType)
|
||||
{
|
||||
case LexerTokenType.Connect:
|
||||
var qc = (b.SqlCmdCommand as ConnectSqlCmdCommand)?.Connect();
|
||||
queryConnection = qc ?? queryConnection;
|
||||
break;
|
||||
case LexerTokenType.OnError:
|
||||
onErrorAction = (b.SqlCmdCommand as OnErrorSqlCmdCommand).Action;
|
||||
break;
|
||||
default:
|
||||
throw new SqlCmdException(string.Format(SR.SqlCmdUnsupportedToken, b.SqlCmdCommand.LexerTokenType));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
b.HasError = true;
|
||||
await BatchMessageSent(new ResultMessage(ex.Message, true, null));
|
||||
if (this.onErrorAction == OnErrorAction.Exit)
|
||||
{
|
||||
HasCancelled = true;
|
||||
CancelledBySqlCmd = true;
|
||||
cancellationSource.Cancel();
|
||||
throw new SqlCmdException(SR.SqlCmdExitOnError);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleOnErrorAction(object sender, bool iserror)
|
||||
{
|
||||
if (iserror && this.onErrorAction == OnErrorAction.Exit)
|
||||
{
|
||||
HasCancelled = true;
|
||||
CancelledBySqlCmd = true;
|
||||
cancellationSource.Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handler for database messages during query execution
|
||||
@@ -511,8 +591,8 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
}
|
||||
|
||||
private void ApplyExecutionSettings(
|
||||
ConnectionInfo connection,
|
||||
QueryExecutionSettings settings,
|
||||
ConnectionInfo connection,
|
||||
QueryExecutionSettings settings,
|
||||
IFileStreamFactory outputFactory)
|
||||
{
|
||||
outputFactory.QueryExecutionSettings = settings;
|
||||
@@ -549,7 +629,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
}
|
||||
|
||||
if (settings.StatisticsIO)
|
||||
{
|
||||
{
|
||||
builderBefore.AppendFormat("{0} ", helper.GetSetStatisticsIOString(true));
|
||||
builderAfter.AppendFormat("{0} ", helper.GetSetStatisticsIOString (false));
|
||||
}
|
||||
@@ -562,35 +642,35 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
}
|
||||
|
||||
if (settings.ParseOnly)
|
||||
{
|
||||
{
|
||||
builderBefore.AppendFormat("{0} ", helper.GetSetParseOnlyString(true));
|
||||
builderAfter.AppendFormat("{0} ", helper.GetSetParseOnlyString(false));
|
||||
}
|
||||
|
||||
// append first part of exec options
|
||||
builderBefore.AppendFormat("{0} {1} {2}",
|
||||
builderBefore.AppendFormat("{0} {1} {2}",
|
||||
helper.SetRowCountString, helper.SetTextSizeString, helper.SetNoCountString);
|
||||
|
||||
if (!connection.IsSqlDW)
|
||||
{
|
||||
// append second part of exec options
|
||||
builderBefore.AppendFormat(" {0} {1} {2} {3} {4} {5} {6}",
|
||||
helper.SetConcatenationNullString,
|
||||
helper.SetArithAbortString,
|
||||
helper.SetLockTimeoutString,
|
||||
helper.SetQueryGovernorCostString,
|
||||
helper.SetDeadlockPriorityString,
|
||||
helper.SetConcatenationNullString,
|
||||
helper.SetArithAbortString,
|
||||
helper.SetLockTimeoutString,
|
||||
helper.SetQueryGovernorCostString,
|
||||
helper.SetDeadlockPriorityString,
|
||||
helper.SetTransactionIsolationLevelString,
|
||||
// We treat XACT_ABORT special in that we don't add anything if the option
|
||||
// isn't checked. This is because we don't want to be overwriting the server
|
||||
// if it has a default of ON since that's something people would specifically
|
||||
// set and having a client change it could be dangerous (the reverse is much
|
||||
// less risky)
|
||||
|
||||
|
||||
// The full fix would probably be to make the options tri-state instead of
|
||||
// just on/off, where the default is to use the servers default. Until that
|
||||
// happens though this is the best solution we came up with. See TFS#7937925
|
||||
|
||||
|
||||
// Note that users can always specifically add SET XACT_ABORT OFF to their
|
||||
// queries if they do truly want to set it off. We just don't want to
|
||||
// do it silently (since the default is going to be off)
|
||||
@@ -655,7 +735,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
||||
/// <summary>
|
||||
/// Does this connection support XML Execution plans
|
||||
/// </summary>
|
||||
private bool DoesSupportExecutionPlan(ConnectionInfo connectionInfo)
|
||||
private bool DoesSupportExecutionPlan(ConnectionInfo connectionInfo)
|
||||
{
|
||||
// Determining which execution plan options may be applied (may be added to for pre-yukon support)
|
||||
return (!connectionInfo.IsSqlDW && connectionInfo.MajorVersion >= 9);
|
||||
|
||||
Reference in New Issue
Block a user