Skip to content

Cleanup | Merge SqlDataRecord and remove context connection remnants #3454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,6 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Server\SmiMetaDataProperty.cs">
<Link>Microsoft\Data\SqlClient\Server\SmiMetaDataProperty.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Server\SmiRecordBuffer.cs">
<Link>Microsoft\Data\SqlClient\Server\SmiRecordBuffer.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Server\SmiSettersStream.cs">
<Link>Microsoft\Data\SqlClient\Server\SmiSettersStream.cs</Link>
</Compile>
Expand All @@ -450,9 +447,6 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Server\SqlDataRecord.cs">
<Link>Microsoft\Data\SqlClient\Server\SqlDataRecord.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Server\SqlDataRecord.netcore.cs">
<Link>Microsoft\Data\SqlClient\Server\SqlDataRecord.netcore.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Server\SqlMetaData.cs">
<Link>Microsoft\Data\SqlClient\Server\SqlMetaData.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,6 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Server\SmiMetaDataProperty.cs">
<Link>Microsoft\Data\SqlClient\Server\SmiMetaDataProperty.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Server\SmiRecordBuffer.cs">
<Link>Microsoft\Data\SqlClient\Server\SmiRecordBuffer.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Server\SmiSettersStream.cs">
<Link>Microsoft\Data\SqlClient\Server\SmiSettersStream.cs</Link>
</Compile>
Expand All @@ -561,9 +558,6 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Server\SqlDataRecord.cs">
<Link>Microsoft\Data\SqlClient\Server\SqlDataRecord.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Server\SqlDataRecord.netfx.cs">
<Link>Microsoft\Data\SqlClient\Server\SqlDataRecord.netfx.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Server\SqlMetaData.cs">
<Link>Microsoft\Data\SqlClient\Server\SqlMetaData.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1988,11 +1988,7 @@ internal TdsParser Parser
{
get
{
SqlInternalConnectionTds tdsConnection = (GetOpenConnection() as SqlInternalConnectionTds);
if (tdsConnection == null)
{
throw SQL.NotAvailableOnContextConnection();
}
SqlInternalConnectionTds tdsConnection = GetOpenTdsConnection();
return tdsConnection.Parser;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1455,14 +1455,6 @@ internal static ArgumentException InvalidValue(string parameterName)
{
return Argument(StringsHelper.GetString(Strings.ADP_InvalidValue), parameterName);
}
internal static ArgumentException InvalidMixedArgumentOfSecureCredentialAndContextConnection()
{
return ADP.Argument(StringsHelper.GetString(Strings.ADP_InvalidMixedUsageOfSecureCredentialAndContextConnection));
}
internal static InvalidOperationException InvalidMixedUsageOfAccessTokenAndContextConnection()
{
return ADP.InvalidOperation(StringsHelper.GetString(Strings.ADP_InvalidMixedUsageOfAccessTokenAndContextConnection));
}
internal static Exception InvalidMixedUsageOfAccessTokenAndCredential()
{
return ADP.InvalidOperation(StringsHelper.GetString(Strings.ADP_InvalidMixedUsageOfAccessTokenAndCredential));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ internal interface ITypedGettersV3

// valid for UniqueIdentifier
Guid GetGuid(int ordinal);
// valid for SqlDbType.Time
TimeSpan GetTimeSpan(int ordinal);
// valid for DateTimeOffset
DateTimeOffset GetDateTimeOffset(int ordinal);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ internal interface ITypedSettersV3

// valid for UniqueIdentifier
void SetGuid(int ordinal, Guid value);
// valid for SqlDbType.Time
void SetTimeSpan(int ordinal, TimeSpan value);
// valid for DateTimeOffset
void SetDateTimeOffset(int ordinal, DateTimeOffset value);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Microsoft.Data.SqlClient.Server
{
// Class for implementing a record object used in out-of-proc scenarios.
internal sealed class MemoryRecordBuffer : SmiRecordBuffer
internal sealed class MemoryRecordBuffer : SmiTypedGetterSetter
{
private SqlRecordBuffer[] _buffer;

Expand All @@ -25,6 +25,12 @@ internal MemoryRecordBuffer(SmiMetaData[] metaData)
}
}

#region Read/Write
protected override bool CanGet => true;

protected override bool CanSet => true;
#endregion

#region Getters
// Null test
// valid for all types
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Microsoft.Data.SqlClient.Server
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml' path='docs/members[@name="SqlDataRecord"]/SqlDataRecord/*' />
public partial class SqlDataRecord : IDataRecord
{
private readonly SmiRecordBuffer _recordBuffer;
private readonly MemoryRecordBuffer _recordBuffer;
private readonly SmiExtendedMetaData[] _columnSmiMetaData;
private readonly SqlMetaData[] _columnMetaData;
private FieldNameLookup _fieldNameLookup;
Expand Down Expand Up @@ -54,10 +54,22 @@ public virtual string GetDataTypeName(int ordinal)
#if NET
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
public virtual Type GetFieldType(int ordinal) => GetFieldTypeFrameworkSpecific(ordinal);
public virtual Type GetFieldType(int ordinal)
{
SqlMetaData md = GetSqlMetaData(ordinal);

#if NETFRAMEWORK
if (md.SqlDbType == SqlDbType.Udt)
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this check only applies to netframework and will return, let's avoid the braces for netcore

#if NETFRAMEWORK
if (md.SqlDbType == SqlDbType.Udt)
{
    return md.Type;
}
#endif

return MetaType.GetMetaTypeFromSqlDbType(md.SqlDbType, false).ClassType;

Copy link
Contributor

Choose a reason for hiding this comment

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

Also tiny request, can we get an argument label for the false here? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes - done!

{
return md.Type;
}
#endif

return MetaType.GetMetaTypeFromSqlDbType(md.SqlDbType, isMultiValued: false).ClassType;
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml' path='docs/members[@name="SqlDataRecord"]/GetValue/*' />
public virtual object GetValue(int ordinal) => GetValueFrameworkSpecific(ordinal);
public virtual object GetValue(int ordinal) => ValueUtilsSmi.GetValue200(_recordBuffer, ordinal, GetSmiMetaData(ordinal));

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml' path='docs/members[@name="SqlDataRecord"]/GetValues/*' />
public virtual int GetValues(object[] values)
Expand Down Expand Up @@ -177,7 +189,7 @@ public virtual SqlMetaData GetSqlMetaData(int ordinal)
public virtual Type GetSqlFieldType(int ordinal) => MetaType.GetMetaTypeFromSqlDbType(GetSqlMetaData(ordinal).SqlDbType, false).SqlType;

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml' path='docs/members[@name="SqlDataRecord"]/GetSqlValue/*' />
public virtual object GetSqlValue(int ordinal) => GetSqlValueFrameworkSpecific(ordinal);
public virtual object GetSqlValue(int ordinal) => ValueUtilsSmi.GetSqlValue200(_recordBuffer, ordinal, GetSmiMetaData(ordinal));

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml' path='docs/members[@name="SqlDataRecord"]/GetSqlValues/*' />
public virtual int GetSqlValues(object[] values)
Expand All @@ -200,10 +212,10 @@ public virtual int GetSqlValues(object[] values)
public virtual SqlBinary GetSqlBinary(int ordinal) => ValueUtilsSmi.GetSqlBinary(_recordBuffer, ordinal, GetSmiMetaData(ordinal));

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml' path='docs/members[@name="SqlDataRecord"]/GetSqlBytes/*' />
public virtual SqlBytes GetSqlBytes(int ordinal) => GetSqlBytesFrameworkSpecific(ordinal);
public virtual SqlBytes GetSqlBytes(int ordinal) => ValueUtilsSmi.GetSqlBytes(_recordBuffer, ordinal, GetSmiMetaData(ordinal));

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml' path='docs/members[@name="SqlDataRecord"]/GetSqlXml/*' />
public virtual SqlXml GetSqlXml(int ordinal) => GetSqlXmlFrameworkSpecific(ordinal);
public virtual SqlXml GetSqlXml(int ordinal) => ValueUtilsSmi.GetSqlXml(_recordBuffer, ordinal, GetSmiMetaData(ordinal));

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml' path='docs/members[@name="SqlDataRecord"]/GetSqlBoolean/*' />
public virtual SqlBoolean GetSqlBoolean(int ordinal) => ValueUtilsSmi.GetSqlBoolean(_recordBuffer, ordinal, GetSmiMetaData(ordinal));
Expand All @@ -212,7 +224,7 @@ public virtual int GetSqlValues(object[] values)
public virtual SqlByte GetSqlByte(int ordinal) => ValueUtilsSmi.GetSqlByte(_recordBuffer, ordinal, GetSmiMetaData(ordinal));

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml' path='docs/members[@name="SqlDataRecord"]/GetSqlChars/*' />
public virtual SqlChars GetSqlChars(int ordinal) => GetSqlCharsFrameworkSpecific(ordinal);
public virtual SqlChars GetSqlChars(int ordinal) => ValueUtilsSmi.GetSqlChars(_recordBuffer, ordinal, GetSmiMetaData(ordinal));

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml' path='docs/members[@name="SqlDataRecord"]/GetSqlInt16/*' />
public virtual SqlInt16 GetSqlInt16(int ordinal) => ValueUtilsSmi.GetSqlInt16(_recordBuffer, ordinal, GetSmiMetaData(ordinal));
Expand Down Expand Up @@ -246,10 +258,78 @@ public virtual int GetSqlValues(object[] values)

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml' path='docs/members[@name="SqlDataRecord"]/SetValues/*' />
// ISqlUpdateableRecord Implementation
public virtual int SetValues(params object[] values) => SetValuesFrameworkSpecific(values);
public virtual int SetValues(params object[] values)
{
if (values == null)
{
throw ADP.ArgumentNull(nameof(values));
}

// Allow values array longer than FieldCount, just ignore the extra cells.
int copyLength = (values.Length > FieldCount) ? FieldCount : values.Length;
Copy link
Contributor

Choose a reason for hiding this comment

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

We can do some early exit optimization here if (copyLength == 0) return 0;


if (copyLength == 0)
{
return 0;
}

ExtendedClrTypeCode[] typeCodes = new ExtendedClrTypeCode[copyLength];

// Verify all data values as acceptable before changing current state.
for (int i = 0; i < copyLength; i++)
{
SqlMetaData metaData = GetSqlMetaData(i);
typeCodes[i] = MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType(
metaData.SqlDbType,
isMultiValued: false,
values[i],
metaData.Type);
if (typeCodes[i] == ExtendedClrTypeCode.Invalid)
{
throw ADP.InvalidCast();
}
}

// Now move the data. We've already validated the element types above, so this will
// only throw if an invalid UDT was sent.
for (int i = 0; i < copyLength; i++)
{
ValueUtilsSmi.SetCompatibleValueV200(
_recordBuffer,
ordinal: i,
GetSmiMetaData(i),
values[i],
typeCodes[i],
offset: 0,
peekAhead: null);
}

return copyLength;
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml' path='docs/members[@name="SqlDataRecord"]/SetValue/*' />
public virtual void SetValue(int ordinal, object value) => SetValueFrameworkSpecific(ordinal, value);
public virtual void SetValue(int ordinal, object value)
{
SqlMetaData metaData = GetSqlMetaData(ordinal);
ExtendedClrTypeCode typeCode = MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType(
metaData.SqlDbType,
isMultiValued: false,
value,
metaData.Type);
if (typeCode == ExtendedClrTypeCode.Invalid)
{
throw ADP.InvalidCast();
}

ValueUtilsSmi.SetCompatibleValueV200(
_recordBuffer,
ordinal,
GetSmiMetaData(ordinal),
value,
typeCode,
offset: 0,
peekAhead: null);
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml' path='docs/members[@name="SqlDataRecord"]/SetBoolean/*' />
public virtual void SetBoolean(int ordinal, bool value) => ValueUtilsSmi.SetBoolean(_recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
Expand Down Expand Up @@ -290,10 +370,10 @@ public virtual int GetSqlValues(object[] values)
public virtual void SetDateTime(int ordinal, DateTime value) => ValueUtilsSmi.SetDateTime(_recordBuffer, ordinal, GetSmiMetaData(ordinal), value);

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml' path='docs/members[@name="SqlDataRecord"]/SetTimeSpan/*' />
public virtual void SetTimeSpan(int ordinal, TimeSpan value) => SetTimeSpanFrameworkSpecific(ordinal, value);
public virtual void SetTimeSpan(int ordinal, TimeSpan value) => ValueUtilsSmi.SetTimeSpan(_recordBuffer, ordinal, GetSmiMetaData(ordinal), value);

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml' path='docs/members[@name="SqlDataRecord"]/SetDateTimeOffset/*' />
public virtual void SetDateTimeOffset(int ordinal, DateTimeOffset value) => SetDateTimeOffsetFrameworkSpecific(ordinal, value);
public virtual void SetDateTimeOffset(int ordinal, DateTimeOffset value) => ValueUtilsSmi.SetDateTimeOffset(_recordBuffer, ordinal, GetSmiMetaData(ordinal), value);

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient.Server/SqlDataRecord.xml' path='docs/members[@name="SqlDataRecord"]/SetDBNull/*' />
public virtual void SetDBNull(int ordinal)
Expand Down

This file was deleted.

Loading
Loading