forked from sergiisyrovatchenko/SQLIndexManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseBox.cs
More file actions
157 lines (120 loc) · 4.5 KB
/
Copy pathDatabaseBox.cs
File metadata and controls
157 lines (120 loc) · 4.5 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using DevExpress.Data;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using SQLIndexManager.Properties;
namespace SQLIndexManager {
public partial class DatabaseBox : XtraForm {
public DatabaseBox() {
InitializeComponent();
Text = Resources.DatabaseBoxTitle;
view.CustomColumnDisplayText += GridMethod.GridColumnDisplayText;
view.RowCellStyle += GridMethod.GridRowCellStyle;
view.DoubleClick += GridMethod.GridDoubleClick;
if (Settings.ServerInfo.IsAzure) {
TotalSize.Visible = DataSize.Visible = LogSize.Visible = DataFreeSize.Visible = LogFreeSize.Visible = false;
view.SortInfo.Clear();
view.SortInfo.Add(new GridColumnSortInfo(DatabaseName, ColumnSortOrder.Ascending));
}
RefreshDatabases();
}
private void ButtonRefreshClick(object sender, EventArgs e) {
RefreshDatabases();
}
public List<string> GetDatabases() {
List<string> dbs = new List<string>();
int[] rows = view.GetSelectedRows();
foreach (int row in rows) {
Database db = (Database)view.GetRow(row);
dbs.Add(db.DatabaseName);
}
return dbs;
}
#region Refresh Databases
private BackgroundWorker _workerScan;
private List<Database> _databases = new List<Database>();
private List<DiskInfo> _disks = new List<DiskInfo>();
private Stopwatch _ts = new Stopwatch();
private void ScanDatabases(object sender, DoWorkEventArgs e) {
using (SqlConnection connection = Connection.Create(Settings.ActiveHost)) {
try {
connection.Open();
_disks = QueryEngine.GetDiskInfo(connection);
_databases = QueryEngine.GetDatabases(connection);
}
catch (Exception ex) {
Output.Current.Add("Refresh failed", ex.Message);
XtraMessageBox.Show(ex.Message.Replace(". ", "." + Environment.NewLine), ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally {
connection.Close();
}
}
}
private void ScanDatabasesFinish(object sender, RunWorkerCompletedEventArgs e) {
Text = _disks.Count > 0
? $"{Resources.DatabaseBoxTitle} {string.Join(" | ", _disks.Select(_ => _.ToString()))}"
: Resources.DatabaseBoxTitle;
grid.DataSource = null;
if (_databases.Count > 0) {
var max = _databases.Max(_ => _.TotalSize);
foreach (var rule in view.FormatRules) {
((FormatConditionRuleDataBar)rule.Rule).Maximum = max;
}
grid.DataSource = _databases;
foreach (string db in Settings.ActiveHost.Databases) {
int index = view.LocateByValue(DatabaseName.FieldName, db);
view.SelectRow(index);
}
}
_ts.Stop();
buttonRefresh.Enabled = true;
Output.Current.Add($"Found {_databases.Count} databases", null, _ts.ElapsedMilliseconds);
}
private void RefreshDatabases() {
if (_workerScan != null && _workerScan.IsBusy) return;
Output.Current.Add("Refresh databases...");
_ts = Stopwatch.StartNew();
buttonOK.Enabled = false;
buttonRefresh.Enabled = false;
TotalSize.Visible = DataSize.Visible = LogSize.Visible = DataFreeSize.Visible = LogFreeSize.Visible = !Settings.ServerInfo.IsAzure;
_workerScan = new ThreadWorker() { WorkerSupportsCancellation = true };
_workerScan.DoWork += ScanDatabases;
_workerScan.RunWorkerCompleted += ScanDatabasesFinish;
_workerScan.RunWorkerAsync();
}
#endregion
#region Override Methods
protected override bool ProcessKeyPreview(ref Message m) {
if ((Keys)m.WParam == Keys.F5) {
RefreshDatabases();
return true;
}
return base.ProcessKeyPreview(ref m);
}
protected override bool ProcessDialogKey(Keys keyData) {
if (keyData == Keys.Escape) {
DialogResult = DialogResult.Cancel;
return true;
}
if (keyData == Keys.Enter && buttonOK.Enabled) {
DialogResult = DialogResult.OK;
return true;
}
return base.ProcessDialogKey(keyData);
}
#endregion
#region Grid Methods
private void GridSelectionChanged(object sender, SelectionChangedEventArgs e) {
buttonOK.Enabled = ((GridView)sender).SelectedRowsCount > 0;
}
#endregion
}
}