using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using Neo.Afx.ComponentModel; using SystemData = System.Data; namespace Neo.Afx.Services.Integration { /// /// A SQL for 's. /// public partial class SqlIntegrationStore : IntegrationStore { /// /// The configuration connection string: Integration /// public const string ConfigConnectionString = "Integration"; const string PrGetDataSource = "Integration.Pr_GetDataSource"; const string PrGetDataSources = "Integration.Pr_GetDataSources"; const string PrGetCacheEnabled = "Integration.Pr_GetCacheEnabled"; SqlConnection _connection; SqlCommand _cacheEnabledCommand; SqlCommand _dataSourcesCommand; SqlCommand _dataSourceCommand; SqlParameter _dataSourceNameParameter; /// /// Initializes a new instance of the class. /// /// The connection string. public SqlIntegrationStore(string connectionString) : base(connectionString) { } #region Open /// /// Establishes a connection to the data store. /// /// Must be invoked before the first call to any Read method. /// /// public override void Open() { _connection = new SqlConnection(ConnectionString); _cacheEnabledCommand = _connection.CreateCommand(); _cacheEnabledCommand.CommandText = PrGetCacheEnabled; _dataSourcesCommand = _connection.CreateCommand(); _dataSourcesCommand.CommandText = PrGetDataSources; _dataSourceCommand = _connection.CreateCommand(); _dataSourceCommand.CommandText = PrGetDataSource; // ReSharper disable RedundantNameQualifier // Needed by Neo.Afx.SqlServer _cacheEnabledCommand.CommandType = SystemData.CommandType.StoredProcedure; _dataSourcesCommand.CommandType = SystemData.CommandType.StoredProcedure; _dataSourceCommand.CommandType = SystemData.CommandType.StoredProcedure; // ReSharper restore RedundantNameQualifier _dataSourceNameParameter = _dataSourceCommand.Parameters.Add("@DataSourceName", SqlDbType.VarChar, 50); _connection.Open(); } #endregion #region Read() /// /// Gets a dictionary of all from the data store. /// /// must be invoked before the first call to this. /// /// /// /// A dictionary of all keyed by name. /// protected override Dictionary Read() { var result = new Dictionary(); using(var reader = _dataSourcesCommand.ExecuteReader()) { while(reader.Read()) { var ds = CreateDataSource(field => GetValue(reader, field)); result.Add(ds.DataSourceName, ds); } } return result; } #endregion #region Read(string dataSourceName) /// /// Reads the from the data store. /// /// must be invoked before the first call to this. /// /// /// The name of the data source to be read. /// /// A if the read was successful; null otherwise. /// protected override DataSource Read(string dataSourceName) { _dataSourceNameParameter.Value = dataSourceName; using(var reader = _dataSourceCommand.ExecuteReader(CommandBehavior.SingleRow)) { if(reader.Read()) { return CreateDataSource(field => GetValue(reader, field)); } } return null; } #endregion #region ReadCacheEnabled() /// /// Reads from the data store whether caching is enabled globally. /// /// must be invoked before the first call to this. /// /// /// /// true if caching is enabled; false otherwise. /// protected override bool ReadCacheEnabled() { return _cacheEnabledCommand.ExecuteScalar().ToBoolean(); } #endregion #region Close /// /// Closes connection to the data store /// /// This should dispose of managed resources e.g. connections that were created when was invoked. /// This method is automatically called when this object is disposed. /// /// public override void Close() { if(_cacheEnabledCommand != null) { _cacheEnabledCommand.Dispose(); _cacheEnabledCommand = null; } if(_dataSourcesCommand != null) { _dataSourcesCommand.Dispose(); _dataSourcesCommand = null; } if(_dataSourceCommand != null) { _dataSourceCommand.Dispose(); _dataSourceCommand = null; } if(_connection != null) { if(_connection.State == ConnectionState.Open) { _connection.Close(); } _connection.Dispose(); _connection = null; } _dataSourceNameParameter = null; } #endregion #region GetValue static string GetValue(IDataRecord record, DataSourceField field) { if(record != null) { switch(field) { case DataSourceField.DataSourceID: return record["DataSourceID"].ToStringSafe(); case DataSourceField.DataSourceName: return record["DataSourceName"].ToStringSafe(); case DataSourceField.CommandType: return record["CommandTypeID"].ToStringSafe(); case DataSourceField.ConnectionString: return record["ConnectionString"].ToStringSafe(); case DataSourceField.ConnectionType: return record["ConnectionTypeID"].ToStringSafe(); case DataSourceField.RequestQuery: return record["RequestQuery"].ToStringSafe(); case DataSourceField.RequestHeaders: return record["RequestHeaders"].ToStringSafe(); case DataSourceField.ResponseXsl: return record["ResponseXsl"].ToStringSafe(); case DataSourceField.EntitySchema: return record["EntitySchema"].ToStringSafe(); case DataSourceField.IsCacheEnabled: return record["IsCacheEnabled"].ToStringSafe(); case DataSourceField.CacheMinutes: return record["CacheMinutes"].ToStringSafe(); case DataSourceField.AuthType: return record["AuthTypeID"].ToStringSafe(); case DataSourceField.AuthDomain: return record["AuthDomain"].ToStringSafe(); case DataSourceField.AuthUser: return record["AuthUser"].ToStringSafe(); case DataSourceField.AuthPassword: return record["AuthPassword"].ToStringSafe(); } } return string.Empty; } #endregion } }