Skip to content

API for retrieving Columns Metadata from DbDataReader #16305

Closed

Description

@saurabh500

API for GetColumnSchema

Problem

DbDataReader on .Net Framework, provides an API called DataTable GetSchemaTable() which was returns the metadata of the columns
being read using the DbDataReader object. Because of the removal of DataTable in .Net Core this API had to be removed.
DbDataReader.GetSchemaTable() needs a replacement in .Net core.

Progress

The data structure DbColumn along with the necessary attributes which should be present
for the various ADO.Net providers to effectively provide the metadata for the columns is provided in the issue.

namespace System.Data.Common
{
   // DbColumn contains the base attributes that are common to most Database columns. An indexer 
  // has been provided in case a property added by a provider needs to be accessed without a dependency on the provider implementation. 
    public class DbColumn
    {
        public bool? AllowDBNull { get; protected set; }
        public string BaseCatalogName { get; protected set; }
        public string BaseColumnName { get; protected set; }
        public string BaseSchemaName { get; protected set; }
        public string BaseServerName { get; protected set; }
        public string BaseTableName { get; protected set; }
        public string ColumnName { get; protected set; }
        public int? ColumnOrdinal { get; protected set; }
        public int? ColumnSize { get; protected set; }
        public bool? IsAliased { get; protected set; }
        public bool? IsAutoIncrement { get; protected set; }
        public bool? IsExpression { get; protected set; }
        public bool? IsHidden { get; protected set; }
        public bool? IsIdentity { get; protected set; }
        public bool? IsKey { get; protected set; }
        public bool? IsLong { get; protected set; }
        public bool? IsReadOnly { get; protected set; }
        public bool? IsUnique { get; protected set; }
        public int? NumericPrecision { get; protected set; }
        public int? NumericScale { get; protected set; }
        public string UdtAssemblyQualifiedName { get; protected set; }
        public Type DataType { get; protected set; }
        public string DataTypeName { get; protected set; }
        public  virtual object this[string property] { get; } 
    }
}

The next part of the API is to expose a ReadOnlyCollection from the DbDataReader

The right way of exposing the API would be to add an API in the DbDataReader class called
ReadOnlyCollection<DbColumn> DbDataReader.GetColumnSchema()

This is however not possible in .Net Core current version as it would hinder portability of apps created on .Net core with .Net framework.

https://github.com/dotnet/corefx/blob/master/Documentation/coding-guidelines/breaking-change-rules.md

Based on the contents of the link above, the following rule is allowed
Adding an abstract member to a public type when there are no accessible (public or protected) constructors, or the type is sealed

The restriction is due to type forwarding, which needs the calls to DbDataReader to be forwarded to the .Net framework's DbDataReader.

Proposal

We introduce an interface called IDbColumnSchemaGenerator, in .Net core which can be implemented by a type which can provide the Column Metadata.
For DbDataReader, this interface needs to be implemented by the subclasses of the DbDataReader to provide the Column metadata provided by the DbDataReader

namespace System.Data.Common
{
    public interface IDbColumnSchemaGenerator
    {
        System.Collections.ObjectModel.ReadOnlyCollection<DbColumn> GetColumnSchema();
    }
}

The subclasses of DbDataReader in the ADO.Net providers implement this interface.

public class SqlDataReader : DbDataReader, IDbColumnSchemaGenerator 
{
  public ReadOnlyCollection<DbColumn> GetColumnSchema(){
      return ... ; 
  }
}

In .Net core, to allow the Column Metadata to be exposed from the DbDataReader, an extension to the DbDataReader will be provided. This extension has the same signature as the interface method.

using System.Collections.ObjectModel;

namespace System.Data.Common
{
    public static class DbDataReaderExtension
    {
        public static System.Collections.ObjectModel.ReadOnlyCollection<DbColumn> GetColumnSchema(this DbDataReader reader)
        {
            if(reader is IDbColumnSchemaGenerator)
            {
                return ((IDbColumnSchemaGenerator)reader).GetColumnSchema();
            }
            throw new NotImplementedException());
        }
    }
}

It is recommended that the extension be used by consumers, for compatibility with .Net Framework. The details are mentioned in the compatibility section below.

Producer

The ADO.Net providers are the producers of the Column Schema. The interface will be implemented by the sub-classes of DbDataReader in the ADO.Net providers.

E.g.

class SqlDataReader : DbDataReader, IDbColumnSchemaGenerator
{
    ...

        public ReadOnlyCollection<DbColumn> GetDbColumnSchema()
        {
            ReadOnlyCollection<DbColumn> metadata = ... ; 
            // Build the list of columns metadata. Each columns metadata is represented by the DbColumn
            return metadata; // 
        }
    ...
}

Consumer

The consumers can retrieve the column metadata using an extension method on the DbDataReader. This extension will be provided in System.Data.Common

For a library consuming the DbDataReader the code will look like the following:

using System.Data.Common;

namespace org.example.my
{
        ...
            public void ProcessMetadata(DbDataReader reader)
            {
                ICollection<DbColumn> columnMetadata = reader.GetColumnSchema();
            }
        ...
}

Compatibility of libraries written on .Net Core with .Net Framework

The extension method will have a different implementation in the partial facade to get the Schema. The implementation will use the DbDataReader.GetSchemaTable() api which returns the schema as a Datatable.
This Datatable will be mapped to a ReadOnlyCollection and returned by the implementation of the extension in the partial facade.

vNext

In the next version of .Net we add a Interface to .Net desktop and the the consumers continue to access the schema using the Interface

Reference

Github issue https://github.com/dotnet/corefx/issues/3423

Activity

  1. self-assigned this
    on Feb 5, 2016
  2. changed the title [-]API for retrieving Columns Schema from DbDataReader[/-] [+]API for retrieving Columns Metadata from DbDataReader[/+] on Feb 5, 2016
  3. saurabh500 commented on Feb 11, 2016

    @saurabh500
    ContributorAuthor
  4. saurabh500 commented on Mar 7, 2016

    @saurabh500
    ContributorAuthor

    The final PR for this enhancement was in dotnet/corefx#6097

  5. roji commented on May 17, 2016

    @roji
    Member

    It's good to see this in rc2. Is there a similar replacement for DbConnection.GetSchema()?

  6. saurabh500 commented on May 17, 2016

    @saurabh500
    ContributorAuthor
  7. roji commented on May 17, 2016

    @roji
    Member

    Oh, right (I even participated in the discussions there). #15953 has milestone Future though, does that mean you're not planning on shipping this for RTM? That seems problematic, especially considering you have the other schema/metadata API here.

  8. saurabh500 commented on May 17, 2016

    @saurabh500
    ContributorAuthor

    That's right. The API needs a lot more work and research and we can only get it in post-RTM.

  9. roji commented on May 17, 2016

    @roji
    Member

    OK, thanks.

  10. transferred this issue fromdotnet/corefxon Jan 31, 2020
  11. added this to the 1.0.0-rc2 milestone on Jan 31, 2020
  12. ghost locked as resolved and limited conversation to collaborators on Jan 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Data

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions