Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge | Add IDBColumnSchemaGenerator interface to netfx SqlDataReader #2967

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,9 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlDataAdapter.cs">
<Link>Microsoft\Data\SqlClient\SqlDataAdapter.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlDbColumn.cs">
<Link>Microsoft\Data\SqlClient\SqlDbColumn.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlDelegatedTransaction.cs">
<Link>Microsoft\Data\SqlClient\SqlDelegatedTransaction.cs</Link>
</Compile>
Expand Down Expand Up @@ -654,7 +657,6 @@
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionFactory.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionHelper.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlDataReader.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlDbColumn.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlDiagnosticListener.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlInternalConnectionTds.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlTransaction.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="$(MicrosoftIdentityModelJsonWebTokensVersion)" />
<PackageReference Include="System.Buffers" Version="$(SystemBuffersVersion)" />
<PackageReference Include="System.Text.Json" Version="$(SystemTextJsonVersion)" />
<PackageReference Include="System.Data.Common" Version="$(SystemDataCommonVersion)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,9 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlDataAdapter.cs">
<Link>Microsoft\Data\SqlClient\SqlDataAdapter.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlDbColumn.cs">
<Link>Microsoft\Data\SqlClient\SqlDbColumn.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlDelegatedTransaction.cs">
<Link>Microsoft\Data\SqlClient\SqlDelegatedTransaction.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
Expand All @@ -27,7 +28,7 @@
namespace Microsoft.Data.SqlClient
{
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/SqlDataReader/*' />
public class SqlDataReader : DbDataReader, IDataReader
public class SqlDataReader : DbDataReader, IDataReader, IDbColumnSchemaGenerator
{
private enum ALTROWSTATUS
{
Expand Down Expand Up @@ -74,6 +75,7 @@ internal class SharedState
private CommandBehavior _commandBehavior;

private static int s_objectTypeCount; // EventSource Counter
private static readonly ReadOnlyCollection<DbColumn> s_emptySchema = new ReadOnlyCollection<DbColumn>(Array.Empty<DbColumn>());
internal readonly int ObjectID = Interlocked.Increment(ref s_objectTypeCount);


Expand Down Expand Up @@ -6362,5 +6364,73 @@ private void SwitchToAsyncWithoutSnapshot()
_stateObj._asyncReadWithoutSnapshot = true;
}

}// SqlDataReader
}// namespace
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetColumnSchema/*' />
public ReadOnlyCollection<DbColumn> GetColumnSchema()
{
SqlStatistics statistics = null;
try
{
statistics = SqlStatistics.StartTimer(Statistics);
if (_metaData == null || _metaData.dbColumnSchema == null)
{
if (this.MetaData != null)
{

_metaData.dbColumnSchema = BuildColumnSchema();
Debug.Assert(_metaData.dbColumnSchema != null, "No schema information yet!");
// filter table?
}
}
if (_metaData != null)
{
return _metaData.dbColumnSchema;
}
return s_emptySchema;
}
finally
{
SqlStatistics.StopTimer(statistics);
}
}

private ReadOnlyCollection<DbColumn> BuildColumnSchema()
{
_SqlMetaDataSet md = MetaData;
DbColumn[] columnSchema = new DbColumn[md.Length];
for (int i = 0; i < md.Length; i++)
{
_SqlMetaData col = md[i];
SqlDbColumn dbColumn = new SqlDbColumn(md[i]);

if (_typeSystem <= SqlConnectionString.TypeSystem.SQLServer2005 && col.Is2008DateTimeType)
{
dbColumn.SqlNumericScale = MetaType.MetaNVarChar.Scale;
}
else if (TdsEnums.UNKNOWN_PRECISION_SCALE != col.scale)
{
dbColumn.SqlNumericScale = col.scale;
}
else
{
dbColumn.SqlNumericScale = col.metaType.Scale;
}

if (_browseModeInfoConsumed)
{
dbColumn.SqlIsAliased = col.IsDifferentName;
dbColumn.SqlIsKey = col.IsKey;
dbColumn.SqlIsHidden = col.IsHidden;
dbColumn.SqlIsExpression = col.IsExpression;
}

dbColumn.SqlDataType = GetFieldTypeInternal(col);

dbColumn.SqlDataTypeName = GetDataTypeNameInternal(col);

columnSchema[i] = dbColumn;
}

return new ReadOnlyCollection<DbColumn>(columnSchema);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="System.Buffers" Version="$(SystemBuffersVersion)" />
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
<PackageReference Include="System.Data.Common" Version="$(SystemDataCommonVersion)" Condition="'$(TargetFramework)' == 'net462'" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,7 @@ internal sealed partial class _SqlMetaDataSet

internal DataTable schemaTable;
private readonly _SqlMetaData[] _metaDataArray;
#if !NETFRAMEWORK
internal ReadOnlyCollection<DbColumn> dbColumnSchema;
#endif

private int _hiddenColumnCount;
private int[] _visibleColumnMap;
Expand All @@ -327,11 +325,9 @@ private _SqlMetaDataSet(_SqlMetaDataSet original)
id = original.id;
_hiddenColumnCount = original._hiddenColumnCount;
_visibleColumnMap = original._visibleColumnMap;
#if !NETFRAMEWORK
dbColumnSchema = original.dbColumnSchema;
#else
schemaTable = original.schemaTable;
#endif

if (original._metaDataArray == null)
{
_metaDataArray = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ private static void TestUdtSqlDataReaderGetStream(CommandBehavior behavior)
}

// Synapse: Parse error at line: 1, column: 41: Incorrect syntax near 'hierarchyid'.
[ActiveIssue("25421", TargetFrameworkMonikers.NetFramework)]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
public static void TestUdtSchemaMetadata()
{
Expand Down
1 change: 1 addition & 0 deletions tools/props/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<SystemBuffersVersion>4.5.1</SystemBuffersVersion>
<SystemMemoryVersion>4.5.5</SystemMemoryVersion>
<SystemTextEncodingsWebVersion>6.0.0</SystemTextEncodingsWebVersion>
<SystemDataCommonVersion>4.3.0</SystemDataCommonVersion>
</PropertyGroup>
<!-- NetFx and NetCore project dependencies -->
<PropertyGroup>
Expand Down
1 change: 1 addition & 0 deletions tools/specs/Microsoft.Data.SqlClient.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ When using NuGet 3.x this package requires at least version 3.4.</description>
<dependency id="System.Buffers" version="4.5.1" />
<dependency id="System.Text.Encodings.Web" version="6.0.0" />
<dependency id="System.Text.Json" version="6.0.10" />
<dependency id="System.Data.Common" version="4.3.0" />
</group>
<group targetFramework="net8.0">
<dependency id="Microsoft.Data.SqlClient.SNI.runtime" version="6.0.0-preview1.24226.4" exclude="Compile" />
Expand Down
Loading