Skip to content

Commit

Permalink
Fixed double replacement tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
erikbra committed Jun 3, 2023
1 parent 9b4f5b1 commit 7b6f8a0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,11 @@ public void slash_with_comment_after()
}

[Test]
public void slash_with_semicolon_directly_after()
public void slash_with_semicolon_directly_after_yields_just_one_splitter()
{
string sql_to_match = @"jalla;
/";
string expected_scrubbed = "jalla" + Batch_terminator_replacement_string + @"
string expected_scrubbed = "jalla" + @"
" + Batch_terminator_replacement_string;
TestContext.WriteLine(sql_to_match);
string sql_statement_scrubbed = Replacer.Replace(sql_to_match);
Expand Down
15 changes: 13 additions & 2 deletions grate/Infrastructure/BatchSplitterReplacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,23 @@ public BatchSplitterReplacer(string pattern, string replacement)
_regex = new Regex(pattern, IgnoreCase | Multiline);
}

public string Replace(string text) => _regex.Replace(text, ReplaceBatchSeparator);
public string Replace(string text)
{
var replace = _regex.Replace(text, ReplaceBatchSeparator);

// Combine multiple consecutive replacement tokens with one (needed for Oracle, if ; and / are on separate lines)
replace = Regex.Replace(
replace,
Regex.Escape(Replacement) + "(\\s*)" + Regex.Escape(Replacement),
"$1" + Replacement);

return replace;
}

private string ReplaceBatchSeparator(Match match)
{
var groups = match.Groups;
var replacement = groups["BATCHSPLITTER"].Success ? Replacement : string.Empty;
return groups["KEEP1"].Value + replacement + groups["KEEP2"].Value;
}
}
}
2 changes: 1 addition & 1 deletion grate/Infrastructure/OracleSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public string StatementSeparatorRegex
const string dashComments = @"(?<KEEP1>--.*$)";
const string starComments = @"(?<KEEP1>/\*[\S\s]*?\*/)";
const string batchSeparator = @"(?<KEEP1>.*)(?<BATCHSPLITTER>;)(?<KEEP2>\s|$)";
const string sqlPlusExecuteCommand = @"(?<KEEP1>^|\s)(?<BATCHSPLITTER>\/|;)(?<KEEP2>\s|$)";
const string sqlPlusExecuteCommand = @"(?<KEEP1>^|\s)(?<BATCHSPLITTER>\/|;)(?<KEEP2>\s*|$|\n)";
return strings + "|" + dashComments + "|" + starComments + "|" + batchSeparator + "|" + sqlPlusExecuteCommand;
}
}
Expand Down

0 comments on commit 7b6f8a0

Please sign in to comment.