Skip to content

Commit

Permalink
Remove regex from SqlServerMigrationsSqlGenerator (#30069)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajcvickers committed Jan 19, 2023
1 parent 8aa2c03 commit db094b7
Showing 1 changed file with 30 additions and 33 deletions.
63 changes: 30 additions & 33 deletions src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.EntityFrameworkCore.SqlServer.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Metadata.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Update.Internal;
Expand Down Expand Up @@ -1398,49 +1397,47 @@ protected override void Generate(
/// <param name="builder">The command builder to use to build the commands.</param>
protected override void Generate(SqlOperation operation, IModel? model, MigrationCommandListBuilder builder)
{
var batches = Regex.Split(
Regex.Replace(
operation.Sql,
@"\\\r?\n",
string.Empty,
default,
TimeSpan.FromMilliseconds(1000.0)),
@"^\s*(GO[ \t]+[0-9]+|GO)(?:\s+|$)",
RegexOptions.IgnoreCase | RegexOptions.Multiline,
TimeSpan.FromMilliseconds(1000.0));
for (var i = 0; i < batches.Length; i++)
{
if (batches[i].StartsWith("GO", StringComparison.OrdinalIgnoreCase)
|| string.IsNullOrWhiteSpace(batches[i]))
var preBatched = operation.Sql
.Replace("\\\n", "")
.Replace("\\\r\n", "")
.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);

var batchBuilder = new StringBuilder();
foreach (var line in preBatched)
{
if (string.IsNullOrWhiteSpace(line))
{
continue;
}

var count = 1;
if (i != batches.Length - 1
&& batches[i + 1].StartsWith("GO", StringComparison.OrdinalIgnoreCase))
var trimmed = line.TrimStart();
if (trimmed.StartsWith("GO", StringComparison.OrdinalIgnoreCase))
{
var match = Regex.Match(
batches[i + 1], "([0-9]+)",
default,
TimeSpan.FromMilliseconds(1000.0));
if (match.Success)
var batch = batchBuilder.ToString();
batchBuilder.Clear();

var count = trimmed.Length >= 4
&& int.TryParse(trimmed.Substring(3), out var specifiedCount)
? specifiedCount
: 1;

for (var j = 0; j < count; j++)
{
count = int.Parse(match.Value);
AppendBatch(batch);
}
}

for (var j = 0; j < count; j++)
else
{
builder.Append(batches[i]);
batchBuilder.AppendLine(line);
}
}

if (i == batches.Length - 1)
{
builder.AppendLine();
}
AppendBatch(batchBuilder.ToString());

EndStatement(builder, operation.SuppressTransaction);
}
void AppendBatch(string batch)
{
builder.Append(batch);
EndStatement(builder, operation.SuppressTransaction);
}
}

Expand Down

0 comments on commit db094b7

Please sign in to comment.