Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toggle system databases visibility #13

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,12 @@ internal void Execute(object sender, EventArgs e)
var connectionDatabases = GetDatabasesFromConnection(connection.ConnectionString);
if (!connectionDatabases.Any())
{
// Show a message box to prove we were here
_messageManager.ShowMessageBox(this.package, title, "The connection has no available databases");
return;
}

serverInformation.ServerName = connection.ServerName;
serverInformation.Databases = connectionDatabases.Select(x => new CheckboxItem { Name = x });
serverInformation.Databases = connectionDatabases.Select(x => new CheckboxItem { Name = x }).ToArray();
}
catch(OnlyOneObjectExplorerNodeAllowedException)
{
Expand Down
11 changes: 11 additions & 0 deletions src/SSMSTools/Models/CheckboxItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace SSMSTools.Models
public class CheckboxItem : INotifyPropertyChanged
{
private bool _isSelected;
private bool _isVisible;

public string Name { get; set; }

Expand All @@ -18,6 +19,16 @@ public bool IsSelected
}
}

public bool IsVisible
{
get => _isVisible;
set
{
_isVisible = value;
OnPropertyChanged(nameof(IsVisible));
}
}

public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
Expand Down
4 changes: 2 additions & 2 deletions src/SSMSTools/Models/ConnectedServerInformation.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;

namespace SSMSTools.Models
{
public class ConnectedServerInformation
{
public string ServerName { get; set; }
public IEnumerable<CheckboxItem> Databases { get; set; } = Enumerable.Empty<CheckboxItem>();
public ICollection<CheckboxItem> Databases { get; set; } = new Collection<CheckboxItem>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SSMSTools.Windows.MultiDbQueryRunner" Height="496.054" Width="420.811"
xmlns:local="clr-namespace:SSMSTools.Windows.MultiDbQueryRunner" Height="540.721" Width="420.811"
>
<Grid Margin="0,0,0,-188">
<StackPanel Margin="10">
Expand All @@ -26,6 +26,11 @@
Databases:
</Label>
<!-- Select All Checkbox -->
<CheckBox Content="Show system databases"
IsChecked="{Binding IsShowSystemDatabasesSelected, Mode=TwoWay}"
FontSize="12"
Margin="0,0,0,10"/>
<!-- Select All Checkbox -->
<CheckBox Content="Select All"
IsChecked="{Binding IsAllSelected, Mode=TwoWay}"
FontSize="12"
Expand All @@ -37,7 +42,18 @@
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}"
IsChecked="{Binding IsSelected, Mode=TwoWay}"/>
IsChecked="{Binding IsSelected, Mode=TwoWay}">
<CheckBox.Style>
<Style TargetType="CheckBox">
<Setter Property="Visibility" Value="Visible"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsVisible}" Value="False">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using SSMSTools.Models;
using SSMSTools.Windows.Interfaces;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
Expand All @@ -17,11 +18,37 @@ public partial class MultiDbQueryRunnerWindow : System.Windows.Window, INotifyPr
public ObservableCollection<CheckboxItem> Databases { get; private set; }
public string ServerName { get; private set; }

private bool _isShowSystemDatabasesSelected;
private bool _isAllSelected;
private bool _isUpdating;
private string _queryContent;
private readonly DTE2 _dte;
private readonly IMessageManager _messageManager;
private readonly ISet<string> _systemDatabases = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"master",
"tempdb",
"model",
"msdb"
};

public bool IsShowSystemDatabasesSelected
{
get => _isShowSystemDatabasesSelected;
set
{
if (_isShowSystemDatabasesSelected != value)
{
_isShowSystemDatabasesSelected = value;
OnPropertyChanged(nameof(IsShowSystemDatabasesSelected));

if (!_isUpdating)
{
UpdateShowSystemDatabases(value);
}
}
}
}

public bool IsAllSelected
{
Expand Down Expand Up @@ -72,6 +99,8 @@ public void SetServerInformation(ConnectedServerInformation serverInformation)
Databases = new ObservableCollection<CheckboxItem>(serverInformation.Databases);
foreach (var item in serverInformation.Databases)
{
item.IsVisible = _systemDatabases.Contains(item.Name) ?
_isShowSystemDatabasesSelected : true;
item.PropertyChanged += Item_PropertyChanged;
}

Expand All @@ -93,6 +122,19 @@ private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e)
}
}

private void UpdateShowSystemDatabases(bool systemDatabasesVisible)
{
_isUpdating = true;
foreach (var item in Databases)
{
if (_systemDatabases.Contains(item.Name))
{
item.IsVisible = systemDatabasesVisible;
}
}
_isUpdating = false;
}

private void UpdateAllItemsSelection(bool isSelected)
{
// Set the _isUpdating flag to true to prevent recursion
Expand All @@ -113,7 +155,7 @@ private void ExecuteButton_Click(object sender, RoutedEventArgs e)
var content = new StringBuilder();
foreach (var database in Databases)
{
if (database.IsSelected)
if (database.IsSelected && database.IsVisible)
{
content.Append($"USE {database.Name}\n");
content.Append($"Print 'Running query in {database.Name}'\n");
Expand Down