我有一个包含“曲线”的大型 SQL 数据库。每条曲线都有一个 ID(curveid)。我正在尝试确定每条曲线的主要用户以及是否使用它。为了实现这一点,DBA 提供了针对数据库执行的所有语句的日志。
这些语句可能非常复杂。我要做的就是提取正在查询的curveids。
示例语句如下:
WITH G AS ( SELECT [Timevalue] FROM [mc].[GranularityLookup]
WHERE [TimeValue] BETWEEN '19-Jul-2017 00:00' AND '30-Sep-2017 00:00'
AND [1 Hr] = 1),
D AS ( SELECT [CurveID], [DeliveryDate], [PublishDate], AVG([Value]) Value, MAX([PeriodNumber]) PeriodNumber
FROM mc.CURVEID_6657_1_LATEST data
JOIN
(SELECT CurveID ID, DeliveryDate dDate, MAX(PublishDate) pDate
FROM mc.CURVEID_6657_1_LATEST
WHERE CurveID = 90564
AND DeliveryDate >= '19-Jul-2017 00:00' AND DeliveryDate <= '30-Sep-2017 00:00'
GROUP BY DeliveryDate, CurveID ) Dates
ON data.DeliveryDate = dates.dDate AND data.PublishDate = dates.pDate
WHERE data.CurveID = 90564
AND data.DeliveryDate >= '19-Jul-2017 00:00' AND data.DeliveryDate <= '30-Sep-2017 00:00'
GROUP BY [CurveID], [PublishDate], [DeliveryDate] )
SELECT
G.[TimeValue] [Deliver
yDate] , D.[PublishDate], D.[Value], D.[PeriodNumber]
FROM
G
LEFT JOIN
D
ON
G.[TimeValue] = D.[DeliveryDate]
ORDER BY DeliveryDate ASC, PeriodNumber ASC, publishDate DESC
从这个语句中,我感兴趣的只是提取用户查询的curveid 90564。
该语句也可能类似于以下任何一种:
SELECT * FROM anytable WHERE curveid = 123 AND deliverydate BETWEEN '2017-01-01' AND 2017-02-01'
或者
SELECT * FROM mc.anytable WHERE curveid IN (1,2,3,4,5,6,7)
同样,我只想知道曲线 ID。我不关心任何其他条款。
我正在使用 Microsoft.SqlServer.TransactSql.ScriptDom 命名空间来解析 SQL,并且已经到了可以使用类似于下面的代码识别所有 WHERE 语句的地步(从其他一些示例拼凑而成):
string sql = @"WITH
G AS ( SELECT [Timevalue] FROM [mc].[GranularityLookup]
WHERE [TimeValue] BETWEEN '19-Jul-2017 00:00' AND '30-Sep-2017 00:00'
AND [1 Hr] = 1),
D AS ( SELECT [CurveID], [DeliveryDate], [PublishDate], AVG([Value]) Value, MAX([PeriodNumber]) PeriodNumber
FROM mc.CURVEID_6657_1_LATEST data
JOIN
(SELECT CurveID ID, DeliveryDate dDate, MAX(PublishDate) pDate
FROM mc.CURVEID_6657_1_LATEST
WHERE CurveID = 90564
AND DeliveryDate >= '19-Jul-2017 00:00' AND DeliveryDate <= '30-Sep-2017 00:00'
GROUP BY DeliveryDate, CurveID ) Dates
ON data.DeliveryDate = dates.dDate AND data.PublishDate = dates.pDate
WHERE data.CurveID = 90564
AND data.DeliveryDate >= '19-Jul-2017 00:00' AND data.DeliveryDate <= '30-Sep-2017 00:00'
GROUP BY [CurveID], [PublishDate], [DeliveryDate] )
SELECT
G.[TimeValue] [Deliver
yDate] , D.[PublishDate], D.[Value], D.[PeriodNumber]
FROM
G
LEFT JOIN
D
ON
G.[TimeValue] = D.[DeliveryDate]
ORDER BY DeliveryDate ASC, PeriodNumber ASC, publishDate DESC";
var parser = new TSql120Parser(false);
IList<ParseError> errors;
var fragment = parser.Parse(new StringReader(sql), out errors);
var whereVisitor = new WhereVisitor();
fragment.Accept(whereVisitor);
// I now have all WHERE clauses in whereVisitor.WhereStatements
class WhereVisitor : TSqlConcreteFragmentVisitor
{
public readonly List<WhereClause> WhereStatements = new List<WhereClause>();
public override void Visit(WhereClause node)
{
WhereStatements.Add(node);
}
}
whereVisitor.WhereStatements(本例中为 3 个)中的每个子句都公开了一个名为 SearchCondition 的属性。不幸的是,这是我没有想法的地方。我想要实现的是如下逻辑:
foreach (var clause in whereVisitor.WhereStatements)
{
// IF any part of the clause filters based on curveid THEN
// Capture curveIDs
// END IF
}
其他详情:
- 使用 C# (.net 4.0)
- SQL Server 2008
- DLL 是 Microsoft.SqlServer.TransactSql.ScriptDom (在我的例子中位于 'c:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\SQLPS\Microsoft.SqlServer.TransactSql.ScriptDom.dll ')
编辑 1
一些附加信息:
- CurveID 是另一个表的键。在这种情况下,对其进行操作是没有意义的(例如curveId+1 或curveId <= 10)。
编辑 2(部分解决方案)
拥有以下访问者有助于解决 where 子句类似于 curveid = 123 的情况:
class CurveIdVisitor : TSqlConcreteFragmentVisitor
{
public readonly List<int> CurveIds = new List<int>();
public override void Visit(BooleanComparisonExpression exp)
{
if (exp.FirstExpression is ColumnReferenceExpression && exp.SecondExpression is IntegerLiteral )
{
// there is a possibility that this is of the ilk 'curveid = 123'
// we will look for the 'identifier'
// we take the last if there are multiple. Example:
// alias.curveid
// goives two identifiers: alias and curveid
if (
((ColumnReferenceExpression) exp.FirstExpression).MultiPartIdentifier.Identifiers.Last().Value.ToLower() ==
"curveid")
{
// this is definitely a curveid filter
// Now to find the curve id
int curveid = int.Parse(((IntegerLiteral) exp.SecondExpression).Value);
CurveIds.Add(curveid);
}
}