以免我遭受一阵乏味的震颤(大致介于震颤谵妄和腕管综合症之间),我需要找到一种方法来自动解析大型 sql 语句文件及其参数值。
我有一个包含大量 sql 语句的文件,格式如下:
select Animal#, RacketThreshold, PeakOil as Oil
from OilAnimalPlatypus2
where OilAnimalPlatypusID = :ID
and Animal# = :Animal
and TelecasterAccessType = 'D'
UNION
select Animal, RacketThreshold, PeakOil as Oil
from OilRequestPlatypus
where PlatypusID = :ID
and Animal = :Animal
order by RacketThreshold
-->ID(VARCHAR[0])=<NULL>
:Animal(INTEGER)=2
...即多行 sql 语句,后跟一个空行,后跟两个破折号和一个带有参数名称、数据类型和参数的箭头,后跟相同的东西,一遍又一遍地反复无止境地恶心(除了 sql 语句有无参数)。
我想从这个伟大的 gob 中为每个唯一查询创建一个单独的字符串(其中许多是相同的,尽管通常为参数分配了不同的参数值)。如果可能的话,我还想跟踪传递给特定查询的所有参数值(例如,如果第一次调用它并为特定参数传递“1”,下一次是“42”,下一次“3.14”等),我想要该 arg 名称的 1、42、3.14 的集合。
有超过 400 个查询,我讨厌“手动”完成这一切的想法——尤其是匹配查询的比较。
更新
好的,添加此代码以使用 Jon 的代码后:
private void buttonOpenAndParseSQLMonFile_Click(object sender, EventArgs e)
{
var queriesAndArgs = (Dictionary<string, List<string>>)ParseFile("SQLMonTraceLog.txt");
foreach(var pair in queriesAndArgs)
{
richTextBoxParsedResults.AppendText(pair.Key);
richTextBoxParsedResults.AppendText(Environment.NewLine);
foreach (String s in pair.Value)
{
richTextBoxParsedResults.AppendText(s);
richTextBoxParsedResults.AppendText(Environment.NewLine);
}
richTextBoxParsedResults.AppendText(Environment.NewLine);
}
}
...我在我的richTextBox 中得到这些类型的结果:
select ABCID from ABCWorker where lower(loginid) = lower(user)
select r.roleid from abcrole r, abcworker w where lower(w.loginid)=lower(user) and r.abcid=w.abcid and r.status='A'
select Tier#, BenGrimm, PeakRate as Ratefrom RageAnimalGreenBayPackers2 where RageAnimalGreenBayPackersID = :ID and Tier# =
:Tier and FlyingVAccessType = 'D' UNION select Tier, BenGrimm, PeakRate as Rate from CaliforniaCondorGreenBayPackers where
GreenBayPackersID = :ID and Tier = :Tier order by BenGrimm
--> :ID(VARCHAR[0])=<NULL> :Tier(INTEGER)=1
--> :ID(VARCHAR[0])=<NULL> :Tier(INTEGER)=1
--> :ID(VARCHAR[0])=<NULL> :Tier(INTEGER)=1
--> :ID(VARCHAR[0])=<NULL> :Tier(INTEGER)=4
select Tier#, BenGrimm, PeakRate as Rate from RageAnimalGreenBayPackers2 where RageAnimalGreenBayPackersID = :ID and Tier# =
:Tier and FlyingVAccessType = 'D' UNION select Tier, BenGrimm, PeakRate as Rate from CaliforniaCondorGreenBayPackers where
GreenBayPackersID = :ID and Tier = :Tier order by BenGrimm
--> :ID(VARCHAR[0])=<NULL> :Tier(INTEGER)=2
--> :ID(VARCHAR[0])=<NULL> :Tier(INTEGER)=5
--> :ID(VARCHAR[0])=<NULL> :Tier(INTEGER)=1
--> :ID(VARCHAR[0])=<NULL> :Tier(INTEGER)=2
--> :ID(VARCHAR[0])=<NULL> :Tier(INTEGER)=3
--> :ID(VARCHAR[0])=<NULL> :Tier(INTEGER)=4
--> :ID(VARCHAR[0])=<NULL> :Tier(INTEGER)=2
--> :ID(VARCHAR[0])=<NULL> :Tier(INTEGER)=3
--> :ID(VARCHAR[0])=<NULL> :Tier(INTEGER)=4
(etc.)
...所以,这很有启发性,但我发现这不是我所需要的,而且这取决于我对文件的 lamo 手动调整。所以,我想我需要退后一步,解析文件,因为它实际上是给我的,递增的数字分隔每个“有趣的”事件:
. . .
6 11:30:46 SQL Execute: select ABCID
from ABCWorker
where lower(loginid) = lower(user)
7 11:30:46 SQL Prepare: select r.roleid from abcrole r, abcworker w where lower(w.loginid)=lower(user) and
r.abcid=w.abcid and r.status='A'
8 11:30:46 SQL Execute: select r.roleid from abcrole r, abcworker w where lower(w.loginid)=lower(user) and
r.abcid=w.abcid and r.status='A'
9 11:30:46 SQL Execute: select Tier#, BenGrimm, PeakRate as Rate
from RageAnimalGreenBayPackers2
where RageAnimalGreenBayPackersID = :ID
and Tier# = :Tier
and FlyingVAccessType = 'D'
UNION
select Tier, BenGrimm, PeakRate as Rate
from CaliforniaCondorGreenBayPackers
where GreenBayPackersID = :ID
and Tier = :Tier
order by BenGrimm
10 11:30:46 :ID(VARCHAR[0])=<NULL>
:Tier(INTEGER)=1
11 11:30:46 SQL Execute: select Tier#, BenGrimm, PeakRate as Rate
from RageAnimalGreenBayPackers2
where RageAnimalGreenBayPackersID = :ID
and Tier# = :Tier
and FlyingVAccessType = 'D'
UNION
select Tier, BenGrimm, PeakRate as Rate
from CaliforniaCondorGreenBayPackers
where GreenBayPackersID = :ID
and Tier = :Tier
order by BenGrimm
12 11:30:46 :ID(VARCHAR[0])=<NULL>
:Tier(INTEGER)=2
. . .