-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
93 lines (77 loc) · 3.51 KB
/
Copy pathProgram.cs
File metadata and controls
93 lines (77 loc) · 3.51 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#nullable enable
// =============================================================================
// Author: Vladyslav Zaiets | https://sarmkadan.com
// CTO & Software Architect
// =============================================================================
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SqlQueryAnalyzer.Configuration;
using SqlQueryAnalyzer.Services;
using SqlQueryAnalyzer.Repositories;
using SqlQueryAnalyzer.Models;
using SqlQueryAnalyzer.Visualization;
namespace SqlQueryAnalyzer;
class Program
{
static async Task Main(string[] args)
{
var services = new ServiceCollection();
// Configure logging
services.AddLogging(config =>
{
config.AddConsole();
config.SetMinimumLevel(LogLevel.Information);
});
// Register configuration
services.AddSingleton<IConnectionConfiguration, SqlServerConfiguration>();
services.AddSingleton(ProfilerSettings.ForDevelopment());
// Register repositories
services.AddSingleton<IQueryRepository, QueryRepository>();
services.AddSingleton<IAnalysisRepository, AnalysisRepository>();
services.AddSingleton<IIndexRepository, IndexRepository>();
// Register services
services.AddSingleton<IQueryAnalyzerService, QueryAnalyzerService>();
services.AddSingleton<IIndexAnalyzerService, IndexAnalyzerService>();
services.AddSingleton<IQueryPlanAnalyzerService, QueryPlanAnalyzerService>();
services.AddSingleton<IPerformanceIssueDetectorService, PerformanceIssueDetectorService>();
services.AddSingleton<IExplainPlanParserService, ExplainPlanParserService>();
services.AddSingleton<IHtmlPlanVisualizer, HtmlPlanVisualizer>();
services.AddSingleton<IIndexRecommendationEngine, IndexRecommendationEngine>();
services.AddSingleton<ISlowQueryLogParser, SlowQueryLogParser>();
var serviceProvider = services.BuildServiceProvider();
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
try
{
logger.LogInformation("Starting SQL Query Analyzer v1.0.0");
var analyzer = serviceProvider.GetRequiredService<IQueryAnalyzerService>();
// Example usage
await RunAnalysisExample(analyzer, logger);
logger.LogInformation("Analysis completed successfully");
}
catch (Exception ex)
{
logger.LogError(ex, "Application terminated with error");
Environment.Exit(1);
}
}
// Demonstrate basic analyzer usage
static async Task RunAnalysisExample(IQueryAnalyzerService analyzer, ILogger logger)
{
var sampleQueries = new[]
{
"SELECT * FROM Orders o JOIN Customers c ON o.CustomerId = c.Id WHERE c.Country = 'USA'",
"SELECT OrderId, CustomerId FROM Orders WHERE OrderDate > GETDATE() - 30",
"SELECT DISTINCT c.Id FROM Customers c WHERE NOT EXISTS (SELECT 1 FROM Orders o WHERE o.CustomerId = c.Id)"
};
foreach (var query in sampleQueries)
{
logger.LogInformation($"Analyzing: {query.Substring(0, Math.Min(60, query.Length))}...");
var result = await analyzer.AnalyzeQueryAsync(query);
logger.LogInformation($"Issues found: {result.Issues.Count}");
foreach (var issue in result.Issues)
{
logger.LogWarning($" - {issue.IssueType}: {issue.Description}");
}
}
}
}