Skip to content

Commit

Permalink
Combine DbDataReaderExtensions.GetColumnSchema implementations and re…
Browse files Browse the repository at this point in the history
…move unused facade

Closes dotnet/corefx#36235


Commit migrated from dotnet/corefx@34d64f4
  • Loading branch information
Wraith2 authored and roji committed Apr 2, 2019
1 parent 837adf0 commit c10cc1e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 90 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,85 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace System.Data.Common
{
public static class DbDataReaderExtensions
{
public static System.Collections.ObjectModel.ReadOnlyCollection<DbColumn> GetColumnSchema(this DbDataReader reader)
private sealed class DataRowDbColumn : DbColumn
{
private DataColumnCollection _schemaColumns;
private DataRow _schemaRow;

public DataRowDbColumn(DataRow readerSchemaRow, DataColumnCollection readerSchemaColumns)
{
_schemaRow = readerSchemaRow;
_schemaColumns = readerSchemaColumns;
PopulateFields();
}

private void PopulateFields()
{
AllowDBNull = GetDbColumnValue<bool?>(SchemaTableColumn.AllowDBNull);
BaseCatalogName = GetDbColumnValue<string>(SchemaTableOptionalColumn.BaseCatalogName);
BaseColumnName = GetDbColumnValue<string>(SchemaTableColumn.BaseColumnName);
BaseSchemaName = GetDbColumnValue<string>(SchemaTableColumn.BaseSchemaName);
BaseServerName = GetDbColumnValue<string>(SchemaTableOptionalColumn.BaseServerName);
BaseTableName = GetDbColumnValue<string>(SchemaTableColumn.BaseTableName);
ColumnName = GetDbColumnValue<string>(SchemaTableColumn.ColumnName);
ColumnOrdinal = GetDbColumnValue<int?>(SchemaTableColumn.ColumnOrdinal);
ColumnSize = GetDbColumnValue<int?>(SchemaTableColumn.ColumnSize);
IsAliased = GetDbColumnValue<bool?>(SchemaTableColumn.IsAliased);
IsAutoIncrement = GetDbColumnValue<bool?>(SchemaTableOptionalColumn.IsAutoIncrement);
IsExpression = GetDbColumnValue<bool>(SchemaTableColumn.IsExpression);
IsHidden = GetDbColumnValue<bool?>(SchemaTableOptionalColumn.IsHidden);
IsIdentity = GetDbColumnValue<bool?>("IsIdentity");
IsKey = GetDbColumnValue<bool?>(SchemaTableColumn.IsKey);
IsLong = GetDbColumnValue<bool?>(SchemaTableColumn.IsLong);
IsReadOnly = GetDbColumnValue<bool?>(SchemaTableOptionalColumn.IsReadOnly);
IsUnique = GetDbColumnValue<bool?>(SchemaTableColumn.IsUnique);
NumericPrecision = GetDbColumnValue<int?>(SchemaTableColumn.NumericPrecision);
NumericScale = GetDbColumnValue<int?>(SchemaTableColumn.NumericScale);
UdtAssemblyQualifiedName = GetDbColumnValue<string>("UdtAssemblyQualifiedName");
DataType = GetDbColumnValue<Type>(SchemaTableColumn.DataType);
DataTypeName = GetDbColumnValue<string>("DataTypeName");
}

private T GetDbColumnValue<T>(string columnName) => _schemaColumns.Contains(columnName) && _schemaRow[columnName] is T value ? value : (default);
}

public static ReadOnlyCollection<DbColumn> GetColumnSchema(this DbDataReader reader)
{
if (reader.CanGetColumnSchema())
if (reader is null)
{
throw new ArgumentNullException(nameof(reader));
}
if (reader is IDbColumnSchemaGenerator schemaGenerator)
{
return ((IDbColumnSchemaGenerator)reader).GetColumnSchema();
return schemaGenerator.GetColumnSchema();
}
throw new NotSupportedException();
return GetColumnSchemaCompatibility(reader);
}

public static bool CanGetColumnSchema(this DbDataReader reader)
{
return reader is IDbColumnSchemaGenerator;
return true;
}

private static ReadOnlyCollection<DbColumn> GetColumnSchemaCompatibility(DbDataReader reader)
{
var columnSchema = new List<DbColumn>();
DataTable schemaTable = reader.GetSchemaTable();
DataColumnCollection schemaTableColumns = schemaTable.Columns;
foreach (DataRow row in schemaTable.Rows)
{
columnSchema.Add(new DataRowDbColumn(row, schemaTableColumns));
}
return new ReadOnlyCollection<DbColumn>(columnSchema);
}
}


}

0 comments on commit c10cc1e

Please sign in to comment.