From 84bc56d145d60c887a532d07165ad10ae2973bb6 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Wed, 7 Aug 2024 07:30:54 -0700 Subject: [PATCH] Include Command Context --- .../Data/SqlClientX/SqlConnectionX.cs | 48 ------------------- .../Microsoft/Data/SqlClientX/SqlConnector.cs | 14 ++---- .../SqlClientX/Tds/State/TdsCommandContext.cs | 20 ++++++++ .../Tds/State/TdsTransactionState.cs | 10 ++++ .../Tds/Tokens/TokenStreamHandler.cs | 3 +- 5 files changed, 36 insertions(+), 59 deletions(-) create mode 100644 src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/Tds/State/TdsCommandContext.cs diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/SqlConnectionX.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/SqlConnectionX.cs index 760fa9f4b3..42ee245297 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/SqlConnectionX.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/SqlConnectionX.cs @@ -35,7 +35,6 @@ internal sealed class SqlConnectionX : DbConnection, ICloneable private SqlConnector? _internalConnection; private bool _disposed; - private int _closeCount; //TODO: Investigate if we can just use dataSource.ConnectionString. Do this when this class can resolve its own data source. private string _connectionString = string.Empty; @@ -337,53 +336,6 @@ protected override DbCommand CreateDbCommand() #region internal helpers - // If wrapCloseInAction is defined, then the action it defines will be run with the connection close action passed in as a parameter - // The close action also supports being run asynchronously - internal void OnError(SqlException exception, bool breakConnection, Action wrapCloseInAction) - { - Debug.Assert(exception != null && exception.Errors.Count != 0, "SqlConnection: OnError called with null or empty exception!"); - - if (breakConnection && (ConnectionState.Open == State)) - { - if (wrapCloseInAction != null) - { - int capturedCloseCount = _closeCount; - - Action closeAction = () => - { - if (capturedCloseCount == _closeCount) - { - Close(); - } - }; - - wrapCloseInAction(closeAction); - } - else - { - Close(); - } - } - - if (exception.Class >= TdsEnums.MIN_ERROR_CLASS) - { - // It is an error, and should be thrown. Class of TdsEnums.MIN_ERROR_CLASS or above is an error, - // below TdsEnums.MIN_ERROR_CLASS denotes an info message. - throw exception; - } - else - { - // If it is a class < TdsEnums.MIN_ERROR_CLASS, it is a warning collection - so pass to handler - this.OnInfoMessage(new SqlInfoMessageEventArgs(exception)); - } - } - - internal void OnInfoMessage(SqlInfoMessageEventArgs imevent) - { - bool notified; - OnInfoMessage(imevent, out notified); - } - internal void OnInfoMessage(SqlInfoMessageEventArgs imevent, out bool notified) { // TODO review event source traces later diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/SqlConnector.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/SqlConnector.cs index 5286aab962..11053e21e2 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/SqlConnector.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/SqlConnector.cs @@ -8,11 +8,7 @@ using System.Data; using System.Threading; using System.Threading.Tasks; -using Microsoft.Data.Common; -using Microsoft.Data.SqlClient; using Microsoft.Data.SqlClientX.Handlers.Connection; -using Microsoft.Data.SqlClientX.Tds; -using Microsoft.Data.SqlClientX.Tds.State; #nullable enable @@ -25,13 +21,12 @@ internal sealed class SqlConnector { private static int s_spoofedServerProcessId = 1; - private readonly TdsParserX _parser; + // private readonly TdsParserX _parser; private readonly ConnectionHandlerContext _connectionHandlerContext; - internal SqlConnector(SqlConnectionX? owningConnection, SqlConnectionString connectionOptions, SqlDataSource dataSource) + internal SqlConnector(SqlConnectionX? owningConnection, SqlDataSource dataSource) { OwningConnection = owningConnection; - ConnectionOptions = connectionOptions; DataSource = dataSource; //TODO: Set this based on the real server process id. @@ -44,7 +39,8 @@ internal SqlConnector(SqlConnectionX? owningConnection, SqlConnectionString conn // TODO initialize and pass ConnectionOptions into connection handler context }; - _parser = new TdsParserX(new TdsContext(this)); + // TODO enable parser registration with Parser introduction. + // _parser = new TdsParserX(new TdsContext(this)); } #region properties @@ -55,8 +51,6 @@ internal SqlConnector(SqlConnectionX? owningConnection, SqlConnectionString conn /// internal SqlDataSource DataSource { get; } - internal DbConnectionOptions ConnectionOptions { get; set; } - /// /// The server version this connector is connected to. /// diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/Tds/State/TdsCommandContext.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/Tds/State/TdsCommandContext.cs new file mode 100644 index 0000000000..cbeff3c075 --- /dev/null +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/Tds/State/TdsCommandContext.cs @@ -0,0 +1,20 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.Data.SqlClientX.Tds.State +{ + internal class TdsCommandContext + { + // TDS stream processing variables + /// + /// PLP data length indicator + /// + public ulong PlpLength; + + /// + /// Length of data left to read (64 bit lengths) + /// + public ulong PlpLengthLeft; + } +} diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/Tds/State/TdsTransactionState.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/Tds/State/TdsTransactionState.cs index 3465216409..ba8419a0ab 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/Tds/State/TdsTransactionState.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/Tds/State/TdsTransactionState.cs @@ -28,6 +28,16 @@ internal class TdsTransactionState internal SqlInternalTransaction PendingTransaction => _pendingTransaction; + internal void UpdateCurrentTransaction(SqlInternalTransaction transaction) + { + _currentTransaction = transaction; + } + + internal void UpdatePendingTransaction(SqlInternalTransaction transaction) + { + _pendingTransaction = transaction; + } + internal int IncrementNonTransactedOpenResultCount() { // IMPORTANT - this increments the connection wide open result count for all diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/Tds/Tokens/TokenStreamHandler.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/Tds/Tokens/TokenStreamHandler.cs index 7be1160edb..0c1ba3e64e 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/Tds/Tokens/TokenStreamHandler.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/Tds/Tokens/TokenStreamHandler.cs @@ -4,6 +4,7 @@ #if NET8_0_OR_GREATER +using System; using System.Collections.Generic; using System.Diagnostics; using System.Threading; @@ -54,7 +55,7 @@ public async ValueTask ReceiveTokenAsync(TdsContext context, bool isAsync { byte type = await context.TdsStream.ReadByteAsync(isAsync, ct).ConfigureAwait(false); - if (!TdsUtils.IsValidTdsToken(type)) + if (!Enum.IsDefined(typeof(TokenType), type)) { Debug.Fail($"unexpected token; token = {type-2:X2}"); context.ParserState = TdsParserState.Broken;