forked from sergiisyrovatchenko/SQLIndexManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainBox.cs
More file actions
891 lines (702 loc) · 29.9 KB
/
Copy pathMainBox.cs
File metadata and controls
891 lines (702 loc) · 29.9 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
using DevExpress.Data;
using DevExpress.Utils;
using DevExpress.Utils.Menu;
using DevExpress.Utils.Taskbar.Core;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Localization;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraPrinting;
using SQLIndexManager.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SQLIndexManager {
public partial class MainBox : RibbonForm {
private long _totalDuration;
public MainBox() {
InitializeComponent();
splitContainer.PanelVisibility = SplitPanelVisibility.Panel1;
Output.Current.SetOutputControl(labelInfo, gridControl2);
Output.Current.Add($"Log folder: {Environment.CurrentDirectory}");
popupIndexOperation.DataSource = Enum.GetValues(typeof(IndexOp)).Cast<IndexOp>().ToList();
RestoreLayout();
}
private void UpdateActiveHost(Host host) {
Settings.ActiveHost = host;
buttonDatabases.Enabled =
buttonRefreshIndex.Enabled = (host != null && host.Databases.Count > 0);
labelServerInfo.Visibility =
labelDatabase.Visibility =
labelIndex.Visibility = (host == null) ? BarItemVisibility.Never : BarItemVisibility.Always;
if (host == null) {
ShowConnectionBox();
}
else {
Output.Current.Add($"Current host: {host}");
labelServerInfo.Caption = host.ToString();
ShowDatabaseBox();
}
}
#region Save/Restore Layout
private void MainBox_FormClosing(object sender, FormClosingEventArgs e) {
SaveLayout();
}
private void RestoreLayout() {
if (File.Exists(Settings.LayoutFileName)) {
try {
string layout = AES.Decrypt(File.ReadAllText(Settings.LayoutFileName));
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(layout))) {
gridView1.RestoreLayoutFromStream(stream);
}
Output.Current.Add($"Grid layout restored: {Settings.LayoutFileName}");
}
catch {
Output.Current.Add("Failed to restore layout");
}
}
}
private void SaveLayout() {
gridView1.Columns["Progress"].Visible = false;
gridView1.Columns["Duration"].Visible = false;
try {
using (StreamWriter file = new StreamWriter(new FileStream(Settings.LayoutFileName, FileMode.Create))) {
Stream str = new MemoryStream();
gridView1.SaveLayoutToStream(str);
str.Seek(0, SeekOrigin.Begin);
StreamReader reader = new StreamReader(str);
string layout = reader.ReadToEnd();
file.Write(AES.Encrypt(layout));
file.Close();
Output.Current.Add($"Grid layout saved: {Settings.LayoutFileName}");
}
}
catch {
Output.Current.Add("Failed to save layout");
}
}
#endregion
#region Scan Indexes
private readonly List<Index> _scanIndexes = new List<Index>();
private BackgroundWorker _workerScan;
private Stopwatch _scanDuration = new Stopwatch();
private void ScanIndexStart(object sender, DoWorkEventArgs e) {
using (ConnectionList connectionList = new ConnectionList(Settings.ActiveHost)) {
foreach (var database in Settings.ActiveHost.Databases) {
List<Index> idx = new List<Index>();
if (_workerScan.CancellationPending) {
e.Cancel = true;
return;
}
SqlConnection connection = connectionList.Get(database);
if (connection == null) continue;
Output.Current.Add($"Describe: {database}");
Stopwatch opw = Stopwatch.StartNew();
short retries = 0;
while (true) {
try {
idx = QueryEngine.GetIndexes(connection);
break;
}
catch (SqlException ex) {
if (ex.Message.Contains("Incorrect syntax") || ex.Message.Contains("Invalid")) {
Output.Current.Add($"Syntax error: {ex.Source}", ex.Message);
XtraMessageBox.Show(ex.Message.Replace(". ", "." + Environment.NewLine), ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Output.Current.Add($"Pre-describe #{retries} failed: {database}. Rescan...", ex.Message);
retries++;
if (retries > 2 || !ex.Message.Contains("timeout")) {
break;
}
}
}
opw.Stop();
Output.Current.Add($"Pre-descibe: {(idx.Count(_ => _.Fragmentation != null))}. " +
$"Post-describe: {(idx.Count(_ => _.Fragmentation == null))}", null, opw.ElapsedMilliseconds);
List<int> clid = new List<int>();
foreach (Index index in idx.Where(_ => _.Fragmentation == null)
.OrderBy(_ => _.ObjectId)
.ThenBy(_ => _.IndexId)
.ThenBy(_ => _.PartitionNumber)) {
try {
if (_workerScan.CancellationPending) {
_scanIndexes.AddRange(idx);
e.Cancel = true;
return;
}
connection = connectionList.Get(database);
if (connection == null) break;
if (index.IndexType == IndexType.ColumnstoreClustered || index.IndexType == IndexType.ColumnstoreNonClustered) {
if (!clid.Exists(_ => _ == index.ObjectId)) {
Output.Current.AddCaption(index.ToString());
opw = Stopwatch.StartNew();
QueryEngine.GetColumnstoreFragmentation(connection, index, idx);
clid.Add(index.ObjectId);
opw.Stop();
Output.Current.Add(index.ToString(), null, opw.ElapsedMilliseconds);
}
}
else {
Output.Current.AddCaption(index.ToString());
opw = Stopwatch.StartNew();
QueryEngine.GetIndexFragmentation(connection, index);
opw.Stop();
Output.Current.Add(index.ToString(), null, opw.ElapsedMilliseconds);
}
}
catch (Exception ex) {
Output.Current.Add($"Failed: {index}", ex.Message);
}
}
_scanIndexes.AddRange(idx);
}
}
}
private void ScanIndexFinish(object sender, RunWorkerCompletedEventArgs e) {
taskbar.ProgressMode = TaskbarButtonProgressMode.NoProgress;
_scanDuration.Stop();
buttonDatabases.Enabled =
buttonRefreshIndex.Enabled =
buttonNewConnection.Enabled =
buttonOptions.Enabled = true;
buttonStopScan.Visibility = BarItemVisibility.Never;
List<Index> indexes = _scanIndexes.Where(_ => _.Fragmentation >= Settings.Options.ReorganizeThreshold
&& _.PagesCount >= Settings.Options.MinIndexSize.PageSize()
&& _.PagesCount <= Settings.Options.MaxIndexSize.PageSize())
.OrderByDescending(_ => Math.Ceiling(Math.Truncate(_.Fragmentation ?? 0) / 20) * 20)
.ThenByDescending(_ => _.PagesCount).ToList();
foreach (Index ix in indexes) {
if (ix.IndexType == IndexType.MissingIndex)
ix.FixType = IndexOp.CreateIndex;
else if (ix.Fragmentation < Settings.Options.RebuildThreshold && ix.IsAllowReorganize)
ix.FixType = IndexOp.Reorganize;
else if (Settings.Options.Online && ix.IsAllowOnlineRebuild)
ix.FixType = IndexOp.RebuildOnline;
else if (!ix.IsColumnstore && ix.IsAllowCompression) {
if (Settings.Options.DataCompression == DataCompression.None.ToDescription() && ix.DataCompression != DataCompression.None)
ix.FixType = IndexOp.RebuildNone;
else if (Settings.Options.DataCompression == DataCompression.Row.ToDescription())
ix.FixType = IndexOp.RebuildRow;
else if (Settings.Options.DataCompression == DataCompression.Page.ToDescription())
ix.FixType = IndexOp.RebuildPage;
else
ix.FixType = IndexOp.Rebuild;
}
else
ix.FixType = IndexOp.Rebuild;
}
labelIndex.Caption = indexes.Count.ToString();
Output.Current.Add($"Processed: {_scanIndexes.Count}. Fragmented: {indexes.Count}", null, _scanDuration.ElapsedMilliseconds);
gridView1.CustomDrawEmptyForeground += CustomDrawEmptyForeground;
gridControl1.DataSource = indexes;
if (indexes.Count > 0) {
var rulePagesCount = gridView1.FormatRules[Resources.PagesCount].RuleCast<FormatConditionRuleDataBar>();
var ruleUnusedPagesCount = gridView1.FormatRules[Resources.UnusedPagesCount].RuleCast<FormatConditionRuleDataBar>();
rulePagesCount.Minimum = 1;
rulePagesCount.Maximum = indexes.Max(_ => _.PagesCount);
ruleUnusedPagesCount.Minimum = 1;
ruleUnusedPagesCount.Maximum = indexes.Max(_ => _.UnusedPagesCount) > 1000
? indexes.Max(_ => _.UnusedPagesCount)
: rulePagesCount.Maximum;
}
}
private void RefreshIndexes() {
if (_workerFix != null && _workerFix.IsBusy) return;
gridView1.CustomDrawEmptyForeground -= CustomDrawEmptyForeground;
gridControl1.DataSource = null;
labelDatabase.Caption = Settings.ActiveHost.Databases.Count.ToString();
labelIndex.Caption = @"0";
RestoreSortRules();
gridView1.OptionsBehavior.Editable = true;
gridView1.OptionsSelection.MultiSelect = true;
gridView1.Columns[Resources.Progress].Visible = false;
gridView1.Columns[Resources.Duration].Visible = false;
gridView1.Columns[Resources.PagesCountBefore].Visible = false;
buttonDatabases.Enabled =
buttonRefreshIndex.Enabled =
buttonNewConnection.Enabled =
buttonOptions.Enabled = false;
buttonStopScan.Visibility = BarItemVisibility.Always;
taskbar.ProgressMode = TaskbarButtonProgressMode.Indeterminate;
_scanIndexes.Clear();
_scanDuration = Stopwatch.StartNew();
_workerScan = new ThreadWorker() { WorkerSupportsCancellation = true, WorkerReportsProgress = true };
_workerScan.DoWork += ScanIndexStart;
_workerScan.RunWorkerCompleted += ScanIndexFinish;
_workerScan.RunWorkerAsync();
}
#endregion
#region Fix Indexes
private void buttonFix_ItemClick(object sender, ItemClickEventArgs e) {
if (_workerFix != null && _workerFix.IsBusy) return;
_totalDuration = 0;
buttonDatabases.Enabled =
buttonRefreshIndex.Enabled =
buttonNewConnection.Enabled =
buttonFix.Enabled =
buttonOptions.Enabled = false;
buttonStopFix.Visibility = BarItemVisibility.Always;
List<Index> selIndex = ((List<Index>)gridView1.DataSource).Where(_ => _.IsSelected).ToList();
List<Index> fixIndex =
selIndex.Where(x => Settings.ActiveHost.Databases.Any(y => y == x.DatabaseName))
.OrderByDescending(_ => Math.Ceiling(Math.Truncate(_.Fragmentation ?? 0) / 20) * 20)
.ThenByDescending(_ => _.PagesCount).ToList();
labelDatabase.Caption = fixIndex.Select(_ => _.DatabaseName).Distinct().Count().ToString();
labelIndex.Caption = fixIndex.Count.ToString();
foreach (Index row in fixIndex) {
row.Progress = imageCollection.Images[0];
}
SaveSortRules();
gridView1.ClearColumnsFilter();
gridView1.ClearSelection();
gridView1.Columns[Resources.Duration].Visible = true;
gridView1.Columns[Resources.Duration].VisibleIndex = 0;
gridView1.Columns[Resources.Progress].Visible = true;
gridView1.Columns[Resources.Progress].VisibleIndex = 0;
gridView1.Columns[Resources.PagesCountBefore].Visible = true;
gridView1.Columns[Resources.PagesCountBefore].VisibleIndex = gridView1.Columns[Resources.PagesCount].VisibleIndex + 1;
gridControl1.DataSource = fixIndex;
gridView1.OptionsBehavior.Editable = false;
gridView1.OptionsSelection.MultiSelect = false;
Output.Current.Add("Start processing");
taskbar.ProgressMaximumValue = fixIndex.Count;
taskbar.ProgressMode = TaskbarButtonProgressMode.Normal;
_workerFix = new BackgroundWorker { WorkerSupportsCancellation = true, WorkerReportsProgress = true };
_workerFix.DoWork += FixIndexes;
_workerFix.RunWorkerCompleted += FixIndexesFinish;
_workerFix.ProgressChanged += backgroundWorker_ProgressChanged;
_workerFix.RunWorkerAsync(fixIndex);
}
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
taskbar.ProgressCurrentValue = e.ProgressPercentage;
gridView1.RefreshData();
}
private BackgroundWorker _workerFix;
private void FixIndexes(object sender, DoWorkEventArgs e) {
List<Index> indexes = (List<Index>)e.Argument;
using (var connectionList = new ConnectionList(Settings.ActiveHost)) {
int count = 1;
foreach (Index item in indexes) {
if (_workerFix.CancellationPending) {
Output.Current.Add("Canceled");
e.Cancel = true;
return;
}
string sql = item.GetQuery();
Output.Current.AddCaption(item.ToString());
Stopwatch watch = Stopwatch.StartNew();
item.Progress = imageCollection.Images[1];
SqlConnection connection = connectionList.Get(item.DatabaseName);
if (connection != null) {
QueryEngine.FixIndex(connection, item);
}
item.Progress = imageCollection.Images[2];
watch.Stop();
_totalDuration += watch.ElapsedMilliseconds;
item.Duration = (new DateTime(0)).AddMilliseconds(watch.ElapsedMilliseconds);
if (string.IsNullOrEmpty(item.Error)) {
Output.Current.Add(item.ToString(), sql, watch.ElapsedMilliseconds);
}
else {
Output.Current.Add(item.ToString(), item.Error);
item.Progress = imageCollection.Images[3];
}
_workerFix.ReportProgress(count++);
}
}
}
private void FixIndexesFinish(object sender, RunWorkerCompletedEventArgs e) {
taskbar.ProgressMaximumValue = 0;
taskbar.ProgressCurrentValue = 0;
taskbar.ProgressMode = TaskbarButtonProgressMode.NoProgress;
buttonDatabases.Enabled =
buttonRefreshIndex.Enabled =
buttonNewConnection.Enabled =
buttonOptions.Enabled = true;
buttonFix.Enabled = false;
buttonStopFix.Visibility = BarItemVisibility.Never;
Output.Current.Add("Finished", null, _totalDuration);
}
#endregion
#region Dialogs
private void MainBox_Shown(object sender, EventArgs e) {
UpdateActiveHost(null);
}
private void ShowDatabaseBox() {
using (DatabaseBox form = new DatabaseBox()) {
DialogResult dr = form.ShowDialog(this);
if (dr == DialogResult.OK) {
Settings.ActiveHost.Databases = form.GetDatabases();
RefreshIndexes();
}
else {
labelDatabase.Caption = Settings.ActiveHost.Databases.Count.ToString();
}
}
}
private void ShowConnectionBox() {
using (ConnectionBox form = new ConnectionBox()) {
DialogResult dr = form.ShowDialog(this);
if (dr == DialogResult.OK) {
UpdateActiveHost(form.GetHost());
}
}
}
#endregion
#region Grid Methods
private string GetFixChanges() {
List<Index> dv = (List<Index>)gridView1.DataSource;
List<Index> fix = dv.Where(_ => _.IsSelected).ToList();
StringBuilder sb = new StringBuilder();
var groupList = fix.GroupBy(u => u.DatabaseName).Select(grp => grp.ToList()).ToList();
foreach (var group in groupList) {
sb.AppendLine($"USE [{group[0].DatabaseName.ToQuota()}]\nGO\n");
foreach (Index i in group.OrderBy(_ => _.SchemaName)
.ThenBy(_ => _.ObjectName)
.ThenBy(_ => _.IndexName)
.ThenBy(_ => _.PartitionNumber)) {
sb.AppendLine($"RAISERROR(N'{i}', 0, 1) WITH NOWAIT\n{i.GetQuery()}\nGO\n");
}
}
return sb.ToString();
}
private void FixedOpPopupValueChanged(object sender, EventArgs e) {
int row = gridView1.GetFocusedDataSourceRowIndex();
gridView1.PostEditor();
gridView1.SelectRow(row);
gridView1.UpdateCurrentRow();
}
private void FixedOpPopup(object sender, EventArgs e) {
LookUpEdit obj = (LookUpEdit)sender;
obj.Properties.DataSource = null;
Index row = (Index)gridView1.GetFocusedRow();
if (row == null) return;
List<IndexOp> i = new List<IndexOp> { IndexOp.Rebuild };
if (row.IsColumnstore) {
i.Add(row.DataCompression == DataCompression.ColumnstoreArchive
? IndexOp.RebuildColumnstore
: IndexOp.RebuildColumnstoreArchive);
i.Add(IndexOp.Reorganize);
if (Settings.ServerInfo.MajorVersion >= 13)
i.Add(IndexOp.ReorganizeCompressAllRowGroup);
}
else {
if (row.IsAllowCompression) {
if (row.DataCompression == DataCompression.None) {
i.Add(IndexOp.RebuildRow);
i.Add(IndexOp.RebuildPage);
}
else {
i.Add(IndexOp.RebuildNone);
i.Add(row.DataCompression == DataCompression.Page
? IndexOp.RebuildRow
: IndexOp.RebuildPage);
}
}
if (row.IsAllowOnlineRebuild)
i.Add(IndexOp.RebuildOnline);
if (row.FillFactor > 0 && row.FillFactor < 100)
i.Add(IndexOp.RebuildFillFactorZero);
if (row.IsAllowReorganize)
i.Add(IndexOp.Reorganize);
if (!row.IsPartitioned) {
if (row.IndexType == IndexType.Clustered || row.IndexType == IndexType.NonClustered) {
i.Add(IndexOp.UpdateStatsFull);
i.Add(IndexOp.UpdateStatsSample);
i.Add(IndexOp.UpdateStatsResample);
}
if (row.IndexType == IndexType.NonClustered) {
i.Add(IndexOp.Disable);
i.Add(IndexOp.Drop);
}
}
}
obj.Properties.DropDownRows = i.Count;
obj.Properties.DataSource = i.Select(_ => new { Fix = _, Name = _.ToDescription() });
}
private void GridDoubleClick(object sender, EventArgs e) {
GridView obj = (GridView)sender;
Point pt = obj.GridControl.PointToClient(MousePosition);
GridHitInfo info = obj.CalcHitInfo(pt);
if (info.Column == null || info.Column.Caption == @"Fix" || info.Column.Caption == @"Selection")
return;
if (info.InRow || info.InRowCell) {
if (obj.IsRowSelected(info.RowHandle))
obj.UnselectRow(info.RowHandle);
else
obj.SelectRow(info.RowHandle);
}
}
private void GridSelectionChanged(object sender, SelectionChangedEventArgs e) {
if (_workerFix != null && _workerFix.IsBusy) return;
GridView obj = (GridView)sender;
int[] b = obj.GetSelectedRows();
foreach (int row in b) {
Index a = (Index)obj.GetRow(row);
a.IsSelected = true;
}
for (int i = 0; i != obj.RowCount; i++) {
if (b.Contains(i)) continue;
Index a = (Index)obj.GetRow(i);
a.IsSelected = false;
}
List<Index> dataView = (List<Index>)obj.DataSource;
int selectedItems = dataView.Count(_ => _.IsSelected);
buttonFix.Enabled = selectedItems > 0;
}
private void GridRowCountChanged(object sender, EventArgs e) {
GridView obj = (GridView)sender;
if (obj.RowCount == 0) return;
List<Index> dv = (List<Index>)obj.DataSource;
obj.SelectionChanged -= GridSelectionChanged;
foreach (Index var in dv.Where(_ => _.IsSelected)) {
for (int i = 0; i < obj.DataRowCount; i++) {
if (var.Equals(obj.GetRow(i)) && obj.IsDataRow(i)) {
obj.SelectRow(i);
}
}
}
obj.SelectionChanged += GridSelectionChanged;
}
private void GridRowCellClick(object sender, RowCellClickEventArgs e) {
if (e.Column != null && e.Column.Name == "FixType") {
GridView obj = (GridView)sender;
obj.ShowEditor();
LookUpEdit ed = (LookUpEdit)obj.ActiveEditor;
ed?.ShowPopup();
}
}
private void GridColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e) {
switch (e.Column.FieldName) {
case "PagesCount":
case "PagesCountBefore":
case "UnusedPagesCount":
if (e.Value != null) {
e.DisplayText = (Convert.ToDecimal(e.Value) * 8).FormatSize();
}
break;
case "Fragmentation":
double value = Convert.ToDouble(e.Value);
e.DisplayText = string.Format(value - Math.Truncate(value) < 0.1 || value > 99.9 ? "{0:n0} %" : "{0:n1} %", value);
break;
case "Compression":
e.DisplayText = ((DataCompression)e.Value).ToDescription();
break;
case "IndexType":
e.DisplayText = ((IndexType)e.Value).ToDescription();
break;
case "FixType":
e.DisplayText = ((IndexOp)e.Value).ToDescription();
break;
}
}
private void GridPopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
if (e.MenuType == GridMenuType.Column) {
string[] columns = {
GridLocalizer.Active.GetLocalizedString(GridStringId.MenuColumnGroup),
GridLocalizer.Active.GetLocalizedString(GridStringId.MenuGroupPanelShow),
GridLocalizer.Active.GetLocalizedString(GridStringId.MenuColumnBestFit),
GridLocalizer.Active.GetLocalizedString(GridStringId.MenuColumnBestFitAllColumns)
};
foreach (DXMenuItem item in e.Menu.Items) {
if (columns.Contains(item.Caption))
item.Visible = false;
}
}
}
private void GridCustomizationMenu(object sender, RibbonCustomizationMenuEventArgs e) {
foreach (BarItemLink itemLink in e.CustomizationMenu.ItemLinks) {
itemLink.Visible = false;
}
}
private void GetActiveObjectInfo(object sender, ToolTipControllerGetActiveObjectInfoEventArgs e) {
if (e.Info == null || e.SelectedControl == gridControl1) {
GridHitInfo info = gridView1.CalcHitInfo(e.ControlMousePosition);
if (info.InRowCell && info.RowHandle != -1 && info.Column != null && info.Column.FieldName == "Progress") {
Index index = (Index)gridView1.GetRow(info.RowHandle);
e.Info = new ToolTipControlInfo($"{info.RowHandle} - {info.Column}", $"{index.GetQuery()}\n{index.Error}");
}
}
}
private void CustomDrawEmptyForeground(object sender, CustomDrawEventArgs e) {
string noIndexesFoundText = "No indexes found";
string trySearchingAgainText = "Try searching again";
int offset = 15;
e.DefaultDraw();
e.Appearance.Options.UseFont = true;
e.Appearance.Font = new Font("Tahoma", 12);
Size size = e.Appearance.CalcTextSize(e.Cache, noIndexesFoundText, e.Bounds.Width).ToSize();
int x = (e.Bounds.Width - size.Width) / 2;
int y = e.Bounds.Y + offset;
Rectangle noIndexesFoundBounds = new Rectangle(new Point(x, y), size);
e.Appearance.DrawString(e.Cache, noIndexesFoundText, noIndexesFoundBounds);
size = e.Appearance.CalcTextSize(e.Cache, trySearchingAgainText, e.Bounds.Width).ToSize();
x = noIndexesFoundBounds.X - (size.Width - noIndexesFoundBounds.Width) / 2;
y = noIndexesFoundBounds.Bottom + offset;
size.Width += offset;
Rectangle trySearchingAgainBounds = new Rectangle(new Point(x, y), size);
e.Appearance.DrawString(e.Cache, trySearchingAgainText, trySearchingAgainBounds);
}
#endregion
#region Controls
private void ButtonLog(object sender, ItemClickEventArgs e) {
splitContainer.PanelVisibility = buttonLog.Down ? SplitPanelVisibility.Both : SplitPanelVisibility.Panel1;
}
private void ButtonNewConnectionClick(object sender, ItemClickEventArgs e) {
ShowConnectionBox();
}
private void ButtonFindClick(object sender, ItemClickEventArgs e) {
RefreshIndexes();
}
private void ButtonDatabasesClick(object sender, ItemClickEventArgs e) {
ShowDatabaseBox();
}
private void ButtonStopScanClick(object sender, ItemClickEventArgs e) {
Output.Current.Add("Canceling...");
_workerScan.CancelAsync();
buttonStopScan.Visibility = BarItemVisibility.Never;
}
private void ButtonStopFixClick(object sender, ItemClickEventArgs e) {
Output.Current.Add("Canceling...");
_workerFix.CancelAsync();
buttonStopFix.Visibility = BarItemVisibility.Never;
}
private void ButtonCopyFixClick(object sender, ItemClickEventArgs e) {
string text = GetFixChanges();
Clipboard.SetText(text);
}
private void ButtonSaveFixClick(object sender, ItemClickEventArgs e) {
SaveFileDialog dialog = new SaveFileDialog {
Filter = @"SQL Files (*.sql)|*.sql|Text Files (*.txt)|*.txt|All Files (*.*)|*.*",
FilterIndex = 0,
RestoreDirectory = true
};
if (dialog.ShowDialog() == DialogResult.OK) {
string text = GetFixChanges();
using (StreamWriter writer = new StreamWriter(dialog.OpenFile())) {
writer.WriteLine(text);
writer.Dispose();
writer.Close();
}
}
}
private void ButtonOptionsClick(object sender, ItemClickEventArgs e) {
using (SettingsBox form = new SettingsBox()) {
DialogResult dr = form.ShowDialog(this);
if (dr == DialogResult.OK) {
Settings.Options = form.GetSettings();
}
}
}
private void ButtonAboutClick(object sender, ItemClickEventArgs e) {
using (AboutBox form = new AboutBox()) {
form.ShowDialog(this);
}
}
#endregion
#region Save/Restore Sort
private readonly List<GridColumnSortInfo> _sortInfo = new List<GridColumnSortInfo>();
private void SaveSortRules() {
foreach (GridColumnSortInfo col in gridView1.SortInfo) {
_sortInfo.Add(col);
}
gridView1.ClearSorting();
}
private void RestoreSortRules() {
if (_sortInfo.Count > 0 && gridView1.SortInfo.Count == 0) {
foreach (GridColumnSortInfo sortCol in _sortInfo) {
gridView1.SortInfo.Add(sortCol);
}
_sortInfo.Clear();
}
}
#endregion
#region Export
private void ExportCsv(object sender, ItemClickEventArgs e) {
SaveFileDialog dialog = new SaveFileDialog {
Filter = @"CSV Files (*.csv)|*.csv",
RestoreDirectory = true
};
if (dialog.ShowDialog() == DialogResult.OK) {
CsvExportOptionsEx advOptions = new CsvExportOptionsEx {
ExportType = DevExpress.Export.ExportType.WYSIWYG,
TextExportMode = TextExportMode.Value
};
try {
gridControl1.ExportToCsv(dialog.FileName, advOptions);
Output.Current.Add($"Export to CSV: {dialog.FileName}");
}
catch (Exception ex) {
Output.Current.Add("Export to CSV failed", ex.Message);
XtraMessageBox.Show(ex.Message.Replace(". ", "." + Environment.NewLine), ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ExportExcel(object sender, ItemClickEventArgs e) {
SaveFileDialog dialog = new SaveFileDialog {
Filter = @"Excel Files (*.xlsx)|*.xlsx",
RestoreDirectory = true
};
if (dialog.ShowDialog() == DialogResult.OK) {
XlsxExportOptionsEx advOptions = new XlsxExportOptionsEx {
ExportType = DevExpress.Export.ExportType.DataAware,
TextExportMode = TextExportMode.Value,
SheetName = Settings.ActiveHost.Server
};
try {
gridControl1.ExportToXlsx(dialog.FileName, advOptions);
Output.Current.Add($"Export to Excel: {dialog.FileName}");
}
catch (Exception ex) {
Output.Current.Add("Export to Excel failed", ex.Message);
XtraMessageBox.Show(ex.Message.Replace(". ", "." + Environment.NewLine), ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ExportText(object sender, ItemClickEventArgs e) {
SaveFileDialog dialog = new SaveFileDialog {
Filter = @"Text Files (*.txt)|*.txt",
RestoreDirectory = true
};
if (dialog.ShowDialog() == DialogResult.OK) {
try {
gridControl1.ExportToText(dialog.FileName);
Output.Current.Add($"Export to Text: {dialog.FileName}");
}
catch (Exception ex) {
Output.Current.Add("Export to Text failed", ex.Message);
XtraMessageBox.Show(ex.Message.Replace(". ", "." + Environment.NewLine), ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ExportHtml(object sender, ItemClickEventArgs e) {
SaveFileDialog dialog = new SaveFileDialog {
Filter = @"HTML Files (*.html)|*.html",
RestoreDirectory = true
};
if (dialog.ShowDialog() == DialogResult.OK) {
try {
gridControl1.ExportToHtml(dialog.FileName);
Output.Current.Add($"Export to Html: {dialog.FileName}");
}
catch (Exception ex) {
Output.Current.Add("Export to Html failed", ex.Message);
XtraMessageBox.Show(ex.Message.Replace(". ", "." + Environment.NewLine), ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
#endregion
}
}