#region License // Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved. // This source code is made available under the terms of the GNU Lesser General Public License v3.0 // https://github.com/paulkohler/minisqlquery/blob/master/LICENSE #endregion using System; using System.Collections.Specialized; using System.Data; using System.Data.Common; using MiniSqlQuery.Core; using MiniSqlQuery.Properties; namespace MiniSqlQuery { /// The application settings. public class ApplicationSettings : IApplicationSettings { /// The _connection definition. private DbConnectionDefinition _connectionDefinition; /// The _db connection. private DbConnection _dbConnection; /// The _db provider factory. private DbProviderFactory _dbProviderFactory; /// The _definition list. private DbConnectionDefinitionList _definitionList; /// The _untitled document counter. private int _untitledDocumentCounter; /// Initializes a new instance of the class. public ApplicationSettings() { _definitionList = new DbConnectionDefinitionList(); } /// The connection definitions changed. public event EventHandler ConnectionDefinitionsChanged; /// The database connection reset. public event EventHandler DatabaseConnectionReset; /// Gets Connection. public DbConnection Connection { get { if (_dbConnection == null) { if (ProviderFactory != null) { _dbConnection = ProviderFactory.CreateConnection(); } if (_dbConnection != null) { _dbConnection.ConnectionString = _connectionDefinition.ConnectionString; } } return _dbConnection; } } /// Gets or sets ConnectionDefinition. public DbConnectionDefinition ConnectionDefinition { get { return _connectionDefinition; } set { if (_connectionDefinition != value) { _connectionDefinition = value; ResetConnection(); } } } /// Gets or sets DateTimeFormat. public string DateTimeFormat { get { return Settings.Default.DateTimeFormat; } set { if (Settings.Default.DateTimeFormat != value) { Settings.Default.DateTimeFormat = value; Settings.Default.Save(); } } } /// Gets or sets DefaultConnectionDefinitionFilename. public string DefaultConnectionDefinitionFilename { get { return Settings.Default.DefaultConnectionDefinitionFilename; } set { if (Settings.Default.DefaultConnectionDefinitionFilename != value) { Settings.Default.DefaultConnectionDefinitionFilename = value; Settings.Default.Save(); } } } /// Gets DefaultFileFilter. public string DefaultFileFilter { get { return Settings.Default.FileDialogFilter; } } /// Gets or sets a value indicating whether EnableQueryBatching. public bool EnableQueryBatching { get { return Settings.Default.EnableQueryBatching; } set { if (Settings.Default.EnableQueryBatching != value) { Settings.Default.EnableQueryBatching = value; Settings.Default.Save(); } } } /// /// Gets or sets a value indicating the command timeout. /// /// The command timeout. public int CommandTimeout { get { return Settings.Default.CommandTimeout; } set { if (Settings.Default.CommandTimeout != value) { Settings.Default.CommandTimeout = value; Settings.Default.Save(); } } } /// Gets or sets a value indicating whether LoadExternalPlugins. public bool LoadExternalPlugins { get { return Settings.Default.LoadExternalPlugins; } set { if (Settings.Default.LoadExternalPlugins != value) { Settings.Default.LoadExternalPlugins = value; Settings.Default.Save(); } } } /// Gets or sets NullText. public string NullText { get { return Settings.Default.NullText; } set { if (Settings.Default.NullText != value) { Settings.Default.NullText = value; Settings.Default.Save(); } } } /// Gets or sets PlugInFileFilter. public string PlugInFileFilter { get { return Settings.Default.PlugInFileFilter; } set { if (Settings.Default.PlugInFileFilter != value) { Settings.Default.PlugInFileFilter = value; Settings.Default.Save(); } } } public StringCollection MostRecentFiles { get { return Settings.Default.MostRecentFiles; } set { if (Settings.Default.MostRecentFiles != value) { Settings.Default.MostRecentFiles = value; Settings.Default.Save(); } } } public bool IncludeReadOnlyColumnsInExport { get { return Settings.Default.IncludeReadOnlyColumnsInExport; } set { if (Settings.Default.IncludeReadOnlyColumnsInExport != value) { Settings.Default.IncludeReadOnlyColumnsInExport = value; Settings.Default.Save(); } } } /// Gets ProviderFactory. public DbProviderFactory ProviderFactory { get { if (_dbProviderFactory == null && _connectionDefinition != null) { _dbProviderFactory = DbProviderFactories.GetFactory(_connectionDefinition.ProviderName); } return _dbProviderFactory; } } /// The close connection. public void CloseConnection() { if (_dbConnection != null && (_dbConnection.State != ConnectionState.Closed && _dbConnection.State != ConnectionState.Broken)) { _dbConnection.Close(); } } /// The get connection definitions. /// public DbConnectionDefinitionList GetConnectionDefinitions() { return _definitionList; } /// The get open connection. /// public DbConnection GetOpenConnection() { DbConnection conn = Connection; if (conn.State != ConnectionState.Open) { if (_connectionDefinition != null && String.IsNullOrEmpty(conn.ConnectionString)) { conn.ConnectionString = _connectionDefinition.ConnectionString; } conn.Open(); } return conn; } /// The get untitled document counter. /// The get untitled document counter. public int GetUntitledDocumentCounter() { return ++_untitledDocumentCounter; } /// The reset connection. public void ResetConnection() { if (_dbConnection != null) { _dbConnection.Dispose(); } _dbProviderFactory = null; _dbConnection = null; OnDatabaseConnectionReset(EventArgs.Empty); } /// The set connection definitions. /// The definition list. public void SetConnectionDefinitions(DbConnectionDefinitionList definitionList) { _definitionList = definitionList; OnPastConnectionStringsChanged(EventArgs.Empty); } /// The on database connection reset. /// The e. protected void OnDatabaseConnectionReset(EventArgs e) { if (DatabaseConnectionReset != null) { DatabaseConnectionReset(this, e); } } /// The on past connection strings changed. /// The e. protected void OnPastConnectionStringsChanged(EventArgs e) { if (ConnectionDefinitionsChanged != null) { ConnectionDefinitionsChanged(this, e); } } } }