Skip to content

Commit 4117175

Browse files
authored
Merge | Add IDBColumnSchemaGenerator interface to netfx SqlDataReader (#2967)
1 parent 7d93424 commit 4117175

File tree

12 files changed

+92
-17
lines changed

12 files changed

+92
-17
lines changed

src/Microsoft.Data.SqlClient/netcore/ref/Microsoft.Data.SqlClient.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@ protected override void OnRowUpdating(System.Data.Common.RowUpdatingEventArgs va
12531253
object System.ICloneable.Clone() { throw null; }
12541254
}
12551255
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/SqlDataReader/*'/>
1256-
public partial class SqlDataReader : System.Data.Common.DbDataReader, System.Data.IDataReader, System.IDisposable
1256+
public partial class SqlDataReader : System.Data.Common.DbDataReader, System.Data.IDataReader, System.Data.Common.IDbColumnSchemaGenerator, System.IDisposable
12571257
{
12581258
internal SqlDataReader() { }
12591259
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/Connection/*'/>
@@ -1290,7 +1290,8 @@ public override void Close() { }
12901290
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetChars/*'/>
12911291
public override long GetChars(int i, long dataIndex, char[] buffer, int bufferIndex, int length) { throw null; }
12921292
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetColumnSchema/*'/>
1293-
public System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Common.DbColumn> GetColumnSchema() { throw null; }/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetDataTypeName/*'/>
1293+
public System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Common.DbColumn> GetColumnSchema() { throw null; }
1294+
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetDataTypeName/*'/>
12941295
public override string GetDataTypeName(int i) { throw null; }
12951296
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetDateTime/*'/>
12961297
public override System.DateTime GetDateTime(int i) { throw null; }

src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,9 @@
437437
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlDataAdapter.cs">
438438
<Link>Microsoft\Data\SqlClient\SqlDataAdapter.cs</Link>
439439
</Compile>
440+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlDbColumn.cs">
441+
<Link>Microsoft\Data\SqlClient\SqlDbColumn.cs</Link>
442+
</Compile>
440443
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlDelegatedTransaction.cs">
441444
<Link>Microsoft\Data\SqlClient\SqlDelegatedTransaction.cs</Link>
442445
</Compile>
@@ -654,7 +657,6 @@
654657
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionFactory.cs" />
655658
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionHelper.cs" />
656659
<Compile Include="Microsoft\Data\SqlClient\SqlDataReader.cs" />
657-
<Compile Include="Microsoft\Data\SqlClient\SqlDbColumn.cs" />
658660
<Compile Include="Microsoft\Data\SqlClient\SqlDiagnosticListener.cs" />
659661
<Compile Include="Microsoft\Data\SqlClient\SqlInternalConnectionTds.cs" />
660662
<Compile Include="Microsoft\Data\SqlClient\SqlTransaction.cs" />

src/Microsoft.Data.SqlClient/netfx/ref/Microsoft.Data.SqlClient.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ protected override void OnRowUpdating(System.Data.Common.RowUpdatingEventArgs va
12551255
protected override void TerminateBatching() { }
12561256
}
12571257
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/SqlDataReader/*'/>
1258-
public partial class SqlDataReader : System.Data.Common.DbDataReader, System.Data.IDataReader, System.IDisposable
1258+
public partial class SqlDataReader : System.Data.Common.DbDataReader, System.Data.IDataReader, System.Data.Common.IDbColumnSchemaGenerator, System.IDisposable
12591259
{
12601260
internal SqlDataReader() { }
12611261
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/Connection/*'/>
@@ -1291,6 +1291,8 @@ public override void Close() { }
12911291
public override char GetChar(int i) { throw null; }
12921292
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetChars/*'/>
12931293
public override long GetChars(int i, long dataIndex, char[] buffer, int bufferIndex, int length) { throw null; }
1294+
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetColumnSchema/*'/>
1295+
public System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Common.DbColumn> GetColumnSchema() { throw null; }
12941296
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetDataTypeName/*'/>
12951297
public override string GetDataTypeName(int i) { throw null; }
12961298
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetDateTime/*'/>

src/Microsoft.Data.SqlClient/netfx/ref/Microsoft.Data.SqlClient.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="$(MicrosoftIdentityModelJsonWebTokensVersion)" />
4545
<PackageReference Include="System.Buffers" Version="$(SystemBuffersVersion)" />
4646
<PackageReference Include="System.Text.Json" Version="$(SystemTextJsonVersion)" />
47+
<PackageReference Include="System.Data.Common" Version="$(SystemDataCommonVersion)" />
4748
<PackageReference Include="System.Security.Cryptography.Pkcs" Version="$(SystemSecurityCryptographyPkcsVersion)" />
4849
<PackageReference Include="Microsoft.Bcl.Cryptography" Version="$(MicrosoftBclCryptographyVersion)" />
4950
</ItemGroup>

src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,9 @@
649649
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlDataAdapter.cs">
650650
<Link>Microsoft\Data\SqlClient\SqlDataAdapter.cs</Link>
651651
</Compile>
652+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlDbColumn.cs">
653+
<Link>Microsoft\Data\SqlClient\SqlDbColumn.cs</Link>
654+
</Compile>
652655
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlDelegatedTransaction.cs">
653656
<Link>Microsoft\Data\SqlClient\SqlDelegatedTransaction.cs</Link>
654657
</Compile>

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDataReader.cs

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Collections;
7+
using System.Collections.ObjectModel;
78
using System.ComponentModel;
89
using System.Data;
910
using System.Data.Common;
@@ -27,7 +28,7 @@
2728
namespace Microsoft.Data.SqlClient
2829
{
2930
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/SqlDataReader/*' />
30-
public class SqlDataReader : DbDataReader, IDataReader
31+
public class SqlDataReader : DbDataReader, IDataReader, IDbColumnSchemaGenerator
3132
{
3233
private enum ALTROWSTATUS
3334
{
@@ -74,6 +75,7 @@ internal class SharedState
7475
private CommandBehavior _commandBehavior;
7576

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

7981

@@ -6360,5 +6362,73 @@ private void SwitchToAsyncWithoutSnapshot()
63606362
_stateObj._asyncReadWithoutSnapshot = true;
63616363
}
63626364

6363-
}// SqlDataReader
6364-
}// namespace
6365+
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetColumnSchema/*' />
6366+
public ReadOnlyCollection<DbColumn> GetColumnSchema()
6367+
{
6368+
SqlStatistics statistics = null;
6369+
try
6370+
{
6371+
statistics = SqlStatistics.StartTimer(Statistics);
6372+
if (_metaData == null || _metaData.dbColumnSchema == null)
6373+
{
6374+
if (this.MetaData != null)
6375+
{
6376+
6377+
_metaData.dbColumnSchema = BuildColumnSchema();
6378+
Debug.Assert(_metaData.dbColumnSchema != null, "No schema information yet!");
6379+
// filter table?
6380+
}
6381+
}
6382+
if (_metaData != null)
6383+
{
6384+
return _metaData.dbColumnSchema;
6385+
}
6386+
return s_emptySchema;
6387+
}
6388+
finally
6389+
{
6390+
SqlStatistics.StopTimer(statistics);
6391+
}
6392+
}
6393+
6394+
private ReadOnlyCollection<DbColumn> BuildColumnSchema()
6395+
{
6396+
_SqlMetaDataSet md = MetaData;
6397+
DbColumn[] columnSchema = new DbColumn[md.Length];
6398+
for (int i = 0; i < md.Length; i++)
6399+
{
6400+
_SqlMetaData col = md[i];
6401+
SqlDbColumn dbColumn = new SqlDbColumn(md[i]);
6402+
6403+
if (_typeSystem <= SqlConnectionString.TypeSystem.SQLServer2005 && col.Is2008DateTimeType)
6404+
{
6405+
dbColumn.SqlNumericScale = MetaType.MetaNVarChar.Scale;
6406+
}
6407+
else if (TdsEnums.UNKNOWN_PRECISION_SCALE != col.scale)
6408+
{
6409+
dbColumn.SqlNumericScale = col.scale;
6410+
}
6411+
else
6412+
{
6413+
dbColumn.SqlNumericScale = col.metaType.Scale;
6414+
}
6415+
6416+
if (_browseModeInfoConsumed)
6417+
{
6418+
dbColumn.SqlIsAliased = col.IsDifferentName;
6419+
dbColumn.SqlIsKey = col.IsKey;
6420+
dbColumn.SqlIsHidden = col.IsHidden;
6421+
dbColumn.SqlIsExpression = col.IsExpression;
6422+
}
6423+
6424+
dbColumn.SqlDataType = GetFieldTypeInternal(col);
6425+
6426+
dbColumn.SqlDataTypeName = GetDataTypeNameInternal(col);
6427+
6428+
columnSchema[i] = dbColumn;
6429+
}
6430+
6431+
return new ReadOnlyCollection<DbColumn>(columnSchema);
6432+
}
6433+
}
6434+
}

src/Microsoft.Data.SqlClient/src/Microsoft.Data.SqlClient.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
<PackageReference Include="Microsoft.Bcl.Cryptography" Version="$(MicrosoftBclCryptographyVersion)" />
1313
<PackageReference Include="System.Buffers" Version="$(SystemBuffersVersion)" />
1414
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
15+
<PackageReference Include="System.Data.Common" Version="$(SystemDataCommonVersion)" Condition="'$(TargetFramework)' == 'net462'" />
1516
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="$(SystemRuntimeInteropServicesRuntimeInformationVersion)" />
1617
<PackageReference Include="System.Security.Cryptography.Pkcs" Version="$(SystemSecurityCryptographyPkcsVersion)" />
1718
</ItemGroup>
18-
1919
</Project>

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserHelperClasses.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,7 @@ internal sealed partial class _SqlMetaDataSet
305305

306306
internal DataTable schemaTable;
307307
private readonly _SqlMetaData[] _metaDataArray;
308-
#if !NETFRAMEWORK
309308
internal ReadOnlyCollection<DbColumn> dbColumnSchema;
310-
#endif
311309

312310
private int _hiddenColumnCount;
313311
private int[] _visibleColumnMap;
@@ -327,11 +325,9 @@ private _SqlMetaDataSet(_SqlMetaDataSet original)
327325
id = original.id;
328326
_hiddenColumnCount = original._hiddenColumnCount;
329327
_visibleColumnMap = original._visibleColumnMap;
330-
#if !NETFRAMEWORK
331328
dbColumnSchema = original.dbColumnSchema;
332-
#else
333329
schemaTable = original.schemaTable;
334-
#endif
330+
335331
if (original._metaDataArray == null)
336332
{
337333
_metaDataArray = null;

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/UdtTest/SqlServerTypesTest.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Collections.Generic;
77
using System.Collections.ObjectModel;
88
using System.Data;
9-
using System.Data.Common;
109
using System.Data.SqlTypes;
1110
using System.Globalization;
1211
using System.IO;
@@ -252,7 +251,6 @@ private static void TestUdtSqlDataReaderGetStream(CommandBehavior behavior)
252251
}
253252

254253
// Synapse: Parse error at line: 1, column: 41: Incorrect syntax near 'hierarchyid'.
255-
[ActiveIssue("25421", TargetFrameworkMonikers.NetFramework)]
256254
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
257255
public static void TestUdtSchemaMetadata()
258256
{
@@ -263,9 +261,9 @@ public static void TestUdtSchemaMetadata()
263261
command.CommandText = "select hierarchyid::Parse('/1/1/3/') as col0, geometry::Parse('LINESTRING (100 100, 20 180, 180 180)') as col1, geography::Parse('LINESTRING(-122.360 47.656, -122.343 47.656)') as col2";
264262
using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.SchemaOnly))
265263
{
266-
ReadOnlyCollection<DbColumn> columns = reader.GetColumnSchema();
264+
ReadOnlyCollection<System.Data.Common.DbColumn> columns = reader.GetColumnSchema();
267265

268-
DbColumn column = null;
266+
System.Data.Common.DbColumn column = null;
269267

270268
// Validate Microsoft.SqlServer.Types.SqlHierarchyId, Microsoft.SqlServer.Types, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91
271269
column = columns[0];

0 commit comments

Comments
 (0)