Skip to content

Refactor TDS parser into focused namespaces and files - #4675

Open
benrr101 wants to merge 12 commits into
mainfrom
dev/russellben/tds-groups
Open

benrr101 wants to merge 12 commits into
mainfrom
dev/russellben/tds-groups

Conversation

@benrr101

Copy link
Copy Markdown
Contributor

Description

Refactors the TDS parser implementation into focused files and namespaces without changing its public API or intended behavior.

  • Splits helper types, enums, safe handles, parser state, and related classes out of the large legacy source files.
  • Moves parser implementation types under Microsoft.Data.SqlClient.Parser.
  • Groups login and token-related types under Parser.Login and Parser.Tokens.
  • Preserves file history through step-wise moves so reviewers can distinguish relocation from structural cleanup.
  • Updates internal references and affected tests for the new namespaces.

This is layer 1 of 3 in the TDS parser organization stack and targets main.

Issues

No linked issue; this is an internal code-organization refactor.

Testing

No behavior change is intended. Existing functional and unit test sources were updated to use the reorganized parser namespaces; CI will exercise the supported target frameworks and platforms.

Guidelines

Copilot AI balanced review requested due to automatic review settings September 10, 2026 23:54
@benrr101
benrr101 requested a review from a team as a code owner September 10, 2026 23:54
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Sep 10, 2026
@benrr101
benrr101 added this pull request to stack #4678 September 10, 2026 23:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Several manual and stress tests retain obsolete reflected type and stack-frame names and will fail or stop filtering correctly.

Pull request overview

Refactors internal TDS parser types into focused Parser, Parser.Login, and Parser.Tokens namespaces without intended public API changes.

Changes:

  • Splits parser helpers, metadata, login, token, and native-handle types into dedicated files.
  • Updates production and test imports for the new namespaces.
  • Relocates existing public enums into standalone files.
