forked from tsqllint/tsqllint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SqlRuleVisitor.cs
54 lines (44 loc) · 1.99 KB
/
SqlRuleVisitor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.IO;
using System.Linq;
using TSQLLint.Common;
using TSQLLint.Lib.Parser.Interfaces;
using TSQLLint.Lib.Parser.RuleExceptions;
using TSQLLint.Lib.Rules.RuleViolations;
namespace TSQLLint.Lib.Parser
{
public class SqlRuleVisitor : IRuleVisitor
{
private readonly IFragmentBuilder fragmentBuilder;
private readonly IRuleVisitorBuilder ruleVisitorBuilder;
private readonly IReporter reporter;
private readonly IRuleExceptionFinder ruleExceptionFinder;
public SqlRuleVisitor(IRuleVisitorBuilder ruleVisitorBuilder, IFragmentBuilder fragmentBuilder, IReporter reporter)
: this(ruleVisitorBuilder, new RuleExceptionFinder(), fragmentBuilder, reporter) { }
public SqlRuleVisitor(IRuleVisitorBuilder ruleVisitorBuilder, IRuleExceptionFinder ruleExceptionFinder, IFragmentBuilder fragmentBuilder, IReporter reporter)
{
this.fragmentBuilder = fragmentBuilder;
this.reporter = reporter;
this.ruleExceptionFinder = ruleExceptionFinder;
this.ruleVisitorBuilder = ruleVisitorBuilder;
}
public void VisitRules(string sqlPath, Stream sqlFileStream)
{
TextReader sqlTextReader = new StreamReader(sqlFileStream);
var sqlFragment = fragmentBuilder.GetFragment(sqlTextReader, out var errors);
if (errors.Count > 0)
{
reporter.ReportViolation(new RuleViolation(sqlPath, null, "TSQL not syntactically correct", 0, 0, RuleViolationSeverity.Error));
Environment.ExitCode = 1;
return;
}
sqlFileStream.Seek(0, SeekOrigin.Begin);
var ignoredRules = ruleExceptionFinder.GetIgnoredRuleList(sqlFileStream).ToList();
var ruleVisitors = ruleVisitorBuilder.BuildVisitors(sqlPath, ignoredRules);
foreach (var visitor in ruleVisitors)
{
sqlFragment.Accept(visitor);
}
}
}
}