解析T-SQL以提取部分WHERE子句

时间:2017-07-25 12:39:31

标签: c# sql-server sql-parser

我有一个包含'曲线的大型SQL数据库。每条曲线都有一个ID(curveid)。我试图确定每条曲线的主要用户以及是否完全使用它。为了实现这一点,DBA提供了针对数据库执行的所有语句的日志。

这些陈述非常复杂。我想要做的就是提取正在查询的曲线。

示例声明如下:

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

从这个声明中,我感兴趣的是提取用户查询的曲线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语句的点(从其他一些示例拼凑而成):< / p>

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(在我的情况下位于&#39; c:\ Program Files(x86)\ Microsoft SQL Server \ 130 \ Tools \ PowerShell \ Modules \ SQLPS \ Microsoft.SqlServer .TransactSql.ScriptDom.dll&#39)

修改1

其他一些信息:

  • CurveID是另一个表的键。在这方面没有意义 对其进行操作的情况(例如,curveId + 1或curveId&lt; = 10)。

编辑2(部分解决方案)

让以下访问者帮助处理类似于子句的情况 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);
            }
        }

1 个答案:

答案 0 :(得分:0)

最后解决了这个问题,希望将来有人能从中受益。也许其他人可能会阅读时间并提供更好的解决方案。

public class SqlParser
{
    public List<int> GetQueriedCurveIds(string sql)
    {
        var parser = new TSql120Parser(false);

        IList<ParseError> errors;
        var fragment = parser.Parse(new StringReader(sql), out errors);

        List<int> curveIds = new List<int>();
        CurveIdVisitor cidv = new CurveIdVisitor();
        InPredicateVisitor inpv = new InPredicateVisitor();
        fragment.AcceptChildren(cidv);
        fragment.AcceptChildren(inpv);

        curveIds.AddRange(cidv.CurveIds);
        curveIds.AddRange(inpv.CurveIds);
        return curveIds.Distinct().ToList();
    }
}



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);
            }
        }
    }
}

class InPredicateVisitor : TSqlConcreteFragmentVisitor
{
    public readonly List<int> CurveIds = new List<int>();

    public override void Visit(InPredicate exp)
    {
        if (exp.Expression is ColumnReferenceExpression)
        {
            if (
                ((ColumnReferenceExpression) exp.Expression).MultiPartIdentifier.Identifiers.Last().Value.ToLower() ==
                "curveid")
            {
                foreach (var value in exp.Values)
                {
                    if (value is IntegerLiteral)
                    {
                        CurveIds.Add(int.Parse(((IntegerLiteral)value).Value));
                    }
                }
            }
        }
    }
}

这是用于演示答案的缩减代码。在现实生活中,您需要检查ParseError集合并添加一些错误处理!