File summaries
File Description
tests/UnitTests/TdsParserInternalsTest.cs Imports parser namespace.
tests/UnitTests/SimulatedServerTests/TdsTokenBoundsTests.cs Imports parser namespace.
tests/UnitTests/SimulatedServerTests/FeatureExtAckBoundsTests.cs Imports parser namespace.
tests/UnitTests/Microsoft/Data/SqlClient/TdsParserStateObjectNativeTests.cs Imports parser namespace.
tests/UnitTests/Microsoft/Data/SqlClient/StateSnapshotPacketRecyclingTests.cs Imports parser namespace.
tests/UnitTests/Microsoft/Data/SqlClient/SqlMetaDataSetTest.cs Imports token namespace.
tests/UnitTests/Microsoft/Data/SqlClient/ManagedSni/SniPacketTests.cs Imports parser namespace.
tests/UnitTests/Microsoft/Data/Common/AdapterUtilTest.cs Imports parser namespace.
tests/FunctionalTests/TdsParserStateObject.TestHarness.cs Moves harness into parser namespace.
tests/FunctionalTests/MultiplexerTests.cs Imports parser namespace.
tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj Updates relocated source paths.
src/Microsoft/Data/SqlTypes/SqlVector.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/TransparentNetworkResolutionState.cs Extracts TNIR state enum.
src/Microsoft/Data/SqlClient/TransactionRequest.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/TdsParserHelperClasses.cs Removes split-out helper definitions.
src/Microsoft/Data/SqlClient/TdsParameterSetter.cs Removes relocated setter.
src/Microsoft/Data/SqlClient/SSPI/SspiContextProvider.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/SSPI/NativeSspiContextProvider.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/SqlUtil.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/SqlTransaction.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/SqlSecurityUtility.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/SqlParameter.cs Imports parser and token namespaces.
src/Microsoft/Data/SqlClient/SqlMetaDataFactory.DataTypes.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/SqlInternalTransaction.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/SqlEnvChange.cs Imports token namespace.
src/Microsoft/Data/SqlClient/SqlEnums.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/SqlDbColumn.cs Imports parser metadata types.
src/Microsoft/Data/SqlClient/SqlDataReader.cs Imports parser metadata types.
src/Microsoft/Data/SqlClient/SqlCredential.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/SqlConnectionStringBuilder.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/SqlConnectionOverrides.cs Extracts public enum.
src/Microsoft/Data/SqlClient/SqlConnectionOptions.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/SqlConnectionIPAddressPreference.cs Extracts public enum.
src/Microsoft/Data/SqlClient/SqlConnectionColumnEncryptionSetting.cs Extracts public enum.
src/Microsoft/Data/SqlClient/SqlConnectionAttestationProtocol.cs Extracts public enum.
src/Microsoft/Data/SqlClient/SqlConnection.cs Imports parser types.
src/Microsoft/Data/SqlClient/SqlCommandColumnEncryptionSetting.cs Extracts public enum.
src/Microsoft/Data/SqlClient/SqlCommand.Xml.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/SqlCommand.Scalar.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/SqlCommand.Reader.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/SqlCommand.NonQuery.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/SqlCommand.Encryption.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/SqlCommand.cs Imports parser and token namespaces.
src/Microsoft/Data/SqlClient/SqlCommand.Batch.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/SqlCollation.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/SqlCachedBuffer.cs Imports parser metadata types.
src/Microsoft/Data/SqlClient/SqlBulkCopy.cs Imports parser metadata types.
src/Microsoft/Data/SqlClient/SqlBuffer.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/Server/MetadataUtilsSmi.cs Imports parser namespace.
src/Microsoft/Data/SqlClient/Parser/Tokens/SqlReturnValue.cs Extracts return-value metadata.
src/Microsoft/Data/SqlClient/Parser/Tokens/SqlMetaDataXmlSchemaCollection.cs Extracts XML metadata.
src/Microsoft/Data/SqlClient/Parser/Tokens/SqlMetaDataUdt.cs Extracts UDT metadata.
src/Microsoft/Data/SqlClient/Parser/Tokens/SqlMetaDataPriv.cs Consolidates private metadata.
src/Microsoft/Data/SqlClient/Parser/Tokens/SqlFedAuthInfo.cs Extracts federated-auth token data.
src/Microsoft/Data/SqlClient/Parser/Tokens/RoutingInfo.cs Extracts routing token data.
src/Microsoft/Data/SqlClient/Parser/Tokens/MultiPartTableName.cs Extracts multipart-name parsing.
src/Microsoft/Data/SqlClient/Parser/Tokens/_SqlMetaDataSetCollection.cs Extracts metadata collection.
src/Microsoft/Data/SqlClient/Parser/Tokens/_SqlMetaDataSet.cs Consolidates metadata-set logic.
src/Microsoft/Data/SqlClient/Parser/Tokens/_SqlMetaData.cs Extracts column metadata.
src/Microsoft/Data/SqlClient/Parser/TdsValueSetter.cs Moves value writer into parser namespace.
src/Microsoft/Data/SqlClient/Parser/TdsRecordBufferSetter.cs Moves record writer into parser namespace.
src/Microsoft/Data/SqlClient/Parser/TdsParserStaticMethods.cs Moves parser utilities.
src/Microsoft/Data/SqlClient/Parser/TdsParserStateObjectNative.cs Moves native parser state.
src/Microsoft/Data/SqlClient/Parser/TdsParserStateObjectManaged.netcore.cs Moves managed parser state.
src/Microsoft/Data/SqlClient/Parser/TdsParserStateObjectFactory.cs Moves parser-state factory.
src/Microsoft/Data/SqlClient/Parser/TdsParserStateObject.Multiplexer.cs Moves multiplexer implementation.
src/Microsoft/Data/SqlClient/Parser/TdsParserStateObject.cs Moves core parser state.
src/Microsoft/Data/SqlClient/Parser/TdsParserState.cs Extracts parser-state enum.
src/Microsoft/Data/SqlClient/Parser/TdsParserSessionPool.cs Moves session pool.
src/Microsoft/Data/SqlClient/Parser/TdsParser.cs Moves core parser.
src/Microsoft/Data/SqlClient/Parser/TdsParameterSetter.cs Relocates parameter setter.
src/Microsoft/Data/SqlClient/Parser/TdsOperationsStatus.cs Extracts operation-status enum.
src/Microsoft/Data/SqlClient/Parser/SslProtocolHelper.cs Extracts TLS warning helper.
src/Microsoft/Data/SqlClient/Parser/SNIPacket.cs Extracts native packet handle.
src/Microsoft/Data/SqlClient/Parser/SNILoadHandle.cs Moves native SNI loader.
src/Microsoft/Data/SqlClient/Parser/SNIHandle.cs Extracts native connection handle.
src/Microsoft/Data/SqlClient/Parser/SniContext.cs Extracts SNI context enum.
src/Microsoft/Data/SqlClient/Parser/SessionHandle.netfx.cs Moves .NET Framework session handle.
src/Microsoft/Data/SqlClient/Parser/SessionHandle.netcore.cs Moves .NET session handle.
src/Microsoft/Data/SqlClient/Parser/RunBehavior.cs Extracts parser run behavior.
src/Microsoft/Data/SqlClient/Parser/ParsingErrorState.cs Extracts parsing-error enum.
src/Microsoft/Data/SqlClient/Parser/PacketHandle.netfx.cs Moves .NET Framework packet handle.
src/Microsoft/Data/SqlClient/Parser/PacketHandle.netcore.cs Moves .NET packet handle.
src/Microsoft/Data/SqlClient/Parser/Packet.cs Moves packet reconstruction helper.
src/Microsoft/Data/SqlClient/Parser/Login/SqlLogin.cs Extracts login state.
src/Microsoft/Data/SqlClient/Parser/Login/SqlFedAuthToken.cs Extracts federated-auth token.
src/Microsoft/Data/SqlClient/Parser/Login/PreLoginOptions.cs Extracts pre-login options.
src/Microsoft/Data/SqlClient/Parser/Login/PreLoginHandshakeStatus.cs Extracts handshake status.
src/Microsoft/Data/SqlClient/Parser/Login/FederatedAuthenticationFeatureExtensionData.cs Extracts federated-auth extension data.
src/Microsoft/Data/SqlClient/Parser/Login/EncryptionOptions.cs Extracts encryption options.
src/Microsoft/Data/SqlClient/Parser/LastIOTimer.cs Extracts I/O timer holder.
src/Microsoft/Data/SqlClient/Parser/DescribeParameterEncryptionResultSet3.cs Extracts third result-set ordinals.
src/Microsoft/Data/SqlClient/Parser/DescribeParameterEncryptionResultSet2.cs Extracts second result-set ordinals.
src/Microsoft/Data/SqlClient/Parser/DescribeParameterEncryptionResultSet1.cs Extracts first result-set ordinals.
src/Microsoft/Data/SqlClient/Parser/CallbackType.cs Extracts callback enum.
src/Microsoft/Data/SqlClient/Parser/_SqlRPC.cs Extracts RPC state.
src/Microsoft/Data/SqlClient/ManagedSni/SslOverTdsStream.netcore.cs Imports parser constants.
src/Microsoft/Data/SqlClient/ManagedSni/SniTcpHandle.netcore.cs Imports parser types.
src/Microsoft/Data/SqlClient/ManagedSni/SniProxy.netcore.cs Imports parser types.
src/Microsoft/Data/SqlClient/ManagedSni/SniPacket.netcore.cs Imports parser types.
src/Microsoft/Data/SqlClient/ManagedSni/SniNpHandle.netcore.cs Imports parser types.
src/Microsoft/Data/SqlClient/ManagedSni/SniMarsHandle.netcore.cs Imports parser types.
src/Microsoft/Data/SqlClient/ManagedSni/SniMarsConnection.netcore.cs Imports parser types.
src/Microsoft/Data/SqlClient/ManagedSni/SniLoadHandle.netcore.cs Imports parser/login types.
src/Microsoft/Data/SqlClient/ManagedSni/SniHandle.netcore.cs Imports parser types.
src/Microsoft/Data/SqlClient/ManagedSni/SniError.netcore.cs Imports parser types.
src/Microsoft/Data/SqlClient/ManagedSni/SniCommon.netcore.cs Imports parser types.
src/Microsoft/Data/SqlClient/ManagedSni/LocalDB.netcore.cs Imports parser types.
src/Microsoft/Data/SqlClient/LocalDb/LocalDbApi.cs Imports parser types.
src/Microsoft/Data/SqlClient/Connection/SqlConnectionInternal.cs Imports parser, login, and token types.
src/Microsoft/Data/SqlClient/Connection/ServerInfo.cs Imports parser routing types.
src/Microsoft/Data/SqlClient/Connection/ConnectionCapabilities.cs Imports parser constants.
src/Microsoft/Data/SqlClient/AlwaysEncryptedHelperClasses.cs Removes relocated metadata partials.
src/Microsoft/Data/SqlClient/ActiveDirectoryAuthenticationTimeoutRetryHelper.cs Imports login token type.
src/Microsoft/Data/Sql/SqlDataSourceEnumeratorNativeHelper.cs Imports parser types.
src/Microsoft/Data/Common/AdapterUtil.cs Imports parser types.
src/Interop/Windows/Sni/SniNativeWrapper.cs Imports relocated native handles.
src/Interop/Windows/Sni/SniNativeMethodsX86.netfx.cs Imports parser handles.
src/Interop/Windows/Sni/SniNativeMethodsX64.netfx.cs Imports parser handles.
src/Interop/Windows/Sni/SniNativeMethodsNotSupported.netfx.cs Imports parser handles.
src/Interop/Windows/Sni/SniNativeMethodsArm64.netfx.cs Imports parser handles.
src/Interop/Windows/Sni/SniNativeMethods.netcore.cs Imports parser handles.
src/Interop/Windows/Sni/ISniNativeMethods.cs Imports parser handles.
Review details

