Skip to content

Commit

Permalink
Global setting for command timeouts.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmp135 committed Feb 24, 2018
1 parent 0190ff6 commit c23e688
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 75 deletions.
11 changes: 11 additions & 0 deletions RPForEachDB/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<configuration>
<configSections>


<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="RPForEachDB.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<connectionStrings />
<startup>
Expand All @@ -20,4 +24,11 @@
</roleManager>
</system.web>

<userSettings>
<RPForEachDB.Properties.Settings>
<setting name="CommandTimeout" serializeAs="String">
<value>30</value>
</setting>
</RPForEachDB.Properties.Settings>
</userSettings>
</configuration>
36 changes: 36 additions & 0 deletions RPForEachDB/AppState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using RPForEachDB.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RPForEachDB
{
public interface IAppState
{
IEnumerable<IServerModel> Servers { get; set; }
int CommandTimeout { get; set; }
void Save();
}
public class AppState: IAppState
{
public IEnumerable<IServerModel> Servers { get; set; }
public int CommandTimeout
{
get => Settings.Default.CommandTimeout;
set => Settings.Default.CommandTimeout = value;
}

public AppState()
{
Servers = Settings.Default.Servers ?? new IServerModel[0];
}

public void Save()
{
Settings.Default.Servers = Servers.Cast<ServerModel>().ToArray();
Settings.Default.Save();
}
}
}
36 changes: 0 additions & 36 deletions RPForEachDB/ConnectionManager.xaml

This file was deleted.

4 changes: 2 additions & 2 deletions RPForEachDB/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Margin="0,0,5,0" Click="OnManageServersBtnClick">Manage Servers</Button>
<Button Grid.Column="0" Margin="0,0,5,0" Padding="5,0,5,0" Click="OnManageServersBtnClick" Content="Settings"/>
<ComboBox ItemsSource="{Binding Servers}" SelectedValue="{Binding CurrentServer}" DisplayMemberPath="Name" Grid.Column="1" Margin="0,0,5,0" VerticalAlignment="Top" HorizontalContentAlignment="Stretch" SelectionChanged="ServerComboOnChange" Height="27" />
<Button Grid.Column="2" IsEnabled="{Binding IsGetDatabaseEnabled}" Click="OnGetDatabasesBtnClick">Get Databases</Button>
<Button Grid.Column="2" Padding="5,0,5,0" IsEnabled="{Binding IsGetDatabaseEnabled}" Click="OnGetDatabasesBtnClick">Get Databases</Button>
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
Expand Down
9 changes: 4 additions & 5 deletions RPForEachDB/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
//using Dapper;
using System.Data.SqlClient;
using System.ComponentModel;
using System.IO;
Expand All @@ -16,7 +15,7 @@ namespace RPForEachDB
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private readonly StateManager _appState;
private readonly IAppState _appState;
public ObservableCollection<DatabaseGridItem> Databases { get; set; }
public ObservableCollection<IServerModel> Servers { get; set; }
private IServerModel _currentServer { get; set; }
Expand Down Expand Up @@ -76,8 +75,8 @@ private void NotifyPropertyChanged(string propertyName)

