当在TSqlFragment中使用访问者扫描ColumnReferenceExpression时,有些东西会被作为真正不具备的列。例如在这个sql中:
select Age, FirstName, LastName, DATEADD(year, Age, getdate())
from table1
inner join table2 on 1 = 1
年参数作为列引用以及Age,FirstName和LastName被选中。没有任何方法可以将年份与其他列区分开来。
我问的原因是我们编写了一个SqlCodeAnalysisRule来检查两个部分名称,当select中有多个表时,它也会拾取这些非列。如果有人对如何排除它们有任何想法,请按以下方式分析规则:
public override IList<SqlRuleProblem> Analyze(SqlRuleExecutionContext ruleExecutionContext)
{
var problems = new List<SqlRuleProblem>();
var sqlObj = ruleExecutionContext.ModelElement;
if (sqlObj == null) { return problems; }
var fragment = ruleExecutionContext.ScriptFragment;
var selectStatementVisitor = new SelectStatementVisitor();
fragment.Accept(selectStatementVisitor);
if (selectStatementVisitor.Statements.Count == 0) { return problems; }
foreach (var select in selectStatementVisitor.Statements)
{
var fromClause = (select.QueryExpression as QuerySpecification)?.FromClause;
if (fromClause == null) { continue; }
//check to ensure we have more than one table
var namedTableVisitor = new NamedTableReferenceVisitor();
fromClause.Accept(namedTableVisitor);
if(namedTableVisitor.Statements.Count <= 1) { continue; }
var columnReferences = new ColumnReferenceExpressionVisitor();
select.Accept(columnReferences);
//TODO: This will erroneously pickup things which appear to be column references as well
// such as the 'dd' in DATEADD(dd, rev.ReviewReferenceDaysStart, @StartDate)
var offenders = columnReferences.Statements
.Where(c => (c as ColumnReferenceExpression).MultiPartIdentifier?.Identifiers.Count == 1)
.Select(n => (n as ColumnReferenceExpression).MultiPartIdentifier.Identifiers[0]);
problems.AddRange(offenders.Select(cr => new SqlRuleProblem(string.Format(Message, cr.Value), sqlObj, cr)));
}
return problems;
}
我们所有的访客都遵循相同的模式。以下是列引用访问者作为其他提及的示例:
internal class ColumnReferenceExpressionVisitor : TSqlFragmentVisitor, IVisitor<ColumnReferenceExpression>
{
public IList<ColumnReferenceExpression> Statements { get; } = new List<ColumnReferenceExpression>();
public override void ExplicitVisit(ColumnReferenceExpression node)
{
Statements.Add(node);
}
}
public interface IVisitor<T> where T : TSqlFragment
{
IList<T> Statements { get; }
}
我已经比较了找到非列的每个列的所有属性,并且没有什么可以用来排除我看到的列。
答案 0 :(得分:1)
当你获取树中的所有ColumnReferences时,dd是一个列引用 - 脚本dom不知道dateadd是什么,它只知道它是一个函数,所以dd很可能是一个列。 / p>
如果是我,我会得到select语句,获取SelectElements并迭代它们(如果你有相关的子查询,例如,通过使用Select访问者,你仍然应该得到所有这些),在这种情况下忽略任何不是ColumnReference。
手动完成(而不是linq),所以它更清晰一点,如:
foreach (var select in selectStatementVisitor.Statements)
{
var columnReferences = new ColumnReferenceExpressionVisitor();
select.Accept(columnReferences);
foreach (var item in (select.QueryExpression as QuerySpecification).SelectElements)
{
//other code here if you want ...
if (item is SelectScalarExpression)
{
var expression = item as SelectScalarExpression;
if (expression.Expression is ColumnReferenceExpression)
{
var column = expression.Expression as ColumnReferenceExpression;
Console.WriteLine(column); // <-- this is only ColumnReferenceExpression's
}
}
}
}