Suppressed comments (3)

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Parser/TdsParser.cs:40

  • This namespace move leaves reflection consumers pointing at Microsoft.Data.SqlClient.TdsParser: ConnectionHelper.cs:52 and TdsParserHelper.cs:13 now receive null from Assembly.GetType (the former dereferences it during static initialization), and the stress-test filter at FilteredDefaultTraceListener.cs:84 no longer matches the new stack-frame name. Update those strings to Microsoft.Data.SqlClient.Parser.TdsParser as part of this move.
    src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Parser/TdsParserStateObject.cs:28
  • Several reflection-based tests still request Microsoft.Data.SqlClient.TdsParserStateObject (ConnectionHelper.cs:57, TdsParserStateObjectHelper.cs:27, and MARSTest.cs:87/135). After this move, Assembly.GetType returns null, causing static-initializer assertion/null-reference failures; the stress filter at FilteredDefaultTraceListener.cs:85-86 also stops matching. Update all of these strings to the new Microsoft.Data.SqlClient.Parser.TdsParserStateObject name.
    src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Parser/TdsParserStateObjectManaged.netcore.cs:21
  • TdsParserStateObjectHelper.cs:48 still reflects Microsoft.Data.SqlClient.ManagedSni.TdsParserStateObjectManaged. On .NET builds this now resolves to null; the helper treats that as framework absence and later throws when a managed session handle is requested. Update the reflected name to Microsoft.Data.SqlClient.Parser.TdsParserStateObjectManaged.
  • Files reviewed: 126/126 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be named SslProtocolsHelper.cs to match the containing type name.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be named TdsOperationStatus.cs to match the containing type name.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This, SNIPacket and SNIHandle are native SNI types, it should go in a new NativeSni namespace (to align with the existing ManagedSni one)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your thinking. This makes loads of sense to me, so despite it growing the scope of the changes in this PR, it's going in.