public MainWindow()
{
_sqlTasks = new SQLTasks();
_appState = new StateManager();
_appState = new AppState();
_sqlTasks = new SQLTasks(_appState);
Servers = new ObservableCollection<IServerModel>(_appState.Servers);
Databases = new ObservableCollection<DatabaseGridItem>();
DataContext = this;
Expand Down Expand Up @@ -186,7 +185,7 @@ protected override void OnClosing(CancelEventArgs e)

private void OnManageServersBtnClick(object sender, RoutedEventArgs e)
{
new ConnectionManager(_appState)
new SettingsWindow(_appState)
{
Owner = this
}.ShowDialog();
Expand Down
12 changes: 12 additions & 0 deletions RPForEachDB/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions RPForEachDB/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
<Setting Name="Servers" Type="RPForEachDB.ServerModel[]" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="CommandTimeout" Type="System.Int32" Scope="User">
<Value Profile="(Default)">30</Value>
</Setting>
</Settings>
</SettingsFile>
8 changes: 4 additions & 4 deletions RPForEachDB/RPForEachDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
<Compile Include="ServerModel.cs" />
<Compile Include="Settings.cs" />
<Compile Include="SQLTasks.cs" />
<Compile Include="StateManager.cs" />
<Page Include="ConnectionManager.xaml">
<Compile Include="AppState.cs" />
<Page Include="SettingsWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
Expand All @@ -88,8 +88,8 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="ConnectionFactory.cs" />
<Compile Include="ConnectionManager.xaml.cs">
<DependentUpon>ConnectionManager.xaml</DependentUpon>
<Compile Include="SettingsWindow.xaml.cs">
<DependentUpon>SettingsWindow.xaml</DependentUpon>
</Compile>
<Compile Include="DatabaseGridItem.cs" />
<Compile Include="MainWindow.xaml.cs">
Expand Down
9 changes: 9 additions & 0 deletions RPForEachDB/SQLTasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ namespace RPForEachDB
{
public class SQLTasks
{
private readonly IAppState _appState;

public SQLTasks(IAppState appState)
{
_appState = appState;
}

public IEnumerable<string> GetAllDatabases(IDbConnection connection)
{
var command = connection.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = "SELECT Name FROM master.dbo.sysdatabases WHERE DATABASEPROPERTYEX(Name, 'Status') = 'ONLINE'";
command.CommandTimeout = _appState.CommandTimeout;
using (var reader = command.ExecuteReader())
{
while (reader.Read())
Expand All @@ -26,6 +34,7 @@ public void Execute(IDbConnection connection, string commandText)
var command = connection.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = commandText;
command.CommandTimeout = _appState.CommandTimeout;
command.ExecuteNonQuery();
}
}
Expand Down
53 changes: 53 additions & 0 deletions RPForEachDB/SettingsWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<Window x:Class="RPForEachDB.SettingsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
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:RPForEachDB"
mc:Ignorable="d"
Title="Settings" Height="330" Width="400" ResizeMode="NoResize" WindowStartupLocation="CenterOwner">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TabControl Grid.Column="0">
<TabItem Header="Servers" Margin="0,-2,-4,0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox Grid.Column="0" HorizontalAlignment="Left" ScrollViewer.HorizontalScrollBarVisibility="Hidden" BorderThickness="0" VerticalAlignment="Top" Width="100" Height="230" ItemsSource="{Binding Servers}" DisplayMemberPath="Name" SelectedValue="{Binding CurrentModel}" SelectedIndex="{Binding CurrentModelIndex, Mode=OneWay}" />
<Label Content="Server" Grid.Column="1" HorizontalAlignment="Left" Margin="10,47,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.547,0.615"/>
<TextBox Grid.Column="1" Height="23" Margin="85,19,10,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding CurrentModel.Name, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Grid.Column="1" Height="23" Margin="85,50,10,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding CurrentModel.Server, UpdateSourceTrigger=PropertyChanged}"/>
<RadioButton GroupName="SecurityMode" IsChecked="{Binding IsWindowsAuthentication, Mode=TwoWay}" Content="Windows" Grid.Column="1" HorizontalAlignment="Left" Margin="85,110,0,0" VerticalAlignment="Top" Checked="OnSecurityModeRadioClick"/>
<RadioButton Name="SQLAuthenticationRadio" GroupName="SecurityMode" IsChecked="{Binding IsSQLAuthentication, Mode=TwoWay}" Content="SQL Server" Grid.Column="1" HorizontalAlignment="Left" Margin="85,130,0,0" VerticalAlignment="Top" Checked="OnSecurityModeRadioClick"/>
<Grid Name="CredentialsGrid" Grid.Column="1" Margin="10,150,10,35">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="115*"/>
</Grid.ColumnDefinitions>
<TextBox Height="23" Margin="68,5,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding CurrentModel.Username, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Name="PasswordField" Height="23" Margin="68,31,0,0" VerticalAlignment="Top" Text="{Binding CurrentModel.Password, UpdateSourceTrigger=PropertyChanged}"/>
<Label Content="Username" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.ColumnSpan="2"/>
<Label Content="Password" HorizontalAlignment="Left" Margin="0,28,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.517,1.559" Width="63" Grid.ColumnSpan="2"/>
<Label Content="Passwords are stored in plain text." TextElement.Foreground="Red" Margin="41,54,0,0" HorizontalContentAlignment="Right" Height="26" VerticalAlignment="Top"/>
</Grid>
<Label Content="Name" Grid.Column="1" HorizontalAlignment="Left" Margin="10,16,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.547,0.615"/>
<Button IsEnabled="{Binding IsDeleteEnabled}" Content="Delete" HorizontalAlignment="Left" Margin="85,236,0,0" Width="80" Grid.Column="1" RenderTransformOrigin="0.468,1.348" Click="OnDeleteBtnClick" Height="20" VerticalAlignment="Top"/>
<Button Content="Add New" HorizontalAlignment="Right" Margin="0,236,10,0" Width="80" Click="OnAddNewBtnClick" Height="20" VerticalAlignment="Top"/>
<Label Content="Security Mode" Grid.Column="1" HorizontalAlignment="Left" Margin="85,78,0,0" VerticalAlignment="Top"/>
<Button Content="Close" Margin="204,236,10,0" Grid.Column="1" RenderTransformOrigin="0.468,1.348" Click="OnCloseBtnClick" Height="20" VerticalAlignment="Top"/>

</Grid>
</TabItem>
<TabItem Header="Global">
<Grid>
<TextBox HorizontalAlignment="Left" Height="23" PreviewTextInput="TextBox_PreviewTextInput" PreviewKeyDown="TextBox_PreviewKeyDown" PreviewMouseWheel="TextBox_PreviewMouseWheel" Margin="127,13,0,0" TextWrapping="Wrap" Text="{Binding CommandTimeout, Mode=TwoWay}" VerticalAlignment="Top" Width="251"/>
<Label Content="Command Timeout" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
<Button Content="Close" Margin="304,236,10,0" Click="OnCloseBtnClick" Height="20" VerticalAlignment="Top"/>
</Grid>
</TabItem>
</TabControl>
</Grid>
</Window>
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Security;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
Expand All @@ -17,9 +19,9 @@

namespace RPForEachDB
{
public partial class ConnectionManager : Window, INotifyPropertyChanged
public partial class SettingsWindow : Window, INotifyPropertyChanged
{
private readonly StateManager appState;
private readonly IAppState appState;

public event PropertyChangedEventHandler PropertyChanged;

Expand Down Expand Up @@ -55,12 +57,22 @@ public bool IsDeleteEnabled
}
}

public int CommandTimeout
{
get => appState.CommandTimeout;
set
{
appState.CommandTimeout = value;
NotifyPropertyChanged("CommandTimeout");
}
}

public int CurrentModelIndex
{
get => Servers.IndexOf(CurrentModel);
}

public ConnectionManager(StateManager appState)
public SettingsWindow(IAppState appState)
{
this.appState = appState;
Servers = new ObservableCollection<IServerModel>(appState.Servers);
Expand Down Expand Up @@ -147,5 +159,22 @@ protected override void OnClosed(EventArgs e)
base.OnClosed(e);
appState.Save();
}

private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
e.Handled = !int.TryParse(e.Text, out var result);
}

private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
e.Handled = e.Key == Key.Space;
}

private void TextBox_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
var source = (TextBox)sender;
var newValue = int.Parse(source.Text) + (e.Delta < 0 ? -1 : 1);
source.Text = Math.Max(newValue, 0).ToString();
}
}
}
25 changes: 0 additions & 25 deletions RPForEachDB/StateManager.cs

This file was deleted.

0 comments on commit c23e688

Please sign in to comment.