我有一个类用于通过批处理分隔符“拆分”一串SQL命令 - 例如“GO” - 进入依次运行的SQL命令列表等。
...
private static IEnumerable<string> SplitByBatchIndecator(string script, string batchIndicator)
{
string pattern = string.Concat("^\\s*", batchIndicator, "\\s*$");
RegexOptions options = RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline;
foreach (string batch in Regex.Split(script, pattern, options))
{
yield return batch.Trim();
}
}
我目前的实施使用Regex
和yield
,但我不确定它是否是“最佳”方式。
目前,以下SQL将错误地拆分:
var batch = QueryBatch.Parse(@"-- issue...
insert into table (name, desc)
values('foo', 'if the
go
is on a line by itself we have a problem...')");
Assert.That(batch.Queries.Count, Is.EqualTo(1), "This fails for now...");
我想过一个基于令牌的解析器,它跟踪打开的闭合引号的状态,但不确定Regex是否会这样做。
任何想法!?
答案 0 :(得分:3)
您可以使用Balancing Group Definition跟踪开盘价和收盘价。
此外,去年就splitting on whitespace as long as the whitespace wasn't contained in quotes提出了一个类似的问题。您可以调整这些答案以获得目的地。