Repository navigation
Description
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.
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
[-]API for retrieving Columns Schema from DbDataReader[/-][+]API for retrieving Columns Metadata from DbDataReader[/+]Adding the GetSchemaTable() replacement APIs as proposed in
saurabh500 commentedon Feb 11, 2016
The API review dotnet/corefx#2 dotnet/apireviews#30
saurabh500 commentedon Mar 7, 2016
The final PR for this enhancement was in dotnet/corefx#6097
roji commentedon May 17, 2016
It's good to see this in rc2. Is there a similar replacement for
DbConnection.GetSchema()?saurabh500 commentedon May 17, 2016
@roji The API for DbConnection is not present in RC2. It will be tracked using https://github.com/dotnet/corefx/issues/5024
roji commentedon May 17, 2016
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.
saurabh500 commentedon May 17, 2016
That's right. The API needs a lot more work and research and we can only get it in post-RTM.
roji commentedon May 17, 2016
OK, thanks.