Skip to content

Cleanup | Remove dead code paths and Regexes from SqlConnectionOptions - #4544

Open
edwardneal wants to merge 12 commits into
dotnet:mainfrom
edwardneal:cleanup/sqlconnectionoptions-regex
Open

edwardneal wants to merge 12 commits into
dotnet:mainfrom
edwardneal:cleanup/sqlconnectionoptions-regex

Conversation

@edwardneal

Copy link
Copy Markdown
Contributor

Description

SqlConnectionOptions has a handful of code paths which were inherited from the need to write a generic parser for ODBC connection strings. These code paths are no longer used, so this PR just cleans them up.

In the case of GetKeyValuePair, there are a few code paths which could never be called, and codecov demonstrates this. In the other cases, they were criteria which would always be met.

One interesting point emerges from this: SqlConnectionOptions has four statically-initialised compiled Regex instances. One of them is completely unused, one is only used by netfx code and two are only used in Debug builds. I've cleaned these up, so the static constructor for the class sheds some load.

This is cleanup work which has an incidental performance benefit - it doesn't have a benchmark attached to it, and I couldn't see any results because we're dealing with static constructors.

For review, this can move commit-by-commit.

Issues

None.

Testing

All unit tests for connection string parsing continue to pass. All changes can be statically verified.

ConnectionStringRegexOdbc was rendered unused by the previous commit.
This helper uses managed code rather than the the s_connectionStringValidKeyRegex Regex, but retains a Debug-only assertion.
These are now only used in debug builds
These are only used by netfx codepaths, and do not need to be instantiated on netcore
BraceQuoteValue and BraceQuoteValueQuote were only accessible from one another, and when useOdbcRules was true.
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@github-actions

Copy link
Copy Markdown

This pull request has been marked as stale due to inactivity for more than 30 days.

If you would like to keep this pull request open, please provide an update or respond to any comments. Otherwise, it will be closed automatically in 7 days.

@github-actions github-actions Bot added the Stale The Issue or PR has become stale and will be automatically closed shortly if no activity occurs. label Sep 16, 2026
@edwardneal

Copy link
Copy Markdown
Contributor Author

This PR is not stale.

@github-actions github-actions Bot removed the Stale The Issue or PR has become stale and will be automatically closed shortly if no activity occurs. label Sep 17, 2026
@mdaigle mdaigle added this to the 8.0.0-preview1 milestone Sep 18, 2026
@cheenamalhotra cheenamalhotra moved this from To triage to In review in SqlClient Board Sep 23, 2026
@paulmedynski
paulmedynski requested a balanced review from Copilot September 29, 2026 12:02
@paulmedynski

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.

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.

Copilot review overview

🟢 Approval recommended

The removed branches correspond to invariant arguments, and all affected callers remain behaviorally consistent.

Review effort: Balanced
Findings: None

What changed in this PR

Removes unreachable ODBC parser branches and avoids unnecessary regex initialization while preserving SQL connection-string behavior.

Changes:

  • Simplifies parser state and method signatures.
  • Restricts validation regexes to required build targets.
  • Updates affected callers.
File Description
SqlConnectionOptions.Debug.cs Removes unused ODBC debug parsing.
SqlConnectionOptions.cs Simplifies parsing and validation paths.
SqlConnectionInternal.cs Updates local-host verification call.
DbConnectionString.netfx.cs Updates restriction parsing call.

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

private const string ConnectionStringValidKeyPattern = "^(?![;\\s])[^\\p{Cc}]+(?<!\\s)$"; // key not allowed to start with semi-colon or space or contain non-visible characters or end with space
private const string ConnectionStringValidValuePattern = "^[^\u0000]*$"; // value not allowed to contain embedded null
#if NETFRAMEWORK
private const string ConnectionStringQuoteValuePattern = "^[^\"'=;\\s\\p{Cc}]*$"; // generally do not quote the value if it matches the pattern

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.

We don't need this string constant either - it is only used in the regex constructor below.

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.

Fixed in c759185, thanks.

internal sealed partial class SqlConnectionOptions
{
#if DEBUG
private const string ConnectionStringValidKeyPattern = "^(?![;\\s])[^\\p{Cc}]+(?<!\\s)$"; // key not allowed to start with semi-colon or space or contain non-visible characters or end with space

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.

These string constants can be folded into the regex constructors as well.

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.

Also fixed in c759185.

Also renamed ConnectionStringRegex to fit wider naming convention
@paulmedynski

Copy link
Copy Markdown
Contributor

/azp run

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Waiting for customer

Development

Successfully merging this pull request may close these issues.

5 participants