Skip to content

Commit

Permalink
Include Command Context
Browse files Browse the repository at this point in the history
  • Loading branch information
cheenamalhotra committed Aug 7, 2024
1 parent 6a8ab66 commit 84bc56d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand All @@ -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
Expand All @@ -55,8 +51,6 @@ internal SqlConnector(SqlConnectionX? owningConnection, SqlConnectionString conn
/// </summary>
internal SqlDataSource DataSource { get; }

internal DbConnectionOptions ConnectionOptions { get; set; }

/// <summary>
/// The server version this connector is connected to.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
/// <summary>
/// PLP data length indicator
/// </summary>
public ulong PlpLength;

/// <summary>
/// Length of data left to read (64 bit lengths)
/// </summary>
public ulong PlpLengthLeft;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#if NET8_0_OR_GREATER

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
Expand Down Expand Up @@ -54,7 +55,7 @@ public async ValueTask<Token> 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;
Expand Down

0 comments on commit 84bc56d

Please sign in to comment.