forked from tloten/bulk-query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
248 lines (215 loc) · 8.56 KB
/
MainWindow.xaml.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
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml;
using BulkQuery.Properties;
namespace BulkQuery
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private readonly List<TreeViewModel<DatabaseTreeNode>> databaseTreeModel = new List<TreeViewModel<DatabaseTreeNode>>();
private readonly string[] systemDatabases = {"master", "model", "msdb", "tempdb"};
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (Settings.Default.NeedsUpgrade)
{
Settings.Default.Upgrade();
Settings.Default.NeedsUpgrade = false;
Settings.Default.Save();
}
if (Settings.Default.ServersList?.Servers == null)
{
Settings.Default.ServersList = new ServersList();
Settings.Default.Save();
}
foreach (var server in GetSavedServers())
{
AddNodesForServer(server);
}
DatabasesTreeView.ItemsSource = databaseTreeModel;
}
private List<ServerDefinition> GetSavedServers()
{
return Settings.Default.ServersList?.Servers ?? new List<ServerDefinition>();
}
private void SaveServers(List<ServerDefinition> servers)
{
Settings.Default.ServersList.Servers = servers;
Settings.Default.Save();
}
private void AddNodesForServer(ServerDefinition server)
{
var serverNode = new DatabaseTreeNode
{
ServerDefinition = server,
IsServerNode = true
};
try
{
var databases = QueryRunner.GetDatabasesForServer(server);
var serverNodeViewModel = new TreeViewModel<DatabaseTreeNode>(server.DisplayName, serverNode);
databaseTreeModel.Add(serverNodeViewModel);
foreach (var db in databases)
{
if (Settings.Default.HideSystemDatabases && systemDatabases.Contains(db.DatabaseName))
{
continue;
}
var dbNode = new DatabaseTreeNode
{
IsServerNode = false,
DatabaseDefinition = db
};
var dbNodeViewModel = new TreeViewModel<DatabaseTreeNode>(db.DatabaseName, dbNode);
serverNodeViewModel.Children.Add(dbNodeViewModel);
}
}
catch(Exception)
{
var serverNodeViewModel = new TreeViewModel<DatabaseTreeNode>(server.DisplayName + " (Connection Failed)", serverNode);
databaseTreeModel.Add(serverNodeViewModel);
}
DatabasesTreeView.Items.Refresh();
}
private void MenuItem_AddServer_OnClick(object sender, RoutedEventArgs e)
{
AddServerDialog dialog = new AddServerDialog();
if (dialog.ShowDialog() == true)
{
var servers = GetSavedServers();
if (servers.Any(s => s.DisplayName == dialog.ServerDisplayName))
{
MessageBox.Show("A server already exists with this name.\nChoose a different name and try again.", "", MessageBoxButton.OK, MessageBoxImage.Exclamation);
return;
}
var server = new ServerDefinition(dialog.ServerDisplayName, dialog.ServerConnectionString);
AddNodesForServer(server);
servers.Add(server);
SaveServers(servers);
}
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Settings.Default.Save();
}
private static DependencyObject GetDependencyObjectFromVisualTree(DependencyObject startObject, Type type)
{
var parent = startObject;
while (parent != null)
{
if (type.IsInstanceOfType(parent))
break;
parent = VisualTreeHelper.GetParent(parent);
}
return parent;
}
private void DatabasesTreeView_PreviewMouseButton(object sender, MouseButtonEventArgs e)
{
DependencyObject obj = e.OriginalSource as DependencyObject;
TreeViewItem item = GetDependencyObjectFromVisualTree(obj, typeof(TreeViewItem)) as TreeViewItem;
var selectedElement = item.Header as TreeViewModel<DatabaseTreeNode>;
ContextMenu menu = new ContextMenu();
if (selectedElement.Value.IsServerNode)
{
var menuItem = new MenuItem
{
Header = "Refresh Databases"
};
menuItem.Click += (o, args) =>
{
RemoveServer(selectedElement.Value.ServerDefinition);
AddNodesForServer(selectedElement.Value.ServerDefinition);
};
menu.Items.Add(menuItem);
menuItem = new MenuItem
{
Header = "Remove Server"
};
menuItem.Click += (o, args) =>
{
RemoveServer(selectedElement.Value.ServerDefinition);
};
menu.Items.Add(menuItem);
}
(sender as TreeViewItem).ContextMenu = menu;
}
private void RemoveServer(ServerDefinition server)
{
var servers = GetSavedServers();
servers.Remove(server);
SaveServers(servers);
var nodeToRemove = databaseTreeModel.FirstOrDefault(s => s.Value.ServerDefinition == server);
databaseTreeModel.Remove(nodeToRemove);
DatabasesTreeView.Items.Refresh();
}
private IEnumerable<DatabaseDefinition> GetSelectedDatabases()
{
return
databaseTreeModel
.SelectMany(serverNode => serverNode.Children.Where(dbNode => dbNode.IsChecked ?? true))
.Select(treeNode => treeNode.Value.DatabaseDefinition);
}
private void ButtonQuery_OnClick(object sender, RoutedEventArgs e)
{
RunQuery();
}
private void RunQuery()
{
var query = QueryTextBox.Text;
var databases = GetSelectedDatabases().ToList();
var timer = Stopwatch.StartNew();
var result = QueryRunner.BulkQuery(databases, query).Result;
Debug.WriteLine("Total query time: " + timer.ElapsedMilliseconds);
if (result.Messages.Count > 0)
{
TextBoxMessages.Text = string.Join(Environment.NewLine, result.Messages);
TextBoxMessages.Visibility = Visibility.Visible;
}
else
{
TextBoxMessages.Text = string.Empty;
TextBoxMessages.Visibility = Visibility.Collapsed;
}
if (result.ResultTable != null)
ResultDataGrid.ItemsSource = result.ResultTable.DefaultView;
}
private void MainGrid_OnDragDelta(object sender, DragDeltaEventArgs e)
{
MainGrid.ColumnDefinitions[0].Width = new GridLength(MainGrid.ColumnDefinitions[0].ActualWidth + e.HorizontalChange);
}
private void RightHandGrid_OnDragDelta(object sender, DragDeltaEventArgs e)
{
RightHandGrid.RowDefinitions[0].Height = new GridLength(RightHandGrid.RowDefinitions[0].ActualHeight + e.VerticalChange);
}
private void MenuItem_Github_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start("https://github.com/tloten/bulk-query");
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.F5)
{
RunQuery();
}
}
}
public class DatabaseTreeNode
{
public bool IsServerNode { get; set; }
public ServerDefinition ServerDefinition { get; set; }
public DatabaseDefinition DatabaseDefinition { get; set; }
}
}