Skip to content

Commit

Permalink
fixed GO error continue
Browse files Browse the repository at this point in the history
  • Loading branch information
Steffen committed Nov 28, 2023
1 parent b27ba3d commit 2b7179d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Updated System.Management.Automation to 7.2.16 and PowerShell 7.2.16.
- Updated Microsoft.Data.SqlClient to 5.1.1.

### Fixed

- ErrorAction Continue did not executed batches after an SQL error.

## [1.2.1] - 2023-09-05

### Added
Expand Down
51 changes: 34 additions & 17 deletions src/PsSmo/InvokeCommandCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Management.Automation;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -45,24 +46,30 @@ protected override void ProcessRecord()
break;

case "File":
WriteVerbose($"Execute SQL script from file '{ InputFile.FullName }'.");
WriteVerbose($"Execute SQL script from file '{InputFile.FullName}'.");
Text = File.ReadAllText(InputFile.FullName);
break;

default:
throw new NotImplementedException($"ParameterSetName {ParameterSetName} is not implemented");
}
try
{
Instance.ConnectionContext.ExecuteNonQuery(sqlCommand: ProcessSqlCmdText(Text, ProcessVariables(Variables)));
}
catch (PipelineStoppedException)
{
throw;
}
catch (Exception ex)

foreach (var sqlCommand in ProcessSqlCmdText(Text, ProcessVariables(Variables)))
{
WriteError(new ErrorRecord(ex, ex.GetType().Name, ErrorCategory.NotSpecified, Text));
try
{
Instance.ConnectionContext.ExecuteNonQuery(
sqlCommand: sqlCommand
);
}
catch (PipelineStoppedException)
{
throw;
}
catch (Exception ex)
{
WriteError(new ErrorRecord(ex, ex.GetType().Name, ErrorCategory.NotSpecified, Text));
}
}
}

Expand All @@ -81,11 +88,12 @@ private static Dictionary<string, string> ProcessVariables(Hashtable variables)
return variableDictionary;
}

private string ProcessSqlCmdText(string text, Dictionary<string, string> variables)
private StringCollection ProcessSqlCmdText(string text, Dictionary<string, string> variables)
{
variables ??= new Dictionary<string, string>();

var result = new List<string>();
var resultCollection = new List<List<string>>() { result };
var variableRegex = new Regex(@"\$\((\w*)\)");
var setVarRegex = new Regex(@":setvar (\w+) ?""(.+)?""");
var blockCommentRegex = new Regex(@"/\*(.|\n)*?\*/");
Expand All @@ -99,14 +107,20 @@ private string ProcessSqlCmdText(string text, Dictionary<string, string> variabl

foreach (var line in processedText.Split(Environment.NewLine))
{
if (line.Trim().StartsWith(":on error", StringComparison.CurrentCultureIgnoreCase))
if (line.Trim().StartsWith("GO", StringComparison.CurrentCultureIgnoreCase))
{
result = new List<string>();
resultCollection.Add(result);
}
else if (line.Trim().StartsWith(":on error", StringComparison.CurrentCultureIgnoreCase))
{
WriteWarning(":on error is not implemented");
}
else if (line.Trim().StartsWith(":setvar", StringComparison.CurrentCultureIgnoreCase))
{
var match = setVarRegex.Match(line);
if (match.Success) {
if (match.Success)
{
var variable = match.Groups[1].Value;
var value = match.Groups[2].Value;
variables[variable] = value;
Expand All @@ -119,15 +133,15 @@ private string ProcessSqlCmdText(string text, Dictionary<string, string> variabl
else
{
var processedLine = line;
foreach(var variable in variables)
foreach (var variable in variables)
{
processedLine = processedLine.Replace($"$({variable.Key})", variable.Value);
}

var match = variableRegex.Match(input: processedLine);
if (match.Success)
{
foreach(var variable in variables)
foreach (var variable in variables)
{
WriteWarning($"$({variable.Key}) = '{variable.Value}'");
}
Expand All @@ -137,7 +151,10 @@ private string ProcessSqlCmdText(string text, Dictionary<string, string> variabl
result.Add(processedLine);
}
}
return string.Join(separator: Environment.NewLine, result);
var batchCollection = new StringCollection();
foreach (var batch in resultCollection)
batchCollection.Add(string.Join(separator: Environment.NewLine, batch));
return batchCollection;
}
}
}
4 changes: 2 additions & 2 deletions test/Invoke-Command.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ PRINT 'bar'
$InformationOutput[0].MessageData | Should -Be "SELECT 1/0`r`nPRINT 'foo'"
$ErrorOutput[0].Exception.Message | Should -Be 'An exception occurred while executing a Transact-SQL statement or batch.'
$ErrorOutput[0].Exception.InnerException.Errors[0].Message | Should -Be 'Divide by zero error encountered.'
# $InformationOutput[1].MessageData | Should -Be "PRINT 'bar'" # still a bug
# $VerboseOutput[1].Message | Should -Be 'bar'
$InformationOutput[1].MessageData | Should -Be "PRINT 'bar'"
$VerboseOutput[1].Message | Should -Be 'bar'
}
}

Expand Down

0 comments on commit 2b7179d

Please sign in to comment.