From 8c769636d6bd21787276528076d4c8a51c6333c3 Mon Sep 17 00:00:00 2001 From: Lawrence LCI Date: Wed, 22 Sep 2021 15:13:33 -0700 Subject: [PATCH 1/2] Move the dotnet version of DbMetaDataFactory to the shared src and update reference in the netfx project --- .../src/Microsoft.Data.SqlClient.csproj | 6 +- .../netfx/src/Microsoft.Data.SqlClient.csproj | 6 +- .../Data/ProviderBase/DbMetaDataFactory.cs | 614 ------------------ .../Data/ProviderBase/DbMetaDataFactory.cs | 0 4 files changed, 7 insertions(+), 619 deletions(-) delete mode 100644 src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/ProviderBase/DbMetaDataFactory.cs rename src/Microsoft.Data.SqlClient/{netcore/src/Common => }/src/Microsoft/Data/ProviderBase/DbMetaDataFactory.cs (100%) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj index d918134b15..5aae81ac8e 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj @@ -73,6 +73,9 @@ Microsoft\Data\ProviderBase\DbConnectionPoolAuthenticationContextKey.cs + + Common\Microsoft\Data\ProviderBase\DbMetaDataFactory.cs + Microsoft\Data\ProviderBase\FieldNameLookup.cs @@ -450,9 +453,6 @@ Common\Microsoft\Data\ProviderBase\DbReferenceCollection.cs - - Common\Microsoft\Data\ProviderBase\DbMetaDataFactory.cs - Common\Microsoft\Data\ProviderBase\DbConnectionClosed.cs diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj index f54e7630c6..fc5f17cce9 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj @@ -131,6 +131,9 @@ Microsoft\Data\ProviderBase\DbConnectionPoolAuthenticationContextKey.cs + + Microsoft\Data\ProviderBase\DbMetaDataFactory.cs + Microsoft\Data\ProviderBase\FieldNameLookup.cs @@ -430,7 +433,7 @@ - + @@ -525,7 +528,6 @@ - diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/ProviderBase/DbMetaDataFactory.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/ProviderBase/DbMetaDataFactory.cs deleted file mode 100644 index bea2e6cae5..0000000000 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/ProviderBase/DbMetaDataFactory.cs +++ /dev/null @@ -1,614 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -namespace Microsoft.Data.ProviderBase -{ - - using System; - using System.Data; - using System.Data.Common; - using System.Diagnostics; - using System.Globalization; - using System.IO; - using System.Xml; - using Microsoft.Data.Common; - - internal class DbMetaDataFactory - { // V1.2.3300 - - private DataSet _metaDataCollectionsDataSet; - private string _normalizedServerVersion; - private string _serverVersionString; - // well known column names - private const string _collectionName = "CollectionName"; - private const string _populationMechanism = "PopulationMechanism"; - private const string _populationString = "PopulationString"; - private const string _maximumVersion = "MaximumVersion"; - private const string _minimumVersion = "MinimumVersion"; - private const string _dataSourceProductVersionNormalized = "DataSourceProductVersionNormalized"; - private const string _dataSourceProductVersion = "DataSourceProductVersion"; - private const string _restrictionDefault = "RestrictionDefault"; - private const string _restrictionNumber = "RestrictionNumber"; - private const string _numberOfRestrictions = "NumberOfRestrictions"; - private const string _restrictionName = "RestrictionName"; - private const string _parameterName = "ParameterName"; - - // population mechanisms - private const string _dataTable = "DataTable"; - private const string _sqlCommand = "SQLCommand"; - private const string _prepareCollection = "PrepareCollection"; - - - public DbMetaDataFactory(Stream xmlStream, string serverVersion, string normalizedServerVersion) - { - - ADP.CheckArgumentNull(xmlStream, "xmlStream"); - ADP.CheckArgumentNull(serverVersion, "serverVersion"); - ADP.CheckArgumentNull(normalizedServerVersion, "normalizedServerVersion"); - - LoadDataSetFromXml(xmlStream); - - _serverVersionString = serverVersion; - _normalizedServerVersion = normalizedServerVersion; - } - - protected DataSet CollectionDataSet - { - get - { - return _metaDataCollectionsDataSet; - } - } - - protected string ServerVersion - { - get - { - return _serverVersionString; - } - } - - - protected string ServerVersionNormalized - { - get - { - return _normalizedServerVersion; - } - } - - protected DataTable CloneAndFilterCollection(string collectionName, string[] hiddenColumnNames) - { - - DataTable sourceTable; - DataTable destinationTable; - DataColumn[] filteredSourceColumns; - DataColumnCollection destinationColumns; - DataRow newRow; - - sourceTable = _metaDataCollectionsDataSet.Tables[collectionName]; - - if ((sourceTable == null) || (collectionName != sourceTable.TableName)) - { - throw ADP.DataTableDoesNotExist(collectionName); - } - - destinationTable = new DataTable(collectionName); - destinationTable.Locale = CultureInfo.InvariantCulture; - destinationColumns = destinationTable.Columns; - - filteredSourceColumns = FilterColumns(sourceTable, hiddenColumnNames, destinationColumns); - - foreach (DataRow row in sourceTable.Rows) - { - if (SupportedByCurrentVersion(row) == true) - { - newRow = destinationTable.NewRow(); - for (int i = 0; i < destinationColumns.Count; i++) - { - newRow[destinationColumns[i]] = row[filteredSourceColumns[i], DataRowVersion.Current]; - } - destinationTable.Rows.Add(newRow); - newRow.AcceptChanges(); - } - } - - return destinationTable; - } - - public void Dispose() - { - Dispose(true); - } - - virtual protected void Dispose(bool disposing) - { - if (disposing) - { - _normalizedServerVersion = null; - _serverVersionString = null; - _metaDataCollectionsDataSet.Dispose(); - } - } - - private DataTable ExecuteCommand(DataRow requestedCollectionRow, String[] restrictions, DbConnection connection) - { - - DataTable metaDataCollectionsTable = _metaDataCollectionsDataSet.Tables[DbMetaDataCollectionNames.MetaDataCollections]; - DataColumn populationStringColumn = metaDataCollectionsTable.Columns[_populationString]; - DataColumn numberOfRestrictionsColumn = metaDataCollectionsTable.Columns[_numberOfRestrictions]; - DataColumn collectionNameColumn = metaDataCollectionsTable.Columns[_collectionName]; - //DataColumn restrictionNameColumn = metaDataCollectionsTable.Columns[_restrictionName]; - - DataTable resultTable = null; - DbCommand command = null; - DataTable schemaTable = null; - - Debug.Assert(requestedCollectionRow != null); - String sqlCommand = requestedCollectionRow[populationStringColumn, DataRowVersion.Current] as string; - int numberOfRestrictions = (int)requestedCollectionRow[numberOfRestrictionsColumn, DataRowVersion.Current]; - String collectionName = requestedCollectionRow[collectionNameColumn, DataRowVersion.Current] as string; - - if ((restrictions != null) && (restrictions.Length > numberOfRestrictions)) - { - throw ADP.TooManyRestrictions(collectionName); - } - - command = connection.CreateCommand(); - command.CommandText = sqlCommand; - command.CommandTimeout = System.Math.Max(command.CommandTimeout, 180); - - for (int i = 0; i < numberOfRestrictions; i++) - { - - DbParameter restrictionParameter = command.CreateParameter(); - - - if ((restrictions != null) && (restrictions.Length > i) && (restrictions[i] != null)) - { - - restrictionParameter.Value = restrictions[i]; - } - else - { - - // This is where we have to assign null to the value of the parameter. - restrictionParameter.Value = DBNull.Value; - - } - - restrictionParameter.ParameterName = GetParameterName(collectionName, i + 1); - restrictionParameter.Direction = ParameterDirection.Input; - command.Parameters.Add(restrictionParameter); - } - - DbDataReader reader = null; - try - { - - try - { - reader = command.ExecuteReader(); - } - catch (Exception e) - { - if (!ADP.IsCatchableExceptionType(e)) - { - throw; - } - throw ADP.QueryFailed(collectionName, e); - } - - // TODO: Consider using the DataAdapter.Fill - - // Build a DataTable from the reader - resultTable = new DataTable(collectionName); - resultTable.Locale = CultureInfo.InvariantCulture; - - schemaTable = reader.GetSchemaTable(); - foreach (DataRow row in schemaTable.Rows) - { - resultTable.Columns.Add(row["ColumnName"] as string, (Type)row["DataType"] as Type); - } - object[] values = new object[resultTable.Columns.Count]; - while (reader.Read()) - { - reader.GetValues(values); - resultTable.Rows.Add(values); - } - } - finally - { - if (reader != null) - { - reader.Dispose(); - reader = null; - } - } - return resultTable; - } - - private DataColumn[] FilterColumns(DataTable sourceTable, string[] hiddenColumnNames, DataColumnCollection destinationColumns) - { - - DataColumn newDestinationColumn; - int currentColumn; - DataColumn[] filteredSourceColumns = null; - - int columnCount = 0; - foreach (DataColumn sourceColumn in sourceTable.Columns) - { - if (IncludeThisColumn(sourceColumn, hiddenColumnNames) == true) - { - columnCount++; - } - } - - if (columnCount == 0) - { - throw ADP.NoColumns(); - } - - currentColumn = 0; - filteredSourceColumns = new DataColumn[columnCount]; - - foreach (DataColumn sourceColumn in sourceTable.Columns) - { - if (IncludeThisColumn(sourceColumn, hiddenColumnNames) == true) - { - newDestinationColumn = new DataColumn(sourceColumn.ColumnName, sourceColumn.DataType); - destinationColumns.Add(newDestinationColumn); - filteredSourceColumns[currentColumn] = sourceColumn; - currentColumn++; - } - } - return filteredSourceColumns; - } - - internal DataRow FindMetaDataCollectionRow(string collectionName) - { - - bool versionFailure; - bool haveExactMatch; - bool haveMultipleInexactMatches; - string candidateCollectionName; - - DataTable metaDataCollectionsTable = _metaDataCollectionsDataSet.Tables[DbMetaDataCollectionNames.MetaDataCollections]; - if (metaDataCollectionsTable == null) - { - throw ADP.InvalidXml(); - } - - DataColumn collectionNameColumn = metaDataCollectionsTable.Columns[DbMetaDataColumnNames.CollectionName]; - - if ((null == collectionNameColumn) || (typeof(System.String) != collectionNameColumn.DataType)) - { - throw ADP.InvalidXmlMissingColumn(DbMetaDataCollectionNames.MetaDataCollections, DbMetaDataColumnNames.CollectionName); - } - - DataRow requestedCollectionRow = null; - String exactCollectionName = null; - - // find the requested collection - versionFailure = false; - haveExactMatch = false; - haveMultipleInexactMatches = false; - - foreach (DataRow row in metaDataCollectionsTable.Rows) - { - - candidateCollectionName = row[collectionNameColumn, DataRowVersion.Current] as string; - if (ADP.IsEmpty(candidateCollectionName)) - { - throw ADP.InvalidXmlInvalidValue(DbMetaDataCollectionNames.MetaDataCollections, DbMetaDataColumnNames.CollectionName); - } - - if (ADP.CompareInsensitiveInvariant(candidateCollectionName, collectionName)) - { - if (SupportedByCurrentVersion(row) == false) - { - versionFailure = true; - } - else - { - if (collectionName == candidateCollectionName) - { - if (haveExactMatch == true) - { - throw ADP.CollectionNameIsNotUnique(collectionName); - } - requestedCollectionRow = row; - exactCollectionName = candidateCollectionName; - haveExactMatch = true; - } - else if (haveExactMatch == false) - { - // have an inexact match - ok only if it is the only one - if (exactCollectionName != null) - { - // can't fail here because we may still find an exact match - haveMultipleInexactMatches = true; - } - requestedCollectionRow = row; - exactCollectionName = candidateCollectionName; - } - } - } - } - - if (requestedCollectionRow == null) - { - if (versionFailure == false) - { - throw ADP.UndefinedCollection(collectionName); - } - else - { - throw ADP.UnsupportedVersion(collectionName); - } - } - - if ((haveExactMatch == false) && (haveMultipleInexactMatches == true)) - { - throw ADP.AmbiguousCollectionName(collectionName); - } - - return requestedCollectionRow; - - } - - private void FixUpVersion(DataTable dataSourceInfoTable) - { - Debug.Assert(dataSourceInfoTable.TableName == DbMetaDataCollectionNames.DataSourceInformation); - DataColumn versionColumn = dataSourceInfoTable.Columns[_dataSourceProductVersion]; - DataColumn normalizedVersionColumn = dataSourceInfoTable.Columns[_dataSourceProductVersionNormalized]; - - if ((versionColumn == null) || (normalizedVersionColumn == null)) - { - throw ADP.MissingDataSourceInformationColumn(); - } - - if (dataSourceInfoTable.Rows.Count != 1) - { - throw ADP.IncorrectNumberOfDataSourceInformationRows(); - } - - DataRow dataSourceInfoRow = dataSourceInfoTable.Rows[0]; - - dataSourceInfoRow[versionColumn] = _serverVersionString; - dataSourceInfoRow[normalizedVersionColumn] = _normalizedServerVersion; - dataSourceInfoRow.AcceptChanges(); - } - - - private string GetParameterName(string neededCollectionName, int neededRestrictionNumber) - { - - DataTable restrictionsTable = null; - DataColumnCollection restrictionColumns = null; - DataColumn collectionName = null; - DataColumn parameterName = null; - DataColumn restrictionName = null; - DataColumn restrictionNumber = null; - ; - string result = null; - - restrictionsTable = _metaDataCollectionsDataSet.Tables[DbMetaDataCollectionNames.Restrictions]; - if (restrictionsTable != null) - { - restrictionColumns = restrictionsTable.Columns; - if (restrictionColumns != null) - { - collectionName = restrictionColumns[_collectionName]; - parameterName = restrictionColumns[_parameterName]; - restrictionName = restrictionColumns[_restrictionName]; - restrictionNumber = restrictionColumns[_restrictionNumber]; - } - } - - if ((parameterName == null) || (collectionName == null) || (restrictionName == null) || (restrictionNumber == null)) - { - throw ADP.MissingRestrictionColumn(); - } - - foreach (DataRow restriction in restrictionsTable.Rows) - { - - if (((string)restriction[collectionName] == neededCollectionName) && - ((int)restriction[restrictionNumber] == neededRestrictionNumber) && - (SupportedByCurrentVersion(restriction))) - { - - result = (string)restriction[parameterName]; - break; - } - } - - if (result == null) - { - throw ADP.MissingRestrictionRow(); - } - - return result; - - } - - virtual public DataTable GetSchema(DbConnection connection, string collectionName, string[] restrictions) - { - Debug.Assert(_metaDataCollectionsDataSet != null); - - //TODO: MarkAsh or EnzoL should review this code for efficiency. - DataTable metaDataCollectionsTable = _metaDataCollectionsDataSet.Tables[DbMetaDataCollectionNames.MetaDataCollections]; - DataColumn populationMechanismColumn = metaDataCollectionsTable.Columns[_populationMechanism]; - DataColumn collectionNameColumn = metaDataCollectionsTable.Columns[DbMetaDataColumnNames.CollectionName]; - DataRow requestedCollectionRow = null; - DataTable requestedSchema = null; - string[] hiddenColumns; - string exactCollectionName = null; - - requestedCollectionRow = FindMetaDataCollectionRow(collectionName); - exactCollectionName = requestedCollectionRow[collectionNameColumn, DataRowVersion.Current] as string; - - if (ADP.IsEmptyArray(restrictions) == false) - { - - for (int i = 0; i < restrictions.Length; i++) - { - if ((restrictions[i] != null) && (restrictions[i].Length > 4096)) - { - // use a non-specific error because no new beta 2 error messages are allowed - // TODO: will add a more descriptive error in RTM - throw ADP.NotSupported(); - } - } - } - - string populationMechanism = requestedCollectionRow[populationMechanismColumn, DataRowVersion.Current] as string; - switch (populationMechanism) - { - - case _dataTable: - if (exactCollectionName == DbMetaDataCollectionNames.MetaDataCollections) - { - hiddenColumns = new string[2]; - hiddenColumns[0] = _populationMechanism; - hiddenColumns[1] = _populationString; - } - else - { - hiddenColumns = null; - } - // none of the datatable collections support restrictions - if (ADP.IsEmptyArray(restrictions) == false) - { - throw ADP.TooManyRestrictions(exactCollectionName); - } - - - requestedSchema = CloneAndFilterCollection(exactCollectionName, hiddenColumns); - - // TODO: Consider an alternate method that doesn't involve special casing -- perhaps _prepareCollection - - // for the data source information table we need to fix up the version columns at run time - // since the version is determined at run time - if (exactCollectionName == DbMetaDataCollectionNames.DataSourceInformation) - { - FixUpVersion(requestedSchema); - } - break; - - case _sqlCommand: - requestedSchema = ExecuteCommand(requestedCollectionRow, restrictions, connection); - break; - - case _prepareCollection: - requestedSchema = PrepareCollection(exactCollectionName, restrictions, connection); - break; - - default: - throw ADP.UndefinedPopulationMechanism(populationMechanism); - } - - return requestedSchema; - } - - private bool IncludeThisColumn(DataColumn sourceColumn, string[] hiddenColumnNames) - { - - bool result = true; - string sourceColumnName = sourceColumn.ColumnName; - - switch (sourceColumnName) - { - - case _minimumVersion: - case _maximumVersion: - result = false; - break; - - default: - if (hiddenColumnNames == null) - { - break; - } - for (int i = 0; i < hiddenColumnNames.Length; i++) - { - if (hiddenColumnNames[i] == sourceColumnName) - { - result = false; - break; - } - } - break; - } - - return result; - } - - private void LoadDataSetFromXml(Stream XmlStream) - { - _metaDataCollectionsDataSet = new DataSet(); - _metaDataCollectionsDataSet.Locale = System.Globalization.CultureInfo.InvariantCulture; - // Reading from a stream has security implications. Create an XmlReader and do not allow the - // XmlReader to open any external resources by setting the XmlResolver property to null. - _metaDataCollectionsDataSet.ReadXml(XmlReader.Create(XmlStream, new XmlReaderSettings() { XmlResolver = null })); - } - - virtual protected DataTable PrepareCollection(String collectionName, String[] restrictions, DbConnection connection) - { - throw ADP.NotSupported(); - } - - private bool SupportedByCurrentVersion(DataRow requestedCollectionRow) - { - - bool result = true; - DataColumnCollection tableColumns = requestedCollectionRow.Table.Columns; - DataColumn versionColumn; - Object version; - - // check the minimum version first - versionColumn = tableColumns[_minimumVersion]; - if (versionColumn != null) - { - version = requestedCollectionRow[versionColumn]; - if (version != null) - { - if (version != DBNull.Value) - { - if (0 > string.Compare(_normalizedServerVersion, (string)version, StringComparison.OrdinalIgnoreCase)) - { - result = false; - } - } - } - } - - // if the minimum version was ok what about the maximum version - if (result == true) - { - versionColumn = tableColumns[_maximumVersion]; - if (versionColumn != null) - { - version = requestedCollectionRow[versionColumn]; - if (version != null) - { - if (version != DBNull.Value) - { - if (0 < string.Compare(_normalizedServerVersion, (string)version, StringComparison.OrdinalIgnoreCase)) - { - result = false; - } - } - } - } - } - - return result; - } - } -} - - - diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Common/src/Microsoft/Data/ProviderBase/DbMetaDataFactory.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/ProviderBase/DbMetaDataFactory.cs similarity index 100% rename from src/Microsoft.Data.SqlClient/netcore/src/Common/src/Microsoft/Data/ProviderBase/DbMetaDataFactory.cs rename to src/Microsoft.Data.SqlClient/src/Microsoft/Data/ProviderBase/DbMetaDataFactory.cs From df125a1856b1950303897f2240aa87962a8d0bb7 Mon Sep 17 00:00:00 2001 From: Lawrence LCI Date: Thu, 23 Sep 2021 11:11:37 -0700 Subject: [PATCH 2/2] Clean up the file to conform to the coding style resolving the info messages IDE1006, IDE10059, IDE0051 and IDE0090 --- .../Data/ProviderBase/DbMetaDataFactory.cs | 126 ++++++++---------- 1 file changed, 59 insertions(+), 67 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/ProviderBase/DbMetaDataFactory.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/ProviderBase/DbMetaDataFactory.cs index 88ca0725c2..6e907d26e1 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/ProviderBase/DbMetaDataFactory.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/ProviderBase/DbMetaDataFactory.cs @@ -19,23 +19,22 @@ internal class DbMetaDataFactory private string _normalizedServerVersion; private string _serverVersionString; // well known column names - private const string _collectionName = "CollectionName"; - private const string _populationMechanism = "PopulationMechanism"; - private const string _populationString = "PopulationString"; - private const string _maximumVersion = "MaximumVersion"; - private const string _minimumVersion = "MinimumVersion"; - private const string _dataSourceProductVersionNormalized = "DataSourceProductVersionNormalized"; - private const string _dataSourceProductVersion = "DataSourceProductVersion"; - private const string _restrictionDefault = "RestrictionDefault"; - private const string _restrictionNumber = "RestrictionNumber"; - private const string _numberOfRestrictions = "NumberOfRestrictions"; - private const string _restrictionName = "RestrictionName"; - private const string _parameterName = "ParameterName"; + private const string CollectionNameKey = "CollectionName"; + private const string PopulationMechanismKey = "PopulationMechanism"; + private const string PopulationStringKey = "PopulationString"; + private const string MaximumVersionKey = "MaximumVersion"; + private const string MinimumVersionKey = "MinimumVersion"; + private const string DataSourceProductVersionNormalizedKey = "DataSourceProductVersionNormalized"; + private const string DataSourceProductVersionKey = "DataSourceProductVersion"; + private const string RestrictionNumberKey = "RestrictionNumber"; + private const string NumberOfRestrictionsKey = "NumberOfRestrictions"; + private const string RestrictionNameKey = "RestrictionName"; + private const string ParameterNameKey = "ParameterName"; // population mechanisms - private const string _dataTable = "DataTable"; - private const string _sqlCommand = "SQLCommand"; - private const string _prepareCollection = "PrepareCollection"; + private const string DataTableKey = "DataTable"; + private const string SqlCommandKey = "SQLCommand"; + private const string PrepareCollectionKey = "PrepareCollection"; public DbMetaDataFactory(Stream xmlStream, string serverVersion, string normalizedServerVersion) { @@ -79,7 +78,7 @@ protected DataTable CloneAndFilterCollection(string collectionName, string[] hid foreach (DataRow row in sourceTable.Rows) { - if (SupportedByCurrentVersion(row) == true) + if (SupportedByCurrentVersion(row)) { newRow = destinationTable.NewRow(); for (int i = 0; i < destinationColumns.Count; i++) @@ -109,13 +108,11 @@ protected virtual void Dispose(bool disposing) private DataTable ExecuteCommand(DataRow requestedCollectionRow, string[] restrictions, DbConnection connection) { DataTable metaDataCollectionsTable = _metaDataCollectionsDataSet.Tables[DbMetaDataCollectionNames.MetaDataCollections]; - DataColumn populationStringColumn = metaDataCollectionsTable.Columns[_populationString]; - DataColumn numberOfRestrictionsColumn = metaDataCollectionsTable.Columns[_numberOfRestrictions]; - DataColumn collectionNameColumn = metaDataCollectionsTable.Columns[_collectionName]; + DataColumn populationStringColumn = metaDataCollectionsTable.Columns[PopulationStringKey]; + DataColumn numberOfRestrictionsColumn = metaDataCollectionsTable.Columns[NumberOfRestrictionsKey]; + DataColumn collectionNameColumn = metaDataCollectionsTable.Columns[CollectionNameKey]; DataTable resultTable = null; - DbCommand command = null; - DataTable schemaTable = null; Debug.Assert(requestedCollectionRow != null); string sqlCommand = requestedCollectionRow[populationStringColumn, DataRowVersion.Current] as string; @@ -127,7 +124,7 @@ private DataTable ExecuteCommand(DataRow requestedCollectionRow, string[] restri throw ADP.TooManyRestrictions(collectionName); } - command = connection.CreateCommand(); + DbCommand command = connection.CreateCommand(); command.CommandText = sqlCommand; command.CommandTimeout = Math.Max(command.CommandTimeout, 180); @@ -173,7 +170,7 @@ private DataTable ExecuteCommand(DataRow requestedCollectionRow, string[] restri Locale = CultureInfo.InvariantCulture }; - schemaTable = reader.GetSchemaTable(); + DataTable schemaTable = reader.GetSchemaTable(); foreach (DataRow row in schemaTable.Rows) { resultTable.Columns.Add(row["ColumnName"] as string, (Type)row["DataType"] as Type); @@ -187,11 +184,7 @@ private DataTable ExecuteCommand(DataRow requestedCollectionRow, string[] restri } finally { - if (reader != null) - { - reader.Dispose(); - reader = null; - } + reader?.Dispose(); } return resultTable; } @@ -201,7 +194,7 @@ private DataColumn[] FilterColumns(DataTable sourceTable, string[] hiddenColumnN int columnCount = 0; foreach (DataColumn sourceColumn in sourceTable.Columns) { - if (IncludeThisColumn(sourceColumn, hiddenColumnNames) == true) + if (IncludeThisColumn(sourceColumn, hiddenColumnNames)) { columnCount++; } @@ -217,9 +210,9 @@ private DataColumn[] FilterColumns(DataTable sourceTable, string[] hiddenColumnN foreach (DataColumn sourceColumn in sourceTable.Columns) { - if (IncludeThisColumn(sourceColumn, hiddenColumnNames) == true) + if (IncludeThisColumn(sourceColumn, hiddenColumnNames)) { - DataColumn newDestinationColumn = new DataColumn(sourceColumn.ColumnName, sourceColumn.DataType); + DataColumn newDestinationColumn = new(sourceColumn.ColumnName, sourceColumn.DataType); destinationColumns.Add(newDestinationColumn); filteredSourceColumns[currentColumn] = sourceColumn; currentColumn++; @@ -267,7 +260,7 @@ internal DataRow FindMetaDataCollectionRow(string collectionName) if (ADP.CompareInsensitiveInvariant(candidateCollectionName, collectionName)) { - if (SupportedByCurrentVersion(row) == false) + if (!SupportedByCurrentVersion(row)) { versionFailure = true; } @@ -275,7 +268,7 @@ internal DataRow FindMetaDataCollectionRow(string collectionName) { if (collectionName == candidateCollectionName) { - if (haveExactMatch == true) + if (haveExactMatch) { throw ADP.CollectionNameIsNotUnique(collectionName); } @@ -283,7 +276,7 @@ internal DataRow FindMetaDataCollectionRow(string collectionName) exactCollectionName = candidateCollectionName; haveExactMatch = true; } - else if (haveExactMatch == false) + else if (!haveExactMatch) { // have an inexact match - ok only if it is the only one if (exactCollectionName != null) @@ -300,7 +293,7 @@ internal DataRow FindMetaDataCollectionRow(string collectionName) if (requestedCollectionRow == null) { - if (versionFailure == false) + if (!versionFailure) { throw ADP.UndefinedCollection(collectionName); } @@ -310,7 +303,7 @@ internal DataRow FindMetaDataCollectionRow(string collectionName) } } - if ((haveExactMatch == false) && (haveMultipleInexactMatches == true)) + if (!haveExactMatch && haveMultipleInexactMatches) { throw ADP.AmbiguousCollectionName(collectionName); } @@ -322,8 +315,8 @@ internal DataRow FindMetaDataCollectionRow(string collectionName) private void FixUpVersion(DataTable dataSourceInfoTable) { Debug.Assert(dataSourceInfoTable.TableName == DbMetaDataCollectionNames.DataSourceInformation); - DataColumn versionColumn = dataSourceInfoTable.Columns[_dataSourceProductVersion]; - DataColumn normalizedVersionColumn = dataSourceInfoTable.Columns[_dataSourceProductVersionNormalized]; + DataColumn versionColumn = dataSourceInfoTable.Columns[DataSourceProductVersionKey]; + DataColumn normalizedVersionColumn = dataSourceInfoTable.Columns[DataSourceProductVersionNormalizedKey]; if ((versionColumn == null) || (normalizedVersionColumn == null)) { @@ -345,9 +338,6 @@ private void FixUpVersion(DataTable dataSourceInfoTable) private string GetParameterName(string neededCollectionName, int neededRestrictionNumber) { - - DataTable restrictionsTable = null; - DataColumnCollection restrictionColumns = null; DataColumn collectionName = null; DataColumn parameterName = null; DataColumn restrictionName = null; @@ -355,16 +345,16 @@ private string GetParameterName(string neededCollectionName, int neededRestricti string result = null; - restrictionsTable = _metaDataCollectionsDataSet.Tables[DbMetaDataCollectionNames.Restrictions]; + DataTable restrictionsTable = _metaDataCollectionsDataSet.Tables[DbMetaDataCollectionNames.Restrictions]; if (restrictionsTable != null) { - restrictionColumns = restrictionsTable.Columns; + DataColumnCollection restrictionColumns = restrictionsTable.Columns; if (restrictionColumns != null) { - collectionName = restrictionColumns[_collectionName]; - parameterName = restrictionColumns[_parameterName]; - restrictionName = restrictionColumns[_restrictionName]; - restrictionNumber = restrictionColumns[_restrictionNumber]; + collectionName = restrictionColumns[DbMetaDataFactory.CollectionNameKey]; + parameterName = restrictionColumns[ParameterNameKey]; + restrictionName = restrictionColumns[RestrictionNameKey]; + restrictionNumber = restrictionColumns[RestrictionNumberKey]; } } @@ -399,17 +389,15 @@ public virtual DataTable GetSchema(DbConnection connection, string collectionNam Debug.Assert(_metaDataCollectionsDataSet != null); DataTable metaDataCollectionsTable = _metaDataCollectionsDataSet.Tables[DbMetaDataCollectionNames.MetaDataCollections]; - DataColumn populationMechanismColumn = metaDataCollectionsTable.Columns[_populationMechanism]; + DataColumn populationMechanismColumn = metaDataCollectionsTable.Columns[PopulationMechanismKey]; DataColumn collectionNameColumn = metaDataCollectionsTable.Columns[DbMetaDataColumnNames.CollectionName]; - DataRow requestedCollectionRow = null; - DataTable requestedSchema = null; + string[] hiddenColumns; - string exactCollectionName = null; - requestedCollectionRow = FindMetaDataCollectionRow(collectionName); - exactCollectionName = requestedCollectionRow[collectionNameColumn, DataRowVersion.Current] as string; + DataRow requestedCollectionRow = FindMetaDataCollectionRow(collectionName); + string exactCollectionName = requestedCollectionRow[collectionNameColumn, DataRowVersion.Current] as string; - if (ADP.IsEmptyArray(restrictions) == false) + if (!ADP.IsEmptyArray(restrictions)) { for (int i = 0; i < restrictions.Length; i++) @@ -424,22 +412,24 @@ public virtual DataTable GetSchema(DbConnection connection, string collectionNam } string populationMechanism = requestedCollectionRow[populationMechanismColumn, DataRowVersion.Current] as string; + + DataTable requestedSchema; switch (populationMechanism) { - case _dataTable: + case DataTableKey: if (exactCollectionName == DbMetaDataCollectionNames.MetaDataCollections) { hiddenColumns = new string[2]; - hiddenColumns[0] = _populationMechanism; - hiddenColumns[1] = _populationString; + hiddenColumns[0] = PopulationMechanismKey; + hiddenColumns[1] = PopulationStringKey; } else { hiddenColumns = null; } // none of the datatable collections support restrictions - if (ADP.IsEmptyArray(restrictions) == false) + if (!ADP.IsEmptyArray(restrictions)) { throw ADP.TooManyRestrictions(exactCollectionName); } @@ -457,11 +447,11 @@ public virtual DataTable GetSchema(DbConnection connection, string collectionNam } break; - case _sqlCommand: + case SqlCommandKey: requestedSchema = ExecuteCommand(requestedCollectionRow, restrictions, connection); break; - case _prepareCollection: + case PrepareCollectionKey: requestedSchema = PrepareCollection(exactCollectionName, restrictions, connection); break; @@ -481,8 +471,8 @@ private bool IncludeThisColumn(DataColumn sourceColumn, string[] hiddenColumnNam switch (sourceColumnName) { - case _minimumVersion: - case _maximumVersion: + case MinimumVersionKey: + case MaximumVersionKey: result = false; break; @@ -507,8 +497,10 @@ private bool IncludeThisColumn(DataColumn sourceColumn, string[] hiddenColumnNam private void LoadDataSetFromXml(Stream XmlStream) { - _metaDataCollectionsDataSet = new DataSet(); - _metaDataCollectionsDataSet.Locale = System.Globalization.CultureInfo.InvariantCulture; + _metaDataCollectionsDataSet = new DataSet + { + Locale = System.Globalization.CultureInfo.InvariantCulture + }; _metaDataCollectionsDataSet.ReadXml(XmlStream); } @@ -525,7 +517,7 @@ private bool SupportedByCurrentVersion(DataRow requestedCollectionRow) object version; // check the minimum version first - versionColumn = tableColumns[_minimumVersion]; + versionColumn = tableColumns[MinimumVersionKey]; if (versionColumn != null) { version = requestedCollectionRow[versionColumn]; @@ -542,9 +534,9 @@ private bool SupportedByCurrentVersion(DataRow requestedCollectionRow) } // if the minimum version was ok what about the maximum version - if (result == true) + if (result) { - versionColumn = tableColumns[_maximumVersion]; + versionColumn = tableColumns[MaximumVersionKey]; if (versionColumn != null) { version = requestedCollectionRow[versionColumn];