//****************************************************************************************************** // AdoMetadataProvider.cs - Gbtc // // Copyright © 2012, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 09/15/2009 - J. Ritchie Carroll // Generated original version of source code. // 09/17/2009 - Pinal C. Patel // Renamed ConnectString to ConnectionString. // 12/11/2009 - Pinal C. Patel // Disabled the encryption of DataProviderString when persisted to the config file. // 02/03/2010 - Pinal C. Patel // Disabled the encryption of ConnectionString when persisted to the config file. // 10/11/2010 - Mihir Brahmbhatt // Updated header and license agreement. // 12/14/2012 - Starlynn Danyelle Gilliam // Modifeid Header. // //****************************************************************************************************** using System; using System.Collections.Generic; using System.Data; using System.Reflection; using GSF.Configuration; using GSF.Data; using GSF.IO; namespace GSF.Historian.MetadataProviders { /// /// Represents a provider of data to a from any ADO.NET based data store. /// /// public class AdoMetadataProvider : MetadataProviderBase { #region [ Members ] // Fields private string m_connectionString; private string m_dataProviderString; private string m_selectString; #endregion #region [ Constructors ] /// /// Initializes a new instance of the class. /// public AdoMetadataProvider() { m_connectionString = string.Empty; m_dataProviderString = string.Empty; m_selectString = string.Empty; } #endregion #region [ Properties ] /// /// Gets or sets the connection string for connecting to the ADO.NET based data store of metadata. /// public string ConnectionString { get { return m_connectionString; } set { m_connectionString = value; } } /// /// Gets or sets the ADO.NET data provider assembly type creation string. /// /// /// Expected keys: AssemblyName;ConnectionType
/// Examples: /// /// /// Database Connection Type /// Example ADO.NET Data Provider String /// /// /// SQL Server /// AssemblyName={System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089};ConnectionType=System.Data.SqlClient.SqlConnection /// /// /// MySQL /// AssemblyName={MySql.Data, Version=5.2.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d};ConnectionType=MySql.Data.MySqlClient.MySqlConnection /// /// /// Oracle /// AssemblyName={System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089};ConnectionType=System.Data.OracleClient.OracleConnection /// /// /// OleDb /// AssemblyName={System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089};ConnectionType=System.Data.OleDb.OleDbConnection /// /// /// ODBC /// AssemblyName={System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089};ConnectionType=System.Data.Odbc.OdbcConnection /// /// ///
public string DataProviderString { get { return m_dataProviderString; } set { m_dataProviderString = value; } } /// /// Gets or sets the SELECT statement for retrieving metadata from the ADO.NET based data store. /// public string SelectString { get { return m_selectString; } set { m_selectString = value; } } #endregion #region [ Methods ] /// /// Saves settings to the config file if the property is set to true. /// public override void SaveSettings() { base.SaveSettings(); if (PersistSettings) { // Save settings under the specified category. ConfigurationFile config = ConfigurationFile.Current; CategorizedSettingsElementCollection settings = config.Settings[SettingsCategory]; settings["ConnectionString", true].Update(m_connectionString); settings["DataProviderString", true].Update(m_dataProviderString); settings["SelectString", true].Update(m_selectString); config.Save(); } } /// /// Loads saved settings from the config file if the property is set to true. /// public override void LoadSettings() { base.LoadSettings(); if (PersistSettings) { // Load settings from the specified category. ConfigurationFile config = ConfigurationFile.Current; CategorizedSettingsElementCollection settings = config.Settings[SettingsCategory]; settings.Add("ConnectionString", "Eval(SystemSettings.ConnectionString)", "Connection string for connecting to the ADO.NET based data store of metadata."); settings.Add("DataProviderString", "Eval(SystemSettings.DataProviderString)", "The ADO.NET data provider assembly type creation string used to create a connection to the data store of metadata."); settings.Add("SelectString", m_selectString, "SELECT statement for retrieving metadata from the ADO.NET based data store."); DataProviderString = settings["DataProviderString"].ValueAs(m_dataProviderString); SelectString = settings["SelectString"].ValueAs(m_selectString); // Load the connection string from the specified category. string connectionString = settings["ConnectionString"].ValueAs(m_connectionString); Dictionary connectionSettings = connectionString.ParseKeyValuePairs(); string connectionSetting; if (connectionSettings.TryGetValue("Provider", out connectionSetting)) { // Check if provider is for Access if (connectionSetting.StartsWith("Microsoft.Jet.OLEDB", StringComparison.OrdinalIgnoreCase)) { // Make sure path to Access database is fully qualified if (connectionSettings.TryGetValue("Data Source", out connectionSetting)) { connectionSettings["Data Source"] = FilePath.GetAbsolutePath(connectionSetting); connectionString = connectionSettings.JoinKeyValuePairs(); } } } ConnectionString = connectionString; } } /// /// Refreshes the from an ADO.NET based data store. /// /// or is set to a null or empty string. protected override void RefreshMetadata() { if (string.IsNullOrEmpty(m_connectionString)) throw new ArgumentNullException("ConnectionString"); if (string.IsNullOrEmpty(m_selectString)) throw new ArgumentNullException("SelectString"); // Attempt to load configuration from an ADO.NET database connection IDbConnection connection = null; Dictionary settings; string assemblyName, connectionTypeName, adapterTypeName; Assembly assembly; Type connectionType, adapterType; try { settings = m_dataProviderString.ParseKeyValuePairs(); assemblyName = settings["AssemblyName"].ToNonNullString(); connectionTypeName = settings["ConnectionType"].ToNonNullString(); adapterTypeName = settings["AdapterType"].ToNonNullString(); if (string.IsNullOrEmpty(connectionTypeName)) throw new InvalidOperationException("Database connection type was not defined"); if (string.IsNullOrEmpty(adapterTypeName)) throw new InvalidOperationException("Database adapter type was not defined"); assembly = Assembly.Load(new AssemblyName(assemblyName)); connectionType = assembly.GetType(connectionTypeName); adapterType = assembly.GetType(adapterTypeName); // Open ADO.NET provider connection connection = (IDbConnection)Activator.CreateInstance(connectionType); connection.ConnectionString = m_connectionString; connection.Open(); // Update existing metadata MetadataUpdater metadataUpdater = new MetadataUpdater(Metadata); metadataUpdater.UpdateMetadata(connection.ExecuteReader(m_selectString)); } finally { if (connection != null) connection.Dispose(); } } #endregion } }