public const byte FEATUREEXT_USERAGENT = 0x10;

[Flags]
public enum FeatureExtension : uint

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the rationale behind leaving these three enums in situ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically ... TdsEnums was too much for me to look into 😅
Most of the file isn't actually enums, they're constants. Some of them should be enums, some of them are better off as constants. It seemed like a bigger change than I wanted to tackle with these mostly unoffensive changes.

@benrr101
benrr101 force-pushed the dev/russellben/tds-groups branch from d3821a1 to 2bbf4b1 Compare September 11, 2026 16:48
@benrr101 benrr101 added this to the 8.0.0-preview1 milestone Sep 11, 2026
This creates a stub namespace that native SNI will go into
Copilot AI review requested due to automatic review settings September 11, 2026 18:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Reflection-based manual and stress tests still use the former parser type names, causing failures after the namespace migration.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details

Suppressed comments (3)

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Parser/TdsParser.cs:40

  • The namespace move was not propagated to reflection-based test infrastructure. tests/ManualTests/SQL/Common/SystemDataInternals/ConnectionHelper.cs:52 and TdsParserHelper.cs:13 still request Microsoft.Data.SqlClient.TdsParser; Assembly.GetType now returns null and their static field initialization dereferences it. Update those type-name strings to Microsoft.Data.SqlClient.Parser.TdsParser; the stress-test frame filter at FilteredDefaultTraceListener.cs:84 also needs the new prefix.
    src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Parser/TdsParserStateObject.cs:28
  • Several manual tests still resolve Microsoft.Data.SqlClient.TdsParserStateObject by name (MARSTest.cs:87,135, ConnectionHelper.cs:57, and TdsParserStateObjectHelper.cs:27). After this move those lookups return null, and the subsequent GetField calls or assertions fail. Update all reflection strings and the stress-test stack-frame entries to Microsoft.Data.SqlClient.Parser.TdsParserStateObject.
    src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Parser/TdsParserStateObjectManaged.netcore.cs:21
  • TdsParserStateObjectHelper.cs:48 still looks up this type as Microsoft.Data.SqlClient.ManagedSni.TdsParserStateObjectManaged. The lookup now returns null, so GetSessionHandle throws “Library being tested does not implement TdsParserStateObjectManaged” for the managed implementation. Update the reflection string to Microsoft.Data.SqlClient.Parser.TdsParserStateObjectManaged.
  • Files reviewed: 126/126 changed files
  • Comments generated: 1
  • Review effort level: Balanced (auto)

Note

Copilot is running an experiment and ran this review at Balanced.

using System;
using System.Data;

namespace Microsoft.Data.SqlClient.Parser.Tokens;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To triage

Development

Successfully merging this pull request may close these issues.

5 participants