forked from erikdarlingdata/PerformanceStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuerySessionControl.Format.cs
More file actions
151 lines (136 loc) · 4.93 KB
/
QuerySessionControl.Format.cs
File metadata and controls
151 lines (136 loc) · 4.93 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.Interactivity;
using Avalonia.Layout;
using Avalonia.Media;
using AvaloniaEdit;
using AvaloniaEdit.CodeCompletion;
using AvaloniaEdit.TextMate;
using Microsoft.Data.SqlClient;
using PlanViewer.App.Dialogs;
using PlanViewer.App.Services;
using PlanViewer.Core.Interfaces;
using PlanViewer.Core.Models;
using PlanViewer.Core.Output;
using PlanViewer.Core.Services;
using TextMateSharp.Grammars;
namespace PlanViewer.App.Controls;
public partial class QuerySessionControl : UserControl
{
private async void CopyRepro_Click(object? sender, RoutedEventArgs e)
{
var viewer = GetSelectedPlanViewer();
if (viewer == null)
{
SetStatus("Select a plan tab first");
return;
}
var planXml = viewer.RawXml;
var queryText = viewer.QueryText ?? "";
if (string.IsNullOrEmpty(queryText) && string.IsNullOrEmpty(planXml))
{
SetStatus("No query or plan data available");
return;
}
/* Extract database name from plan XML StmtSimple/@DatabaseContext if available,
otherwise fall back to the currently selected database */
var database = ExtractDatabaseFromPlanXml(planXml) ?? _selectedDatabase;
var reproScript = ReproScriptBuilder.BuildReproScript(
queryText,
database,
planXml,
isolationLevel: null,
source: "Performance Studio",
isAzureSqlDb: IsAzureConnection);
try
{
var topLevel = TopLevel.GetTopLevel(this);
if (topLevel?.Clipboard != null)
{
await topLevel.Clipboard.SetTextAsync(reproScript);
SetStatus("Repro script copied to clipboard");
}
}
catch (Exception ex)
{
SetStatus($"Clipboard error: {ex.Message}");
}
}
private async void Format_Click(object? sender, RoutedEventArgs e)
{
var sql = QueryEditor.Text;
if (string.IsNullOrWhiteSpace(sql))
return;
FormatButton.IsEnabled = false;
SetStatus("Formatting...");
try
{
var settings = AppSettingsService.Load().FormatOptions ?? new SqlFormatSettings();
var (formatted, errors) = await Task.Run(() => SqlFormattingService.Format(sql, settings));
if (errors != null && errors.Count > 0)
{
var errorMessages = string.Join("\n", errors.Select(err => $"Line {err.Line}: {err.Message}"));
var dialog = new Window
{
Title = "SQL Format Error",
Width = 500,
Height = 250,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
Icon = GetParentWindow().Icon,
Background = (IBrush)this.FindResource("BackgroundBrush")!,
Foreground = (IBrush)this.FindResource("ForegroundBrush")!,
Content = new StackPanel
{
Margin = new Avalonia.Thickness(20),
Children =
{
new TextBlock
{
Text = $"Could not format: {errors.Count} parse error(s)",
FontWeight = Avalonia.Media.FontWeight.Bold,
FontSize = 14,
Margin = new Avalonia.Thickness(0, 0, 0, 10)
},
new TextBlock
{
Text = errorMessages,
TextWrapping = TextWrapping.Wrap,
FontSize = 12
}
}
}
};
await dialog.ShowDialog(GetParentWindow());
SetStatus($"Format failed: {errors.Count} error(s)");
return;
}
var caretOffset = QueryEditor.CaretOffset;
QueryEditor.Document.BeginUpdate();
try
{
QueryEditor.Document.Replace(0, QueryEditor.Document.TextLength, formatted);
}
finally
{
QueryEditor.Document.EndUpdate();
}
QueryEditor.CaretOffset = Math.Min(caretOffset, QueryEditor.Document.TextLength);
SetStatus("Formatted");
}
finally
{
FormatButton.IsEnabled = true;
}
}
}